Rule of Zero

Hi folks,

I've read much about rules (zero, three and five) on the Web, and tried well enough, but understood none of them "completely". Each rule has some ambiguous spots.
To get rid of that whole complexity, do you agree to stick only to the "rule of zero"?

I use that this way: If my class has a resource management (that is, it allocates memory from the heap), I define all 7 member functions for it. Otherwise, I leave it alone and define no member function for that. :)

I don't know how clear my perspective is. But do you agree, please?
Please also bear in mind that I run away from too much technical language and find it by no way constructive. (I hope those great reference websites weren't that complicated so that everyone could get across the contents easily making the whole tech more interesting). :(
Note that memory isn't the only resource that needs to be managed. Concurrency locks, file handles, network connections, database connections, GPU objects, etc.

What do you mean by "allocates memory"? Also, what if a class doesn't allocate memory but needs non-trivial constructors?
Rule of Zero? Too many rules.
@helios:

By "allocating memory" I mean to grasp some memory from the heap by new.

Also, what if a class doesn't allocate memory but needs non-trivial constructors?
Yes. So in this case, too, we'd better to define all 7 functions explicitly.

@kbw:
Yes, I also suppose there are too many to that.
This one? https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rc-zero

IMHO, the 0/3/5/6/? is just one, unambiguous rule:
Do what you have to and no more.

The key, of course, is to understand what you have to do. The rule has become quite verbose and explained in many places (to varying degree of clarity and success), because most humans by default fail to understand.
I've visited the page and seen the contents a couple of times. I assume it's rather simpler to understand but it's good for professionals (on the other hand, professional don't need it, because they're professionals! Hhhh)

I agree to a great extent with you on what you said: Do what you have to and no more. The key, of course, is to understand what you have to do.

I think this can be achieved by dedicating much time and being quiet experienced over time to "understand what the person needs to do".

Thanks to you all. :)
Last edited on
frek wrote:
my class has a resource management (that is, it allocates memory from the heap), I define all 7 member functions for it. Otherwise, I leave it alone and define no member function for that. :)

What's almost exactly how Core Guidelines put it: "C.21: If you define or =delete any default operation, define or =delete them all" https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rc-five

same mentioned on cppreference at the very end of https://en.cppreference.com/w/cpp/language/rule_of_three
Last edited on
IMHO, the 0/3/5/6/? is just one, unambiguous rule:
Do what you have to and no more.

This is really how it should be presented. These rules are just sections of the same rule, which is just a decision tree: if I have these things, I must also have these other things, else if I have a second set of things, I need certain different things, else if ...

This is the kind of thing that is worth printing on a quick reference sheet near your workstation, at least for a while, until you find that one day you no longer need to refer to it because you now know what it says.
Topic archived. No new replies allowed.