C++20 - The Complete Guide

C++20 - The Complete Guide

C++20 - The Complete Guide
Автор: Josuttis Nicolai M.
Дата выхода: 2021
Издательство: Lean Publishing
Количество страниц: 474
Размер файла: 2.5 MB
Тип файла: PDF
Добавил: codelibs
 Проверить на вирусы

C++20 - The Complete Guide....1

Contents....3

Preface....15

An Experiment....15

Versions of this Book....15

Acknowledgments....17

About This Book....19

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

Overall Structure of the Book....19

How to Read This Book....20

The Way I Implement....20

Initializations....20

Error Terminology....20

Code Simplifications....20

The C++ Standards....21

Example Code and Additional Information....21

Feedback....21

1 Comparisons and Operator <=>....23

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

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

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

1.2 Defining and Using Comparisons....29

1.2.1 Using Operator<=>....29

1.2.2 Comparison Category Types....29

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

1.2.4 Calling operator<=>....31

1.2.5 Dealing with Multiple Ordering Criteria....32

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

1.3.1 Defaulted operator<=>....34

1.3.2 Defaulted operator<=> and Inheritance....36

1.4 Overload Resolution with Rewritten Expressions....37

1.5 Compatibility Issues with the Comparison Operators....39

1.6 Afternotes....40

2 Placeholder Types for Function Parameters....41

2.1 auto for Parameters of Ordinary Functions....41

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

2.1.2 auto Functions versus Lambdas....43

2.1.3 auto for Parameters in Detail....44

2.2 Other Placeholder Types for Parameters of Ordinary Functions....44

2.3 Afternotes....44

3 Concepts and Requirements....45

3.1 Motivating Example of Concepts and Requirements....45

3.1.1 Improving the Template Step-by-Step....45

3.1.2 The Whole Resulting Program....50

3.2 Typical Application of Concepts and Requirements in Practice....52

3.2.1 Requirements to Understand Code and Error Messages....52

3.2.2 Requirements to Disable Generic Code....54

3.2.3 Requirements to Use Different Statements....58

3.2.4 The Example as a Whole....61

3.2.5 Former Workarounds....63

3.3 Constraints and Requirements in Detail....66

3.3.1 Constraints....66

3.3.2 Ad hoc Boolean Expressions....66

3.3.3 requires Expressions....68

3.4 Concepts in Detail....73

3.4.1 Defining Concepts....74

3.4.2 Special Abilities of Concepts....74

3.4.3 Using Concepts as Type Constraints....75

3.5 Subsuming Constraints and Concepts....77

3.5.1 Indirect Subsumptions....79

3.6 Semantic Constraints....80

3.7 Design Guidelines for Concepts....82

3.7.1 Dealing with Multiple Requirements....82

3.7.2 Concepts versus Traits and Expressions....82

3.7.3 When to Use if constexpr....86

3.8 Other Stuff of Concepts....86

3.9 Afternotes....86

4 Standard Concepts in Detail....87

4.1 Overview of all Standard Concepts....87

4.1.1 Header Files and Namespaces....87

4.2 Language-Related Concepts....90

4.2.1 Arithmetic Concepts....90

4.2.2 Object Concepts....90

4.2.3 Concepts for Relations between Types....91

4.2.4 Comparison Concepts....93

4.3 Concepts for Iterators and Ranges....95

4.3.1 Concepts for Ranges and Views....95

4.3.2 Concepts for Pointers-Like Objects....98

4.3.3 Concepts for Iterators....100

4.3.4 Iterator Concepts for Algorithms....102

4.4 Concepts for Callables....103

4.4.1 Basic Concepts for Callables....103

4.4.2 Concepts for Callables Used by Iterators....106

4.5 Auxiliary Concepts....107

4.5.1 Concepts for Specific Type Attributes....107

4.5.2 Concepts for Incrementable Types....108

4.6 Afternotes....109

5 Ranges and Views....111

5.1 A Tour of Ranges by Example....111

5.1.1 Passing Containers to Algorithms as Ranges....111

5.1.2 Algorithms with Requirements....112

5.1.3 Views....114

5.1.4 Sentinels....117

5.1.5 Range Definitions with Sentinels and Counts....120

5.1.6 Projections....123

5.1.7 Utilities to Implement Code for Ranges....124

5.1.8 Limitations and Drawbacks of Ranges....125

5.2 Using Views....126

5.2.1 Views from Ranges....128

5.2.2 Pipelines for Temporary Ranges....129

5.2.3 Lazy Evaluation....130

5.2.4 Performance Issues with Filters....131

5.2.5 Views and Pipelines with Write Access....133

5.2.6 Write Access with Filter Views....134

5.3 Borrowed Iterators and Ranges....136

5.3.1 Borrowed Iterators....136

5.3.2 Borrowed Ranges....139

5.4 Ranges and const....140

5.4.1 Views Remove the Propagation of const....140

5.4.2 Bringing Back Deep Constness to Views....142

5.4.3 Generic Code Should Take Ranges with Non-const &&....143

5.5 Afternotes....145

6 Components for Ranges and View....147

6.1 Major Range Adaptors....147

6.1.1 Range Adaptor all()....147

6.1.2 Range Adaptor counted()....149

6.1.3 Range Adaptor common()....150

6.2 New Iterators....151

6.2.1 std::counted_iterator....151

6.2.2 std::common_iterator....152

6.2.3 std::default_sentinel....153

6.2.4 std::unreachable_sentinel....154

6.3 Helper Functions in std::ranges....154

6.4 Helper Types in std::ranges....156

6.5 Open....157

6.6 Afternotes....157

7 View Types in Detail....159

7.1 Overview of all Views....159

7.1.1 Overview of Features That Create Views....159

7.1.2 Overview of Modifying Views....160

7.2 Base Classes for Views....160

7.3 Creating Views to External Elements....162

7.3.1 std::ranges::subrange....162

7.3.2 std::ranges::ref_view....164

7.3.3 std::ranges::common_view....165

7.4 Generating Views....167

7.4.1 std::ranges::iota_view....167

7.4.2 std::ranges::single_view....169

7.4.3 std::ranges::empty_view....170

7.4.4 IStream View....172

7.4.5 String View....172

7.5 Filtering Views....174

7.5.1 Take View....174

7.5.2 Take-While View....174

7.5.3 Drop View....175

7.5.4 Drop-While View....176

7.5.5 Filter View....178

7.6 Transforming Views....180

7.6.1 Transform View....180

7.6.2 Elements View....180

7.6.3 Keys View....182

7.6.4 Values View....183

7.7 Mutating Views....184

7.7.1 std::ranges::reverse_view....184

7.8 Views for Multiple Ranges....186

7.8.1 Split and Lazy-Split View....186

7.8.2 Join View....189

7.9 Open....191

7.10 Afternotes....191

8 Spans....193

8.1 Using Spans....193

8.1.1 Fixed and Dynamic Extent....193

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

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

8.2 Spans Considered Harmful....202

8.3 Design Aspects of Spans....203

8.3.1 Performance of Spans....204

8.3.2 const Correctness of Spans....205

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

8.4 Span Operations....208

8.4.1 Span Operations and Member Types Overview....208

8.5 Afternotes....211

9 Non-Type Template Parameter (NTTP) Extensions....213

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

9.1.1 double Values as Non-Type Template Parameters....213

9.1.2 Objects as Non-Type Template Parameters....214

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

9.2 Details of Floating-Point Values as NTTP's....218

9.3 Details of Objects as NTTP's....219

9.4 Afternotes....219

10 Compile-Time Computing....221

10.1 Keyword constinit....221

10.1.1 Using constinit in Practice....222

10.1.2 How constinit Solves the Static Initialization Order Fiasco....223

10.2 Keyword consteval....226

10.2.1 A First consteval Example....226

10.2.2 constexpr versus consteval....229

10.2.3 Using consteval in Practice....231

10.2.4 Compile-Time Value versus Compile-Time Context....233

10.3 Relaxed Constraints for constexpr Functions....234

10.4 std::is_constant_evaluated()....234

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

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

10.5.1 Using Vectors at Compile Time....239

10.5.2 Returning a Collection at Compile Time....241

10.5.3 Using Strings at Compile Time....244

10.6 Other constexpr Extensions....247

10.6.1 constexpr Language Extensions....247

10.6.2 constexpr Library Extensions....247

10.7 Afternotes....248

11 Lambda Extensions....249

11.1 Generic Lambdas with Template Parameters....249

11.1.1 Using Template Parameters for Generic Lambdas in Practice....250

11.1.2 Explicit Specification of Lambda Template Parameters....251

11.2 Calling the Default Constructor of Lambdas....252

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

11.4 consteval Lambdas....255

11.5 Changes for Capturing....256

11.5.1 Capturing this and *this....256

11.5.2 Capturing Structured Bindings....257

11.5.3 Capturing Parameter Packs of Variadic Templates....257

11.6 Afternotes....259

12 Other C++ Language Improvements....261

12.1 New Character Type char8_t....261

12.1.1 Changes in the C++ Standard Library for char8_t....263

12.1.2 Broken Backward Compatibility....263

12.2 Designated Initializers....266

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

12.3.1 Rules for Implicit typename....268

12.4 Afternotes....269

13 Formatted Output....271

13.1 Formatted Output by Example....271

13.1.1 Using std::format()....271

13.1.2 Using std::format_to_n()....272

13.1.3 Using std::format_to()....273

13.1.4 Using std::formatted_size()....274

13.2 Formatted Output in Detail....274

13.2.1 General Format of Format Strings....274

13.2.2 Standard Format Specifiers....275

13.2.3 Width, Precision, and Fill Characters....275

13.2.4 Format/Type Specifiers....276

13.3 Error Handling....279

13.4 Internationalization....280

13.5 User-Defined Formatted Output....282

13.5.1 Basic Formatter API....282

13.5.2 Improved Parsing....285

13.5.3 Parsing with the Help of Standard Formatters....286

13.6 Afternotes....289

14 Dates and Time Zones for ....291

14.1 Overview by Example....291

14.1.1 Schedule a Meeting on the 5th of Every Month....291

14.1.2 Schedule a Meeting Every First Monday....296

14.2 Basic Chrono Concepts and Terminology....301

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

14.3.1 Duration Types....302

14.3.2 Clocks....303

14.3.3 Timepoint Types....304

14.3.4 Calendrical Types....304

14.3.5 Time Type hh_mm_ss....307

14.4 Time Zones....309

14.4.1 Characteristics of Time Zones....310

14.4.2 Using Time Zones....311

14.5 I/O with Chrono Types....315

14.5.1 Default Output Formats....315

14.5.2 Formatted Output....316

14.5.3 Locale Dependent Output....318

14.5.4 Formatted Input....321

14.6 Using the Chrono Extensions in Practice....326

14.6.1 Invalid Dates....326

14.6.2 Dealing with months and years....328

14.6.3 Parsing Time Points and Durations....329

14.6.4 Dealing with Time Zone Abbreviations....332

14.6.5 Custom Timezones....333

14.7 Clocks in Detail....335

14.7.1 Clocks with a Specified Epoch....335

14.7.2 The Pseudo Clock local_t....336

14.7.3 Dealing with Leap Seconds....337

14.7.4 Conversions between Clocks....338

14.7.5 Dealing with the File Clock....340

14.8 Other New Chrono Features....341

14.9 Afternotes....342

15 Coroutines....343

15.1 What Are Coroutines?....343

15.2 A First Coroutine Example....344

15.2.1 Defining a Coroutine....344

15.3 Further Coroutine Examples....352

15.3.1 Coroutine with co_yield....353

15.3.2 Coroutine with co_return....359

15.4 Coroutines in Detail....362

15.5 Afternotes....362

16 std::jthread and Stop Tokens....363

16.1 Motivation for std::jthread....363

16.1.1 The Problem of std::thread....363

16.1.2 Using std::jthread....365

16.1.3 Stop Tokens and Stop Callbacks....366

16.2 Stop Sources and Stop Tokens....367

16.2.1 Stop Sources and Stop Tokens in Detail....368

16.2.2 Using Stop Callbacks....369

16.2.3 Constraints and Guarantees of Stop Tokens....374

16.3 std::jthread In Detail....375

16.3.1 Using Stop Tokens with std::jthread....375

16.4 Afternotes....377

17 Concurrency Features....379

17.1 Thread Synchronization with Latches and Barriers....379

17.1.1 Latches....379

17.1.2 Barriers....383

17.2 Semaphores....387

17.2.1 Example of Using Counting Semaphores....387

17.2.2 Example of Using Binary Semaphores....391

17.3 Extensions for and New Atomic Types....394

17.4 Atomic References with std::atomic_ref<>....395

17.4.1 Atomic Shared Pointers....397

17.4.2 Atomic Floating-Point Types....401

17.4.3 Thread Synchronization with Atomic Types....401

17.4.4 Extensions for std::atomic_flag....405

17.5 Synchronized Output Streams....406

17.5.1 Motivation of Synchronized Output Streams....406

17.5.2 Using of Synchronized Output Streams....407

17.5.3 Using Synchronized Output Streams for Files....408

17.5.4 Using Synchronized Output Streams as Output Streams....409

17.5.5 Synchronized Output Streams in Detail....410

17.6 Afternotes....410

18 Other C++ Standard Library Improvements....413

18.1 Updates for String Types....413

18.2 String Members starts_with() and ends_with()....414

18.3 Restricted String Member reserve()....414

18.4 New Utility Functions....415

18.4.1 ssize()....415

18.5 std::source_location....415

18.6 New Type Traits....417

18.6.1 Type Traits is_bounded_array<> and is_unbounded_array....418

18.6.2 Type Trait is_nothrow_convertible<>....419

18.6.3 Type Trait is_layout_compatible<>....419

18.6.4 Type Trait is_layout_pointer_interconvertible_base_of<>....419

18.6.5 Type Trait remove_cvref<>....419

18.6.6 Type Traits unwrap_reference<> and unwrap_ref_decay....419

18.6.7 Type Trait common_reference<>....420

18.6.8 Type Trait type_identity<>....420

18.6.9 is_pointer_interconvertible_with_class<>() and is_corresponding_member<>()....421

18.7 Mathematical Constants....422

18.8 Utilities to Deal with Bits....423

18.8.1 Bit Operations....423

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

18.8.3 std::endian....426

18.9 ....427

18.10 Afternotes....428

19 Modules....431

19.1 Motivation of Modules by a First Example....431

19.1.1 Implementing and Exporting a Module....431

19.1.2 Importing and Using a Module....433

19.1.3 Reachable versus Visible....434

19.1.4 Modules and Namespaces....435

19.1.5 Modules and Filenames....436

19.2 Modules with Multiple Files....436

19.2.1 Module Units....437

19.2.2 A Module with Multiple Implementation Units....437

19.2.3 Internal Module Partitions....441

19.2.4 Interface Partitions....443

19.2.5 How Modules Replace Traditional Code....445

19.2.6 Module Declaration/Export in Detail....446

19.2.7 Module Import in Detail....446

19.2.8 Dealing with Module Files in Compilers....446

19.3 Exporting....447

19.4 Importing....447

19.5 Private Module Fragments....448

19.6 Dealing with Header Files....449

19.7 Status of Modules in Practice....450

19.8 Afternotes....450

Glossary....451

A....451

C....451

F....451

G....451

I....452

L....452

P....452

R....452

S....453

U....454

V....454

X....455

Index....457

A....457

B....458

C....458

D....462

E....462

F....463

G....464

H....464

I....464

J....465

K....466

L....466

M....466

N....467

O....467

P....467

R....468

S....469

T....471

U....472

V....473

W....474

X....474

Y....474

Z....474

C++20 is the next evolution in modern C++ programming, now supported by major C++ compilers. However, C++20 is huge. It will change the way we program more dramatically than C++11 did. Combining new features gives you even more power. However, not everything is self-explanatory and there are hidden traps. Therefore, it is key to understand all the concepts of C++20 and learn how to benefit best.

This book covers the motivation for and context of all new C++20 features (covering both language and library), providing many examples and unique background information for application programmers as well as for library developers. The focus is on how these features impact day-to-day programming, what effect combining features can have, and how you can benefit from them in practice.


Похожее:

Список отзывов:

Нет отзывов к книге.