Preface
- An Experiment
- Versions of This Book
- Acknowledgments
About This Book
- What You Should Know Before Reading This Book
- Overall Structure of the Book
- How to Read This Book
- The Way I Implement
- Initializations
- Error Terminology
- Code Simplifications
- The C++ Standards
- Example Code and Additional Information
- Feedback
- IBasic Features of Move Semantics
1.The Power of Move Semantics
- 1.1Motivation for Move Semantics
- 1.1.1Example with C++03 (Before Move Semantics)
- 1.1.2Example Since C++11 (Using Move Semantics)
- 1.2Implementing Move Semantics
- 1.2.1Using the Copy Constructor
- 1.2.2Using the Move Constructor
- 1.3Copying as a Fallback
- 1.4Move Semantics for
constObjects - 1.4.1
constReturn Values - 1.5Summary
2.Core Features of Move Semantics
- 2.1Rvalue References
- 2.1.1Rvalue References in Detail
- 2.1.2Rvalue References as Parameters
- 2.2
std::move() - 2.2.1Header File for
std::move() - 2.2.2Implementation of
std::move() - 2.3Moved-From Objects
- 2.3.1Valid but Unspecified State
- 2.3.2Reusing Moved-From Objects
- 2.3.3Move Assignments of Objects to Themselves
- 2.4Overloading by Different References
- 2.4.1
constRvalue References - 2.5Passing by Value
- 2.6Summary
3.Move Semantics in Classes
- 3.1Move Semantics in Ordinary Classes
- 3.1.1When is Move Semantics Automatically Enabled in Classes?
- 3.1.2When Generated Move Operations Are Broken
- 3.2Implementing Special Copy/Move Member Functions
- 3.2.1Copy Constructor
- 3.2.2Move Constructor
- 3.2.3Copy Assignment Operator
- 3.2.4Move Assignment Operator
- 3.2.5Using the Special Copy/Move Member Functions
- 3.3Rules for Special Member Functions
- 3.3.1Special Member Functions
- 3.3.2By Default, We Have Copying and Moving
- 3.3.3Declared Copying Disables Moving (Fallback Enabled)
- 3.3.4Declared Moving Disables Copying
- 3.3.5Deleting Moving Makes No Sense
- 3.3.6Disabling Move Semantics with Enabled Copy Semantics
- 3.3.7Moving for Members with Disabled Move Semantics
- 3.3.8Exact Rules for Generated Special Member Functions
- 3.4The Rule of Five or Three
- 3.5Summary
4.How to Benefit From Move Semantics
- 4.1Avoid Objects with Names
- 4.1.1When You Cannot Avoid Using Names
- 4.2Avoid Unnecessary
std::move() - 4.3Initialize Members with Move Semantics
- 4.3.1Initialize Members the Classical Way
- 4.3.2Initialize Members via Moved Parameters Passed by Value
- 4.3.3Initialize Members via Rvalue References
- 4.3.4Compare the Different Approaches
- 4.3.5Summary for Member Initialization
- 4.3.6Should We Now Always Pass by Value and Move?
- 4.4Move Semantics in Class Hierarchies
- 4.4.1Implementing a Polymorphic Base Class
- 4.4.2Implementing a Polymorphic Derived Class
- 4.5Summary
5.Overloading on Reference Qualifiers
- 5.1Return Type of Getters
- 5.1.1Return by Value
- 5.1.2Return by Reference
- 5.1.3Using Move Semantics to Solve the Dilemma
- 5.2Overloading on Qualifiers
- 5.3When to Use Reference Qualifiers
- 5.3.1Reference Qualifiers for Assignment Operators
- 5.3.2Reference Qualifiers for Other Member Functions
- 5.4Summary
6.Moved-From States
- 6.1Required and Guaranteed States of Moved-From Objects
- 6.1.1Required States of Moved-From Objects
- 6.1.2Guaranteed States of Moved-From Objects
- 6.1.3Broken Invariants
- 6.2Destructible and Assignable
- 6.2.1Assignable and Destructible Moved-From Objects
- 6.2.2Non-Destructible Moved-From Objects
- 6.3Dealing with Broken Invariants
- 6.3.1Breaking Invariants Due to a Moved Value Member
- 6.3.2Breaking Invariants Due to Moved Consistent Value Members
- 6.3.3Breaking Invariants Due to Moved Pointer-Like Members
- 6.4Summary
7.Move Semantics and noexcept
- 7.1Move Constructors with and without
noexcept - 7.1.1Move Constructors without
noexcept - 7.1.2Move Constructors with
noexcept - 7.1.3Is
noexceptWorth It? - 7.2Details of
noexceptDeclarations - 7.2.1Rules for Declaring Functions with
noexcept - 7.2.2
noexceptfor Special Member Functions - 7.3
noexceptDeclarations in Class Hierarchies - 7.3.1Checking for
noexceptMove Constructors in Abstract Base Classes - 7.4When and Where to Use
noexcept - 7.5Summary
8.Value Categories
- 8.1Value Categories
- 8.1.1History of Value Categories
- 8.1.2Value Categories Since C++11
- 8.1.3Value Categories Since C++17
- 8.2Special Rules for Value Categories
- 8.2.1Value Category of Functions
- 8.2.2Value Category of Data Members
- 8.3Impact of Value Categories When Binding References
- 8.3.1Overload Resolution with Rvalue References
- 8.3.2Overloading by Reference and Value
- 8.4When Lvalues become Rvalues
- 8.5When Rvalues become Lvalues
- 8.6Checking Value Categories with
decltype - 8.6.1Using
decltypeto Check the Type of Names - 8.6.2Using
decltypeto Check the Value Category - 8.7Summary
- IIMove Semantics in Generic Code
9.Perfect Forwarding
- 9.1Motivation for Perfect Forwarding
- 9.1.1What we Need to Perfectly Forward Arguments
- 9.2Implementing Perfect Forwarding
- 9.2.1Universal (or Forwarding) References
- 9.2.2
std::forward<>() - 9.2.3The Effect of Perfect Forwarding
- 9.3Rvalue References versus Universal References
- 9.3.1Rvalue References of Actual Types
- 9.3.2Rvalue References of Function Template Parameters
- 9.4Overload Resolution with Universal References
- 9.4.1Fixing Overload Resolution with Universal References
- 9.5Perfect Forwarding in Lambdas
- 9.6Summary
10.Tricky Details of Perfect Forwarding
- 10.1Universal References as Non-Forwarding References
- 10.1.1Universal References and
const - 10.1.2Universal References in Detail
- 10.1.3Universal References of Specific Types
- 10.2Universal or Ordinary Rvalue Reference?
- 10.2.1Rvalue References of Members of Generic Types
- 10.2.2Rvalue References of Parameters in Class Templates
- 10.2.3Rvalue References of Parameters in Full Specializations
- 10.3How the Standard Specifies Perfect Forwarding
- 10.3.1Explicit Specification of Types for Universal References
- 10.3.2Conflicting Template Parameter Deduction with Universal References
- 10.3.3Pure RValue References of Generic Types
- 10.4Nasty Details of Perfect Forwarding
- 10.4.1“Universal” versus “Forwarding” Reference
- 10.4.2Why
&&for Both Ordinary Rvalues and Universal References? - 10.5Summary
11.Perfect Passing with auto&&
- 11.1Default Perfect Passing
- 11.1.1Default Perfect Passing in Detail
- 11.2Universal References with
auto&& - 11.2.1Type Deduction of
auto&& - 11.2.2Perfectly Forwarding an
auto&&Reference - 11.3
auto&&as Non-Forwarding Reference - 11.3.1Universal References and the Range-Based
forLoop - 11.4Perfect Forwarding in Lambdas
- 11.5Using
auto&&in C++20 Function Declarations - 11.6Summary
12.Perfect Returning with decltype(auto)
- 12.1Perfect Returning
- 12.2
decltype(auto) - 12.2.1Return Type
decltype(auto) - 12.2.2Deferred Perfect Returning
- 12.2.3Perfect Forwarding and Returning with Lambdas
- 12.3Summary
- IIIMove Semantics in the C++ Standard Library
13.Move-Only Types
- 13.1Declaring and Using Move-Only Types
- 13.1.1Declaring Move-Only Types
- 13.1.2Using Move-Only Types
- 13.1.3Passing Move-Only Objects as Arguments
- 13.1.4Returning Move-Only Objects by Value
- 13.1.5Moved-From States of Move-Only Objects
- 13.2Summary
14.Moving Algorithms and Iterators
- 14.1Moving Algorithms
- 14.2Removing Algorithms
- 14.3Move Iterators
- 14.3.1Move Iterators in Algorithms
- 14.3.2Move Iterators in Constructors and Member Functions
- 14.4Summary
15.Move Semantics in Types of the C++ Standard Library
- 15.1Move Semantics for Strings
- 15.1.1String Assignments and Capacity
- 15.2Move Semantics for Containers
- 15.2.1Basic Move Support for Containers as a Whole
- 15.2.2Insert and Emplace Functions
- 15.2.3Move Semantics for
std::array<> - 15.3Move Semantics for Vocabulary Types
- 15.3.1Move Semantics for Pairs
- 15.3.2Move Semantics for
std::optional<> - 15.4Move Semantics for Smart Pointers
- 15.4.1Move Semantics for
std::shared_ptr<> - 15.4.2Move Semantics for
std::unique_ptr<> - 15.5Move Semantics for IOStreams
- 15.5.1Moving IOStream Objects
- 15.5.2Using Temporary IOStreams
- 15.6Move Semantics for Multithreading
- 15.6.1
std::thread<>andstd::jthread<> - 15.6.2Futures, Promises, and Packaged Tasks
- 15.7Summary
Glossary
- C
- CPP file
- F
- forwarding reference
- full specialization
- G
- glvalue
- H
- header file
- I
- include file
- incomplete type
- L
- lvalue
- N
- named return value optimization (NRVO)
- P
- prvalue
- R
- return value optimization (RVO)
- rvalue
- S
- small/short string optimization (SSO)
- T
- translation unit
- U
- universal reference
- V
- value category
- variadic template
- X
- xvalue
