2016-03-30 29 views
1

我目前正在與PC-Lint(版本9.00j和l)苦苦掙扎,這給了我一些錯誤和一段代碼的警告。代碼編譯良好並按預期運行。下面是它的一個簡化版本:成員模板奇怪的PC-Lint錯誤

#include <iostream> 
#include <vector> 

typedef unsigned char uint8_t; 

class Test 
{ 
    uint8_t   inputList[10]; 
    std::vector<int> resultList; 

public: 

    Test() : resultList() 
    { 
    for (uint8_t ii = 0; ii < 10; ++ii) 
     inputList[ii] = ii; 
    } 

    template<int list_size, typename ResultListType> 
    void loadList(const uint8_t (& inputList)[list_size], 
       ResultListType & resultList) const 
    { 
    for (uint8_t ii = 0; ii < list_size; ++ii) 
     resultList.push_back(inputList[ii]); 
    } 

    void run() 
    { 
    loadList(inputList, resultList); 
    } 

    void print() 
    { 
    std::vector<int>::iterator it; 
    for (it = resultList.begin(); it != resultList.end(); ++it) 
     std::cout << *it << std::endl; 
    } 
}; 

int main() 
{ 
    Test t; 
    t.run(); 
    t.print(); 
} 

當金培爾的在線演示運行此,我得到這些錯誤和警告:

30  loadList(inputList, resultList); 
diy.cpp 30 Error 1025: No template matches invocation 'Test::loadList(unsigned char [10], std::vector<int>)', 1 candidates found, 1 matched the argument count 
diy.cpp 30 Info 1703: Function 'Test::loadList(const unsigned char (&)[V], <2>&) const' arbitrarily selected. Refer to Error 1025 
diy.cpp 30 Error 1032: Member 'loadList' cannot be called without object 
diy.cpp 30 Error 1058: While calling 'Test::loadList(const unsigned char (&)[V], <2>&) const': Initializing a non-const reference '<2>&' with a non-lvalue (a temporary object of type 'std::vector<int>') 
diy.cpp 30 Warning 1514: Creating temporary to copy 'std::vector<int>' to '<2>&' (context: arg. no. 2) 

所以基本上,PC-Lint是想告訴我,這將偶然找到正確的模板參數,並且只填充矢量的臨時副本。但是代碼運行良好,resultList包含數據!

任何人都可以告訴我這裏發生了什麼? PC-Lint是否正確,有什麼問題或者這只是一個PC-Lint的bug?

回答

0

的問題是,loadList被標記爲const,但你傳遞一個非恆定的參考成員變量​​您修改

這是真實的,loadList函數不修改this例如直接但因爲你仍可以修改成員變量的函數不能是常數。

要麼創建一個臨時向量,您傳遞給函數,或者使函數不是const

+0

當忽略該const時,我會得到相同的錯誤/警告。我添加了它,因爲它在錯誤描述[1058](http://gimpel-online.com/MsgRef.html#1058)中提到 – craesh

+0

@craesh如果你明確地指定了模板參數,它確實工作正常, oadList <10,std :: vector >(inputList,resultList)'。我謹慎地說這是PC-lint中模板匹配和重載分辨率的問題。 –

+0

它甚至可以工作,如果我只是指定第一個參數,大小。我記得前一段時間有類似的問題,它是由'lint -fvl'解決的。可變長度數組是GNU擴展,在默認的lint config中默認啓用... – craesh