Is the order of initialization guaranteed by the standard?
c++
Solution
I don't have the standard handy right now so I can't quote the section, but structure or class member initialisation always happens in declared order. The order in which members are mentioned in the constructor initialiser list is not relevant.
Gcc has a warning `-Wreorder` that warns when the order is different:
-Wreorder (C++ only)
Warn when the order of member initializers given in the code does
not match the order in which they must be executed. For instance:
struct A {
int i;
int j;
A(): j (0), i (1) { }
};
The compiler will rearrange the member initializers for i and j to
match the declaration order of the members, emitting a warning to
that effect. This warning is enabled by -Wall.
Problem
In the following code snippet, `d1`'s initializer is passed `d2` (which has not been constructed yet, correct?) So is the `d.j` in `D`'s copy constructor an uninitialized memory access? ``` struct D { int j; D(const D& d) { j = d.j; } D(int i) { j = i; } }; struct A { D d1; D d2; A() : d2(2), d1(d2) {} }; ``` Which section of the C++ standard discusses the initialization order of data members?