Rule of Zero -- R. Martinho Fernandes
Long-time C++98 developers know about the Rule of Three. But with C++11's addition of move semantics, we need to be talking instead about the Rule of Five.
This article argues for limiting the use of the Rule of Five, and instead mostly aiming for a "Rule of Zero" -- why you don't have and don't want to write logic for copying, for moving, and for destruction all the time in C++, and why you should encapsulate that logic as much as possible in the remaining situations when you actually need to write it. (And, in passing, he shows once again why C++11 developers seem to be drawn to reusing unique_ptr
s with custom deleters like moths to flames bees to flowers. See also recent examples like this one on std-proposals.)
Rule of Zero
In C++ the destructors of objects with automatic storage duration are invoked whenever their scope ends. This property is often used to handle cleanup of resources automatically in a pattern known by the meaningless name RAII.
An essential part of RAII is the concept of resource ownership: the object responsible for cleaning up a resource in its destructor owns that resource. Proper ownership policies are the secret to avoid resource leaks...