Do the requirements placed on the function arguments also match in the initializer lists?

So, I read here: https://stackoverflow.com/a/3606266/2326 that this is illegal:

foo(i++, i++); 

But I believe that since there is no forced sequence that I understand, this is the case for initializer lists. So, this legal code?

 const int foo[] = { i++, i++ }; 
+7
c ++ language-lawyer arguments initializer-list
source share
1 answer

Yes, the evaluation order of initializer offers is guaranteed in the bit-init list.

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

(my accent)

In the list of initializers of the list with binding-initialization initializer-clauses, including any that result from decomposition of packages, are evaluated in the order in which they appear. That is, each value calculation and side effect associated with a given initializer parameter is sequenced to each value calculation and side effect with any initializer sentence that follows it in a comma-separated list of initializers list. [Note. This ranking of the evaluation is independent of the semantics of initialization; for example, this applies when elements of an initializer list are interpreted as arguments to a constructor call, although there is usually no sequence restriction on call arguments. - final note]

From cppreference.com :

Each initializer clause is sequenced before any initializer that follows it in the bit-init list. This contrasts with the arguments to the function call expression , which are unsequenced .

An example of a standard note,

 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 
+8
source share

All Articles