Notebook C++: About move semantics


The idea of the Notebook C++ series is to share some tips and tricks about various C++ elements. All books in this series are short and small, one for each major topic. Such that the paperback version can be carried around easily.

About Move Semantics

This part of the series is all about move semantics. I will introduce the feature to you in an unconventional but easy-to-understand way. Move semantics are nothing special, after all.

We'll start looking at what move semantics is, how it works, and why we should stay away from std::move most of the time. We establish some rules about when to use std::move when std::forward. You learn about why not to move return values or temporary objects. Do you want the best speed from your custom data type and the STL? No problem, you will learn what your class must look like to achieve this.

In the end, you also learn about features not seen that often, such as reference qualifiers, how they work, why they are there, and when to use them.

All in all, after having read this book, you have a solid understanding of move semantics.

Stuttgart, September 2022

Andreas Fertig

Table of Contents

  • Notes by Standard at a Glance
    • Notes belonging to C++11
    • Notes belonging to C++17
    • Notes belonging to C++20
  • 2 Move Semantics
    • Note 1 : Understand the type of move used in C++
    • Note 2 : Move is nothing special
    • Note 3 : Move vs. copy
    • Note 4 : Move is a partial swap
    • Note 5 : std::move doesn't move
    • Note 6 : Understand the value categories
    • Note 7 : Make your rvalue parameters modifiable
    • Note 8 : Only classes with dynamic memory profit from move semantics
    • Note 9 : A moved-from object isn't special
    • Note 10 : Never use std::move on a return value
    • Note 11 : Remember to forward the move to all the base classes
    • Note 12 : Never use std::move on a temporary
    • Note 13 : When is it a forwarding reference
    • Note 14 : When to use std::forward
    • Note 15 : Your custom class and the STL
    • Note 16 : Known when you lose a special member
    • Note 17 : Even with a defaulted destructor, you lose the move operations
    • Note 18 : Be aware of std::initializer_list
    • Note 19 : Test for noexcept move
    • Note 20 : Use ref-qualifiers for more efficiency
    • Note 21 : Use reference qualifiers on assignment operators
    • Note 22 : Understand user-declared and user-defined
  • Acronyms
  • Bibliography
  • Index


Preview[ pdf ]