In this chapter, we will learn some advanced C++ move semantics. We will first discuss the Big Five, which is an idiom that simply encourages programmers to explicitly define the destruction and move/copy semantics of a class. Next, we will learn how to define a move constructor and move assignment operator; the different combinations of move semantics (including move-only and non-copyable); non-movable classes; and how to implement these classes and why they are important.
This chapter will also discuss some common pitfalls such as why a const && move makes no sense, and how to overcome l-value versus r-value reference types. The recipes in this chapter are important because once you enable C++11 or higher, move semantics is enabled, which changes how C++ fundamentally handles classes in numerous situations. The recipes in this chapter provide the foundation for writing efficient code in C++ that behaves as intended.
The recipes in this chapter are as follows:
- Using compiler-generated special class member functions and the Big Five
- Making your class movable
- Moving only types
- Implementing the noexcept move constructor
- Learning to be wary of const &&
- Referencing qualified member functions
- Exploring objects that cannot be moved or copied