views:

194

answers:

3

So I ran into something interesting that I didn't realize about the ternary operator (at least in Visual C++ 98-2010). As pointed out in http://msdn.microsoft.com/en-us/library/e4213hs1(VS.71).aspx if both the expression and conditional-expression are l-values the result is an l-value.

Of course normally in c/c++ you'd write something like:

int value = (x == 1) ? 1 : 0;

and never even care about the r-value/l-value involvment, and in this case neither 1 nor 0 are convertible to l-values.

However, take something like:

int value = (x == 1) ? y : z;

both y and z are l-values and they, or more precisely, one of them is the actual result of the ternary operator (not its stored value) which isn't necessarily obvious (at least I had never thought about it at any length).

But, what that leads to is the ability to write the following

(x == 1 ? y : z) = 99;

Which assigns 99 to y if x == 1 or 99 to z if x != 1

I've never seen that described anywhere and in all the discussions I've read about the use (or, usually, whether to use) the ternary operator.

Of course it only works if both the expression and conditional-expression are l-values something like

(x == 1 ? 0 : z) = 99;

fails to compile because 0 is an r-value as happily pointed out by the compiler.

And this only works if you include the parenthesis

x == 1 ? y : z = 99;

is something entirely different which assigns 99 to z only if (x != 1) and the beautiful part is that both sides are still l-values so there is the serious rat-hole of what things like (x == 1 ? y : z = 99) = 100 do (it assigns 100 to y or z depending on the truth of x == 1, stomping on the z = 99 assignment if x==1 is false)

So, this leads me to my questions:

A) Is this part of the actual c++ standard (which seems like it would be) and not just a Microsoft thing -- I've looked but have failed, so far, to find this info.

B) If this is widely realized and I've been living under a rock? I've never seen it used in any code that I can recall, and never seen it mentioned when the ternary operator is discussed.

C) Do I need to get out more often?

+7  A: 

A) Yes, this is part of the standard.

B) It's not widely realized, though it may be here on SO. There's a reason it was voted the #1 hidden feature of C++: http://stackoverflow.com/questions/75538/hidden-features-of-c-closed.

C) No comment. :)

Personally, I recommend steering clear of using this feature. It is a lot less intuitive than using if/else statements, and clearly not everyone knows about it.

Going against my own warning, I actually tried using this once on a personal project, and I got burned by missing the parentheses and wasting 30 minutes trying to find the error.

Justin Ardini
Well i wish i had come across that before i posted :) ... i did look but clearly missed it.
Ruddy
+2  A: 

You have never seen it used because this usage is less intuitive and readable than the most common one. I have never seen it used this way in production code, either, and I hope I never see it.

Remember Herb Sutter & Andrei Alexandrescu's C++ Coding Standards, rule 6: "Correctness, simplicity, and clarity come first."

Daniel Daranas
I'm certainly not advocating its use - merely a curiosity at this point
Ruddy
@Ruddy: I was just answering to B. I wasn't sure about A (though I was 99% that it was true) or C :)
Daniel Daranas
+3  A: 
KennyTM
FWIW: I've verified in Visual 2010 conforms to footnote 93, in that you will get a compilation error ("... left operand must be l-value") when trying to compile `(x == 1 ? y : z) = 100;` in a .c (straight-c) file.
Ruddy