You have two questions.
First question: "Is the operator a comma-free side effect?"
The answer is no. The comma operator naturally makes it easier to write expressions with side effects, and intentionally writing expressions with side effects is what the operator is usually used for. For example, in while (cin >> str, str != "exit") state of the input stream changes, which is an intentional side effect.
But maybe you do not mean a side effect in the sense of computer science, but in a specific sense.
Your second question: "For example, for such a statement: c += 2, c -= 1 Is it true that c + = 2 will always be evaluated first, and c in the second expression c- = 1 will always be updated with the value from expression c + = 2? "
The answer is yes in the case of an operator or expression, unless the comma operator is overloaded (very unusual). However, sequences like c += 2, c -= 1 can also be found in argument lists, in which case what you have is not an expression, and the comma is not a sequence operator, and the order of evaluation is not defined. In foo(c += 2, c -= 1) comma is not a comma operator, but in foo((c += 2, c -= 1)) it is, so it can pay to pay attention to the parentheses in function calls.
Bruce Attah Oct. 16 '11 at 2:56 a.m. 2011-10-16 14:56
source share