Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of operation when performing dynamic initialization of arrays

I skimmed through section dcl.init.aggr and couldn't find a clear answer.

Consider:

static int x[2] = { f(), g() };

Does the standard say which gets initalized first: x[0] or x[1] ?

In other words, which function runs first: f(), or g() ?

like image 228
Earl Avatar asked Nov 04 '11 23:11

Earl


1 Answers

Here are some relevant excerpts from the standard that answer your question:

8.5.1/2 "When an aggregate is initialized by an initializer list, as specified in 8.5.4, the elements of the initializer list are taken as initializers for the members of the aggregate, in increasing subscript or member order."

8.5.4/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. [ Note: This evaluation ordering holds regardless of the semantics of the initialization; for example, it applies when the elements of the initializer-list are interpreted as arguments of a constructor call, even though ordinarily there are no sequencing constraints on the arguments of a call. —end note ]

like image 176
Gene Bushuyev Avatar answered Oct 24 '22 03:10

Gene Bushuyev