ここでは移動コンストラクタで、ほとんどの任意のクラスのための移動の割り当てを定義するための非常に簡単な方法です:destruct + move構造による移動代入は安全ですか?
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;
}
// ...
};
は、標準C++ 11でこのポインタ安全上の新しい配置に続いて、独自のデストラクタを呼び出して、この一連のです?
あなたの移動コンストラクタは 'noexcept'である方が良いでしょう。もしUB土地でスローされてさまよっていれば、既に破壊されたオブジェクトを破壊しようとします。 – ildjarn
移動/コピー割り当てのための良いトリックは単純に[値によってパラメータを取る]ことです(http://stackoverflow.com/questions/9746748/does-it-make-sense-to-reuse-destructor-logic-by- using-stdswap-in-a-move-assign/9746772#9746772)。ユーザーは、値のパラメータを移動または構成するか(またはその中に入る)かのどちらかを行います。次に、 'std :: swap'を使って値をあなたのオブジェクトに入れ替えることができます。 –