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?