Hi all,
Please consider the following example:
struct S {int a, b;};
S s{s.b = 1, 2};
Does this code well-defined?
The first question is whether the lifetime of s begins in the initializer. In my opinion, according to [basic.life] paragraph 1:
An object is said to have non-vacuous initialization if it is of a class or aggregate type and it or one of its subobjects is initialized by a constructor other than a trivial default constructor.
Here s is not initialized by a constructor, so the lifetime of s has begun even under initialization.
Is my understanding above wrong? If it is right, then the initializer s.b = 1 is well-defined, though s.b may have an unspecified value after the initialization of s.a according to [class.ctor] paragraph 14:
During the construction of an object, if the value of the object or any of its subobjects is accessed through a glvalue that is not obtained, directly or indirectly, from the constructor's this pointer, the value of the object or subobject thus obtained is unspecified.
Anyway, because the order of initializations is the element order [dcl.init.aggr] paragraph 6:
The initializations of the elements of the aggregate are evaluated in the element order. That is, all value computations and side effects associated with a given element are sequenced before those of any element that follows it in order.
The initialization of s.b will cause the value 2 to cover the unspecified value, thus s.b will eventually have a value 2.
However, GCC compiles the code and make s.b eventually have a value 1 (LIVE EXAMPLE).
Is there something wrong with my comprehension or it is just a GCC bug?
Thanks,
xskxzr