繁体   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