VS2017 中未生成警告

Warnings not being generated in VS2017

在我看来,VS2017 未能捕捉到一些相当明显的编译器警告,而旧版本则没有。这对我来说似乎非常重要,以至于我猜测问题 has 是我遗漏的东西(例如,也许是一些新的默认编译器设置?)。还有其他人看过吗?

为了对此进行测试,我在 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.

据说这不是 VS 2013 中的警告,默认警告级别为 W3。只有使用提升的 W4 设置,VS 2013 才会报告警告。它在 VS 2017 中被完全删除。

为了比较,GCC 也不会生成警告: Live example on Coliru.