Do the Requirements Placed on Function Arguments Also Apply to Initializer Lists?
Asked Answered
G

1

8

So I've read here: https://mcmap.net/q/109883/-is-it-legal-to-use-the-increment-operator-in-a-c-function-call that this is illegal:

foo(i++, i++);

But I believe that's because there is not a forced sequence, which I understand is the case for Initializer Lists. So is this legal code?

const int foo[] = { i++, i++ };
Gimlet answered 15/1, 2018 at 15:59 Comment(0)
F
9

Yes, the order of the evaluation of initializer-clauses is guaranteed in braced-init-list.

From the standard, §11.6.4/4 List-initialization [dcl.init.list]:

(emphasis mine)

Within the initializer-list of a braced-init-list, the initializer-clauses, including any that result from pack expansions, 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 ]

From cppreference.com:

Every initializer clause is sequenced before any initializer clause that follows it in the braced-init-list. This is in contrast with the arguments of a function call expression, which are unsequenced.

Example for the note of the standard,

struct A { A(int, int) {} };
...
int i = 0;
A a1(i++, i++); // used as the arguments of the constructor; unsequenced
A a2{i++, i++}; // used as the arguments of the constructor; sequenced, within the initializer-list of a braced-init-list
Fullfaced answered 15/1, 2018 at 16:6 Comment(2)
Can you please provide an example of the note section i.e. the constructor call part.Myongmyopia
@GauravSehgal Added to the answer.Fullfaced

© 2022 - 2024 — McMap. All rights reserved.