对类中的方法正确使用`= delete`

tow*_*owi 31 c++ operator-overloading rvalue-reference move-semantics c++11

下面的snipplet是否正确用于取消定义所有其他方法和类的构造函数?

struct Picture {

  // 'explicit': no accidental cast from string to Picture
  explicit Picture(const string &filename) { /* load image from file */ }

  // no accidental construction, i.e. temporaries and the like
  Picture() = delete;

  // no copy
  Picture(const Picture&) = delete;

  // no assign
  Picture& operator=(const Picture&) = delete;

  // no move
  Picture(Picture&&) = delete;

  // no move-assign
  Picture& operator=(Picture&&) = delete; // return type correct?
};
Run Code Online (Sandbox Code Playgroud)

这会删除每个默认的编译器实现,只留下析构函数,对吧?没有它,我猜这个类(几乎)无法使用,但我也可以删除它,对吗?

Picture&move-assign 的返回类型operator=(Picture&&)是否正确?如果我Picture&&为回归类型写的,它会有所作为吗?

How*_*ant 27

除了Xeo的回答:

是的,一切都是正确的.如果您希望可以删除所有已删除的成员,但删除的复制构造函数和已删除的副本分配具有相同的效果:

struct Picture {  // Also ok

  // 'explicit': no accidental cast from string to Picture
  explicit Picture(const string &filename) { /* load image from file */ }

  // no copy
  Picture(const Picture&) = delete;

  // no assign
  Picture& operator=(const Picture&) = delete;
};
Run Code Online (Sandbox Code Playgroud)

复制构造函数的显式声明禁止隐式生成默认构造函数,移动构造函数和移动赋值成员.明确删除这些成员是一个品味问题.有些人可能会将其视为良好的文档.其他人可能会认为它过于冗长.