Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of evaluation in initializer_list c++11

Tags:

c++

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() };
like image 841
linus linus Avatar asked Jul 10 '15 06:07

linus linus


1 Answers

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.

like image 142
rici Avatar answered Oct 27 '22 04:10

rici