Josuttis: C++MoveSemantics2022/04/1913:28 pagei Nicolai M. Josuttis C++ Move Semantics - The Complete Guide First Edition Josuttis: C++MoveSemantics2022/04/1913:28 pageii C++ Move Semantics - The Complete Guide First Edition Nicolai M. Josuttis Thisversionwaspublishedon2022-04-19. ©2022byNicolaiJosuttis. Allrightsreserved. Thispublicationisprotectedbycopyright,andpermissionmustbeobtainedfromtheauthorprior toanyprohibitedreproduction,storageinaretrievalsystem,ortransmissioninanyformorbyany means,electronic,mechanical,photocopying,recording,orlikewise. ThisbookwastypesetbyNicolaiM.JosuttisusingtheLATEXdocumentprocessingsystem. Thisbookisforsaleathttp://leanpub.com/cppmove. This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. LeanPublishingistheactofpublishinganin-progressebookusinglightweighttoolsand manyiterationstogetreaderfeedback,pivotuntilyouhavetherightbook,andbuildtractiononce youdo. Josuttis: C++MoveSemantics2022/04/1913:28 pageiii Contents Preface xi AnExperiment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi VersionsofThisBook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xii Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii AboutThisBook xv WhatYouShouldKnowBeforeReadingThisBook . . . . . . . . . . . . . . . . . . . . . . . . . . xv OverallStructureoftheBook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv HowtoReadThisBook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi TheWayIImplement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi TheC++Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii ExampleCodeandAdditionalInformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii PartI:BasicFeaturesofMoveSemantics 1 1 ThePowerofMoveSemantics 3 1.1 MotivationforMoveSemantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.1.1 ExamplewithC++03(BeforeMoveSemantics) . . . . . . . . . . . . . . . . . . . . . 3 1.1.2 ExampleSinceC++11(UsingMoveSemantics) . . . . . . . . . . . . . . . . . . . . . 11 1.2 ImplementingMoveSemantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.2.1 UsingtheCopyConstructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.2.2 UsingtheMoveConstructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 1.3 CopyingasaFallback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 1.4 MoveSemanticsforconstObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.4.1 constReturnValues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 iii Josuttis: C++MoveSemantics2022/04/1913:28 pageiv iv Contents 1.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2 CoreFeaturesofMoveSemantics 25 2.1 RvalueReferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 2.1.1 RvalueReferencesinDetail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 2.1.2 RvalueReferencesasParameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.2 std::move() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.2.1 HeaderFileforstd::move() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.2.2 Implementationofstd::move() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.3 Moved-FromObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.3.1 ValidbutUnspecifiedState . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.3.2 ReusingMoved-FromObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.3.3 MoveAssignmentsofObjectstoThemselves. . . . . . . . . . . . . . . . . . . . . . . 30 2.4 OverloadingbyDifferentReferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 2.4.1 constRvalueReferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 2.5 PassingbyValue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 2.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3 MoveSemanticsinClasses 35 3.1 MoveSemanticsinOrdinaryClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 3.1.1 WhenisMoveSemanticsAutomaticallyEnabledinClasses? . . . . . . . . . . . . . 38 3.1.2 WhenGeneratedMoveOperationsAreBroken . . . . . . . . . . . . . . . . . . . . . 39 3.2 ImplementingSpecialCopy/MoveMemberFunctions . . . . . . . . . . . . . . . . . . . . . . 40 3.2.1 CopyConstructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 3.2.2 MoveConstructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 3.2.3 CopyAssignmentOperator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3.2.4 MoveAssignmentOperator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 3.2.5 UsingtheSpecialCopy/MoveMemberFunctions . . . . . . . . . . . . . . . . . . . . 46 3.3 RulesforSpecialMemberFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 3.3.1 SpecialMemberFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 3.3.2 ByDefault,WeHaveCopyingandMoving . . . . . . . . . . . . . . . . . . . . . . . . 50 3.3.3 DeclaredCopyingDisablesMoving(FallbackEnabled) . . . . . . . . . . . . . . . . 50 3.3.4 DeclaredMovingDisablesCopying . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3.3.5 DeletingMovingMakesNoSense . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 3.3.6 DisablingMoveSemanticswithEnabledCopySemantics . . . . . . . . . . . . . . . 53 Josuttis: C++MoveSemantics2022/04/1913:28 pagev Contents v 3.3.7 MovingforMemberswithDisabledMoveSemantics. . . . . . . . . . . . . . . . . . 54 3.3.8 ExactRulesforGeneratedSpecialMemberFunctions . . . . . . . . . . . . . . . . . 54 3.4 TheRuleofFiveorThree. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 3.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 4 HowtoBenefitFromMoveSemantics 59 4.1 AvoidObjectswithNames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 4.1.1 WhenYouCannotAvoidUsingNames . . . . . . . . . . . . . . . . . . . . . . . . . . 60 4.2 AvoidUnnecessarystd::move() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 4.3 InitializeMemberswithMoveSemantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 4.3.1 InitializeMemberstheClassicalWay . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 4.3.2 InitializeMembersviaMovedParametersPassedbyValue . . . . . . . . . . . . . . 63 4.3.3 InitializeMembersviaRvalueReferences. . . . . . . . . . . . . . . . . . . . . . . . . 66 4.3.4 ComparetheDifferentApproaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 4.3.5 SummaryforMemberInitialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 4.3.6 ShouldWeNowAlwaysPassbyValueandMove? . . . . . . . . . . . . . . . . . . . 73 4.4 MoveSemanticsinClassHierarchies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 4.4.1 ImplementingaPolymorphicBaseClass . . . . . . . . . . . . . . . . . . . . . . . . . 75 4.4.2 ImplementingaPolymorphicDerivedClass . . . . . . . . . . . . . . . . . . . . . . . 77 4.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 5 OverloadingonReferenceQualifiers 79 5.1 ReturnTypeofGetters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 5.1.1 ReturnbyValue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 5.1.2 ReturnbyReference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 5.1.3 UsingMoveSemanticstoSolvetheDilemma . . . . . . . . . . . . . . . . . . . . . . 81 5.2 OverloadingonQualifiers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 5.3 WhentoUseReferenceQualifiers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 5.3.1 ReferenceQualifiersforAssignmentOperators . . . . . . . . . . . . . . . . . . . . . 84 5.3.2 ReferenceQualifiersforOtherMemberFunctions. . . . . . . . . . . . . . . . . . . . 86 5.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 6 Moved-FromStates 89 6.1 RequiredandGuaranteedStatesofMoved-FromObjects. . . . . . . . . . . . . . . . . . . . . 89 6.1.1 RequiredStatesofMoved-FromObjects . . . . . . . . . . . . . . . . . . . . . . . . . 90 Josuttis: C++MoveSemantics2022/04/1913:28 pagevi vi Contents 6.1.2 GuaranteedStatesofMoved-FromObjects . . . . . . . . . . . . . . . . . . . . . . . . 91 6.1.3 BrokenInvariants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 6.2 DestructibleandAssignable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 6.2.1 AssignableandDestructibleMoved-FromObjects . . . . . . . . . . . . . . . . . . . 93 6.2.2 Non-DestructibleMoved-FromObjects . . . . . . . . . . . . . . . . . . . . . . . . . . 94 6.3 DealingwithBrokenInvariants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 6.3.1 BreakingInvariantsDuetoaMovedValueMember. . . . . . . . . . . . . . . . . . . 97 6.3.2 BreakingInvariantsDuetoMovedConsistentValueMembers . . . . . . . . . . . . 100 6.3.3 BreakingInvariantsDuetoMovedPointer-LikeMembers . . . . . . . . . . . . . . . 102 6.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 7 MoveSemanticsandnoexcept 107 7.1 MoveConstructorswithandwithoutnoexcept . . . . . . . . . . . . . . . . . . . . . . . . . . 107 7.1.1 MoveConstructorswithoutnoexcept . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 7.1.2 MoveConstructorswithnoexcept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 7.1.3 IsnoexceptWorthIt? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 7.2 DetailsofnoexceptDeclarations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 7.2.1 RulesforDeclaringFunctionswithnoexcept . . . . . . . . . . . . . . . . . . . . . . 116 7.2.2 noexceptforSpecialMemberFunctions . . . . . . . . . . . . . . . . . . . . . . . . . 117 7.3 noexceptDeclarationsinClassHierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 7.3.1 CheckingfornoexceptMoveConstructorsinAbstractBaseClasses . . . . . . . . 120 7.4 WhenandWheretoUsenoexcept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 7.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 8 ValueCategories 125 8.1 ValueCategories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 8.1.1 HistoryofValueCategories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 8.1.2 ValueCategoriesSinceC++11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 8.1.3 ValueCategoriesSinceC++17 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 8.2 SpecialRulesforValueCategories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 8.2.1 ValueCategoryofFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 8.2.2 ValueCategoryofDataMembers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 8.3 ImpactofValueCategoriesWhenBindingReferences . . . . . . . . . . . . . . . . . . . . . . 133 8.3.1 OverloadResolutionwithRvalueReferences. . . . . . . . . . . . . . . . . . . . . . . 133 8.3.2 OverloadingbyReferenceandValue . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Josuttis: C++MoveSemantics2022/04/1913:28 pagevii Contents vii 8.4 WhenLvaluesbecomeRvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 8.5 WhenRvaluesbecomeLvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 8.6 CheckingValueCategorieswithdecltype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 8.6.1 UsingdecltypetoChecktheTypeofNames . . . . . . . . . . . . . . . . . . . . . . 136 8.6.2 UsingdecltypetoChecktheValueCategory . . . . . . . . . . . . . . . . . . . . . . 137 8.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 PartII:MoveSemanticsinGenericCode 139 9 PerfectForwarding 141 9.1 MotivationforPerfectForwarding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 9.1.1 WhatweNeedtoPerfectlyForwardArguments . . . . . . . . . . . . . . . . . . . . . 141 9.2 ImplementingPerfectForwarding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 9.2.1 Universal(orForwarding)References . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 9.2.2 std::forward<>() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 9.2.3 TheEffectofPerfectForwarding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 9.3 RvalueReferencesversusUniversalReferences . . . . . . . . . . . . . . . . . . . . . . . . . . 147 9.3.1 RvalueReferencesofActualTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 9.3.2 RvalueReferencesofFunctionTemplateParameters . . . . . . . . . . . . . . . . . . 148 9.4 OverloadResolutionwithUniversalReferences . . . . . . . . . . . . . . . . . . . . . . . . . . 149 9.4.1 FixingOverloadResolutionwithUniversalReferences. . . . . . . . . . . . . . . . . 150 9.5 PerfectForwardinginLambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 9.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 10 TrickyDetailsofPerfectForwarding 153 10.1 UniversalReferencesasNon-ForwardingReferences . . . . . . . . . . . . . . . . . . . . . . . 153 10.1.1 UniversalReferencesandconst . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 10.1.2 UniversalReferencesinDetail. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 10.1.3 UniversalReferencesofSpecificTypes . . . . . . . . . . . . . . . . . . . . . . . . . . 157 10.2 UniversalorOrdinaryRvalueReference? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 10.2.1 RvalueReferencesofMembersofGenericTypes . . . . . . . . . . . . . . . . . . . . 159 10.2.2 RvalueReferencesofParametersinClassTemplates . . . . . . . . . . . . . . . . . . 161 10.2.3 RvalueReferencesofParametersinFullSpecializations . . . . . . . . . . . . . . . . 162 10.3 HowtheStandardSpecifiesPerfectForwarding . . . . . . . . . . . . . . . . . . . . . . . . . . 164 10.3.1 ExplicitSpecificationofTypesforUniversalReferences . . . . . . . . . . . . . . . . 166 Josuttis: C++MoveSemantics2022/04/1913:28 pageviii viii Contents 10.3.2 ConflictingTemplateParameterDeductionwithUniversalReferences . . . . . . . 167 10.3.3 PureRValueReferencesofGenericTypes. . . . . . . . . . . . . . . . . . . . . . . . . 168 10.4 NastyDetailsofPerfectForwarding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 10.4.1 “Universal”versus“Forwarding”Reference . . . . . . . . . . . . . . . . . . . . . . . 169 10.4.2 Why&&forBothOrdinaryRvaluesandUniversalReferences? . . . . . . . . . . . . 170 10.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 11 PerfectPassingwithauto&& 173 11.1 DefaultPerfectPassing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 11.1.1 DefaultPerfectPassinginDetail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 11.2 UniversalReferenceswithauto&& . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 11.2.1 TypeDeductionofauto&& . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 11.2.2 PerfectlyForwardinganauto&&Reference . . . . . . . . . . . . . . . . . . . . . . . . 177 11.3 auto&&asNon-ForwardingReference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 11.3.1 UniversalReferencesandtheRange-BasedforLoop . . . . . . . . . . . . . . . . . 178 11.4 PerfectForwardinginLambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 11.5 Usingauto&&inC++20FunctionDeclarations . . . . . . . . . . . . . . . . . . . . . . . . . . 183 11.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 12 PerfectReturningwithdecltype(auto) 185 12.1 PerfectReturning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 12.2 decltype(auto) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 12.2.1 ReturnTypedecltype(auto) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 12.2.2 DeferredPerfectReturning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 12.2.3 PerfectForwardingandReturningwithLambdas . . . . . . . . . . . . . . . . . . . . 191 12.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 PartIII:MoveSemanticsintheC++StandardLibrary 193 13 Move-OnlyTypes 195 13.1 DeclaringandUsingMove-OnlyTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 13.1.1 DeclaringMove-OnlyTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 13.1.2 UsingMove-OnlyTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 13.1.3 PassingMove-OnlyObjectsasArguments . . . . . . . . . . . . . . . . . . . . . . . . 197 13.1.4 ReturningMove-OnlyObjectsbyValue . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Josuttis: C++MoveSemantics2022/04/1913:28 pageix Contents ix 13.1.5 Moved-FromStatesofMove-OnlyObjects . . . . . . . . . . . . . . . . . . . . . . . . 198 13.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 14 MovingAlgorithmsandIterators 201 14.1 MovingAlgorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 14.2 RemovingAlgorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 14.3 MoveIterators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 14.3.1 MoveIteratorsinAlgorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 14.3.2 MoveIteratorsinConstructorsandMemberFunctions . . . . . . . . . . . . . . . . . 209 14.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 15 MoveSemanticsinTypesoftheC++StandardLibrary 211 15.1 MoveSemanticsforStrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 15.1.1 StringAssignmentsandCapacity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 15.2 MoveSemanticsforContainers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 15.2.1 BasicMoveSupportforContainersasaWhole . . . . . . . . . . . . . . . . . . . . . 214 15.2.2 InsertandEmplaceFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 15.2.3 MoveSemanticsforstd::array<> . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 15.3 MoveSemanticsforVocabularyTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 15.3.1 MoveSemanticsforPairs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 15.3.2 MoveSemanticsforstd::optional<> . . . . . . . . . . . . . . . . . . . . . . . . . 223 15.4 MoveSemanticsforSmartPointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 15.4.1 MoveSemanticsforstd::shared_ptr<> . . . . . . . . . . . . . . . . . . . . . . . . 224 15.4.2 MoveSemanticsforstd::unique_ptr<> . . . . . . . . . . . . . . . . . . . . . . . . 225 15.5 MoveSemanticsforIOStreams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 15.5.1 MovingIOStreamObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 15.5.2 UsingTemporaryIOStreams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 15.6 MoveSemanticsforMultithreading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 15.6.1 std::thread<>andstd::jthread<>. . . . . . . . . . . . . . . . . . . . . . . . . . 229 15.6.2 Futures,Promises,andPackagedTasks . . . . . . . . . . . . . . . . . . . . . . . . . . 230 15.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Glossary 233 Index 237 Josuttis: C++MoveSemantics2022/04/1913:28 pagex