In a given place in an aggregate initialization list, are values passed into previous places safe to read from the corresponding members? [duplicate]

StackOverflow https://stackoverflow.com/questions/21294831

  •  01-10-2022
  •  | 
  •  

Question

struct
{
    int a,b;
} s = {5, s.a+1};

According to the standard, is it safe to read "s.a" in the above example, so that s is initialized to a=5 and b=6? If so, do most compilers obey this rule?

(The above compiles in VC10.)

Was it helpful?

Solution

Yes, because aggregate member initialization is sequenced.

[dcl.init.aggr]/2 has:

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. Each member is copy-initialized from the corresponding initializer-clause.

[dcl.init.list]/4 has:

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.

The copy-initialization of an aggregate member is certainly a side effect ([intro.execution]/12), and must be "associated with" the corresponding initializer-clause, because that is its full-expression (since an initializer-list is not an expression).

Every up-to-date compiler I have tried (MSVC, Clang, g++) compiles this correctly. It may be possible that some older compilers get it wrong (older versions of g++ were known for getting the sequencing of aggregate initializers wrong).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top