Why is this an issue?
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 {rule:cpp: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 {rule:cpp: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
{rule:cpp: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 {rule:cpp: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;
};
Resources