linus linus
linus linus

Reputation: 171

Order of evaluation in initializer_list c++11

In the code below is it required that f1 be called before f2 (or vice-versa) or is it unspecified?

int f1();
int f2();

std::initializer_list<int> list { f1(), f2() };

Upvotes: 10

Views: 408

Answers (1)

rici
rici

Reputation: 241681

This is one interesting corner of the C++ standard where execution order is well defined. Section 8.5.4 [dcl.init.list], paragraph 4:

Within the initializer-list of a braced-init-list, the initializer-clauses, including any that result from pack expansions (14.5.3), are evaluated in the order in which they appear. That is, every value computation and side effect associated with a given initializer-clause is sequenced before every value computation and side effect associated with any initializer-clause that follows it in the comma-separated list of the initializer-list.

So in the initializer list, the function calls are evaluated left-to-right.

Upvotes: 11

Related Questions