关于c ++:返回类型’?:’(三元条件运算符)

Return type of '?:' (ternary conditional operator)

为什么第一个返回引用?

1
2
3
int x = 1;
int y = 2;
(x > y ? x : y) = 100;

而第二个没有?

1
2
3
int x = 1;
long y = 2;
(x > y ? x : y) = 100;

实际上,第二个根本没有编译-"不是赋值的左值"。


(P)Expressions don't have return type s,they have a type and-a s it's known in the latest C++standard-a value category.(p)(P)a Conditional expression can be an avaloe or an rvalue.这是它的价值分类。(This is some of a simplification,in EDOCX1 universal 4 we have lvalues,xvalues and prvues.)(p)(P)In very broad and simple terms,an a 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.(p)(P)1.An assignment expression assigns a value to an object so the thing being assigned to must be an lvalue.(p)(P)For a conditional expression(EDOCX1)to be an lvaloue(again,in broad and simple terms),the second and third operations must be lvaloes of the same type.这是因为一种条件表达的类型和价值分类是在竞争的时间决定的,并且必须是适当的,条件是真实的。如果操作人员中有一人必须转为不同类型的操作人员,以杀害其他人,则条件表达方式不能作为这一转换的结果而被拯救。(p)布尔奇1


(P)The type of the ternary EDOCX1 pental 0 common expression is the common type of its second and third argument.如果你有相同的类型,你会得到一个参考回来。如果他们是可以转换成each other,one gets chosen and the other gets converted(promoted in this case).由于你无法返回到一个临时的参考点(转换/升级变量),你的类型是一个价值类型。(p)


(P)It cannot return a lvaluue since it will have to implicitly promote the type of EDOCX1 original 1 to match the type of EDOCX1 disciplinary 2(since both sides of EDOCX1 disciplinary 3)are not of the same type),and that it has to create a temporary.(p)What does the standard say?(N1905)(P)Expressions 5.17 assignment and compound assignment operators(p)布尔奇1布尔奇1布尔奇1