根据标准,可以透明地替换对象。 https://eel.is/c++draft/basic#life-8
但是它是否允许多次替换并且仍然继续使用旧指针?
特别是在下面的示例中第二次调用 new (this) C(other) 仍然满足以下标准引用吗?
如果,在对象的生命周期结束后并且在原始对象占用之前<--- the storage which the object occupied is reused or released, a new object is created at the storage location, 指向原始对象的指针,引用的引用 到原始对象,或者原始对象的名称将 自动引用新对象,并且一旦该对象的生命周期结束 新对象已经启动,可以用来操作新对象,如果 原始对象可以透明地替换(见下文) 新对象
例如,以下来自标准的修改示例是否定义明确?
struct C {
int i;
void f();
const C& operator=( const C& );
};
const C& C::operator=( const C& other) {
if ( this != &other ) {
this->~C(); // lifetime of *this ends
new (this) C(other); // new object of type C created ( storage re-used after?)
new (this) C(other); // new object of type C created again ( storage re-used after again?)!
f(); // IS IT STILL well-defined?
}
return *this;
}
C c1;
C c2;
c1 = c2; // IS IT STILL well-defined?