Move Semantics in C++. The Complete Guide
262 pages
English
PDF
4.5

Move Semantics in C++. The Complete Guide

Move Semantics in C++. The Complete Guide

Author:

Year:

Summary:

Introduction

The book "C++ Move Semantics — The Complete Guide" by Nicolai Josuttis is not just another C++ textbook. It is a fundamental work dedicated to one of the most powerful and simultaneously complex topics of modern C++ — move semantics. Published in 2020, the book covers the C++11, C++14, C++17, and partially C++20 standards, providing the reader with a complete and structured understanding of how rvalue references, move constructors, move assignment operators, and perfect forwarding work.

Nicolai Josuttis is known for his deep technical books (e.g., "C++17 — The Complete Guide"), and this volume is no exception. It is written with the author's characteristic thoroughness: each concept is explained with simple examples, then gradually becomes more complex, and at the end of each chapter, practical recommendations and warnings about typical mistakes are given.

What is this book about?

The book consists of 15 chapters, logically divided into three parts: fundamentals, advanced techniques, and special cases. Here is a brief summary of the key sections:

  • Chapters 1–4: Fundamentals — explanation of the concepts of lvalue, rvalue, xvalue, prvalue, as well as the mechanism of how move semantics works. The author thoroughly analyzes how the compiler chooses between copying and moving, and why move semantics does not always provide a performance gain.
  • Chapters 5–8: Implementing Move Operations — rules for writing move constructors and move assignment operators, including the copy-and-swap idiom, noexcept handling, and interaction with STL containers.
  • Chapters 9–11: Perfect Forwarding and Universal References — a detailed breakdown of templates with forwarding references (T&&), the use of std::forward, std::move, and their differences. Special attention is paid to problems with automatic type deduction and reference collapsing.
  • Chapters 12–15: Advanced Topics — move semantics in lambda expressions, in return values (RVO/NRVO), in smart pointers, as well as features of working with arrays, strings, and user-defined types. The book concludes with a discussion of move-only types and std::optional/std::variant.

Who is this book for?

The book is intended for C++ programmers who already have experience with the language. If you are just starting to learn C++, you will find it difficult: the author assumes knowledge of templates, STL, classes, and inheritance. However, for those who already write in C++11/14/17 and want to understand how to make their code faster and safer, this book is an ideal choice.

What will the reader learn?

  • A clear distinction between lvalue, rvalue, xvalue, and glvalue.
  • How and when the compiler automatically generates move constructors and move assignment operators.
  • The noexcept rules for move operations and their impact on container performance.
  • The technique of perfect forwarding and its implementation via std::forward.
  • How to avoid accidental copying when returning objects from functions.
  • Features of move semantics for strings, vectors, smart pointers, and other STL types.
  • How to write move-only classes (e.g., for resource ownership).
  • The impact of move semantics on lambda expressions and variable capture.

Strengths

  • Depth and precision: the book does not just list the rules, but explains why they work the way they do. For example, the author thoroughly explains why std::move is just a type cast, not some kind of magic.
  • Practical focus: each chapter contains code examples that can be immediately applied in real projects. The sections with typical mistakes (e.g., accidentally moving from a const object) are especially valuable.
  • Relevance: the book covers standards up to C++20, making it useful for modern projects.<

Recommendations