當您編譯在Visual Studio 2010中使用以下C++源文件級警告/ W4啓用的Visual Studio 2010(C++):抑制C4706警告暫時
#include <cstdio> // for printf
#include <cstring> // for strcmp
char str0[] = "Hello";
char str1[] = "World";
int main()
{
int result;
if (result = strcmp(str0, str1)) // line 11
{
printf("Strings are different\n");
}
}
你會得到如下警告
警告C4706:條件表達式
內分配用於線路11
我想在這個地方準確地壓制這個警告。所以,我想谷歌和找到了這個網頁:http://msdn.microsoft.com/en-us/library/2c8f766e(v=VS.100).aspx
所以我改變了代碼如下 - 希望這將解決這個問題:
#include <cstdio> // for printf
#include <cstring> // for strcmp
char str0[] = "Hello";
char str1[] = "World";
int main()
{
int result;
#pragma warning(push)
#pragma warning(disable : 4706)
if (result = strcmp(str0, str1))
#pragma warning(pop)
{
printf("Strings are different\n");
}
}
它並沒有幫助。
這種變異並沒有幫助:
#include <cstdio> // for printf
#include <cstring> // for strcmp
char str0[] = "Hello";
char str1[] = "World";
int main()
{
int result;
#pragma warning(push)
#pragma warning(disable : 4706)
if (result = strcmp(str0, str1))
{
#pragma warning(pop)
printf("Strings are different\n");
}
}
爲了避免一個進一步詢問:我每次清洗編譯之前的解決方案。所以這可能不是問題。
所以最後:我該如何在這個地方準確地壓制C4706?
編輯是的,重寫是可能的 - 但我真的想知道爲什麼我試圖壓制警告的方法(MSDN上正式記錄)不起作用 - 錯誤在哪裏?
大多數編譯器實現通過要求一對額外的括號的「你確定」的因素,我希望MSVC可以做同樣的。 – Thomas