简体   繁体   English

C ++中条件运算符中逗号运算符的优先级是什么?

[英]What's the precedence of comma operator inside conditional operator in C++?

What's happening here?这里发生了什么事?

#include <iostream>
using namespace std;

int main(){

    int x=0,y=0;
    true? ++x, ++y : --x, --y; 
    cout << "x: " << x << endl;
    cout << "y: " << y << endl; //why does y=0 here?

    x=0,y=0;
    false ? ++x, ++y : --x, --y; 
    cout << "x: " << x << endl;
    cout << "y: " << y << endl;
}

x: 1
y: 0

x: -1
y: -1

The second case seems fine.第二种情况似乎不错。 I would expect both x and y to increment to 1 in the first case but only the left hand operand increments.我希望在第一种情况下 x 和 y 都增加到 1 但只有左侧操作数增加。

The first one is equivalent to:第一个相当于:

(true  ? (++x, ++y) : (--x)), --y; 

The second one is equivalent to:第二个相当于:

(false ? (++x, ++y) : (--x)), --y; 

Thus the --y is always executed.因此--y总是被执行。 In the first line, the increments are executed first so x = 1, y = 0 is expected.在第一行中,首先执行增量,因此x = 1, y = 0是预期的。 In the second line, the decrement of x is executed first so x = -1, y = -1 is expected.在第二行中,首先执行x的递减,因此x = -1, y = -1是预期的。


As noted in a comment (to another answer) by Barmar :正如Barmar的评论(对另一个答案)所述

And in case anyone is wondering why the comma between ++x and ++y doesn't have the same effect, it's because (true? ++x) would not be valid at all.如果有人想知道为什么++x++y之间的逗号没有相同的效果,那是因为(true? ++x)根本无效。 So the compiler keeps scanning until it finds the : , but beyond that it stops when it reaches a lower precedence operator [( , in this example) or the end of statement] .因此,编译器会一直扫描,直到找到: ,但除此之外,它会在到达较低优先级运算符[( ,在本例中) 或语句结尾] 时停止

The y is zero because comma has the lowest precedence among all C++ operators . y为零是因为逗号 在所有 C++ 运算符中的优先级最低 Because its precedence is lower than that of the ternary conditional operator, the conditional operators are parsed as true? ++x, ++y : --x因为它的优先级低于三元条件运算符,所以条件运算符被解析为true? ++x, ++y : --x true? ++x, ++y : --x and false? ++x, ++y : --x true? ++x, ++y : --xfalse? ++x, ++y : --x false? ++x, ++y : --x . false? ++x, ++y : --x In both cases, the --y statement is executed unconditionally .在这两种情况下,-- --y语句都是无条件执行的。

EDIT The first comma is different because the compiler has found a ?编辑第一个逗号是不同的,因为编译器发现了一个? , so now it needs a : to complete the "when true" expression of the conditional. ,所以现在它需要一个:来完成条件的“when true”表达式。 That is why both ++x and ++y are taken in.这就是++x++y都被采用的原因。

Read the standard阅读标准

§5.18 Comma operator [expr.comma] §5.18 逗号运算符 [expr.comma]

¶1 The comma operator groups left-to-right. ¶1 逗号运算符从左到右分组。

 expression: assignment-expression expression , assignment-expression

A pair of expressions separated by a comma is evaluated left-to-right;一对用逗号分隔的表达式从左到右求值; the left expression is a discardedvalue expression (Clause 5).左边的表达式是一个丢弃值表达式(第 5 条)。 83 Every value computation and side effect associated with the left expression is sequenced before every value computation and side effect associated with the right expression. 83与左表达式相关的每个值计算和副作用在与右表达式相关的每个值计算和副作用之前排序。 The type and value of the result are the type and value of the right operand;结果的类型和值是右操作数的类型和值; the result is of the same value category as its right operand, and is a bit-field if its right operand is a glvalue and a bit-field.结果与其右操作数属于相同的值类别,并且如果其右操作数是泛左值和位域,则结果是位域。

¶2 In contexts where comma is given a special meaning, [ Example: in lists of arguments to functions (5.2.2) and lists of initializers (8.5) —end example ] the comma operator as described in Clause 5 can appear only in parentheses. ¶2 在逗号被赋予特殊含义的上下文中,[ 示例:在函数参数列表 (5.2.2) 和初始值设定项列表 (8.5) 中 - 结束示例 ] 第 5 条中描述的逗号运算符只能出现在括号中. [ Example: [ 例子:

 f(a, (t=3, t+2), c);

has three arguments, the second of which has the value 5. —end example ]有三个参数,其中第二个的值为 5。--end example ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM