Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditions for automatic generation of default/copy/move ctor and copy/move assignment operator?

People also ask

Is move constructor automatically generated?

No move constructor is automatically generated.

What does a move assignment operator do?

In the C++ programming language, the move assignment operator = is used for transferring a temporary object to an existing object. The move assignment operator, like most C++ operators, can be overloaded. Like the copy assignment operator it is a special member function.

What does a default move constructor do?

For non-union class types (class and struct), the move constructor performs full member-wise move of the object's bases and non-static members, in their initialization order, using direct initialization with an xvalue argument.

What is the default copy constructor C++?

The default constructor creates the exact copy or shallow copy of the existing object. Thus, the pointer p of both the objects point to the same memory location.


In the following, "auto-generated" means "implicitly declared as defaulted, but not defined as deleted". There are situations where the special member functions are declared, but defined as deleted.

  • The default constructor is auto-generated if there is no user-declared constructor (§12.1/5).
  • The copy constructor is auto-generated if there is no user-declared move constructor or move assignment operator (because there are no move constructors or move assignment operators in C++03, this simplifies to "always" in C++03) (§12.8/8).
  • The copy assignment operator is auto-generated if there is no user-declared move constructor or move assignment operator (§12.8/19).
  • The destructor is auto-generated if there is no user-declared destructor (§12.4/4).

C++11 and later only:

  • The move constructor is auto-generated if there is no user-declared copy constructor, copy assignment operator or destructor, and if the generated move constructor is valid (§12.8/10).
  • The move assignment operator is auto-generated if there is no user-declared copy constructor, copy assignment operator or destructor, and if the generated move assignment operator is valid (e.g. if it wouldn't need to assign constant members) (§12.8/21).

I've found the diagram below very useful.

C++ rules for automatic constructors and assignment operators from Sticky Bits - Becoming a Rule of Zero Hero


C++17 N4659 standard draft

For a quick cross standard reference, have a look at the "Implicitly-declared" sections of the following cppreference entries:

  • https://en.cppreference.com/w/cpp/language/copy_constructor
  • https://en.cppreference.com/w/cpp/language/move_constructor
  • https://en.cppreference.com/w/cpp/language/copy_assignment
  • https://en.cppreference.com/w/cpp/language/move_assignment

The same information can of course be obtained from the standard. E.g. on C++17 N4659 standard draft:

15.8.1 "Copy/move constructors" says for for copy constructor:

6 If the class definition does not explicitly declare a copy constructor, a non-explicit one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted (11.4). The latter case is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor.

and for move constructor:

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

  • (8.1) — X does not have a user-declared copy constructor,

  • (8.2) — X does not have a user-declared copy assignment operator,

  • (8.3) — X does not have a user-declared move assignment operator, and

  • (8.4) — X does not have a user-declared destructor.

15.8.2 "Copy/move assignment operator" says for copy assignment:

2 If the class definition does not explicitly declare a copy assignment operator, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy assignment operator is defined as deleted; otherwise, it is defined as defaulted (11.4). The latter case is deprecated if the class has a user-declared copy constructor or a user-declared destructor.

and for move assignment:

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

  • (4.1) — X does not have a user-declared copy constructor,
  • (4.2) — X does not have a user-declared move constructor,
  • (4.3) — X does not have a user-declared copy assignment operator, and
  • (4.4) — X does not have a user-declared destructor.

15.4 "Destructors" says it for destructors:

4 If a class has no user-declared destructor, a destructor is implicitly declared as defaulted (11.4). An implicitly-declared destructor is an inline public member of its class.