簡體   English   中英

為什么clang不對模板中的無效代碼發出警告?

[英]Why doesn't clang warn of dead code in templates?

使用-Weverything進行編譯時,為什么clang不會在下面的模板中標記無效代碼,而是在函數中對其進行標記? 請注意,在兩種情況下,它都標記未使用的變量警告。

#include <iostream>

template <class Item> class ItemBase {
  public:
    bool performWork() {
        int i;
        std::cout << "foo" << std::endl;
        return true;
        std::cout << "dead code in template" << std::endl;
    }
};

bool badFunc();
bool badFunc() {
    int i;
    std::cout << "foo" << std::endl;
    return true;
    std::cout << "dead code in function" << std::endl;
}

int main() {
    ItemBase<float> tester;
    tester.performWork();

    badFunc();
}

lang輸出:

test.cpp:24:13: warning: unused variable 'i' [-Wunused-variable]
        int i;
            ^
test.cpp:33:9: warning: unused variable 'i' [-Wunused-variable]
    int i;
        ^
test.cpp:36:42: warning: code will never be executed [-Wunreachable-code]
    std::cout << "dead code in function" << std::endl;
                                         ^~
3 warnings generated.

我沒有看到沒有發出任何警告的任何原因(叮叮中的錯誤除外)。

我猜clang對模板中的警告過於謹慎,因為它無法告訴代碼模板的任何實例都不會執行代碼(即使對人類來說是顯而易見的),所以它不會警告。 但這只是一個假設。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM