문제

I am trying to figure out how the following expression is grouped according to precedence:

int x = 0, y = 0;
someValue ? ++x, ++y : --x, --y;

When someValue is 0 (false) the second part is evaluated, and both x and y become -1. However if someValue is 1 (true) then somehow x becomes 1 but y remains 0, which means the second part of the conditional expression is evaluated too, but only the --y part, which I assume is because of the comma operator, but I can't for the life of me figure out how this is achieved. Any insight would be appreciated.

도움이 되었습니까?

해결책

, has the lowest precedence of all operators, so your expression is parsed as

(someValue ? ++x, ++y : --x), --y;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top