When compiling with -Weverything, why would clang not flag the dead code in the template below, but flag it in the function? 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:
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. But that's just an assumption.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.