c++ - Return type of '?:' (ternary conditional operator) -
why first return reference?
int x = 1; int y = 2; (x > y ? x : y) = 100;
while second not?
int x = 1; long y = 2; (x > y ? x : y) = 100;
actually, second did not compile @ - "not lvalue left of assignment".
expressions don't have return types, have type , - it's known in latest c++ standard - value category.
a conditional expression can lvalue or rvalue. value category. (this of simplification, in c++11
have lvalues, xvalues , prvalues.)
in broad , simple terms, lvalue refers object in memory , rvalue value may not attached object in memory.
an assignment expression assigns value object thing being assigned must lvalue.
for conditional expression (?:
) lvalue (again, in broad , simple terms), the second , third operands must lvalues of same type. because type , value category of conditional expression determined @ compile time , must appropriate whether or not condition true. if 1 of operands must converted different type match other conditional expression cannot lvalue result of conversion not lvalue.
iso/iec 14882:2011 references:
3.10 [basic.lval] lvalues , rvalues (about value categories)
5.15 [expr.cond] conditional operator (rules type , value category conditional expression has)
5.17 [expr.ass] assignment , compound assignment operators (requirement l.h.s. of assignment must modifiable lvalue)
Comments
Post a Comment