在条件三元运算符中使用逗号时我们发现了什么?
好吧,我有一个关于三元运算符中逗号的问题。 废话少说,代码如下:
void test_comma_in_condition(void)
{
int ia, ib, ic;
ia = ib = ic = 0;
bool condition=true;
cout<<"Original:"<<endl;
cout<<"ia: "<<ia<<endl;
cout<<"ib: "<<ib<<endl;
condition?(ia=1, ib=2):(ia=11, ib=12);
cout<<"After:"<<endl;
cout<<"ia: "<<ia<<endl;
cout<<"ib: "<<ib<<endl;
ia = ib = ic = 0;
condition?ia=1, ib=2, ic=3:ib=22,ia=21, ic=23;
cout<<"The operation must be bracketed, or you'll see..."<<endl;
cout<<"ia: "<<ia<<endl;
cout<<"ib: "<<ib<<endl;
cout<<"ic: "<<ic<<endl;
condition?ia=1, ib=2, ic=3:ia=21, ib=22, ic=23;
cout<<"The operation must be bracketed, or you'll see..."<<endl;
cout<<"ia: "<<ia<<endl;
cout<<"ib: "<<ib<<endl;
cout<<"ic: "<<ic<<endl;
return;
}
输出将是这样的:
Original:
ia: 0
ib: 0
After:
ia: 1
ib: 2
The operation must be bracketed, or you'll see...
ia: 21
ib: 2
ic: 23
The operation must be bracketed, or you'll see...
ia: 1
ib: 22
ic: 23
这合法吗?
Well, I had a question about comma in ternary operator.
Cut the crap, the code is below:
void test_comma_in_condition(void)
{
int ia, ib, ic;
ia = ib = ic = 0;
bool condition=true;
cout<<"Original:"<<endl;
cout<<"ia: "<<ia<<endl;
cout<<"ib: "<<ib<<endl;
condition?(ia=1, ib=2):(ia=11, ib=12);
cout<<"After:"<<endl;
cout<<"ia: "<<ia<<endl;
cout<<"ib: "<<ib<<endl;
ia = ib = ic = 0;
condition?ia=1, ib=2, ic=3:ib=22,ia=21, ic=23;
cout<<"The operation must be bracketed, or you'll see..."<<endl;
cout<<"ia: "<<ia<<endl;
cout<<"ib: "<<ib<<endl;
cout<<"ic: "<<ic<<endl;
condition?ia=1, ib=2, ic=3:ia=21, ib=22, ic=23;
cout<<"The operation must be bracketed, or you'll see..."<<endl;
cout<<"ia: "<<ia<<endl;
cout<<"ib: "<<ib<<endl;
cout<<"ic: "<<ic<<endl;
return;
}
The output will be like:
Original:
ia: 0
ib: 0
After:
ia: 1
ib: 2
The operation must be bracketed, or you'll see...
ia: 21
ib: 2
ic: 23
The operation must be bracketed, or you'll see...
ia: 1
ib: 22
ic: 23
Is this legal?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个运算符优先级的问题。您的表达式:
被编译器理解为:
此时您应该能够明白为什么会得到程序输出。
This is a matter of operator precedence. Your expression:
is understood by the compiler as:
At this point you should be able to see why you get the program output.
是的,条件表达式的相关语法是:
对于赋值表达式(也可以是条件表达式或抛出表达式):
以及对于带有逗号运算符(赋值表达式也可以是表达式):
这意味着构造
a ? b : c, d
无法解析为与a 等效吗? b : (c, d)
因为c, d
不是赋值表达式,但必须解析为等价于(a ? b : c)、d
。condition 中没有未定义的行为? ia=1,ib=2,ic=3 : ia=21, ib=22, ic=23;
因为条件
的求值在第二个或第三个条件的求值之前排序?:
的操作数以及在每个包含逗号运算符的子表达式中,逗号运算符的第一个操作数的计算顺序在第二个操作数的计算之前进行。Yes, the relevant grammar for a conditional expression is:
for assignment expressions (which can also be a conditional-expression or a throw-expression):
and for an expression with a comma operator (an assignment-expression can also be an expression):
This means that the construct
a ? b : c, d
cannot be parsed as equivalent toa ? b : (c, d)
becausec, 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 ofcondition
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.这是合法的,但是编写这样的代码
stupid并不是很有用。该代码
相当于
只是更难阅读。
It's legal, but
stupidnot very useful to write code like that.The code
is equivalent to
just harder to read.
问题是逗号运算符的优先级是最低的。因此,条件运算符的 else 部分只是第一个赋值,之后逗号运算符开始起作用,其他两个语句也将被执行。
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.