The rule of zero, or six, is one of the advanced rules in modern C++. I wrote in my current book “C++ Core Guidelines Explained: Best Practices for Modern C++” about them. Today, I want to quote the relevant parts of my book in this post.
Cippie reasons about the rule of zero, five, or six.
By default, the compiler can generate the big six if needed. You can define the six special member functions, but can also ask explicitly the compiler to provide them with
= default or delete them with
This rule is also known as “the rule of zero“. That means, that you can avoid writing any custom constructor, copy/move constructors, assignment operators, or destructors by using types that support the appropriate copy/move semantics. This applies to the regular types such as the built-in types
double, but also the containers of the Standard Template Library (STL)such as
The default construction and the copy construction work because they are already defined for
std::map. When the compiler auto-generates the copy constructor for a class, it invokes the copy constructor for all members and all bases of the class.
The big six are closely related. Due to this relation, you have to define or
=delete all six. Consequently, this rule is called “the rule of six“. Sometimes, you hear “the rule of five“, because the default constructor is special, and, therefore, sometimes excluded.
Dependencies between the special member functions
Howard Hinnant developed in his talk at the ACCU 2014 conference an overview to the automatically generated special member functions.
Automatically generated special member functions
Howard’s table demands a deep explanation.
First of all, user-declared means for one of these six special member functions that you define it explicitly or auto request it from the compiler with
=default. Deletion of the special member function with
=delete is also regarded as user declared. Essentially, when you just use the name, such the name of the default constructor, it counts as user declared.
When you define any constructor, you get no default constructor. A default constructor is a constructor which can be invoked without an argument.
When you define or delete a default constructor with
=delete, no other of the six special member functions is affected.
When you define or delete a destructor, a copy constructor, or a copy-assignment operator with
=delete, you get no compiler-generated move-constructor and move-assignment constructor. This means move operations such as move construction or move assignment fall back to copy operations such as copy construction or copy assignment. This fallback automatism is marked in red in the table.
When you define or delete with =default
or =delete a move constructor or a move assignment operator, you get only the defined
=delete move constructor or move assignment operator. Consequently, the copy constructor and the copy assignment operator are set to
=delete. Invoking a copy operation such as copy construction or copy assignment causes, therefore, a compilation error.
When you don’t follow this rule, you get very unintuitive objects. Here is an unintuitive example from the guidelines.
Why does this program have undefined behavior? The default copy assignment operation
bigArray2 = bigArray1 (1) of the example copies all members of
bigArray2. Copying means, in particular, that pointer data is copied but not the data. Hence, the destructor for
bigArray2 is called (2), and we get undefined behavior because of double free.
The unintuitive behavior of the example is, that the compiler-generated copy assignment operator of
BigArray makes a shallow copy of BigArray, but the explicitly implemented destructor of
BigArray assumes ownership of data.
AddressSanitizer makes the undefined behavior visible.
Double free detected with Address Sanitizer
C.22 Make default operations consistent
This rule is related to the previous rule. If you implement the default operations with different semantics, the users of the class may become very confused. This strange behavior may also appear if you partially implement the member functions and partially request them via
=default. You cannot assume that the compiler-generated special member functions have the same semantics as yours.
As an example of the odd behavior, here is the class
Strange includes a pointer to
Strange has a copy constructor (1) and a copy-assignment operator (2). The copy constructor applies deep copy, and the assignment operator applies shallow copy. By the way, the compiler-generated copy constructor or copy assignment operator also applies shallow copy. Most of the time, you want deep copy semantics (value semantics) for your types, but you probably never want to have different semantics for these two related operations. The difference is that deep copy semantics creates two new separate storage
p(new int(*a.p)) while shallow copy semantics just copies the pointer
p = a.p. Let’s play with the Strange types. The following figure shows the output of the program.
Line 3 uses the copy constructor to create
s2. Displaying the addresses of the pointer and changing the value of the pointer
s2.p (line 4) shows that
s2 are two distinct objects. This is not the case for
s3. The copy-assignment operation in line (5) performs a shallow copy. The result is that changing the pointer
s3.p (line 6) also affects the pointer
s1.p because both pointers refer to the same value.
The fun starts if I delete the pointer
s1.p (line 7). Thanks to the deep copy, nothing bad happens to
s2.p, but the value of
s3.p becomes an invalid pointer. To be more precise: Dereferencing an invalid pointer such as in
*s3.p (line 8) is undefined behavior.
The idea of a regular type is deeply embodied in the Standard Template Library (STL). The idea goes back to Alexander Stephanov, who is the creator of the STL. Let me write more about regular types in my next post.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, and Ben Atakora.
Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, Ralf Abramowitsch, John Nebel, Mipko, and Alicja Kaminska.
My special thanks to Embarcadero
My special thanks to PVS-Studio
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.