Why does the first conditional operator result in a reference?
int x = 1;
int y = 2;
(x > y ? x : y) = 100;
However, the second does not.
int x = 1;
long y = 2;
(x > y ? x : y) = 100;
Actually, the second does not compile at all:
error: lvalue required as left operand of assignment | (x > y ? x : y) = 100; | ~~~~~~~^~~~~~~~
Expressions don’t have return types, they have a type and – as it’s known in the latest C++ standard – a value category.
A conditional expression can be an lvalue or an rvalue. This is its value category. (This is somewhat of a simplification, in
C++11we have lvalues, xvalues and prvalues.)In very broad and simple terms, an lvalue refers to an object in memory and an rvalue is just a value that may not necessarily be attached to an object in memory.
An assignment expression assigns a value to an object so the thing being assigned to must be an lvalue.
For a conditional expression (
?:) to be an lvalue (again, in broad and simple terms), the second and third operands must be lvalues of the same type. This is because the type and value category of a conditional expression is determined at compile time and must be appropriate whether or not the condition is true. If one of the operands must be converted to a different type to match the other then the conditional expression cannot be an lvalue as the result of this conversion would not be an lvalue.