All special member functions (default constructor, copy and move constructors, copy and move assignment operators, destructor) can be automatically generated by the compiler if you don't prevent it (for many classes, it is good practice to organize your code so that you can use these default versions, see S4963).


There are cases where it's still useful to manually write such a function, because the default implementation is not doing what you need. But if the manually written function is equivalent to the default implementation, this is an issue:

* It's more code to write, test and maintain for no good reason
* Writing the code of those functions correctly is surprisingly difficult
* Once you write one such function, you will typically have to write several (see S3624) 
* If you want your class to be _trivial_ or to be an _aggregate_, those functions cannot be user-provided anyways

In most cases, you should just remove the code of the redundant function. In some cases, the compiler will not automatically generate the default version of the function, but you can force it to do so by using the ``++= default++`` syntax. 


For default constructors, you will often be able to use the default version if you use in-class initialization instead of the initializer list (see S5424). You will have to make it explicitly defaulted if your class has any other constructor.


For destructors, you may want to use the ``++=default++`` syntax to be able to declare it as virtual (see S1235).


This rule raises an issue when any of the following is implemented in a way equivalent to the default implementation:

* default constructor
* destructor
* move constructor
* move-assignment operator
* copy constructor
* copy-assignment operator


== Noncompliant Code Example

----
struct Book {
  string Name;

  Book() { } // Noncompliant
  Book(const Book &Other) : Name(Other.Name) { } // Noncompliant
  Book &operator=(const Book &);
};

Book &Book::operator=(const Book &Other) { // Noncompliant
  Name = Other.Name;
  return *this;
}
----


== Compliant Solution

----
struct Book {
  string Name;

  Book() = default; // Restores generation of default
  Book(const Book &Other) = default;
  Book &operator=(const Book &) = default;
};

// Or, more common:
struct Book {
  string Name;
};
----


== See

* https://github.com/isocpp/CppCoreGuidelines/blob/036324/CppCoreGuidelines.md#c30-define-a-destructor-if-a-class-needs-an-explicit-action-at-object-destruction[{cpp} Core Guidelines C.30] - Define a destructor if a class needs an explicit action at object destruction


ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)

include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]
