From a comment on this answer:
Class members are initialized in their order of declaration. By this logic, the following constructor should invoke undefined behaviour:
struct Foo
{
Bar a;
Bar b;
Foo(Bar c) : a(b = c) { }
};
Patently, we are assigning to b
first before a
has been initialized. Assigning to an uninitialized object should be UB. It's not surprising that the code "works" with Bar = int
, but if I make Bar
a heavy class with constructors, I see that b
does indeed get initialized before a
.
(For extra lunacy, we can even say Foo(Bar c, Bar d) : a(b = c), b(d) { }
, still with no warning.)
Yet GCC 4.6.1 doesn't warn about this. Is this acceptable, well-defined behaviour, or is this strictly wrong?