Visual Studio 2010 (C++):暂时抑制 C4706 警告

发布于 2024-11-28 12:27:17 字数 1564 浏览 1 评论 0原文

当您在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜灵血窟げ 2024-12-05 12:27:17

不要试图隐藏警告,而是修复问题它在抱怨;您的赋值有一个可以在另一个表达式中合法使用的值(赋值左侧的值)。

您可以通过显式测试分配的结果来解决此问题:

if ((result = strcmp(str0, str1)) != 0) 
{
    printf("Strings are different\n");
}

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:

if ((result = strcmp(str0, str1)) != 0) 
{
    printf("Strings are different\n");
}
梦巷 2024-12-05 12:27:17

在 MSDN 库中:http://msdn.microsoft。 com/en-us/library/2c8f766e(v=VS.100).aspx,有如下部分。

对于 4700-4999 范围内的警告编号,这些是
与代码生成相关,有效警告的状态
当编译器遇到函数的左大括号时将
对函数的其余部分有效。使用警告杂注
更改数字较大的警告状态的函数
大于4699只有在函数结束后才会生效。这
以下示例显示了警告编译指示的正确位置
禁用代码生成警告消息,然后恢复它。

因此“#pragma warning”仅适用于每个函数/方法。

请参阅以下代码了解更多详细信息。

#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");
    }
}

In MSDN Libray: http://msdn.microsoft.com/en-us/library/2c8f766e(v=VS.100).aspx, There is the section as follows.

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.

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");
    }
}
物价感观 2024-12-05 12:27:17

明智的解决方案是重写条件,以

if( (result = strcmp(str0, str1)) != 0 )

告知任何 C 编译器您确实想要分配,并且 几乎肯定会生成相同的目标代码。

The sane solution is to rewrite the condition to

if( (result = strcmp(str0, str1)) != 0 )

which will inform any C compiler that you really want to assign, and is almost certain to generate the same object code.

π浅易 2024-12-05 12:27:17

还有另一种解决方案可以避免警告:逗号运算符

这里的主要优点是不需要括号,因此当变量名称很短时,它比 !=0 解决方案短一些。

例如:

if (result = strcmp(str0, str1), result) 
{
    printf("Strings are different\n");
}

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:

if (result = strcmp(str0, str1), result) 
{
    printf("Strings are different\n");
}
岁月苍老的讽刺 2024-12-05 12:27:17

有一个简单的构造 !! 将类型转换为 bool。像这样:

if (!!(result = strcmp(str0, str1)))

但是,在某些情况下,直接比较 != 0 对于读者来说可能会更清楚。

There is a simple construction !! to cast a type to bool. Like this:

if (!!(result = strcmp(str0, str1)))

However, in some cases direct comparison != 0 might be more clear to a reader.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文