Something we found when using comma in condition ternary operator? [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-11-28 14:28:49

This is a matter of operator precedence. Your expression:

condition?ia=1, ib=2, ic=3:ib=22,ia=21, ic=23;

is understood by the compiler as:

(condition?(ia=1, ib=2, ic=3):(ib=22)),ia=21, ic=23;

At this point you should be able to see why you get the program output.

Yes, the relevant grammar for a conditional expression is:

logical-or-expression ? expression : assignment-expression

for assignment expressions (which can also be a conditional-expression or a throw-expression):

logical-or-expression assignment-operator assignment-expression

and for an expression with a comma operator (an assignment-expression can also be an expression):

expression , assignment-expression

This means that the construct a ? b : c, d cannot be parsed as equivalent to a ? b : (c, d) because c, d is not an assignment-expression but must be parsed as equivalent to (a ? b : c), d.

There is no undefined behaviour in condition ? ia=1,ib=2,ic=3 : ia=21, ib=22, ic=23; because evaluation of condition is sequenced before the evaluation of either the second or third operands of ?: and in every sub-expression containing a comma operator the evaluation of the first operand of the comma operator is sequenced before the evaluation of the second operand.

It's legal, but stupid not very useful to write code like that.

The code

condition?ia=1, ib=2, ic=3:ia=21, ib=22, ic=23;

is equivalent to

condition?(ia=1, ib=2, ic=3):ia=21;
ib=22;
ic=23;

just harder to read.

The problem is that the comma operator has the lowest precedence there is. Thanks to that, the else part of the conditional operator is just the first assignment, after that the comma operator kicks in and the other two statements will be executed just aswell.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!