The following C++20 program is accepted without any warning in all compiles I have tried:
struct A { const int & x = z; int y = x; int z; };
int main()
{
return A{.z=3}.y;
}
https://gcc.godbolt.org/z/nqb95zb7c
But every program returns some arbitrary value. Is it right to assume that this is undefined behavior?
Members are initialized in the order they appear in the class definition, hence the designated initializer is not that relevant, and also this
is undefined for the same reason.
See also the example from cppreference:
The example is actually to illustrate something else, but it also shows how members are initialized in order.