Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

if (somevar = 2)
{
    ...

?

I try to pay attention to writing == instead of =, but not only using C++ but also VB and other languages, it just happens sometimes.

It takes really much time sometimes to find errors associated with my fault.

I think the possiblity that one really wants to write an if statement that also assigns a new value to a variable (as in my example above) is rather small. That is why I am hoping that VS2010 might have included an option that tells me about my possible flaw, but I haven't found one.

Is there any option like this?

like image 313
tmighty Avatar asked Dec 11 '25 14:12

tmighty


1 Answers

Yes there is:

warning C4706: assignment within conditional expression

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

The alternative is using YODA conditions (2 == somevar), but that's ugly. :)

like image 105
Luchian Grigore Avatar answered Dec 15 '25 04:12

Luchian Grigore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!