Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 335
Ruddy Avatar asked Jul 15 '10 16:07

Ruddy


People also ask

What is ternary operator in C with example?

It helps to think of the ternary operator as a shorthand way or writing an if-else statement. Here's a simple decision-making example using if and else: int a = 10, b = 20, c; if (a < b) { c = a; } else { c = b; } printf("%d", c); This example takes more than 10 lines, but that isn't necessary.

How do you write a 3 condition ternary operator?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

What is a ternary operator?

In computer science, a ternary operator is an operator that takes three arguments (or operands). The arguments and result can be of different types. Many programming languages that use C-like syntax feature a ternary operator, ?: , which defines a conditional expression.

What is ternary operator in C w3schools?

C offers a ternary operator, which is the conditional operator ( ?: in combination), to construct conditional expressions.


2 Answers

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.

like image 149
Justin Ardini Avatar answered Oct 21 '22 17:10

Justin Ardini


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;
like image 28
kennytm Avatar answered Oct 21 '22 15:10

kennytm