I know that when I define an empty class and provide no declarations at all, the compiler will provide definitions for the default and copy constructor, destructor and copy assignment operator.
What are the rules for that? When does the compiler not provide a, say, copy constructor? What about the move constructor and move assignment operator?
(Example: The compiler will not provide definitions for any assignment operator if my class has a reference member like int&
. When else will something like this happen?)
Edit: In C++11 it's more complicated than implicitly declared or not. They can either be implicitly declared and defaulted, implicitly declared and deleted, or undeclared. Read this to distinguish the latter 2. The following information isn't entirely correct since it doesn't distinguish declared and deleted vs undeclared.
The following is a work in progress. (?)
indicates I would like to clarify or quantify the statement.
Special Member Functions §12/1
The implementation will implicitly declare these member functions for some class types when the user does not explicitly declare them:
- default constructor
- copy constructor
- copy assignment operator
- move constructor
- move assignment operator
- destructor
A special member function will NOT be implicitly declared if the type has a...
Default Constructor §12.1/5
Copy Constructor §12.8/8, §12.8/12
- user-declared move constructor
- user-declared move assignment operator
- non-static data member of rvalue reference type
- variant member with a non-trivial copy constructor and is a union-like class (?)
- non-static data member of type (or array thereof) that cannot be copied
- direct or virtual base class without accessible copy constructor
*Such an implicit declaration is deprecated if the class has a user-declared copy assignment operator or a user-declared destructor (?)
Copy Assignment Operator §12.8/19, §12.8/24
- user-declared move constructor
- user-declared move assignment operator
- a variant member with a non-trivial copy assignment operator and is a union-like class (?)
- non-static data member of const non-class type (or array thereof)
- non-static data member of reference type
- non-static data member with inaccessible copy assignment operator
- direct or virtual base class with inaccessible copy assignment operator
*Such implicit declaration is deprecated if the class has a user-declared copy constructor or a user-declared destructor (?)
Move Constructor §12.8/10, §12.8/12
- user-declared copy constructor
- user-declared copy assignment operator
- user-declared move assignment operator
- user-declared destructor
- move constructor would not be implicitly defined as deleted (?)
- variant member with a non-trivial move constructor and is a union-like class (?)
- non-static data member of type (or array thereof) that cannot be moved
- non-static data member or direct or virtual base class with a type that does not have a move constructor and is not trivially copyable
- direct or virtual base class without accessible move constructor
Move Assignment Operator §12.8/21, §12.8/24
- user-declared copy constructor
- user-declared move constructor
- user-declared copy assignment operator
- user-declared destructor
- move assignment operator would not be implicitly defined as deleted (?)
- a variant member with a non-trivial move assignment operator and is a union-like class (?)
- non-static data member of const non-class type (or array thereof)
- non-static data member of reference type
- non-static data member with inaccessible move assignment operator and is not trivially copyable
- direct or virtual base class with inaccessible move assignment operator and is not trivially copyable
Destructor §12.4/4