Abstract
This chapter complements and completes several key topics discussed in the middle parts of the book. In Chapters 12 and 13, for instance, you gained insight into the mechanics behind the copying of objects, that is, copy constructors and assignment operators. And from Chapter 8 you know to prefer pass-by-reference over pass-by-value to avoid undue copying of parameters. And for a long time, that was basically all you needed to know. C++ offered facilities to copy objects, and if you wanted to avoid costly copies, then you simply used either references or pointers. Starting with C++11, however, there is a powerful new alternative. You can no longer only copy objects; you can now also move them.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
The C++ standard actually defines three additional expression categories with the names glvalue, prvalue, and xvalue. But trust us, you really do not need to know all these gory details. Our brief, more informal, discussion here will serve you just fine!
- 2.
 Technically, std::move() is defined in the <utility> module. In practice, though, you’ll rarely have to explicitly import this module to use std::move().
- 3.
 If you are running this example using either a debug build or a less optimizing compiler, you may see some extra output that is printed by Array<>’s move members. Even then, however, only one array should be copied, which is the main thing.
- 4.
 This output may again slightly differ from compiler to compiler, or from compiler configuration to compiler configuration. The array of 1,000 elements will always be copied, though, which is the main takeaway here.
Author information
Authors and Affiliations
Rights and permissions
Copyright information
© 2020 Ivor Horton and Peter Van Weert
About this chapter
Cite this chapter
Horton, I., Van Weert, P. (2020). Move Semantics. In: Beginning C++20. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-5884-2_18
Download citation
DOI: https://doi.org/10.1007/978-1-4842-5884-2_18
Published:
Publisher Name: Apress, Berkeley, CA
Print ISBN: 978-1-4842-5883-5
Online ISBN: 978-1-4842-5884-2
eBook Packages: Professional and Applied ComputingApress Access BooksProfessional and Applied Computing (R0)