如何获取 if 语句中赋值的编译器错误?

Tho*_*ler 3 c++ compiler-warnings visual-studio

在 Visual Studio 中是否有可能因语句中的赋值而出现编译器错误if?如何?

#include <iostream>
int main()
{
    int a = 2;
    if (a = 3)  // Want a warning here
        std::cout << "Avoid this!\n";
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以切换到尤达条件 ( if (3=a)),但我真的不想这样做。

我尝试过:将警告级别设置为/Wall,但我仍然没有收到可以将其视为错误的警告。

我正在 Visual Studio 2019 (16.11.19) 中执行此操作:

视觉工作室 2019

构建输出是

Rebuild started...
1>------ Rebuild All started: Project: Yoda2019, Configuration: Debug Win32 ------
1>Yoda2019.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\limits.h(70,5): error C2220: the following warning is treated as an error
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\limits.h(70,5): warning C4668: '__STDC_WANT_SECURE_LIB__' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(154,5): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xmemory(164,5): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\atomic(284,9): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\atomic(299,9): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\atomic(315,9): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\atomic(375,9): warning C4365: 'argument': conversion from 'long' to 'unsigned int', signed/unsigned mismatch
1>Done building project "Yoda2019.vcxproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Run Code Online (Sandbox Code Playgroud)

和 Visual Studio 2022 (17.4.0)

视觉工作室 2022

构建输出是

Rebuild started...
1>------ Rebuild All started: Project: YodaCheck, Configuration: Debug x64 ------
1>YodaCheck.cpp
1>YodaCheck.vcxproj -> B:\Projekte\C++\Dynamic Linking\YodaCheck\x64\Debug\YodaCheck.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
========== Elapsed 00:01,379 ==========
Run Code Online (Sandbox Code Playgroud)

所以我没有看到可以转换为错误的警告。

phu*_*clv 7

使用/W4 /WX

/WX将所有编译器警告视为错误。对于一个新项目,最好在所有编译中使用/WX;解决所有警告可确保尽可能减少难以发现的代码缺陷。

https://learn.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level?view=msvc-170

确保在相关配置中进行该设置。在这样一个简单的演示中,选择“所有配置”和“所有平台”可能是最简单的。

Visual Studio 属性页

  • 或者特别是对于 C4706,`#pragma warning( error : 4706 )` 是一个选项。 (2认同)

Adr*_*ica 7

您可以使用该指令将任何特定警告转变为错误#pragma warning(error:nnnn)。在您的情况下,警告是:

警告 C4706:条件表达式内的赋值

#pragma因此,在代码中添加相关指令将产生编译器错误

#include <iostream>
#pragma warning(error:4706)

int main()
{
    int a = 2;
    if (a = 3)  // Want a warning here
        std::cout << "Avoid this!\n";
}
Run Code Online (Sandbox Code Playgroud)

现在给出:

错误 C4706:条件表达式内的赋值

对于解决方案范围的设置,请在属性页中进行更改:

属性页中的 C4706