在条件三元运算符中使用逗号时我们发现了什么?

发布于 2025-01-03 17:02:08 字数 1296 浏览 0 评论 0原文

好吧,我有一个关于三元运算符中逗号的问题。 废话少说,代码如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦太阳 2025-01-10 17:02:10

这是一个运算符优先级的问题。您的表达式:

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

被编译器理解为:

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

此时您应该能够明白为什么会得到程序输出。

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.

软糖 2025-01-10 17:02:10

是的,条件表达式的相关语法是:

logical-or-expression ? expression : assignment-expression

对于赋值表达式(也可以是条件表达式抛出表达式):

logical-or-expression assignment-operator assignment-expression

以及对于带有逗号运算符(赋值表达式也可以是表达式):

expression , assignment-expression

这意味着构造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:

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.

哆兒滾 2025-01-10 17:02:10

这是合法的,但是编写这样的代码stupid并不是很有用。

该代码

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

相当于

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

只是更难阅读。

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.

美男兮 2025-01-10 17:02:10

问题是逗号运算符的优先级是最低的。因此,条件运算符的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文