Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
josuttis_nm_c20_the_complete_guide.pdf
Скачиваний:
44
Добавлен:
27.03.2023
Размер:
5.85 Mб
Скачать

To the nation of Ukraine

and all the children, women, men, and soldiers killed in a terrible war driven by a Russian dictator

C++20 - The Complete Guide

First Edition

Nicolai M. Josuttis

This version was published on 2022-10-30.

© 2022 by Nicolai Josuttis. All rights reserved.

This publication is protected by copyright, and permission must be obtained from the author 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.

This book was typeset by Nicolai M. Josuttis using the LATEX document processing system.

This book is for sale at http://leanpub.com/cpp20.

This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book, and build traction once you do.

Contents

Preface

 

xix

An Experiment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xix

Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xix

Versions of This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xx

About This Book

 

xxiii

What You Should Know Before Reading This Book . . . . . . . . . . . . . . . . . . . . . . . . . .

xxiii

Overall Structure of the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xxiii

How to Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xxiv

The Way I Implement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xxiv

The C++ Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xxv

Example Code and Additional Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xxv

Feedback . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

xxvi

1 Comparisons and Operator <=>

1

1.1 Motivation for Operator<=> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1

1.1.1 Defining Comparison Operators Before C++20 . . . . . . . . . . . . . . . . . . . . .

1

1.1.2 Defining Comparison Operators Since C++20 . . . . . . . . . . . . . . . . . . . . . .

4

1.2 Defining and Using Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7

1.2.1

Using Operator<=> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7

1.2.2

Comparison Category Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

8

1.2.3 Using Comparison Categories with operator<=> . . . . . . . . . . . . . . . . . . . .

9

1.2.4 Calling Operator <=> Directly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

10

1.2.5 Dealing with Multiple Ordering Criteria . . . . . . . . . . . . . . . . . . . . . . . . . .

11

1.3 Defining operator<=> and operator== . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

13

1.3.1 Defaulted operator== and operator<=> . . . . . . . . . . . . . . . . . . . . . . . .

14

iii

iv

 

 

Contents

 

1.3.2 Defaulted operator<=> Implies Defaulted operator== . . . . . . . . . . . . .

. . 14

 

1.3.3 Implementation of the Defaulted operator<=> . . . . . . . . . . . . . . . . . . .

. . 16

1.4

Overload Resolution with Rewritten Expressions . . . . . . . . . . . . . . . . . . . . . . . .

. . 17

1.5

Using Operator <=> in Generic Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 19

 

1.5.1 compare_three_way . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 19

 

1.5.2

Algorithm lexicographical_compare_three_way() . . . . . . . . . . . . .

. . 19

1.6

Compatibility Issues with the Comparison Operators . . . . . . . . . . . . . . . . . . . . .

. . 21

 

1.6.1 Delegating Free-Standing Comparison Operators . . . . . . . . . . . . . . . . . .

. . 21

 

1.6.2 Inheritance with Protected Members . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 22

1.7

Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 23

2 Placeholder Types for Function Parameters

25

2.1

auto for Parameters of Ordinary Functions . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 25

 

2.1.1 auto for Parameters of Member Functions . . . . . . . . . . . . . . . . . . . . . .

. . 26

2.2

Using auto for Parameters in Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 27

 

2.2.1 Deferred Type Checks with auto . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 27

 

2.2.2 auto Functions versus Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 29

2.3

auto for Parameters in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 30

 

2.3.1 Basic Constraints for auto Parameters . . . . . . . . . . . . . . . . . . . . . . . . .

. . 30

 

2.3.2 Combining Template and auto Parameters . . . . . . . . . . . . . . . . . . . . . .

. . 31

2.4

Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 32

3 Concepts, Requirements, and Constraints

33

3.1

Motivating Example of Concepts and Requirements . . . . . . . . . . . . . . . . . . . . . .

. . 33

 

3.1.1 Improving the Template Step by Step . . . . . . . . . . . . . . . . . . . . . . . . .

. . 33

 

3.1.2 A Complete Example with Concepts . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 39

3.2

Where Constraints and Concepts Can Be Used . . . . . . . . . . . . . . . . . . . . . . . . .

. . 40

 

3.2.1

Constraining Alias Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 41

 

3.2.2

Constraining Variable Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 41

 

3.2.3

Constraining Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 42

 

3.2.4 Constraining Non-Type Template Parameters . . . . . . . . . . . . . . . . . . . . .

. . 43

3.3

Typical Applications of Concepts and Constraints in Practice . . . . . . . . . . . . . . . .

. . 44

 

3.3.1 Using Concepts to Understand Code and Error Messages . . . . . . . . . . . . .

. . 44

 

3.3.2 Using Concepts to Disable Generic Code . . . . . . . . . . . . . . . . . . . . . . .

. . 47

 

3.3.3 Using Requirements to Call Different Functions . . . . . . . . . . . . . . . . . . .

. . 51

Contents

 

 

v

 

3.3.4 The Example as a Whole . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

56

 

3.3.5

Former Workarounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

58

3.4

Semantic Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

59

 

3.4.1 Examples of Semantic Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

59

3.5

Design Guidelines for Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

62

 

3.5.1 Concepts Should Group Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . .

62

 

3.5.2 Define Concepts with Care . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

62

 

3.5.3 Concepts versus Type Traits and Boolean Expressions . . . . . . . . . . . . . . . . .

62

3.6

Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

64

4 Concepts, Requirements, and Constraints in Detail

65

4.1

Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

65

4.2

requires Clauses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

66

 

4.2.1 Using && and || in requires Clauses . . . . . . . . . . . . . . . . . . . . . . . . . . .

66

4.3

Ad-hoc Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

67

4.4

requires Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

68

 

4.4.1

Simple Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

69

 

4.4.2

Type Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

71

 

4.4.3

Compound Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

73

 

4.4.4

Nested Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

74

4.5

Concepts in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

75

 

4.5.1

Defining Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

75

 

4.5.2 Special Abilities of Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

75

 

4.5.3 Concepts for Non-Type Template Parameters . . . . . . . . . . . . . . . . . . . . . . .

76

4.6

Using Concepts as Type Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

77

4.7

Subsuming Constraints with Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

80

 

4.7.1

Indirect Subsumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

82

 

4.7.2

Defining Commutative Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

83

5 Standard Concepts in Detail

87

5.1

Overview of All Standard Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

87

 

5.1.1 Header Files and Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

87

 

5.1.2

Standard Concepts Subsume . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

90

5.2

Language-Related Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

91

 

5.2.1

Arithmetic Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

91

vi

 

 

Contents

 

5.2.2

Object Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 91

 

5.2.3 Concepts for Relationships between Types . . . . . . . . . . . . . . . . . . . . . .

. . 93

 

5.2.4

Comparison Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 94

5.3 Concepts for Iterators and Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 96

 

5.3.1 Concepts for Ranges and Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 96

 

5.3.2 Concepts for Pointer-Like Objects . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 100

 

5.3.3

Concepts for Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 102

 

5.3.4 Iterator Concepts for Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 105

5.4

Concepts for Callables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 106

 

5.4.1 Basic Concepts for Callables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 106

 

5.4.2 Concepts for Callables Used by Iterators . . . . . . . . . . . . . . . . . . . . . . .

. . 109

5.5

Auxiliary Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 111

 

5.5.1 Concepts for Specific Type Attributes . . . . . . . . . . . . . . . . . . . . . . . . .

. . 111

 

5.5.2 Concepts for Incrementable Types . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 112

6 Ranges and Views

115

6.1 A Tour of Ranges and Views Using Examples . . . . . . . . . . . . . . . . . . . . . . . . .

. . 116

 

6.1.1 Passing Containers to Algorithms as Ranges . . . . . . . . . . . . . . . . . . . . .

. . 116

 

6.1.2 Constraints and Utilities for Ranges . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 118

 

6.1.3

Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 121

 

6.1.4

Sentinels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 126

 

6.1.5 Range Definitions with Sentinels and Counts . . . . . . . . . . . . . . . . . . . . .

. . 129

 

6.1.6

Projections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 133

 

6.1.7 Utilities for Implementing Code for Ranges . . . . . . . . . . . . . . . . . . . . . .

. . 133

 

6.1.8 Limitations and Drawbacks of Ranges . . . . . . . . . . . . . . . . . . . . . . . . .

. . 135

6.2 Borrowed Iterators and Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 136

 

6.2.1

Borrowed Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 136

 

6.2.2

Borrowed Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 138

6.3

Using Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 140

 

6.3.1

Views on Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 142

 

6.3.2

Lazy Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 143

 

6.3.3

Caching in Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 146

 

6.3.4 Performance Issues with Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 149

6.4 Views on Ranges That Are Destroyed or Modified . . . . . . . . . . . . . . . . . . . . . . .

. . 151

 

6.4.1 Lifetime Dependencies Between Views and Their Ranges . . . . . . . . . . . . .

. . 151

Contents

 

 

vii

 

6.4.2 Views with Write Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

152

 

6.4.3 Views on Ranges That Change . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

153

 

6.4.4 Copying Views Might Change Behavior . . . . . . . . . . . . . . . . . . . . . . . . . .

154

6.5

Views and const . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

155

 

6.5.1 Generic Code for Both Containers and Views . . . . . . . . . . . . . . . . . . . . . . .

156

 

6.5.2 Views May Remove the Propagation of const . . . . . . . . . . . . . . . . . . . . . .

163

 

6.5.3 Bringing Back Deep Constness to Views . . . . . . . . . . . . . . . . . . . . . . . . .

165

6.6

Summary of All Container Idioms Broken By Views . . . . . . . . . . . . . . . . . . . . . . .

168

6.7

Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

168

7 Utilities for Ranges and Views

171

7.1

Key Utilities for Using Ranges as Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

171

 

7.1.1

std::views::all() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

172

 

7.1.2

std::views::counted() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

174

 

7.1.3

std::views::common() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

176

7.2

New Iterator Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

177

7.3

New Iterator and Sentinel Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

178

 

7.3.1

std::counted_iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

179

 

7.3.2

std::common_iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

179

 

7.3.3

std::default_sentinel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

180

 

7.3.4

std::unreachable_sentinel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

182

 

7.3.5

std::move_sentinel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

182

7.4

New Functions for Dealing with Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

183

 

7.4.1 Functions for Dealing with the Elements of Ranges (and Arrays) . . . . . . . . . . .

183

 

7.4.2 Functions for Dealing with Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . .

185

 

7.4.3 Functions for Swapping and Moving Elements/Values . . . . . . . . . . . . . . . . .

186

 

7.4.4 Functions for Comparisons of Values . . . . . . . . . . . . . . . . . . . . . . . . . . . .

188

7.5

New Type Functions/Utilities for Dealing with Ranges . . . . . . . . . . . . . . . . . . . . . .

189

 

7.5.1 Generic Types of Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

189

 

7.5.2 Generic Types of Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

189

 

7.5.3

New Functional Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

190

 

7.5.4 Other New Types for Dealing with Iterators . . . . . . . . . . . . . . . . . . . . . . . .

191

7.6

Range Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

192

 

7.6.1 Benefits and Restrictions for Range Algorithms . . . . . . . . . . . . . . . . . . . . .

192

 

7.6.2

Algorithm Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

193

viii

 

 

Contents

8 View Types in Detail

199

8.1

Overview of All Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 199

 

8.1.1 Overview of Wrapping and Generating Views . . . . . . . . . . . . . . . . . . . .

. . 199

 

8.1.2 Overview of Adapting Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 201

8.2

Base Class and Namespace of Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 202

 

8.2.1 Base Class for Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 202

 

8.2.2 Why Range Adaptors/Factories Have Their Own Namespace . . . . . . . . . . .

. . 203

8.3

Source Views to External Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 205

 

8.3.1

Subrange . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 205

 

8.3.2

Ref View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 211

 

8.3.3

Owning View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 214

 

8.3.4

Common View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 218

8.4

Generating Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 221

 

8.4.1

Iota View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 221

 

8.4.2

Single View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 226

 

8.4.3

Empty View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 230

 

8.4.4

IStream View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 233

 

8.4.5

String View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 237

 

8.4.6

Span . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 239

8.5

Filtering Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 241

 

8.5.1

Take View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 241

 

8.5.2

Take-While View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 244

 

8.5.3

Drop View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 247

 

8.5.4

Drop-While View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 252

 

8.5.5

Filter View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 257

8.6

Transforming Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 265

 

8.6.1

Transform View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 265

 

8.6.2

Elements View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 270

 

8.6.3 Keys and Values View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 276

8.7

Mutating Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 279

 

8.7.1

Reverse View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 279

8.8

Views for Multiple Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 284

 

8.8.1 Split and Lazy-Split View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 284

 

8.8.2

Join View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 290

Contents

 

 

ix

9 Spans

 

295

9.1

Using Spans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

295

 

9.1.1

Fixed and Dynamic Extent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

295

 

9.1.2

Example Using a Span with a Dynamic Extent . . . . . . . . . . . . . . . . . . . . . .

296

 

9.1.3

Example Using a Span with Non-const Elements . . . . . . . . . . . . . . . . . . . .

301

 

9.1.4

Example Using a Span with Fixed Extent . . . . . . . . . . . . . . . . . . . . . . . . .

303

 

9.1.5

Fixed vs. Dynamic Extent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

305

9.2

Spans Considered Harmful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

306

9.3

Design Aspects of Spans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

306

 

9.3.1

Lifetime Dependencies of Spans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

307

 

9.3.2

Performance of Spans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

307

 

9.3.3

const Correctness of Spans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

308

 

9.3.4

Using Spans as Parameters in Generic Code . . . . . . . . . . . . . . . . . . . . . . .

310

9.4

Span Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

312

 

9.4.1

Span Operations and Member Types Overview . . . . . . . . . . . . . . . . . . . . . .

312

 

9.4.2

Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

314

 

9.4.3

Operations for Sub-Spans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

317

9.5

Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

318

10 Formatted Output

319

10.1

Formatted Output by Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

319

 

10.1.1

Using std::format() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

319

 

10.1.2

Using std::format_to_n() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

321

 

10.1.3

Using std::format_to() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

322

 

10.1.4

Using std::formatted_size() . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

322

10.2

Performance of the Formatting Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

322

 

10.2.1

Using std::vformat() and vformat_to() . . . . . . . . . . . . . . . . . . . . . .

323

10.3

Formatted Output in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

324

 

10.3.1

General Format of Format Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

324

 

10.3.2

Standard Format Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

324

 

10.3.3

Width, Precision, and Fill Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . .

325

 

10.3.4

Format/Type Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

326

10.4

Internationalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

329

10.5

Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

331

x

 

 

Contents

10.6

User-Defined Formatted Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 333

 

10.6.1

Basic Formatter API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 333

 

10.6.2

Improved Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 335

 

10.6.3 Using Standard Formatters for User-Defined Formatters . . . . . . . . . . . . . .

. . 337

 

10.6.4 Using Standard Formatters for Strings . . . . . . . . . . . . . . . . . . . . . . . . .

. . 340

10.7

Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 342

11 Dates and Timezones for <chrono>

343

11.1

Overview by Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 343

 

11.1.1 Scheduling a Meeting on the 5th of Every Month . . . . . . . . . . . . . . . . . .

. . 343

 

11.1.2 Scheduling a Meeting on the Last Day of Every Month . . . . . . . . . . . . . . .

. . 348

 

11.1.3 Scheduling a Meeting Every First Monday . . . . . . . . . . . . . . . . . . . . . .

. . 350

 

11.1.4

Using Different Timezones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 352

11.2

Basic Chrono Concepts and Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 357

11.3

Basic Chrono Extensions with C++20 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 358

 

11.3.1

Duration Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 358

 

11.3.2

Clocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 359

 

11.3.3

Timepoint Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 360

 

11.3.4

Calendrical Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 361

 

11.3.5

Time Type hh_mm_ss . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 364

 

11.3.6

Hours Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 366

11.4

I/O with Chrono Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 367

 

11.4.1

Default Output Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 367

 

11.4.2

Formatted Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 368

 

11.4.3

Locale-Dependent Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 371

 

11.4.4

Formatted Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 372

11.5

Using the Chrono Extensions in Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 378

 

11.5.1

Invalid Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 378

 

11.5.2 Dealing with months and years . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 380

 

11.5.3 Parsing Timepoints and Durations . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 382

11.6

Timezones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 385

 

11.6.1

Characteristics of Timezones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 385

 

11.6.2 The IANA Timezone Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 385

 

11.6.3

Using Timezones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 387

 

11.6.4 Dealing with Timezone Abbreviations . . . . . . . . . . . . . . . . . . . . . . . . .

. . 390

Contents

 

 

xi

 

11.6.5

Custom Timezones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

391

11.7

Clocks in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

394

 

11.7.1 Clocks with a Specified Epoch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

394

 

11.7.2 The Pseudo Clock local_t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

395

 

11.7.3 Dealing with Leap Seconds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

396

 

11.7.4

Conversions between Clocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

397

 

11.7.5 Dealing with the File Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

399

11.8

Other New Chrono Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

401

11.9

Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

401

12 std::jthread and Stop Tokens

403

12.1

Motivation for std::jthread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

403

 

12.1.1 The Problem of std::thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

403

 

12.1.2

Using std::jthread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

405

 

12.1.3 Stop Tokens and Stop Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

406

 

12.1.4 Stop Tokens and Condition Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . .

408

12.2

Stop Sources and Stop Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

410

 

12.2.1 Stop Sources and Stop Tokens in Detail . . . . . . . . . . . . . . . . . . . . . . . . . .

411

 

12.2.2

Using Stop Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

412

 

12.2.3 Constraints and Guarantees of Stop Tokens . . . . . . . . . . . . . . . . . . . . . . . .

417

12.3

std::jthread in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

418

 

12.3.1 Using Stop Tokens with std::jthread . . . . . . . . . . . . . . . . . . . . . . . . . .

419

12.4

Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

421

13 Concurrency Features

423

13.1

Thread Synchronization with Latches and Barriers . . . . . . . . . . . . . . . . . . . . . . . . .

423

 

13.1.1

Latches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

423

 

13.1.2

Barriers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

427

13.2

Semaphores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

431

 

13.2.1 Example of Using Counting Semaphores . . . . . . . . . . . . . . . . . . . . . . . . .

432

 

13.2.2 Example of Using Binary Semaphores . . . . . . . . . . . . . . . . . . . . . . . . . . .

436

13.3

Extensions for Atomic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

439

 

13.3.1 Atomic References with std::atomic_ref<> . . . . . . . . . . . . . . . . . . . . .

439

 

13.3.2

Atomic Shared Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

443

 

13.3.3

Atomic Floating-Point Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

447

xii

 

 

Contents

 

13.3.4 Thread Synchronization with Atomic Types . . . . . . . . . . . . . . . . . . . . .

. . 447

 

13.3.5

Extensions for std::atomic_flag . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 451

13.4

Synchronized Output Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 452

 

13.4.1 Motivation for Synchronized Output Streams . . . . . . . . . . . . . . . . . . . . .

. . 452

 

13.4.2 Using Synchronized Output Streams . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 453

 

13.4.3 Using Synchronized Output Streams for Files . . . . . . . . . . . . . . . . . . . .

. . 454

 

13.4.4 Using Synchronized Output Streams as Output Streams . . . . . . . . . . . . . .

. . 455

 

13.4.5 Synchronized Output Streams in Practice . . . . . . . . . . . . . . . . . . . . . . .

. . 456

13.5

Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 457

14 Coroutines

 

459

14.1

What Are Coroutines? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 459

14.2

A First Coroutine Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 461

 

14.2.1

Defining the Coroutine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 461

 

14.2.2

Using the Coroutine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 462

 

14.2.3 Lifetime Issues with Call-by-Reference . . . . . . . . . . . . . . . . . . . . . . . .

. . 466

 

14.2.4

Coroutines Calling Coroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 468

 

14.2.5 Implementing the Coroutine Interface . . . . . . . . . . . . . . . . . . . . . . . . .

. . 471

 

14.2.6 Bootstrapping Interface, Handle, and Promise . . . . . . . . . . . . . . . . . . . .

. . 477

 

14.2.7

Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 479

14.3

Coroutines That Yield or Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 481

 

14.3.1

Using co_yield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 481

 

14.3.2

Using co_return . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 487

14.4

Coroutine Awaitables and Awaiters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 491

 

14.4.1

Awaiters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 491

 

14.4.2

Standard Awaiters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 493

 

14.4.3

Resuming Sub-Coroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 494

 

14.4.4 Passing Values From Suspension Back to the Coroutine . . . . . . . . . . . . . .

. . 498

14.5

Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 503

15 Coroutines in Detail

505

15.1

Coroutine Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 505

 

15.1.1

Coroutine Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 505

15.2

The Coroutine Frame and the Promises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 507

 

15.2.1 How Coroutine Interfaces, Promises, and Awaitables Interact . . . . . . . . . . .

. . 508

Contents

 

 

xiii

15.3

Coroutine Promises in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

515

 

15.3.1

Mandatory Promise Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

515

 

15.3.2 Promise Operations to Return or Yield Values . . . . . . . . . . . . . . . . . . . . . .

518

 

15.3.3

Optional Promise Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

520

15.4

Coroutine Handles in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

521

 

15.4.1

std::coroutine_handle<void> . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

522

15.5

Exceptions in Coroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

523

15.6

Allocating Memory for the Coroutine Frame . . . . . . . . . . . . . . . . . . . . . . . . . . . .

525

 

15.6.1 How Coroutines Allocate Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

525

 

15.6.2 Avoiding Heap Memory Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

526

 

15.6.3 get_return_object_on_allocation_failure() . . . . . . . . . . . . . . . . .

530

15.7

co_await and Awaiters in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

531

 

15.7.1 Details of the Awaiter Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

531

 

15.7.2 Letting co_await Update Running Coroutines . . . . . . . . . . . . . . . . . . . . .

532

 

15.7.3 Symmetric Transfer with Awaiters for Continuation . . . . . . . . . . . . . . . . . . .

536

15.8

Other Ways of Dealing with co_await . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

538

 

15.8.1

await_transform() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

538

 

15.8.2

operator co_await() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

541

15.9

Concurrent Use of Coroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

542

 

15.9.1

co_await Coroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

542

 

15.9.2 A Thread Pool for Coroutine Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

546

 

15.9.3 What C++ Libraries Will Provide After C++20 . . . . . . . . . . . . . . . . . . . . .

555

15.10

Coroutine Traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

556

16 Modules

 

559

16.1

Motivation for Modules Using a First Example . . . . . . . . . . . . . . . . . . . . . . . . . . .

559

 

16.1.1 Implementing and Exporting a Module . . . . . . . . . . . . . . . . . . . . . . . . . .

559

 

16.1.2

Compiling Module Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

561

 

16.1.3 Importing and Using a Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

562

 

16.1.4

Reachable versus Visible . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

563

 

16.1.5

Modules and Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

564

16.2

Modules with Multiple Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

566

 

16.2.1

Module Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

566

 

16.2.2

Using Implementation Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

566

 

16.2.3

Internal Partitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

571

xiv

 

 

Contents

 

16.2.4

Interface Partitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 572

 

16.2.5 Summary of Splitting Modules into Different Files . . . . . . . . . . . . . . . . .

. . 575

16.3

Dealing with Modules in Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 576

 

16.3.1 Dealing with Module Files with Different Compilers . . . . . . . . . . . . . . . .

. . 576

 

16.3.2 Dealing with Header Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 578

16.4

Modules in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 580

 

16.4.1

Private Module Fragments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 580

 

16.4.2 Module Declaration and Export in Detail . . . . . . . . . . . . . . . . . . . . . . .

. . 582

 

16.4.3

Umbrella Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 583

 

16.4.4 Module Import in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 584

 

16.4.5 Reachable versus Visible Symbols in Detail . . . . . . . . . . . . . . . . . . . . .

. . 584

16.5

Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 587

17 Lambda Extensions

589

17.1

Generic Lambdas with Template Parameters . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 589

 

17.1.1 Using Template Parameters for Generic Lambdas in Practice . . . . . . . . . . .

. . 590

 

17.1.2 Explicit Specification of Lambda Template Parameters . . . . . . . . . . . . . . .

. . 591

17.2

Calling the Default Constructor of Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 592

17.3

Lambdas as Non-Type Template Parameters . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 594

17.4

consteval Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 595

17.5

Changes for Capturing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 596

 

17.5.1 Capturing this and *this . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 596

 

17.5.2

Capturing Structured Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 597

 

17.5.3 Capturing Parameter Packs of Variadic Templates . . . . . . . . . . . . . . . . . .

. . 597

 

17.5.4

Lambdas as Coroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 599

17.6

Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 600

18 Compile-Time Computing

601

18.1

Keyword constinit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 601

 

18.1.1 Using constinit in Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 603

 

18.1.2 How constinit Solves the Static Initialization Order Fiasco . . . . . . . . . . .

. . 604

18.2

Keyword consteval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 606

 

18.2.1 A First consteval Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 607

 

18.2.2

constexpr versus consteval . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 609

 

18.2.3 Using consteval in Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 612

Contents

 

 

xv

 

18.2.4 Compile-Time Value versus Compile-Time Context . . . . . . . . . . . . . . . . . . .

613

18.3

Relaxed Constraints for constexpr Functions . . . . . . . . . . . . . . . . . . . . . . . . . . .

614

18.4

std::is_constant_evaluated() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

614

 

18.4.1

std::is_constant_evaluated() in Detail . . . . . . . . . . . . . . . . . . . . . .

616

18.5

Using Heap Memory, Vectors, and Strings at Compile Time . . . . . . . . . . . . . . . . . . .

620

 

18.5.1 Using Vectors at Compile Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

620

 

18.5.2 Returning a Collection at Compile Time . . . . . . . . . . . . . . . . . . . . . . . . . .

622

 

18.5.3 Using Strings at Compile Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

625

18.6

Other constexpr Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

628

 

18.6.1

constexpr Language Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

628

 

18.6.2

constexpr Library Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

628

18.7

Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

629

19 Non-Type Template Parameter (NTTP) Extensions

631

19.1

New Types for Non-Type Template Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . .

631

 

19.1.1 Floating-Point Values as Non-Type Template Parameters . . . . . . . . . . . . . . . .

632

 

19.1.2 Objects as Non-Type Template Parameters . . . . . . . . . . . . . . . . . . . . . . . .

634

 

19.1.3 Lambdas as Non-Type Template Parameters . . . . . . . . . . . . . . . . . . . . . . .

638

19.2

Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

640

20 New Type Traits

641

20.1

New Type Traits for Type Classification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

642

 

20.1.1

is_bounded_array_v<> and is_unbounded_array_v . . . . . . . . . . . . . . .

642

20.2

New Type Traits for Type Inspection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

642

 

20.2.1 is_nothrow_convertible_v<> . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

642

20.3

New Type Traits for Type Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

643

 

20.3.1 remove_cvref_t<> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

643

 

20.3.2

unwrap_reference<> and unwrap_ref_decay_t . . . . . . . . . . . . . . . . . .

643

 

20.3.3 common_reference<>_t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

643

 

20.3.4 type_identity_t<> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

644

20.4

New Type Traits for Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

645

 

20.4.1 iter_difference_t<> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

645

 

20.4.2 iter_value_t<> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

645

 

20.4.3

iter_reference_t<> and iter_rvalue_reference_t<> . . . . . . . . . . . . .

646

xvi

 

 

Contents

20.5

Type Traits and Functions for Layout Compatibility . . . . . . . . . . . . . . . . . . . . . .

. . 647

 

20.5.1 is_layout_compatible_v<> . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 647

 

20.5.2 is_pointer_interconvertible_base_of_v<> . . . . . . . . . . . . . . . . .

. . 648

 

20.5.3 is_corresponding_member() . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 648

 

20.5.4 is_pointer_interconvertible_with_class() . . . . . . . . . . . . . . . .

. . 649

20.6

Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 649

21 Small Improvements for the Core Language

651

21.1

Range-Based for Loop with Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 651

21.2

using for Enumeration Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 652

21.3

Delegating Enumeration Types to Different Scopes . . . . . . . . . . . . . . . . . . . . . .

. . 654

21.4

New Character Type char8_t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 655

 

21.4.1 Changes in the C++ Standard Library for char8_t . . . . . . . . . . . . . . . . .

. . 657

 

21.4.2

Broken Backward Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 657

21.5

Improvements for Aggregates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 660

 

21.5.1

Designated Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 660

 

21.5.2 Aggregate Initialization with Parentheses . . . . . . . . . . . . . . . . . . . . . . .

. . 661

 

21.5.3

Definition of Aggregates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 665

21.6

New Attributes and Attribute Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 667

 

21.6.1 Attributes [[likely]] and [[unlikely]] . . . . . . . . . . . . . . . . . . . . .

. . 667

 

21.6.2

Attribute [[no_unique_address]] . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 668

 

21.6.3 Attribute [[nodiscard]] with Parameter . . . . . . . . . . . . . . . . . . . . . .

. . 670

21.7

Feature Test Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 671

21.8

Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 672

22 Small Improvements for Generic Programming

673

22.1

Implicit typename for Type Members of Template Parameters . . . . . . . . . . . . . . .

. . 673

 

22.1.1 Rules for Implicit typename in Detail . . . . . . . . . . . . . . . . . . . . . . . . .

. . 674

22.2

Improvements for Aggregates in Generic Code . . . . . . . . . . . . . . . . . . . . . . . . .

. . 676

 

22.2.1 Class Template Argument Deduction (CTAD) for Aggregates . . . . . . . . . . .

. . 676

22.3

Conditional explicit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 678

 

22.3.1 Conditional explicit in the Standard Library . . . . . . . . . . . . . . . . . . . .

. . 680

22.4

Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . 681

Contents

 

 

xvii

23 Small Improvements for the C++ Standard Library

683

23.1

Updates for String Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

683

 

23.1.1 String Members starts_with() and ends_with() . . . . . . . . . . . . . . . . . .

684

 

23.1.2 Restricted String Member reserve() . . . . . . . . . . . . . . . . . . . . . . . . . . .

684

23.2

std::source_location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

685

23.3

Safe Comparisons of Integral Values and Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . .

687

 

23.3.1 Safe Comparisons of Integral Values . . . . . . . . . . . . . . . . . . . . . . . . . . . .

687

 

23.3.2

ssize() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

688

23.4

Mathematical Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

688

23.5

Utilities for Dealing with Bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

689

 

23.5.1

Bit Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

690

 

23.5.2

std::bit_cast<>() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

692

 

23.5.3

std::endian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

692

23.6

<version> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

693

23.7

Extensions for Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

694

 

23.7.1

Range Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

694

 

23.7.2

New Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

694

 

23.7.3 unseq Execution Policy for Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . .

696

23.8

Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

698

24 Deprecated and Removed Features

701

24.1

Deprecated and Removed Core Language Features . . . . . . . . . . . . . . . . . . . . . . . .

701

24.2

Deprecated and Removed Library Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

701

 

24.2.1

Deprecated Library Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

701

 

24.2.2

Removed Library Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

701

24.3

Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

702

Glossary

 

 

703

Index

 

 

709