Visual Studio 2010 (C++):暂时抑制 C4706 警告
当您在 Visual Studio 2010 中编译以下 C++ 源文件并启用警告级别 /W4 时,
#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 上正式记录的)不起作用 - 错误在哪里?
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");
}
}
you get the following warning
warning C4706: assignment within conditional expression
for line 11.
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");
}
}
It didn't help.
This variant didn't help either:
#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.
So in conclusion: how do I suppress the C4706 exactly at this place?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不要试图隐藏警告,而是修复问题它在抱怨;您的赋值有一个可以在另一个表达式中合法使用的值(赋值左侧的值)。
您可以通过显式测试分配的结果来解决此问题:
Instead of trying to hide your warning, fix the issue it's complaining about; your assignment has a value (the value on the left side of the assignment) that can be legally used in another expression.
You can fix this by explicitly testing the result of the assignment:
在 MSDN 库中:http://msdn.microsoft。 com/en-us/library/2c8f766e(v=VS.100).aspx,有如下部分。
因此“#pragma warning”仅适用于每个函数/方法。
请参阅以下代码了解更多详细信息。
In MSDN Libray: http://msdn.microsoft.com/en-us/library/2c8f766e(v=VS.100).aspx, There is the section as follows.
So '#pragma warning' only works for an each function/method.
Please see the following code for more detail.
明智的解决方案是重写条件,以
告知任何 C 编译器您确实想要分配,并且 几乎肯定会生成相同的目标代码。
The sane solution is to rewrite the condition to
which will inform any C compiler that you really want to assign, and is almost certain to generate the same object code.
还有另一种解决方案可以避免警告:逗号运算符。
这里的主要优点是不需要括号,因此当变量名称很短时,它比
!=0
解决方案短一些。例如:
There is another solution which avoids the warning: the comma operator.
The main advantage here will be that you don't need parentheses so it's a bit shorter than the
!=0
solution when your variable name is short.For example:
有一个简单的构造
!!
将类型转换为bool
。像这样:但是,在某些情况下,直接比较
!= 0
对于读者来说可能会更清楚。There is a simple construction
!!
to cast a type tobool
. Like this:However, in some cases direct comparison
!= 0
might be more clear to a reader.