Consider:
struct A { void do_something() {} };
struct B {
A& a;
B(A& a) : a{a} { a.do_something(); }
};
struct C {
A a; B b;
C(A& a) : b{a} {}
};
int main() {
C c{ c.a };
}
It seems possible that this could be made to work, because:
- even before
c
is initialized, we know its memory layout and the address ofc.a
- we don't actually use
c.a
until it is initialized.
Additionally, I didn't get a warning under a few different compilers.
However, I experienced some extremely odd behaviour (a bit later on) that could only be down to doing something undefined, and which only went away when I reorganised my program to avoid this pattern.
Thanks.