Passing references to later members during construction

Viewed 55

Suppose we have the following:

struct C;
struct B {
  C& c;
  B(C& c) : c{c} {}
};

struct A;
struct C {
  A& a;
  C(A& a) : a{a} {}
};

struct A {
  B b;
  C c;
  A() : b(c), c(*this) {}
};

It seems to be legal to initialize member variable c with this because this can be used to refer to the object being initialized in the initializer list. But when passing c to b's constructor, c's lifetime has not begun and since it does have a reference member, an old pointer cannot be used to manipulate the new C object.

I believe the above is well behaved / intuitively reasonable as b never uses it's c reference before the object c is fully constructed though.

What part of the language specifies that this works appropriately?

Edit: to clarify, what makes it legal to access c through b after construction for the A object has been completed? i.e after all lifetimes have begun.

0 Answers
Related