Rule of three / five / zero
If you write one of the special member functions, you should write or delete all of them that the class needs. Three is destructor plus copy constructor plus copy assignment. Five adds move constructor and move assignment. Zero is the goal: members manage themselves, so you write none.
Detailed Explanation
The compiler generates special members you do not declare. The implicit destructor destroys members. The implicit copy copies members. The implicit move moves members (when it is allowed to exist). Those defaults are correct when every member is already a well-behaved value (string, vector, unique_ptr).
They are wrong when a member is a raw owning pointer. The implicit copy copies the pointer. Two destructors free it. That is the bug that created the rule of three: if you need a destructor to delete something, you also need a copy constructor and copy assignment that deep-copy (or = delete if the type should not be copied).
C++11 added moves. Declaring a destructor or a copy operation suppresses implicit moves. A class with a custom destructor then copies on return, slowly, unless you also write moves. That is the rule of five: destructor, copy ctor, copy assign, move ctor, move assign — define or delete each.
The rule of zero is the design that makes the other two rare. Store the resource in unique_ptr, vector, or a small RAII wrapper you wrote once. The outer class declares no special members. The compiler generates all five correctly. Exception safety and move performance come for free.
When you must write them (a raw FILE*, an arena pointer, a class that caches this):
- Write all five, or delete the copies and write the moves.
- Prefer
= defaultwhen memberwise is correct, so the class stays trivial if it can. - Prefer
= deleteon copies for exclusive-ownership types. - Mark moves
noexceptwhen they only steal pointers.
std::exchange and swap-based assignment are the usual implementation tools. Copy-and-swap implements copy assign with the strong guarantee.
A class that defines a destructor only to log or to be virtual is a common exception: you may = default the moves and copies explicitly so they come back. A virtual destructor on an interface that is not copied is = default plus deleted copies if the type is polymorphic and should not slice.
Code Examples
#include <memory>
#include <string>
struct User { // rule of zero
std::string name;
std::unique_ptr<int> id;
};
int main() {
User a{"ada", std::make_unique<int>(1)};
User b = std::move(a); // generated move
(void)b;
}#include <cstdio>
#include <utility>
class File { // rule of five
public:
explicit File(std::FILE* f) : f_(f) {}
~File() { if (f_) std::fclose(f_); }
File(const File&) = delete;
File& operator=(const File&) = delete;
File(File&& other) noexcept : f_(std::exchange(other.f_, nullptr)) {}
File& operator=(File&& other) noexcept {
if (this != &other) {
if (f_) std::fclose(f_);
f_ = std::exchange(other.f_, nullptr);
}
return *this;
}
private:
std::FILE* f_{};
};Common Gotchas
A user-declared destructor kills implicit moves
your class silently starts copying. Default or write the moves.
Custom destructor plus implicit copy of a raw pointer
double free. Delete the copies or write a deep copy.
Rule of zero is not "never write a destructor"
a virtual destructor on a base is still required. Default it, and delete copies if slicing is a risk.
`= default` move in the header with a PIMPL `unique_ptr<Impl>`
`Impl` is incomplete. Default the move in the `.cpp`.