Say I have the following:
struct A
{
int x;
};
//...
A* aOriginal = new A(); //value construct aOriginal
assert( aOriginal->x == 0 );
A* aSecond = new (aOriginal) A;
assert( aSecond->x == 0 );
Is the second assert guaranteed to hold, even though aSecond is not value-initialized? Logically, it should, because the memory isn’t overwritten, but is it specified by the standard?
No.
When you construct the second object on the same storage location, the lifetime of the previous one ends (§3.8/1):
When the second object is created, since
Ahas the implicit default constructor, thexmember is default-initialized, and thus no initialization is performed (§8.5/6):And this means the object has indeterminate value (§5.3.4/15):
And in case you think that the value is not indeterminate because you previously initialized another object on that storage location: the standard discards that possibility as well by saying the properties of the previous object no longer apply once its lifetime ends (§3.8/3):