c++ ternary operator
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开发者_Go百科 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?
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++: Hidden Features of C++?.
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.
A. Yes. §[expr.cond]/4:
If the second and third operands are lvalues and have the same type, the result is of that type and is an lvalue ...
(Note that it is not true in C. Explicitly written in the C99 standard, footnote 93, "A conditional expression does not yield an lvalue.")
B. I don't think it's widely used as the usage is pretty obscure. It's more common to see
if (x == 1)
y = 99;
else
z = 99;
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."
精彩评论