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 most classes, the good practice is to organize your code so that you can use these compiler
generated versions, which is known as the "Rule of Zero").
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 when
the manually written function is equivalent to the default implementation, this is an issue because:
- It’s more code to write, test, and maintain for no good reason
- Correctly writing the code of those functions 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 can often use the default version if you use in-class initialization instead of the initializer list. You must 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;
};