What is the Rule of Zero in C++?

You may have heard of the Rule of Three & the Rule of Five before in C++. These two rules are based around resource management, and the writing of the exception-safe code. There is a third Rule, called the Rule of Zero in C++ that brings in a unique and very modern outlook on resource management in C++.

If you have no knowledge about either the Rule Of Three or Rule Of Five, it is recommended you refer to our dedicated articles on these topics first. Prior knowledge of these will help in your understanding of the Rule of Zero.


What is the Rule of Zero?

The Rule of Three emphasizes the need for Three Member functions. The Rule of Five emphasizes the need for Five Member functions. It thus follows, based on the naming convention, that the Rule of Zero emphasizes the need for ZERO special member functions.

In other words, the Rule of Zero, you should NEVER implement a destructor, copy constructor, move constructor or assignment operators in your code.

Now that might seem a little confusing. No Destructor? No Copy Constructor? Chances are you use these member functions very often, and you can’t imagine creating Classes without them.

The Rule of Zero is actually a concept introduced in 2012, with the advent of C++11. Following the Rule of Zero is not possible on versions prior to C++11, as it relies on the new features introduced by C++11. For example, the introduction of Smart Pointers, replacing raw/traditional pointers, forgoes the need to manually deallocate memory. This helps us avoid the use of a Destructor when dealing with dynamic memory.

The Rule of Zero promotes the use of these new features, as well as the use of the various Containers in the STL Library. It’s not exactly compulsory to follow this rule, rather it’s more an official guideline that helps you write clean, minimal and understandable code.

Now that we have a solid grasp over what the Rule of Zero is, let’s take a look at some examples that highlight it’s behavior and where we can observe the Rule of Zero being used.


Rule of Zero – Examples

Here we will take a look at some code snippets that help us understand the Rule of Zero better.

The following example makes use of a Shared Pointer. This is a type of Smart Pointer that we can use, that automatically handles the deletion of memory. The Shared Pointer maintains a “reference” count for the object that it points to. Once the reference count has dropped to zero, the object is then deleted. Every time you create a new reference to that object, the reference counts increases by one.

#include <iostream>
#include <memory>
using namespace std;

class MyClass {
public:
    MyClass(int value) {    make_shared<int>(value);    }

private:
    shared_ptr<int> ptr;
};

int main() {
    MyClass obj = MyClass(5);
}

The make_shared command here is similar to the new command, and used to allocate memory on the heap. The only difference is that this time we don’t have to make a destructor.

Smart Pointers are recommended over Raw Pointers, as they are much safer, and require less code. There are even multiple types of them, that we can use based on our requirements.


Another interesting example, is where we explicitly declare all of the Five main member functions as Default. This causes the default implementations to be used.

class X  {
public:
   // Copy Constructor
   X(X const& other) = default;
    
   // Copy Assignment Operator
   X& operator=(X const& other) = default;
    
   // Move Constructor
   X(X&& other) = default;
   
   // Move Assignment Operator
   X& operator=(X&& other) = default;
    
   // Destructor
   ~X() = default;
};

This is also known as the Rule of Five defaults.


Conclusion

So what’s the main takeaway? Well, out of the Three Rules in C++, it is generally recommended to be following the Rule of Zero where-ever possible. This is because it results in minimal and clean code that is also safe.

You will have to write your Classes in a slightly different way, and take advantage of all the newer features in the STL library in-order to avoid having to declare any of the Five Special Member Functions. A small example of this, is how Smart Pointers don’t work well with regular arrays, instead they should be used with containers like vector from the STL library.


This marks the end of the Rule of Zero in C++ Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments