简体   繁体   English

为什么clang不对模板中的无效代码发出警告?

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

When compiling with -Weverything, why would clang not flag the dead code in the template below, but flag it in the function? 使用-Weverything进行编译时,为什么clang不会在下面的模板中标记无效代码,而是在函数中对其进行标记? Note that in both cases, it flags the unused variable warning. 请注意,在两种情况下,它都标记未使用的变量警告。

#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();
}

clang output: 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.

I don't see that there's any reason for that warning not being emitted (other than a bug in clang). 我没有看到没有发出任何警告的任何原因(叮叮中的错误除外)。

I'm guessing clang is being over-cautious about warnings in templates since it isn't able to tell that code will never be executed by any instantiation of the template (even though it's obvious to a human), so it just doesn't warn. 我猜clang对模板中的警告过于谨慎,因为它无法告诉代码模板的任何实例都不会执行代码(即使对人类来说是显而易见的),所以它不会警告。 But that's just an assumption. 但这只是一个假设。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么没有clang -Wunused-member-function警告未使用的成员函数? - Why doesn't clang -Wunused-member-function warn about an unused member function? 为什么没有clang警告从double到int的隐式转换,但是从long到int时呢? - Why doesn't clang warn about implicit conversion from double to int, but do it when from long to int? 为什么VC ++编译代码而clang没有? - Why does VC++ compile the code while clang doesn't? 为什么clang ++不编译以下代码? - why doesn't clang++ compile the following code? 为什么带有模板 arguments 的 C++ function 在 clang++ 7 上没有警告“控制到达非无效函数的末尾”? - why doesn't a C++ function with template arguments warn “control reaches the end of a non-void function” on clang++ 7? 为什么clang / llvm不优化这个? - Why doesn't clang/llvm optimize this? 为什么可执行文件如此之大? (为什么不删除死代码?) - Why is the executable so big? (Why isn't dead code removed?) 如果可能存在未定义的行为,为什么编译器不会警告您? - Why doesn't the compiler warn you if there is possible Undefined Behaviour? 为什么编译器不警告unsigned to signed conversion? - Why doesn't the compiler warn about unsigned to signed conversion? C#与C ++性能 - 为什么.NET不执行最基本的优化(如死代码消除)? - C# vs. C++ performance — why doesn't .NET perform the most basic optimizations (like dead code elimination)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM