Short question, is the following ok:
struct X
{
A& x;
A y;
X() : x(y) {}
};
Reversing the order of the two members in the struct is definitely ok, since it guarantees y is initialized first, but does this work or invoke UB?
Moreso, would the following be ok:
struct X
{
X& x;
X() : x(*this) {}
};
?
I don’t think that would invoke undefined behavior.
I don’t see that case is any different from this:
The expression
new intis a pointer to an uninitialized int. Here we are initializing the pointerpwithnew int. The content is not read.Similarly,
Here we are initializing the reference
rwith*p. The content is not readIn both cases, the content is not read. Reading an uninitialized variable invokes undefined behavior. In both cases, it is the content which is uninitialized, not the address, and we are not reading the content.