I reduced this:
struct A
{
int * x;
A() : x( x = new int() )
{
}
};
to the following:
int m = m = 3;
//or
struct X;
//...
X x = x = X();
Seems legal to me. I don’t see why you’d want to do it, but is it legal? Are there cases where you’d want to do this (not the int case, I realize that’s completely useless)?
It depends on how you define “legal”. It will compile; that doesn’t mean that it is guaranteed to work.
Until the full statement
X x = ...executes,xis uninitialized. It is not anXyet. Therefore, performingx = X()means to create a temporaryXand callX::operator=(const X&)on the uninitialized variablex.Calling a function on a non-POD class instance that has not been initialized (who’s constructor has not yet been called) yields undefined behavior. If
Xis a POD type (or trivial in C++11), then it will work. But otherwise no.