Logo

Notebook C++ - About move semantics


Foreword

The idea of the Notebook C++ series is to share some tips and tricks about various C++ elements. All books in this series are somewhat short and small books, 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 maybe unconventional but easy-to-understand way. Move semantics are nothing special, afterall.

We'll start looking at what move semantics is, how it works, and why we should most times stay away from std::move. 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. You want to get 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 some feature that is not seen that often, ref-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 paramaters 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 ref-qualifiers on assignment operators
    • Note 22 : Understand user-defined and user-declared
  • Bibliography

Extract

Preview[ pdf ]