移动分配是通过destruct + move构造安全吗?

Bja*_*une 8 c++ placement-new move-semantics c++11 move-assignment-operator

这是使用移动构造函数为大多数类定义移动赋值的一种非常简单的方法:

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;
  }
  // ...
};
Run Code Online (Sandbox Code Playgroud)

这个调用你自己的析构函数的序列是否跟随在标准C++ 11中的this指针安全位置新增?

Pet*_*ker 6

只有你永远不会从这个类中派生出一个类型.如果这样做,这将把对象变成怪物.遗憾的是,该标准以此作为解释对象生命周期的一个例子.在现实世界的代码中做这件事真的很糟糕.