在 Visual Studio 中是否有可能為陳述句中的賦值獲取編譯器錯誤if?如何?
#include <iostream>
int main()
{
int a = 2;
if (a = 3) // Want a warning here
std::cout << "Avoid this!\n";
}
我知道我可以切換到尤達條件(if (3=a)),但我真的不想。
我試過:將警告級別設定為,/Wall但我仍然沒有收到可以視為錯誤的警告。
我在 Visual Studio 2019 (16.11.19) 中執行此操作。:

構建輸出是
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 ==========
和 Visual Studio 2022 (17.4.0)

構建輸出是
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 ==========
所以我看不到可以轉換為錯誤的警告。
uj5u.com熱心網友回復:
您可以使用該指令將任何特定警告轉換為錯誤。#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";
}
這現在給出:
錯誤 C4706:條件運算式內的賦值
對于解決方案范圍的設定,請在屬性頁中更改它:

uj5u.com熱心網友回復:
利用/W4 /WX
/WX將所有編譯器警告視為錯誤。對于一個新專案,最好在所有編譯中使用 /WX;解決所有警告可確保盡可能少的難以發現的代碼缺陷。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/512401.html
標籤:C 视觉工作室编译器警告
上一篇:ID3D11RenderTargetView在ID3D11DeviceContext::OMSetRenderTargets()上被移除
下一篇:如何填充二維陣列C 的每一行

