A Tour of C++ Third Edition C ++ In-Depth Series Bjarne Stroustrup, Series Editor Visit informit.com/series/indepth for a complete list of available publications. The C ++ In-Depth Series is a collection of concise and focused books that provide real-world programmers with reliable information about the C ++ programming language. Selected by the designer and original implementor of C ++, Bjarne Stroustrup, and written by carefully chosen experts in the field, each book in this series presents either a single topic, at a technical level appropriate to that topic, or a fast-paced overview, for a quick understanding of broader language features. In either case, the series’ practical approach is designed to lift professionals (and aspiring professionals) to the next level of programming skill or knowledge. Make sure to connect with us! informit.com/socialconnect A Tour of C++ Third Edition Bjarne Stroustrup Boston • Columbus • NewYork • SanFrancisco • Amsterdam • CapeTo wn Dubai • London • Madrid • Milan • Munich • Paris • Montreal • Toronto • Delhi •Mexico City São Paulo • Sydney • Hong Kong • Seoul • Singapore • Taipei • Tokyo Coverphoto by: Marco Pregnolato (Unsplash.com: @marco_pregnolato). Author photo courtesy of Bjarne Stroustrup. Manyofthe designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but makenoexpressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. Forinformation about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom coverdesigns; and content particular to your business, training goals, marketing focus, or branding inter- ests), please contact our corporate sales department at [email protected] or (800) 382-3419. Forgovernment sales inquiries, please contact [email protected]. Forquestions about sales outside the U.S., please contact [email protected]. Visit us on the Web: informit.com/aw Library of Congress Control Number: 2022938722 Copyright © 2023 by Pearson Education, Inc. All rights reserved. This publication is protected by copyright, and permission must be obtained from the publisher prior to anyprohibited reproduction, storage in a retrievalsystem, or transmission in anyform or by anymeans, electronic, mechani- cal, photocopying, recording, or likewise. For information regarding permissions, request forms and the appropriate con- tacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearson.com/permissions. This book was typeset in Times and Helvetica by the author. ISBN-13: 978-0-13-681648-5 ISBN-10: 0-13-681648-7 First printing, October 2022 ScoutAutomatedPr intCode Contents Preface xi 1TheBasics 1 1.1 Introduction .................................................................................1 1.2 Programs .....................................................................................2 1.3 Functions .....................................................................................4 1.4 Types,Variables,andArithmetic ................................................5 1.5 ScopeandLifetime .....................................................................9 1.6 Constants .....................................................................................10 1.7 Pointers,Arrays,andReferences ................................................11 1.8 Tests ............................................................................................14 1.9 MappingtoHardware .................................................................16 1.10 Advice .........................................................................................19 2User-DefinedTypes 21 2.1 Introduction .................................................................................21 2.2 Structures ....................................................................................22 2.3 Classes ........................................................................................23 2.4 Enumerations ..............................................................................25 2.5 Unions .........................................................................................27 2.6 Advice .........................................................................................28 vi Contents 3Modularity 29 3.1 Introduction .................................................................................29 3.2 SeparateCompilation ..................................................................30 3.3 Namespaces.................................................................................35 3.4 FunctionArgumentsandReturnValues .....................................37 3.5 Advice .........................................................................................42 4ErrorHandling 43 4.1 Introduction .................................................................................43 4.2 Exceptions ...................................................................................44 4.3 Invariants .....................................................................................45 4.4 Error-HandlingAlternatives........................................................47 4.5 Assertions....................................................................................48 4.6 Advice .........................................................................................51 5Classes 53 5.1 Introduction .................................................................................53 5.2 ConcreteTypes............................................................................54 5.3 AbstractTypes ............................................................................60 5.4 VirtualFunctions.........................................................................62 5.5 ClassHierarchies ........................................................................63 5.6 Advice .........................................................................................69 6EssentialOperations 71 6.1 Introduction .................................................................................71 6.2 CopyandMove ...........................................................................74 6.3 ResourceManagement ................................................................78 6.4 OperatorOverloading .................................................................80 6.5 ConventionalOperations.............................................................81 6.6 User-DefinedLiterals ..................................................................84 6.7 Advice .........................................................................................85 7Templates 87 7.1 Introduction .................................................................................87 7.2 ParameterizedTypes ...................................................................88 7.3 ParameterizedOperations ...........................................................93 7.4 TemplateMechanisms ................................................................99 7.5 Advice .........................................................................................102 vii 8ConceptsandGenericProgramming 103 8.1 Introduction .................................................................................103 8.2 Concepts......................................................................................104 8.3 GenericProgramming .................................................................112 8.4 VariadicTemplates ......................................................................114 8.5 TemplateCompilationModel .....................................................117 8.6 Advice .........................................................................................117 9LibraryOverview 119 9.1 Introduction .................................................................................119 9.2 Standard-LibraryComponents ....................................................120 9.3 Standard-LibraryOrganization ...................................................121 9.4 Advice .........................................................................................124 10StringsandRegularExpressions 125 10.1 Introduction .................................................................................125 10.2 Strings .........................................................................................125 10.3 StringViews ................................................................................128 10.4 RegularExpressions....................................................................130 10.5 Advice .........................................................................................136 11InputandOutput 137 11.1 Introduction .................................................................................137 11.2 Output .........................................................................................138 11.3 Input ............................................................................................139 11.4 I/OState ......................................................................................141 11.5 I/OofUser-DefinedTypes ..........................................................141 11.6 OutputFormatting.......................................................................143 11.7 Streams........................................................................................146 11.8 C-styleI/O...................................................................................149 11.9 FileSystem .................................................................................150 11.10 Advice .........................................................................................154 12Containers 157 12.1 Introduction .................................................................................157 12.2 vector ...........................................................................................158 12.3 list ................................................................................................162 12.4 forward_list ..................................................................................164 12.5 map ..............................................................................................164 viii Contents 12.6 unordered_map ............................................................................165 12.7 Allocators ....................................................................................167 12.8 ContainerOverview ....................................................................168 12.9 Advice .........................................................................................170 13Algorithms 173 13.1 Introduction .................................................................................173 13.2 UseofIterators............................................................................175 13.3 IteratorTypes ..............................................................................178 13.4 UseofPredicates.........................................................................181 13.5 AlgorithmOverview ...................................................................181 13.6 ParallelAlgorithms .....................................................................183 13.7 Advice .........................................................................................183 14Ranges 185 14.1 Introduction .................................................................................185 14.2 Views...........................................................................................186 14.3 Generators ...................................................................................188 14.4 Pipelines ......................................................................................188 14.5 ConceptsOverview .....................................................................190 14.6 Advice .........................................................................................194 15PointersandContainers 195 15.1 Introduction .................................................................................195 15.2 Pointers .......................................................................................196 15.3 Containers ...................................................................................201 15.4 Alternatives .................................................................................208 15.5 Advice .........................................................................................212 16Utilities 213 16.1 Introduction .................................................................................213 16.2 Time ............................................................................................214 16.3 FunctionAdaption ......................................................................216 16.4 TypeFunctions ............................................................................217 16.5 source_location ............................................................................222 16.6 move()andforward().....................................................................223 16.7 BitManipulation .........................................................................224 16.8 ExitingaProgram .......................................................................225 16.9 Advice .........................................................................................225 ix 17Numerics 227 17.1 Introduction .................................................................................227 17.2 MathematicalFunctions ..............................................................228 17.3 NumericalAlgorithms ................................................................229 17.4 ComplexNumbers ......................................................................230 17.5 RandomNumbers .......................................................................231 17.6 VectorArithmetic ........................................................................233 17.7 NumericLimits ...........................................................................234 17.8 TypeAliases ................................................................................234 17.9 MathematicalConstants ..............................................................234 17.10 Advice .........................................................................................235 18Concurrency 237 18.1 Introduction .................................................................................237 18.2 Tasksandthreads ........................................................................238 18.3 SharingData................................................................................241 18.4 WaitingforEvents ......................................................................243 18.5 CommunicatingTasks .................................................................245 18.6 Coroutines ...................................................................................250 18.8 Advice .........................................................................................253 19HistoryandCompatibility 255 19.1 History ........................................................................................255 19.2 C++FeatureEvolution................................................................263 19.3 C/C++Compatibility ..................................................................268 19.4 Bibliography ...............................................................................271 19.5 Advice .........................................................................................274 Modulestd 277 A.1 Introduction .................................................................................277 A.2 UseWhatYourImplementationOffers ......................................278 A.3 UseHeaders ................................................................................278 A.4 MakeYourOwnmodulestd ........................................................278 A.5 Advice .........................................................................................279 Index 281