여기에 이동 생성자와 함께 대부분의 모든 클래스에 대한 이동 할당을 정의하는 아주 쉬운 방법 :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'을 사용하여 값을 객체로 바꿀 수 있습니다. –