c++ - VS2017 中未生成警告

标签 c++ visual-studio visual-studio-2013 visual-studio-2017

在我看来,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;
}

我是否遗漏了一些明显的东西...?

最佳答案

当在 ifwhile 条件中使用时,1true 等普通常量不会生成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/

相关文章:

c++ - 什么数据结构更适合用于存储和排序<int, int>结构?

asp.net - 如何阻止 Visual Studio 的 'Publish Web Site' 发布我的 _ReSharper 文件夹?

visual-studio - Windows 上的 cmake RUNTIME_OUTPUT_DIRECTORY

c++ - 从具有尝试限制的 txt 文件登录

c++ - 未初始化警告需要-O2

c++ - Logger 应该什么时候刷新?

visual-studio-2012 - 在 Visual Studio 中查找对重写方法的所有引用

c# - 链接 : warning AL1073: Referenced assembly 'mscorlib.dll' targets a different processor

c++ - 如何自定义我的按钮?

visual-studio - 如何从 Visual Studio 调试在 Linux Docker 容器中运行的 .NET Core 应用程序