REVIEW - C++ Move Semantics - The Complete Guide

Title:

C++ Move Semantics - The Complete Guide

Author:

Nicolai Josuttis

ISBN:

9783967309003

Publisher:

leanpub (2020)

Pages:

262pp

Reviewer:

Anthony Williams

Reviewed:

March 2021

Rating:

★★★★★

Verdict: Highly recommended.

This is the latest book by Nicolai Josuttis, and it is as good as you might expect; the moniker of ‘The Complete Guide’ is well earned. I’ve encountered people claiming that the mere existence of this book demonstrates that move semantics in C++ are too complicated. This is definitely not the case, and the book clearly demonstrates that for the vast majority of cases, move semantics are easy to use and understand.

It’s an easy read with plenty of examples, guiding you through all aspects of move semantics. The book starts off with a bit of history, demonstrating why move semantics are needed, then shows how you can take advantage of them with standard library types such as `std::string` and class templates such as `std::vector`, and how the compiler will often use them automatically, with no visible marker in the code.

Moving on, the book covers the details of the language features that make move semantics work, including rvalue references, "universal" references, and overload resolution rules.

The requirements for move semantics on your own classes are explained clearly, with a whole section on how objects should behave after being used as the source of a move operation. This "moved from" state has been the focus of a lot of active discussion. Nico highlights the basic requirements, and explains the choices you then face as a developer, with straightforward recommendations.

The book also covers the support in the standard library for facilitating your use of move semantics, such as the use of `std::move_iterator` to move objects around when using algorithms.

Overall, this is an excellent book, well worth a read for anyone working with modern C++ code.

Website: www.cppmove.com