What is calculated earlier in vector element assignment

Viewed 106

If i have that line of code:

vec[f1(x, y)] = f2(a, b);

What would compiler run first: f1(x, y) or f2(a, b)?

1 Answers

f2(a, b) is computed first, per [expr.ass#1]1 (emphasis is mine):

[...] In all cases, the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression. The right operand is sequenced before the left operand. [...]

Note 1: that this is only true for C++17 onward, for previous standards, the order of evaluation was implementation-specific.

Note 2: This is not specific to vector item assignment (there is no such thing in the standard), any assignment (or compound assignment) follows these rules.

1 This is from the C++17 latest draft (section 8.18), but the wording has not changed (yet), see [expr.ass#1] in the latest draft.

Related