haccks
haccks

Reputation: 106012

Evaluation of operands in assignment operation

At this link, it is stated that

The order of evaluation of sub-expressions, including operands of operators (e.g., +, -, =, * , /) is Unspecified.

It is clear that in the expression

 foo1() = foo2() = foo3() = 7; // snippet of C++ code

the operands of the = operator can be evaluated in any order. Your compiler is free to choose (link, answered by Ralph Tandetzky).

This question may look foolish but I am really confused about this. Is the above statement also true if foo1(), foo2() and foo3() are replaced by a, b, and c? As in:

a = b = c = 7;

Upvotes: 0

Views: 131

Answers (1)

Jashaszun
Jashaszun

Reputation: 9270

It is exactly the same situation. The only difference is that in your second example, a, b, and c have no side effects. No matter which way your compiler's implementation decides to evaluate them, the result will still be the same since none of the evaluations does anything.

Upvotes: 3

Related Questions