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 (methods, functions 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. class Tmy_map { public: // … no default operations private: std::string title; std::map<int, int> coords; }; Tmy_map m; // default construct Tmy_map m2 {m}; // copy construct 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++. class Tx { public: Tx() = default; Tx(Tx const& other) = default; Tx& operator=(Tx const& other) = default; Tx(Tx&& other) = default; Tx& operator=(Tx&& other) = default; ~Tx() = default; } Use a std::unique_ptr if your class can be moved but should not […]
