Visual Studio 2010(C ++):暂时禁止显示C4706警告 [英] Visual Studio 2010 (C++): suppress C4706 warning temporarily
问题描述
当在警告级别为/W4的情况下在Visual Studio 2010中编译以下C ++源文件时
When you compile the following C++ source file in Visual Studio 2010 with warning level /W4 enabled
#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:条件表达式中的赋值
warning C4706: assignment within conditional expression
第11行.
我想在这个地方完全禁止显示此警告.因此,我尝试使用Google并找到了以下页面: http://msdn.microsoft. com/en-us/library/2c8f766e(v = VS.100).aspx
I want to suppress this warning exactly at this place. So I tried Google and found this page: http://msdn.microsoft.com/en-us/library/2c8f766e(v=VS.100).aspx
所以我将代码更改为以下代码-希望这可以解决问题:
So I changed the code to the following - hoping this would solve the problem:
#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");
}
}
为避免进一步询问,我在每次编译之前都已清理解决方案.因此,这可能不是问题.
To avoid one further inquiry: I cleaned the solution before each compilation. So this is probably not the fault.
因此,结论是:我该如何在这个位置精确抑制C4706?
So in conclusion: how do I suppress the C4706 exactly at this place?
编辑是的,可以重写-但是我真的很想知道为什么我尝试抑制警告的方式(在MSDN上正式记录)不起作用-错误在哪里?
Edit Yes, rewriting is possible - but I really want to know why the way I try to suppress the warning (that is documented officially on MSDN) doesn't work - where is the mistake?
推荐答案
In MSDN Libray: http://msdn.microsoft.com/en-us/library/2c8f766e(v=VS.100).aspx, There is the section as follows.
对于4700-4999范围内的警告号 与代码生成相关联,警告的状态有效 当编译器遇到一个函数的大括号时, 对其余功能有效.在中使用警告语 更改具有更大数字的警告状态的功能 4699将仅在功能结束后生效.这 下面的示例显示警告语用正确的位置 禁用代码生成警告消息,然后将其还原.
For warning numbers in the range 4700-4999, which are the ones associated with code generation, the state of the warning in effect when the compiler encounters the open curly brace of a function will be in effect for the rest of the function. Using the warning pragma in the function to change the state of a warning that has a number larger than 4699 will only take effect after the end of the function. The following example shows the correct placement of warning pragmas to disable a code-generation warning message, and then to restore it.
因此,#pragma警告"仅适用于每种功能/方法.
So '#pragma warning' only works for an each function/method.
有关更多详细信息,请参见以下代码.
Please see the following code for more detail.
#include <cstdio> // for printf
#include <cstring> // for strcmp
char str0[] = "Hello";
char str1[] = "World";
#pragma warning(push)
#pragma warning( disable : 4706 )
void func()
{
int result;
if (result = strcmp(str0, str1)) // No warning
{
printf("Strings are different\n");
}
#pragma warning(pop)
}
int main()
{
int result;
if (result = strcmp(str0, str1)) // 4706 Warning.
{
printf("Strings are different\n");
}
}
这篇关于Visual Studio 2010(C ++):暂时禁止显示C4706警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!