liveBook · Manning

Big five

As explained in Learn C++ by Example

The rule of five is a C++ design guideline that a class managing resources and declaring one relevant special member function should explicitly consider all five copy and move special member functions.

The rule of five says that a class needing special resource-handling should consider its destructor, copy constructor, copy assignment, move constructor, and move assignment together. A user-declared destructor, copy constructor, or copy assignment operator can prevent the compiler from implicitly providing move operations. section 6.1.4 section 6.3

The guideline connects several related choices:

  • Before C++11, the rule of three grouped the destructor, copy constructor, and copy assignment operator. section 6.1.4
  • Since C++11, move construction and move assignment must also be considered. section 6.1.4
  • If a type cannot move, vector push_back may use copying instead, losing an optimization opportunity. section 6.1.4
  • The rule of zero prefers leaving all six special member functions undeclared when the compiler-generated behavior is sufficient. section 6.3

Checking a polymorphic type

A Blob base class can be inspected with type traits rather than instantiated. Static assertions check whether it is default constructible, destructible, copy constructible, copy assignable, move constructible, and move assignable. section 6.1.4

Listing 6.10 Type traits to check for special member functions
#include <type_traits>
void check_properties()
{
    ...                                               #1
    static_assert(
        !std::is_default_constructible_v<Race::Blob>
    );                                                #2
    static_assert(
         std::is_destructible_v<Race::Blob>
    );                                                #2
    static_assert(
        !std::is_copy_constructible_v<Race::Blob>
    );                                                #2
    static_assert(
        !std::is_copy_assignable_v<Race::Blob>
    );                                                #2
    static_assert(
        !std::is_move_constructible_v<Race::Blob>
    );                                                #2
    static_assert(
        !std::is_move_assignable_v<Race::Blob>
    );                                                #2
    static_assert(
         std::has_virtual_destructor_v<Race::Blob>
    );                                                #3
}

Inspect the compile-time assertions that reveal which special member operations Blob supports. section 6.1.4

How Learn C++ by Example covers it

How deep it goes
Explains the five operations, compiler suppression of moves, and compile-time inspection, but does not implement resource ownership.
Examples
Uses vector push_back and a polymorphic Blob class with static type-trait assertions.
What it assumes
Assumes the rule of three and special member functions, while introducing move operations and type-trait queries.
What you can do after
Evaluate copy and move support with std::is_* type traits and recognize when implicit moves are unavailable.
How it connects
Connects the rule of five with the rule of three, rule of zero, move semantics, special member functions, and type traits.

Also explained in

Compare how 2 books explain big five →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.