为什么 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();
}
叮当声输出:
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中的错误)。
我猜clang对模板中的警告过于谨慎,因为它无法判断模板的任何实例化都不会执行代码(尽管这对人类来说是显而易见的),所以它只是不发出警告。但这只是一个假设。
相关文章:
- GCC 和 Clang 代码性能的巨大差异
- Clang 给了我符号更改的警告,但代码仍然产生正确的输出
- 获取 clang++:错误:在编译文件系统库的代码时
- 与clang++一起使用的VS代码在构建良好的C++文件中显示错误
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 如何修复 clang: 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- Clang拒绝类模板的嵌套类仅通过专门化定义的代码是正确的吗
- Qt创建者-如何删除Clang代码模型
- Android NDK.Build命令失败.未定义的引用.clang++:错误:链接器命令失败,退出代码为1
- 聚合初始化,clang想让我破坏代码吗
- 使用 Android NDK 使用 clang++ 编译C++代码时对"_Unwind_Resume"的未定义引用
- 为什么clang仍然需要libgcc.a来编译我的代码
- 为什么这段代码无法使用 gcc 4.8.5 编译,而使用 clang 编译正常
- 为什么我在以下代码中收到 clang 错误?
- C++ XCODE ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -
- clang:错误:链接器命令失败,C++代码中的退出代码为 1(使用 -v 查看调用)
- C++汇编代码分析(使用 clang 编译)
- Clang vs GCC:挥发性访问的不同代码
- 为什么以下代码不使用GCC编译,而是用Clang编译罚款
- LLVM Clang C 代码注入