特殊成员函数的生成规则

 

大体的规则

image-1


以上都是用户自定义了某个特殊成员函数之后,编译器会不会自动生成其他特殊成员函数的规则。


删除了拷贝构造就不会生成移动构造2

If the definition of a class X does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if

— X does not have a user-declared copy constructor,

— X does not have a user-declared copy assignment operator,

— X does not have a user-declared move assignment operator, and

— X does not have a user-declared destructor.

注意看第一条 X does not have a user-declared copy constructor

根据参考资料2的第二个回答对 user-declared 的解释,删除的函数也算是用户自定义的函数

Note the user-declared. But if you look at §8.4.3:

A function definition of the form:

attribute-specifier-seqopt decl-specifier-seqopt declarator virt-specifier-seqopt = delete ;

is called a deleted definition. A function with a deleted definition is also called a deleted function.

A program that refers to a deleted function implicitly or explicitly, other than to declare it, is ill-formed.

So the standard defines deleted functions as user-declared

带有 unique_ptr 的类会删除拷贝构造3

因为 unique_ptr 不能拷贝,所以编译器会删除拷贝构造函数。

参考资料

  1. What are the rules for automatic generation of move operations?
  2. Default move constructor/assignment and deleted copy constructor/assignment
  3. Deleted copy constructor when member is an unique_ptr [duplicate]