C++C++ Explainer
C++ · 3 · Memory & Smart Pointers14 / 36 · 39%

Rule of 0 / 3 / 5

Which special member functions you must write — usually none.

Examples: copy ctor, assignment, destructor, =default, =delete

shortcuts: ← prev · → next · M mark
1

Rule of Zero

If your members manage themselves, write none of the five.

Example
example
class Config {
    std::string name_;
    std::vector<int> values_;
    // no destructor, no copy, no move — all correct by default
};
2

The Five

If you write one, you almost certainly need all five.

Example
example
class Buffer {
public:
    ~Buffer();                              // 1 destructor
    Buffer(const Buffer&);                  // 2 copy ctor
    Buffer& operator=(const Buffer&);       // 3 copy assign
    Buffer(Buffer&&) noexcept;              // 4 move ctor
    Buffer& operator=(Buffer&&) noexcept;   // 5 move assign
};
3

=default and =delete

State your intent explicitly instead of relying on implicit rules.

Example
example
class Handle {
public:
    Handle(const Handle&)            = delete;   // non-copyable
    Handle& operator=(const Handle&) = delete;
    Handle(Handle&&) noexcept        = default;  // but movable
    Handle& operator=(Handle&&) noexcept = default;
};
WATCH OUT
Declaring a destructor suppresses the implicit move operations — your class silently falls back to copying.
4

Copy-and-Swap

One idiom that gives self-assignment safety and strong exception safety.

Example
example
Buffer& Buffer::operator=(Buffer other) noexcept {  // by value
    swap(*this, other);
    return *this;
}   // 'other' destructor releases the old resource