Here's a very easy way to define move assignment for most any class with a move constructor:
class Foo {
public:
Foo(Foo&& foo); // you still have to write this one
Foo& operator=(Foo&& foo) {
if (this != &foo) { // avoid destructing the only copy
this->~Foo(); // call your own destructor
new (this) Foo(std::move(foo)); // call move constructor via placement new
}
return *this;
}
// ...
};
Is this sequence of calling your own destructor followed by placement new on the this pointer safe in standard C++11?