What Is The Rule of Zero in C++?

Object Oriented Programming (OOP) is a way to integrate with objects which can contain data in the form (attributes or properties of objects), and code blocks in the form of procedures (methodsfunctions of objects). These attributes and methods are variables and functions that belong to the class – part of the class’s code and they are generally referred to as class members. Classes and structs are very useful in modern programming. There are some rules to support the principles of programming, one of which is the Rule of Zero in C++. In this post, we explain what is Rule of Zero in C++ with examples.

What is resource acquisition in C++?

The principle of Resource Acquisition Is Initialization (RAII) term used in several OOP programming languages, which relates to the ability to manage resources, such as memory, through the copy and move constructors, destruction, and assignment operators. RAII is about the declaration and use of destructors, copy-move operators, and memory management in these members and methods. These cause new rules in development.

What is the Single Responsibility Principle in C++?

The Single Responsibility Principle (SRP) is a computer programming principle that states “A module should be responsible to one, and only one, actor.” This principle exposes a rule for the classes in C++, called Rule of Zero. Now, let’s see what the Rule of Zero in C++ is.

What is the Rule of Zero in C++?

Rule of Zero means that, if all members have default member functions, no further work is needed. This is the simplest and cleanest semantics of programming. The compiler provides default implementations for all of the default member functions if there are no special member functions that are user-defined. In other words, you should prefer the case where no special member functions need to be defined. In this rule, classes that have custom destructors, copy/move constructors, or copy/move assignment operators should deal exclusively with ownership; other classes should not have custom destructors, copy/move constructors or copy/move assignment operators.

Rule of Zero helps simplify our C++ code without risking resource management issues at run time. The term The Rule of Zero was coined by R. Martinho Fernandes in his paper in 2012. This paper is highly recommended reading to get the full details of this concept.

The Rule of Zero rule can be seen in the C++ Core Guidelines here C.20: If you can avoid defining default operations, do. As in the example below, std::map and std::string have all the special functions, so you don’t need to create a constructor and destructor for them.

How can we apply the Rule of Zero in C++?

Here are some notes as to-dos in Rule of Zero.

  • A simple empty C++ class is perfectly equivalent to default implementations (Rule of Five) in a class. A modern compiler is able to provide a default implementation, in example, this simple class.

This class is exactly the same as the one below in modern C++.

  • Use a std::unique_ptr if your class can be moved but should not be copied.
  • Use a std::shared_ptr if you need to support copying as well as moving.
  • Use std::string, std::wstring or other basic_strings over arrays of characters, they support both copying and move semantics.
  • Use Standard Library container classes. Prefer to use std::vector, std::map std::array etc.

Is there an example of the Rule of Zero in C++?

Here is a simple C++ example of the Eule of Zero in C++.

If this base Tzero class is intended for polymorphic use, its destructor may have to be declared public and virtual. This will not allow implicit moves, deprecates implicit copies. Thus, the special member functions have to be declared as defaulted as in base declarations of five defaults. For example:

What Is The Rule of Zero in C++? C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.