编译器如何根据优先级和关联性来解释此表达式?

Yue*_*ang 5 c++

这是来自C++ Primer 5th的练习:

Exercise 4.33: Explain what the following expression does(Page158):
someValue ? ++x, ++y : --x, --y
Run Code Online (Sandbox Code Playgroud)

代码:

bool someVlaue = 1;
int x = 0;
int y = 0;
someVlaue ? ++x, ++y : --x,--y;
std::cout << x << std::endl << y << std::endl;
Run Code Online (Sandbox Code Playgroud)

我试过Gcc4.81Clang3.5,都递给我:

1
0
Press <RETURN> to close this window...
Run Code Online (Sandbox Code Playgroud)

为什么不11?谁能解释它是如何被解释的?

Mic*_*urr 11

由于逗号运算符的优先级非常低,因此表达式

someValue ? ++x, ++y : --x,--y;
Run Code Online (Sandbox Code Playgroud)

相当于:

(someValue ? ++x, ++y : --x),--y;
Run Code Online (Sandbox Code Playgroud)

因此,++x, ++y表达被执行(设置xy为1),其次是表达--y在结束,恢复y到0.

注 - 逗号运算符引入了一个序列点,因此不会y多次修改未定义的行为.