A Tour of C++ [3 ed.] 0136816487, 9780136816485

In A Tour of C++, Third Edition, Bjarne Stroustrup provides an overview of ISO C++, C++20, that aims to give experienced

516 94 16MB

English Pages 320 [312] Year 2022

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Cover
Half Title
Title Page
Copyright Page
Contents
Preface
1 The Basics
1.1 Introduction
1.2 Programs
1.3 Functions
1.4 Types, Variables, and Arithmetic
1.5 Scope and Lifetime
1.6 Constants
1.7 Pointers, Arrays, and References
1.8 Tests
1.9 Mapping to Hardware
1.10 Advice
2 User-Defined Types
2.1 Introduction
2.2 Structures
2.3 Classes
2.4 Enumerations
2.5 Unions
2.6 Advice
3 Modularity
3.1 Introduction
3.2 Separate Compilation
3.3 Namespaces
3.4 Function Arguments and Return Values
3.5 Advice
4 Error Handling
4.1 Introduction
4.2 Exceptions
4.3 Invariants
4.4 Error-Handling Alternatives
4.5 Assertions
4.6 Advice
5 Classes
5.1 Introduction
5.2 Concrete Types
5.3 Abstract Types
5.4 Virtual Functions
5.5 Class Hierarchies
5.6 Advice
6 Essential Operations
6.1 Introduction
6.2 Copy and Move
6.3 Resource Management
6.4 Operator Overloading
6.5 Conventional Operations
6.6 User-Defined Literals
6.7 Advice
7 Templates
7.1 Introduction
7.2 Parameterized Types
7.3 Parameterized Operations
7.4 Template Mechanisms
7.5 Advice
8 Concepts and Generic Programming
8.1 Introduction
8.2 Concepts
8.3 Generic Programming
8.4 Variadic Templates
8.5 Template Compilation Model
8.6 Advice
9 Library Overview
9.1 Introduction
9.2 Standard-Library Components
9.3 Standard-Library Organization
9.4 Advice
10 Strings and Regular Expressions
10.1 Introduction
10.2 Strings
10.3 String Views
10.4 Regular Expressions
10.5 Advice
11 Input and Output
11.1 Introduction
11.2 Output
11.3 Input
11.4 I/O State
11.5 I/O of User-Defined Types
11.6 Output Formatting
11.7 Streams
11.8 C-style I/O
11.9 File System
11.10 Advice
12 Containers
12.1 Introduction
12.2 vector
12.3 list
12.4 forward_list
12.5 map
12.6 unordered_map
12.7 Allocators
12.8 Container Overview
12.9 Advice
13 Algorithms
13.1 Introduction
13.2 Use of Iterators
13.3 Iterator Types
13.4 Use of Predicates
13.5 Algorithm Overview
13.6 Parallel Algorithms
13.7 Advice
14 Ranges
14.1 Introduction
14.2 Views
14.3 Generators
14.4 Pipelines
14.5 Concepts Overview
14.6 Advice
15 Pointers and Containers
15.1 Introduction
15.2 Pointers
15.3 Containers
15.4 Alternatives
15.5 Advice
16 Utilities
16.1 Introduction
16.2 Time
16.3 Function Adaption
16.4 Type Functions
16.5 source_location
16.6 move() and forward()
16.7 Bit Manipulation
16.8 Exiting a Program
16.9 Advice
17 Numerics
17.1 Introduction
17.2 Mathematical Functions
17.3 Numerical Algorithms
17.4 Complex Numbers
17.5 Random Numbers
17.6 Vector Arithmetic
17.7 Numeric Limits
17.8 Type Aliases
17.9 Mathematical Constants
17.10 Advice
18 Concurrency
18.1 Introduction
18.2 Tasks and threads
18.3 Sharing Data
18.4 Waiting for Events
18.5 Communicating Tasks
18.6 Coroutines
18.7 Advice
19 History and Compatibility
19.1 History
19.2 C++ Feature Evolution
19.3 C/C++ Compatibility
19.4 Bibliography
19.5 Advice
Module std
A.1 Introduction
A.2 Use What Your Implementation Offers
A.3 Use Headers
A.4 Make Your Own module std
A.5 Advice
Index
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Recommend Papers

A Tour of C++ [3 ed.]
 0136816487, 9780136816485

  • Commentary
  • First printing, October 2022 (True PDF)
  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

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.

T

he 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/socialconnec t

A Tour of C++ Third Edition

Bjarne Stroustrup

Boston • Columbus • New York • San Francisco • Amsterdam • Cape Town Dubai • London • Madrid • Milan • Munich • Paris • Montreal • Toronto • Delhi • Mexico City São Paulo • Sydney • Hong Kong • Seoul • Singapore • Taipei • Tokyo

Cover photo by: Marco Pregnolato (Unsplash.com: @marco_pregnolato). Author photo courtesy of Bjarne Stroustrup. Many of the 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 make no expressed 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. For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at [email protected] or (800) 382-3419. For government sales inquiries, please contact [email protected]. For questions 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 any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, request forms and the appropriate contacts 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 ScoutAutomatedPrintCode

Contents

Preface

xi

1 The Basics

1

1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 1.10

Introduction ................................................................................. 1 Programs ..................................................................................... 2 Functions ..................................................................................... 4 Types, Variables, and Arithmetic ................................................ 5 Scope and Lifetime ..................................................................... 9 Constants ..................................................................................... 10 Pointers, Arrays, and References ................................................ 11 Tests ............................................................................................ 14 Mapping to Hardware ................................................................. 16 Advice ......................................................................................... 19

2 User-Defined Types 2.1 2.2 2.3 2.4 2.5 2.6

Introduction ................................................................................. 21 Structures .................................................................................... 22 Classes ........................................................................................ 23 Enumerations .............................................................................. 25 Unions ......................................................................................... 27 Advice ......................................................................................... 28

21

vi

Contents

3 Modularity 3.1 3.2 3.3 3.4 3.5

29 Introduction ................................................................................. 29 Separate Compilation .................................................................. 30 Namespaces ................................................................................. 35 Function Arguments and Return Values ..................................... 37 Advice ......................................................................................... 42

4 Error Handling 4.1 4.2 4.3 4.4 4.5 4.6

43

Introduction ................................................................................. 43 Exceptions ................................................................................... 44 Invariants ..................................................................................... 45 Error-Handling Alternatives ........................................................ 47 Assertions .................................................................................... 48 Advice ......................................................................................... 51

5 Classes

53 5.1 5.2 5.3 5.4 5.5 5.6

Introduction ................................................................................. 53 Concrete Types ............................................................................ 54 Abstract Types ............................................................................ 60 Virtual Functions ......................................................................... 62 Class Hierarchies ........................................................................ 63 Advice ......................................................................................... 69

6 Essential Operations 6.1 6.2 6.3 6.4 6.5 6.6 6.7

Introduction ................................................................................. 71 Copy and Move ........................................................................... 74 Resource Management ................................................................ 78 Operator Overloading ................................................................. 80 Conventional Operations ............................................................. 81 User-Defined Literals .................................................................. 84 Advice ......................................................................................... 85

7 Templates 7.1 7.2 7.3 7.4 7.5

71

87 Introduction ................................................................................. 87 Parameterized Types ................................................................... 88 Parameterized Operations ........................................................... 93 Template Mechanisms ................................................................ 99 Advice ......................................................................................... 102

vii

8 Concepts and Generic Programming 8.1 8.2 8.3 8.4 8.5 8.6

Introduction ................................................................................. 103 Concepts ...................................................................................... 104 Generic Programming ................................................................. 112 Variadic Templates ...................................................................... 114 Template Compilation Model ..................................................... 117 Advice ......................................................................................... 117

9 Library Overview 9.1 9.2 9.3 9.4

137

Introduction ................................................................................. 137 Output ......................................................................................... 138 Input ............................................................................................ 139 I/O State ...................................................................................... 141 I/O of User-Defined Types .......................................................... 141 Output Formatting ....................................................................... 143 Streams ........................................................................................ 146 C-style I/O ................................................................................... 149 File System ................................................................................. 150 Advice ......................................................................................... 154

12 Containers 12.1 12.2 12.3 12.4 12.5

125

Introduction ................................................................................. 125 Strings ......................................................................................... 125 String Views ................................................................................ 128 Regular Expressions .................................................................... 130 Advice ......................................................................................... 136

11 Input and Output 11.1 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 11.10

119

Introduction ................................................................................. 119 Standard-Library Components .................................................... 120 Standard-Library Organization ................................................... 121 Advice ......................................................................................... 124

10 Strings and Regular Expressions 10.1 10.2 10.3 10.4 10.5

103

157 Introduction ................................................................................. 157 vector ........................................................................................... 158 list ................................................................................................ 162 forward_list .................................................................................. 164 map .............................................................................................. 164

viii

Contents

12.6 12.7 12.8 12.9

............................................................................ 165 Allocators .................................................................................... 167 Container Overview .................................................................... 168 Advice ......................................................................................... 170 unordered_map

13 Algorithms 13.1 13.2 13.3 13.4 13.5 13.6 13.7

173 Introduction ................................................................................. 173 Use of Iterators ............................................................................ 175 Iterator Types .............................................................................. 178 Use of Predicates ......................................................................... 181 Algorithm Overview ................................................................... 181 Parallel Algorithms ..................................................................... 183 Advice ......................................................................................... 183

14 Ranges 14.1 14.2 14.3 14.4 14.5 14.6

185 Introduction ................................................................................. 185 Views ........................................................................................... 186 Generators ................................................................................... 188 Pipelines ...................................................................................... 188 Concepts Overview ..................................................................... 190 Advice ......................................................................................... 194

15 Pointers and Containers 15.1 15.2 15.3 15.4 15.5

195

Introduction ................................................................................. 195 Pointers ....................................................................................... 196 Containers ................................................................................... 201 Alternatives ................................................................................. 208 Advice ......................................................................................... 212

16 Utilities 16.1 Introduction ................................................................................. 213 16.2 Time ............................................................................................ 214 16.3 Function Adaption ...................................................................... 216 16.4 Type Functions ............................................................................ 217 16.5 source_location ............................................................................ 222 16.6 move() and forward() ..................................................................... 223 16.7 Bit Manipulation ......................................................................... 224 16.8 Exiting a Program ....................................................................... 225 16.9 Advice ......................................................................................... 225

213

ix

17 Numerics 17.1 17.2 17.3 17.4 17.5 17.6 17.7 17.8 17.9 17.10

227 Introduction ................................................................................. 227 Mathematical Functions .............................................................. 228 Numerical Algorithms ................................................................ 229 Complex Numbers ...................................................................... 230 Random Numbers ....................................................................... 231 Vector Arithmetic ........................................................................ 233 Numeric Limits ........................................................................... 234 Type Aliases ................................................................................ 234 Mathematical Constants .............................................................. 234 Advice ......................................................................................... 235

18 Concurrency 18.1 18.2 18.3 18.4 18.5 18.6 18.8

Introduction ................................................................................. 237 Tasks and threads ........................................................................ 238 Sharing Data ................................................................................ 241 Waiting for Events ...................................................................... 243 Communicating Tasks ................................................................. 245 Coroutines ................................................................................... 250 Advice ......................................................................................... 253

19 History and Compatibility 19.1 19.2 19.3 19.4 19.5

History ........................................................................................ 255 C++ Feature Evolution ................................................................ 263 C/C++ Compatibility .................................................................. 268 Bibliography ............................................................................... 271 Advice ......................................................................................... 274

Module std A.1 A.2 A.3 A.4 A.5

Introduction ................................................................................. 277 Use What Your Implementation Offers ...................................... 278 Use Headers ................................................................................ 278 Make Your Own module std ........................................................ 278 Advice ......................................................................................... 279

Index

237

255

277

281

This page intentionally left blank

Preface When you wish to instruct, be brief. – Cicero

C++ feels like a new language. That is, I can express my ideas more clearly, more simply, and more directly today than I could in C++98 or C++11. Furthermore, the resulting programs are better checked by the compiler and run faster. This book gives an overview of C++ as defined by C++20, the current ISO C++ standard, and implemented by the major C++ suppliers. In addition, it mentions a couple library components in current use, but not scheduled for inclusion into the standard until C++23. Like other modern languages, C++ is large and there are a large number of libraries needed for effective use. This thin book aims to give an experienced programmer an idea of what constitutes modern C++. It covers most major language features and the major standard-library components. This book can be read in just a day or two but, obviously, there is much more to writing good C++ than can be learned in that amount of time. However, the aim here is not mastery, but to give an overview, to give key examples, and to help a programmer get started. The assumption is that you have programmed before. If not, please consider reading a textbook, such as Programming: Principles and Practice Using C++ (Second edition) [Stroustrup,2014], before continuing here. Even if you have programmed before, the language you used or the applications you wrote may be very different from the style of C++ presented here. Think of a sightseeing tour of a city, such as Copenhagen or New York. In just a few hours, you are given a quick peek at the major attractions, told a few background stories, and given some suggestions about what to do next. You do not know the city after such a tour. You do not understand all you have seen and heard; some stories may sound strange or even implausible. You do not know how to navigate the formal and informal rules that govern life in the city. To really know a city, you have to live in it, often for years. However, with a bit of luck, you will have gained a bit of an overview, a notion of what is special about the city, and ideas of what might be of interest to you. After the tour, the real exploration can begin.

xii

Preface

This tour presents the major C++ language features as they support programming styles, such as object-oriented and generic programming. It does not attempt to provide a detailed, reference-manual, feature-by-feature view of the language. In the best textbook tradition, I try to explain a feature before I use it, but that is not always possible and not everybody reads the text strictly sequentially. I assume some technical maturity from my readers. So, the reader is encouraged to use the cross references and the index. Similarly, this tour presents the standard libraries in terms of examples, rather than exhaustively. The reader is encouraged to search out additional and supporting material as needed. There is far more to the C++ ecosystem than just the facilities offered by ISO standard (e.g., libraries, build systems, analysis tools, and development environments). There is an enormous amount of material (of varying quality) available on the Web. Most readers will find useful tutorial and overview videos from conferences such as CppCon and Meeting C++. For technical details of the language and library offered by the ISO C++ standard, I recommend [Cppreference]. For example, when I mention a standard-library function or class, its definition can easily be looked up, and by examining its documentation, many related facilities can be found. This tour presents C++ as an integrated whole, rather than as a layer cake. Consequently, I rarely identify language features as present in C, C++98, or later ISO standards. Such information can be found in Chapter 19 (History and Compatibility). I focus on fundamentals and try to be brief, but I have not completely resisted the temptation to overrepresent novel features, such as modules (§3.2.2), concepts (§8.2), and coroutines (§18.6). Slightly favoring recent developments also seems to satisfy the curiosity of many readers who already know some older version of C++. A programming language reference manual or standard simply states what can be done, but programmers are often more interested in learning how to use the language well. This aspect is partly addressed in the selection of topics covered, partly in the text, and specifically in the advice sections. More advice about what constitutes good modern C++ can be found in the C++ Core Guidelines [Stroustrup,2015]. The Core Guidelines can be a good source for further exploration of the ideas presented in this book. You may note a remarkable similarity of the advice formulation and even the numbering of advice between the Core Guidelines and this book. One reason is that the first edition of A Tour of C++ was a major source of the initial Core Guidelines.

Acknowledgments Thanks to all who helped complete and correct the earlier editions of A Tour of C++, especially to the students in my ‘‘Design Using C++’’ course at Columbia University. Thanks to Morgan Stanley for giving me time to write this third edition. Thanks to Chuck Allison, Guy Davidson, Stephen Dewhurst, Kate Gregory, Danny Kalev, Gor Nishanov, and J.C. van Winkel for reviewing the book and suggesting many improvements. This book was set using troff by the author using macros originating from Brian Kernighan. Manhattan, New York

Bjarne Stroustrup

1 The Basics The first thing we do, let’s kill all the language lawyers. – Henry VI, Part II

• • • • • • • • • •

Introduction Programs Hello, World! Functions Types, Variables, and Arithmetic Arithmetic; Initialization Scope and Lifetime Constants Pointers, Arrays, and References The Null Pointer Tests Mapping to Hardware Assignment; Initialization Advice

1.1 Introduction This chapter informally presents the notation of C++, C++’s model of memory and computation, and the basic mechanisms for organizing code into a program. These are the language facilities supporting the styles most often seen in C and sometimes called procedural programming.

2

The Basics

Chapter 1

1.2 Programs C++ is a compiled language. For a program to run, its source text has to be processed by a compiler, producing object files, which are combined by a linker yielding an executable program. A C++ program typically consists of many source code files (usually simply called source files). source file 1

compile

object file 1

source file 2

compile

object file 2

link

executable file

An executable program is created for a specific hardware/system combination; it is not portable, say, from an Android device to a Windows PC. When we talk about portability of C++ programs, we usually mean portability of source code; that is, the source code can be successfully compiled and run on a variety of systems. The ISO C++ standard defines two kinds of entities: • Core language features, such as built-in types (e.g., char and int) and loops (e.g., for-statements and while-statements) • Standard-library components, such as containers (e.g., vector and map) and I/O operations (e.g., >c && c==',') { int number = 0; if (is>>number>>c && c=='}') { // read the number and a } e = {name,number}; // assign to the entry return is; } } } is.setstate(ios_base::failbit); return is;

// register the failure in the stream

}

An input operation returns a reference to its istream that can be used to test if the operation succeeded. For example, when used as a condition, is>>c means ‘‘Did we succeed in reading a char from is into c?’’ The is>>c skips whitespace by default, but is.get(c) does not, so this Entry-input operator ignores (skips) whitespace outside the name string, but not within it. For example: { "John Marwood Cleese", 123456 {"Michael Edward Palin", 987654}

}

We can read such a pair of values from input into an Entry like this:

Section 11.5

I/O of User-Defined Types

143

for (Entry ee; cin>>ee; ) // read from cin into ee cout