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?

0

1 Answer 1

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;
7
  • Well, in all those second points, it will be implicitly defined, just as deleted. Mar 23, 2013 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, 2013 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 Mar 23, 2013 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, 2015 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, 2016 at 19:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.