Is the following well-defined?
class A;
class B;
// define A, which takes B& in constructor
// define B, which takes A& in constructor
class C
{
A a;
B b;
public:
C() : a(b), b(a) { /* stuff with a and b */ }
}
Full example at ideone.com.
Is it safe/well-defined so long as the constructors for A
and B
don't do anything with the references they get?
N4140 [class.cdtor]/1 reads:
While this passage itself doesn't imply that the behavior is otherwise well-defined, the following example shows that it is. Here is an excerpt:
So the answer is: yes, the behavior in your case is well defined if you aren't referring to members or base classes of
b
in the constructor ofA
.