隐式移动和复制

请记住,声明析构函数会禁止编译器生成隐式移动构造函数和移动赋值运算符。如果声明析构函数,请记住还要为移动操作添加适当的定义。

此外,声明移动操作将抑制复制操作的生成,因此也应添加这些操作(如果此类的对象需要具有复制语义)。

class Movable {
public:
    virtual ~Movable() noexcept = default;

    //    compiler won't generate these unless we tell it to
    //    because we declared a destructor
    Movable(Movable&&) noexcept = default;
    Movable& operator=(Movable&&) noexcept = default;

    //    declaring move operations will suppress generation
    //    of copy operations unless we explicitly re-enable them
    Movable(const Movable&) = default;
    Movable& operator=(const Movable&) = default;
};