C ++: Make VS2010 complain about "If" with "="

Is there any way in VS2010 C ++ to complain about the IDE if I do something like

if (somevar = 2)
{
    ...

      

?

I am trying to draw attention to not only writing == instead of =, but not only using C ++ but also VB and other languages, it happens sometimes.

It takes a long time to find errors related to my error.

I think the possibility that an if statement really wants to write that also assigns a new value to a variable (like in my example above) is pretty small. This is why I hope VS2010 might include an option that tells me about my possible flaw, but I haven't found one.

Is there such an option?

+3


source to share


2 answers


Yes there is:

warning C4706: assignment within conditional expression

      



Just make sure you turn on all (not all on MSVS) warnings. ( project properties -> C/C++ -> General -> Warning Level

)

An alternative is to use YODA ( 2 == somevar

) conditions , but that's ugly. :)

+6


source


Right-click the project name in Solution Explorer, select Properties , go to Configuration Properties | C / C ++ | General and set the Warning Level to Level 4 . You won't get an error, but a warning:



warning C4706: assignment in conditional expression

0


source







All Articles