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
This is a discussion on No Compile Error for if(x=1) within the C++ Programming forums, part of the General Programming Boards category; 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 ...
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
Manasij Mukherjee | gcc-4.8.0 @Arch Linux
Slow and Steady wins the race... if and only if :
1.None of the other participants are fast and steady.
2.The fast and unsteady suddenly falls asleep while running !
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"
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
Manasij Mukherjee | gcc-4.8.0 @Arch Linux
Slow and Steady wins the race... if and only if :
1.None of the other participants are fast and steady.
2.The fast and unsteady suddenly falls asleep while running !
@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) ...
Manasij Mukherjee | gcc-4.8.0 @Arch Linux
Slow and Steady wins the race... if and only if :
1.None of the other participants are fast and steady.
2.The fast and unsteady suddenly falls asleep while running !
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) ....
Right 98% of the time, and don't care about the other 3%.
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.