隱式移動和複製

請記住,宣告解構函式會禁止編譯器生成隱式移動建構函式和移動賦值運算子。如果宣告解構函式,請記住還要為移動操作新增適當的定義。

此外,宣告移動操作將抑制複製操作的生成,因此也應新增這些操作(如果此類的物件需要具有複製語義)。

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;
};