How it works...

In this recipe, we will learn how to make a class movable. To start, let's examine a basic class definition:

#include <iostream>class the_answer{    int m_answer{42};public:    the_answer() = default;public:    ~the_answer()    {        std::cout << "The answer is: " << m_answer << '\n';    }};int main(void){    the_answer is;    return 0;}

In the preceding example, we create a simple class with a private integer member that is initialized. We then define a default constructor and a destructor that outputs to stdout when an instance of the class is destroyed. By default, this class is movable, but the move operation mimics a copy (in other words, there is no difference between a move or a copy with this simple example).

To really make this class movable, ...

Get Advanced C++ Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.