Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2010 (C++): suppress C4706 warning temporarily

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?

like image 995
Nubok Avatar asked Aug 08 '11 17:08

Nubok


People also ask

How do I turn on all warnings in Visual Studio?

If you want to turn it on (or off) in the project setting, you have to go to: Configuration Properties -> C/C++ -> Command Line and then under Additional Options you can enter: /w3#### to set your warning to level 3, and thus enable it; or you can enter /wd#### to disable a warning.

How do you turn off warnings in C++?

To disable a set of warnings for a given piece of code, you have to start with a “push” pre-processor instruction, then with a disabling instruction for each of the warning you want to suppress, and finish with a “pop” pre-processor instruction.


2 Answers

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");
}
like image 63
Rob Avatar answered Oct 01 '22 16:10

Rob


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");
    }
}
like image 40
yamau6809 Avatar answered Oct 01 '22 18:10

yamau6809