The rule of zero, or six, is one of the advanced rules in modern C++. I wrote in my current book

The Rule of Zero, or Six

submited by
Style Pass
2023-01-23 10:30:08

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.

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 = delete.

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 bool or double, but also the containers of the Standard Template Library (STL)such as std::vector or std::string.

The default construction and the copy construction work because they are already defined for std::string and 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.

Leave a Comment