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_ptrs 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...

Continue reading...

Add a Comment

Comments are closed.

Comments (3)

0 0

Germán said on Nov 22, 2012 09:05 PM:

The only thing I wonder. What would be the reason not to add dynamic_bitset, like in boost, instead of a bit_vector? I think it would be more consistent with the standard library, which already has bitset. About vector<bool>, I think it should be a vector of bools, not of bits. But that's the thinking of most of us I guess.
0 0

chico said on Feb 15, 2013 09:28 PM:

I myself wouldn't stroke out "moths to flames", for me, this is a sad truth.

[url=""]Is there a proper 'ownership-in-a-package' for 'handles' available?[/url]
1 0

ponnuvel said on Oct 6, 2015 07:03 AM:

The link is dead!