30

This is supposed to be a trivial question but I could not find it explicitly on stackoverflow.

The following will be defined implicitly if not provided by the user.

  1. default (parameterless) constructor
  2. copy constructor
  3. copy assignment operator
  4. destructor

But I have read somewhere (which I cant seem to find now), that there are some conditions where the compiler will not implicitly implement them.

What are these conditions?

aiao
  • 4,621
  • 3
  • 25
  • 47

1 Answers1

53

The Default Constuctor (e.g., X()) will not be implicitly generated if:

  • you have explicitly declared any constructor at all
  • there is a data member that is not default-constructible (such as a reference, a const object, or a class with no or inaccessible default constructor)
  • (C++11) you have explicitly told the compiler to not generate one using X() = delete;

The Copy Constructor (e.g., X(const X&)) will not be implicitly generated if:

  • you have explicitly declared a copy constructor (for class X a constructor taking X, X& or const X&)
  • there is a data member that is not copy-constructible (such as a class with no or inaccessible copy constructor)
  • the base class is not copy-constructible
  • (C++11) you have declared a move constructor or move assignment operator
  • (C++11) you have explicitly told the compiler to not generate one using X(const X&) = delete;

The Copy Assignment Operator (e.g., X& operator=(const X&)) will not be implicitly generated if:

  • you have explicitly declared a copy assignment operator (for class X an operator= taking X, X& or const X&)
  • there is a data member in your class that is not copy-assignable (such as a reference, a const object, or a class with no or inaccessible assignment operator)
  • the base class is not copy-assignable
  • (C++11) you have declared a move constructor or move assignment operator
  • (C++11) you have explicitly told the compiler to not generate one using X& operator=(const X&) = delete;

The Destructor (e.g., ~X()) will not be implicitly generated if:

  • you have explicitly declared a destructor
  • (C++11) you have explicitly told the compiler to not generate one using ~X() = delete;

The Move Constructor (C++11) (e.g., X(X&&)) will not be implicitly generated if:

  • you have explicitly declared a move constructor (for class X, a constructor taking X&&)
  • you have declared a copy assignment operator, copy constructor, destructor, or move assignment operator
  • there is a data member in your class that cannot be move-constructed (is const, is a reference, or has a deleted, inaccessible, or ambiguous move constructor)
  • the base class cannot be move-constructed
  • you have explicitly told the compiler to not generate one using X(X&&) = delete;

The Move Assignment Operator (C++11) (e.g., X& operator=(X&&)) will not be implicitly generated if:

  • you have explicitly declared a move assignment operator (for class X, an operator= taking X&&)
  • you have declared a copy assignment operator, copy constructor, destructor, or move constructor
  • there is a data member in your class that cannot be move-assigned (is const, is a reference, or has a deleted, inaccessible, or ambiguous move assignment operator)
  • the base class cannot be move-assigned
  • you have explicitly told the compiler to not generate one using X& operator=(X&&) = delete;
Richard Hansen
  • 51,690
  • 20
  • 90
  • 97
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • Well, in all those second points, it will be implicitly defined, just as `delete`d. – Joseph Mansfield Mar 23 '13 at 19:06
  • 1
    +1 Don't forget the C++11 additions where you can explicitly declare that they should not be created (`A(const A&) = delete;`), and you should add Move Constructors for completeness. – pickypg Mar 23 '13 at 19:10
  • @pickypg: I'm not really sure about move constructors, I'd be happy if you added the info you wanted to my answer – Armen Tsirunyan Mar 23 '13 at 19:11
  • 1
    Copy constructor and assignment will also only be implicitly generated, when "each direct base B of T has a copy assignment operator whose parameters are B or const B& or const volatile B&" (according to cppreference). – Robert May 26 '15 at 16:19
  • I don't understand why if there is a reference, it cannot be copy construct-able. Please explain. Can't you copy a reference from other reference? – Narek Apr 27 '16 at 19:48
  • @Narek: That's a mistake. Fixed now. You can have a reference and still be copyable. Fixed now – Armen Tsirunyan Apr 28 '16 at 09:37
  • Thanks, Armen jan. I just see whom I talk to :) – Narek Apr 28 '16 at 11:04