Search code examples
c++c++11default-constructordefaulted-functions

What are the pros and cons of always declaring defaulted constructors?


What are the pro and cons of declaring always defaulted constructors for each non user-defined constructor?

Consider a class with a user-defined constuctor which does not need other user-defined constructors, it would be:

class Foo
{
public:
   Foo() { // user-defined declaration }
   Foo(const Foo&) = default;
   Foo(Foo&&) noexcept = default;
   ~Foo() = default;

   Foo& operator=(const Foo&) = default;
   Foo& operator=(Foo&&) = default;
}

There are other actual advantages/disadvantages of doing that?


Solution

  • Several disadvantages of the top of my head:

    1. You can forget special functions. The example you show doesn't define copy/move assignment operators.
    2. declaring the destructor as default in the header will make it so you are unable to use forward declared classes in e.g. a std::unique_ptr member of a class. You can mitigate this by either including the class definition (leading to an eventual overall increase in (re)build time for non-trivial projects, especially when the not forward declared class changes often). You can mitigate this by moving the = default to a definition in a source file.
    3. (subjective) It overloads the class definition visually: every capable C++ developer knows these are usually automatically generated anyway, so they provide no extra information although they take time to read and understand. Note that I'm only talking about the "everything default" case, so no special functions are deleted or some such.
    4. Adding a move-only data member will require you to also remove the =default copy constructor and assignment operators. Whether or not you want this to be necessary is partially a question of style, but this does lead to more involved code changes, especially when refactoring classes like these in a more general sense.
    5. If any other special compiler-generated functions are added to later versions of C++, you miss them automatically, whereas otherwise you'd have already had them implicitly defined.

    There are probably some more, rising in subtility level beyond these.