Search code examples
c++copy-constructordefault-constructormove-constructormove-assignment-operator

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


I want to refresh my memory on the conditions under which a compiler typically auto generates a default constructor, copy constructor and assignment operator.

I recollect there were some rules, but I don't remember, and also can't find a reputable resource online. Can anyone help?


Solution

  • 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).