Move-only types

In this recipe, we will learn how to make a class move-only. A great example of the difference between a copy and a move is the difference between std::unique_ptr and std::shared_ptr.

The point of std::unique_ptr is to enforce a single owner for dynamically allocated types while std::shared_ptr allows for multiple owners of dynamically allocated types. Both allow the user to move the contents of a pointer type from one instantiation to another, but only std::shared_ptr allows the user to make a copy of the pointer (as copying the pointer would create more than one owner).

In this recipe, we will use these two classes to show how to make a move-only class and to show why this type of class is used so heavily in C++ (as most of the time we wish to move and not copy).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset