I discovered that VC++ Express 2008 complies:
if(x=1)doTask();
*Note: (x=1) not (x==1)
Is there a way to get VC++ to give me a warning for this?
And, why is this is allowed in the first place?
Thanks
I discovered that VC++ Express 2008 complies:
if(x=1)doTask();
*Note: (x=1) not (x==1)
Is there a way to get VC++ to give me a warning for this?
And, why is this is allowed in the first place?
Thanks
I'm not sure I understand, but do you mean...
I can do math inside an IF and the IF is true when the math result is not 0 and false when the result is 0?
And if this is what you mean, then...
Shouldn't I test: if((x=y+z)!==FALSE) or if((x=y+z)===FALSE)
Also, is this okay too: if(x++) and if(x+=3)
I'm just curious to know.
Not as if I will start using such code.
I think it is ugly.
Make sure that your project is set to warning level 4. I believe that should pick it up.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
@manasij, you've forgotten a set of parens:
But this isn't really the same as what the OP posted.Code:if((someflag = test_condition()) != some_value)
The case of this:
is really just a case of thisCode:if (x = a) ...
In such a case, where no logical test is explicitly performed, it is interpreted as if it were written:Code:if (b) ...
Code:if (b != 0) ... if ((x = a) != 0) ...
Because assignment operations yield a value.
Apart from particular options with your compiler (iMalc mentioned warning level 4) you can also trigger warnings from such things by swapping left and right operands.
This will not compile with any compiler
but this willCode:if (1 = x)
The weakness of this technique is that it only works if one of the operands is an rvalue (i.e. it cannot be on the left hand side of an assignment). So it won't catchCode:if (1 == x)
because the assignment is still valid.Code:if (some_variable = another_variable) ....
iMalc,
Thank you.
Level 4 did it.
And I actually found a semi-copycat post when Googling "VC++ warning level":
VC++ General: How to avoid making an assignment when the intention is a comparison? - CodeGuru Forums
Of course, WOW, the list of warnings is HUGE.
I guess I'm about to really learn C++ now.