在我看来,VS2017 未能捕捉到一些相当明显的编译器警告,而旧版本则没有。这对我来说似乎非常重要,以至于我猜测问题必须是我遗漏的东西(例如,也许是一些新的默认编译器设置?)。还有其他人看到这个吗?
为了对此进行测试,我在 2013 年和 2017 年都创建了一个简单的控制台应用程序。我对项目设置所做的唯一更改是将编译器警告级别设置为 4 并将警告报告为错误。以下是完整的源代码
在 VS2013 中,这失败了。在 2017 年,它构建得很好......
// TestWarning4127.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
int main()
{
if (true) // *** SHOULD generate warning 4127
std::cout << "Warning";
return 0;
}
我是否遗漏了一些明显的东西...?
最佳答案
当在 if
或 while
条件中使用时,1
或 true
等普通常量不会生成VS 2017 中的警告,如 Compiler Warning (level4) 中所述官方文档。官方文档摘录:
The controlling expression of an if statement or while loop evaluates to a constant. Because of their common idiomatic usage, trivial constants such as 1 or true do not trigger the warning, unless they are the result of an operation in an expression.
话虽如此,它也不是默认警告级别 W3 的 VS 2013 中的警告。只有使用提升的 W4 设置,VS 2013 才会报告警告。它在 VS 2017 中被完全删除。
相比之下,GCC 也不会生成警告: Live example on Coliru .
关于c++ - VS2017 中未生成警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48527755/