Is the comma operator free of side effects?

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 the expression c + = 2?

+5
c ++ expression
Oct 16 '11 at 2:05 a.m.
source share
5 answers

Yes, this is guaranteed by the standard if this comma is an unloaded operator of the comma. Quoting n3290 Β§5.18:

The comma operator groups from left to right.

expression:
expression assignment
expression, assignment expression

A pair of expressions separated by a comma is evaluated from left to right; the left expression is discarded, (section 5) 83 . Each value calculation and side effect associated with the left expression is sequenced until each value calculation and side effect associated with the correct expression. The type and value of the result is the type and value of the right operand; the result has the same category of values ​​as its right operand, and is a bit field if its right operand is a glvalue and a bit field.

And the corresponding footnote:

83 However, calling the overloaded comma operator is a normal function call; consequently, the estimates of its argument expression are unchanged relative to each other (see 1.9).

Therefore, this value is only for the non-overloaded comma operator.

, between function arguments are not comma operators. This rule also does not apply.

For C ++ 03, the situation is similar:

The comma operator groups from left to right.

expression:
expression assignment
expression, assignment expression

A pair of expressions separated by a comma is evaluated from left to right, and the value of the left expression is discarded. The lvalue-to-rvalue parameter (4.1), the pointer array (4.2), and the standard conversion of the to-pointer function (4.3) sions do not apply to the left expression. All side effects (1.9) of the left expression, except the destruction of time series (12.2), are performed until the evaluation of the correct expression. The type and value of the result are the type and value of the right operand; the result is an lvalue if its right operand.

The restrictions are the same: it does not apply to overloaded functions with a comma or function argument lists.

+9
Oct 16 '11 at 2:11 a.m.
source share

Yes, the comma operator ensures that the operators are evaluated in order from left to right, and the return value is a computed right statement.

Remember, however, that a comma in some contexts is not a comma operator. For example, the above is not guaranteed for function argument lists.

+7
Oct 16 '11 at
source share

Yes, in C ++, the comma operator is a point in the sequence, and this expression will be evaluated in the order in which they are written. See 5.18 in the current working draft:

[snip] is evaluated from left to right. [Incision]

I feel that your question is missing some explanation as to what you mean by β€œside effects”. Each statement in C ++ is allowed to have a side effect, and is also an overloaded comma operator.

Why is the expression you wrote invalid in the function call?

All about sequence points. In C ++ and C, it is forbidden to change the value twice inside between two points in a sequence. If your example really uses operator, each self-name is inside its own point in the sequence. If you use it like foo(c += 2, c -= 2) , the evaluation order is undefined. Actually, I'm not sure if the second case is undefined behavior, because I don't know if the argument list is one or more points in the sequence. I have to ask a question about this.

+4
Oct 16 '11 at
source share

It should always be evaluated from left to right, as it is defined in the definition of a comma operator: Link

+1
Oct 16 2018-11-11T00:
source share

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.

0
Oct. 16 '11 at 2:56 a.m.
source share



All Articles