[英]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.