C++20 is the next evolution in modern C++ programming, which is already (partially) supported by the
latest version of gcc, clang, and Visual C++. The move to C++20 is at least as big a step as the move to
C++11. C++20 contains a significant number of new language features and libraries that again will change
the way we program in C++. This applies to both application programmers and programmers who provide
foundation libraries.
Author(s): Nicolai M. Josuttis
Edition: 1
Publisher: Leanpub
Year: 2021
Language: English
Commentary: This version was published on 2021-12-31.
Pages: 474
Tags: C++; C++20
C++20 - The Complete Guide
Contents
Preface
An Experiment
Versions of this Book
Acknowledgments
About This Book
What You Should Know Before Reading This Book
Overall Structure of the Book
How to Read This Book
The Way I Implement
Initializations
Error Terminology
Code Simplifications
The C++ Standards
Example Code and Additional Information
Feedback
1 Comparisons and Operator <=>
1.1 Motivation for Operator<=>
1.1.1 Defining Comparison Operators Before C++20
1.1.2 Defining Comparison Operators Since C++20
1.2 Defining and Using Comparisons
1.2.1 Using Operator<=>
1.2.2 Comparison Category Types
1.2.3 Using Comparison Categories with operator<=>
1.2.4 Calling operator<=>
1.2.5 Dealing with Multiple Ordering Criteria
1.3 Defining operator<=> and operator==
1.3.1 Defaulted operator<=>
1.3.2 Defaulted operator<=> and Inheritance
1.4 Overload Resolution with Rewritten Expressions
1.5 Compatibility Issues with the Comparison Operators
1.6 Afternotes
2 Placeholder Types for Function Parameters
2.1 auto for Parameters of Ordinary Functions
2.1.1 auto for Parameters of Member Functions
2.1.2 auto Functions versus Lambdas
2.1.3 auto for Parameters in Detail
2.2 Other Placeholder Types for Parameters of Ordinary Functions
2.3 Afternotes
3 Concepts and Requirements
3.1 Motivating Example of Concepts and Requirements
3.1.1 Improving the Template Step-by-Step
3.1.2 The Whole Resulting Program
3.2 Typical Application of Concepts and Requirements in Practice
3.2.1 Requirements to Understand Code and Error Messages
3.2.2 Requirements to Disable Generic Code
3.2.3 Requirements to Use Different Statements
3.2.4 The Example as a Whole
3.2.5 Former Workarounds
3.3 Constraints and Requirements in Detail
3.3.1 Constraints
3.3.2 Ad hoc Boolean Expressions
3.3.3 requires Expressions
3.4 Concepts in Detail
3.4.1 Defining Concepts
3.4.2 Special Abilities of Concepts
3.4.3 Using Concepts as Type Constraints
3.5 Subsuming Constraints and Concepts
3.5.1 Indirect Subsumptions
3.6 Semantic Constraints
3.7 Design Guidelines for Concepts
3.7.1 Dealing with Multiple Requirements
3.7.2 Concepts versus Traits and Expressions
3.7.3 When to Use if constexpr
3.8 Other Stuff of Concepts
3.9 Afternotes
4 Standard Concepts in Detail
4.1 Overview of all Standard Concepts
4.1.1 Header Files and Namespaces
4.2 Language-Related Concepts
4.2.1 Arithmetic Concepts
4.2.2 Object Concepts
4.2.3 Concepts for Relations between Types
4.2.4 Comparison Concepts
4.3 Concepts for Iterators and Ranges
4.3.1 Concepts for Ranges and Views
4.3.2 Concepts for Pointers-Like Objects
4.3.3 Concepts for Iterators
4.3.4 Iterator Concepts for Algorithms
4.4 Concepts for Callables
4.4.1 Basic Concepts for Callables
4.4.2 Concepts for Callables Used by Iterators
4.5 Auxiliary Concepts
4.5.1 Concepts for Specific Type Attributes
4.5.2 Concepts for Incrementable Types
4.6 Afternotes
5 Ranges and Views
5.1 A Tour of Ranges by Example
5.1.1 Passing Containers to Algorithms as Ranges
5.1.2 Algorithms with Requirements
5.1.3 Views
5.1.4 Sentinels
5.1.5 Range Definitions with Sentinels and Counts
5.1.6 Projections
5.1.7 Utilities to Implement Code for Ranges
5.1.8 Limitations and Drawbacks of Ranges
5.2 Using Views
5.2.1 Views from Ranges
5.2.2 Pipelines for Temporary Ranges
5.2.3 Lazy Evaluation
5.2.4 Performance Issues with Filters
5.2.5 Views and Pipelines with Write Access
5.2.6 Write Access with Filter Views
5.3 Borrowed Iterators and Ranges
5.3.1 Borrowed Iterators
5.3.2 Borrowed Ranges
5.4 Ranges and const
5.4.1 Views Remove the Propagation of const
5.4.2 Bringing Back Deep Constness to Views
5.4.3 Generic Code Should Take Ranges with Non-const &&
5.5 Afternotes
6 Components for Ranges and View
6.1 Major Range Adaptors
6.1.1 Range Adaptor all()
6.1.2 Range Adaptor counted()
6.1.3 Range Adaptor common()
6.2 New Iterators
6.2.1 std::counted_iterator
6.2.2 std::common_iterator
6.2.3 std::default_sentinel
6.2.4 std::unreachable_sentinel
6.3 Helper Functions in std::ranges
6.4 Helper Types in std::ranges
6.5 Open
6.6 Afternotes
7 View Types in Detail
7.1 Overview of all Views
7.1.1 Overview of Features That Create Views
7.1.2 Overview of Modifying Views
7.2 Base Classes for Views
7.3 Creating Views to External Elements
7.3.1 std::ranges::subrange
7.3.2 std::ranges::ref_view
7.3.3 std::ranges::common_view
7.4 Generating Views
7.4.1 std::ranges::iota_view
7.4.2 std::ranges::single_view
7.4.3 std::ranges::empty_view
7.4.4 IStream View
7.4.5 String View
7.5 Filtering Views
7.5.1 Take View
7.5.2 Take-While View
7.5.3 Drop View
7.5.4 Drop-While View
7.5.5 Filter View
7.6 Transforming Views
7.6.1 Transform View
7.6.2 Elements View
7.6.3 Keys View
7.6.4 Values View
7.7 Mutating Views
7.7.1 std::ranges::reverse_view
7.8 Views for Multiple Ranges
7.8.1 Split and Lazy-Split View
7.8.2 Join View
7.9 Open
7.10 Afternotes
8 Spans
8.1 Using Spans
8.1.1 Fixed and Dynamic Extent
8.1.2 Example Using a Span with Fixed Extent
8.1.3 Example Using a Span with a Dynamic Extent
8.2 Spans Considered Harmful
8.3 Design Aspects of Spans
8.3.1 Performance of Spans
8.3.2 const Correctness of Spans
8.3.3 Using Spans as Parameters in Generic Code
8.4 Span Operations
8.4.1 Span Operations and Member Types Overview
8.5 Afternotes
9 Non-Type Template Parameter (NTTP) Extensions
9.1 New Types for Non-Type Template Parameters
9.1.1 double Values as Non-Type Template Parameters
9.1.2 Objects as Non-Type Template Parameters
9.1.3 Lambdas as Non-Type Template Parameters
9.2 Details of Floating-Point Values as NTTP's
9.3 Details of Objects as NTTP's
9.4 Afternotes
10 Compile-Time Computing
10.1 Keyword constinit
10.1.1 Using constinit in Practice
10.1.2 How constinit Solves the Static Initialization Order Fiasco
10.2 Keyword consteval
10.2.1 A First consteval Example
10.2.2 constexpr versus consteval
10.2.3 Using consteval in Practice
10.2.4 Compile-Time Value versus Compile-Time Context
10.3 Relaxed Constraints for constexpr Functions
10.4 std::is_constant_evaluated()
10.4.1 std::is_constant_evaluated() in Detail
10.5 Using Heap Memory, Vectors, and Strings at Compile Time
10.5.1 Using Vectors at Compile Time
10.5.2 Returning a Collection at Compile Time
10.5.3 Using Strings at Compile Time
10.6 Other constexpr Extensions
10.6.1 constexpr Language Extensions
10.6.2 constexpr Library Extensions
10.7 Afternotes
11 Lambda Extensions
11.1 Generic Lambdas with Template Parameters
11.1.1 Using Template Parameters for Generic Lambdas in Practice
11.1.2 Explicit Specification of Lambda Template Parameters
11.2 Calling the Default Constructor of Lambdas
11.3 Lambdas as Non-Type Template Parameters
11.4 consteval Lambdas
11.5 Changes for Capturing
11.5.1 Capturing this and *this
11.5.2 Capturing Structured Bindings
11.5.3 Capturing Parameter Packs of Variadic Templates
11.6 Afternotes
12 Other C++ Language Improvements
12.1 New Character Type char8_t
12.1.1 Changes in the C++ Standard Library for char8_t
12.1.2 Broken Backward Compatibility
12.2 Designated Initializers
12.3 Implicit typename for Type Members of Template Parameters
12.3.1 Rules for Implicit typename
12.4 Afternotes
13 Formatted Output
13.1 Formatted Output by Example
13.1.1 Using std::format()
13.1.2 Using std::format_to_n()
13.1.3 Using std::format_to()
13.1.4 Using std::formatted_size()
13.2 Formatted Output in Detail
13.2.1 General Format of Format Strings
13.2.2 Standard Format Specifiers
13.2.3 Width, Precision, and Fill Characters
13.2.4 Format/Type Specifiers
13.3 Error Handling
13.4 Internationalization
13.5 User-Defined Formatted Output
13.5.1 Basic Formatter API
13.5.2 Improved Parsing
13.5.3 Parsing with the Help of Standard Formatters
13.6 Afternotes
14 Dates and Time Zones for
14.1 Overview by Example
14.1.1 Schedule a Meeting on the 5th of Every Month
14.1.2 Schedule a Meeting Every First Monday
14.2 Basic Chrono Concepts and Terminology
14.3 Basic Chrono Extensions with C++20
14.3.1 Duration Types
14.3.2 Clocks
14.3.3 Timepoint Types
14.3.4 Calendrical Types
14.3.5 Time Type hh_mm_ss
14.4 Time Zones
14.4.1 Characteristics of Time Zones
14.4.2 Using Time Zones
14.5 I/O with Chrono Types
14.5.1 Default Output Formats
14.5.2 Formatted Output
14.5.3 Locale Dependent Output
14.5.4 Formatted Input
14.6 Using the Chrono Extensions in Practice
14.6.1 Invalid Dates
14.6.2 Dealing with months and years
14.6.3 Parsing Time Points and Durations
14.6.4 Dealing with Time Zone Abbreviations
14.6.5 Custom Timezones
14.7 Clocks in Detail
14.7.1 Clocks with a Specified Epoch
14.7.2 The Pseudo Clock local_t
14.7.3 Dealing with Leap Seconds
14.7.4 Conversions between Clocks
14.7.5 Dealing with the File Clock
14.8 Other New Chrono Features
14.9 Afternotes
15 Coroutines
15.1 What Are Coroutines?
15.2 A First Coroutine Example
15.2.1 Defining a Coroutine
15.3 Further Coroutine Examples
15.3.1 Coroutine with co_yield
15.3.2 Coroutine with co_return
15.4 Coroutines in Detail
15.5 Afternotes
16 std::jthread and Stop Tokens
16.1 Motivation for std::jthread
16.1.1 The Problem of std::thread
16.1.2 Using std::jthread
16.1.3 Stop Tokens and Stop Callbacks
16.2 Stop Sources and Stop Tokens
16.2.1 Stop Sources and Stop Tokens in Detail
16.2.2 Using Stop Callbacks
16.2.3 Constraints and Guarantees of Stop Tokens
16.3 std::jthread In Detail
16.3.1 Using Stop Tokens with std::jthread
16.4 Afternotes
17 Concurrency Features
17.1 Thread Synchronization with Latches and Barriers
17.1.1 Latches
17.1.2 Barriers
17.2 Semaphores
17.2.1 Example of Using Counting Semaphores
17.2.2 Example of Using Binary Semaphores
17.3 Extensions for and New Atomic Types
17.4 Atomic References with std::atomic_ref<>
17.4.1 Atomic Shared Pointers
17.4.2 Atomic Floating-Point Types
17.4.3 Thread Synchronization with Atomic Types
17.4.4 Extensions for std::atomic_flag
17.5 Synchronized Output Streams
17.5.1 Motivation of Synchronized Output Streams
17.5.2 Using of Synchronized Output Streams
17.5.3 Using Synchronized Output Streams for Files
17.5.4 Using Synchronized Output Streams as Output Streams
17.5.5 Synchronized Output Streams in Detail
17.6 Afternotes
18 Other C++ Standard Library Improvements
18.1 Updates for String Types
18.2 String Members starts_with() and ends_with()
18.3 Restricted String Member reserve()
18.4 New Utility Functions
18.4.1 ssize()
18.5 std::source_location
18.6 New Type Traits
18.6.1 Type Traits is_bounded_array<> and is_unbounded_array
18.6.2 Type Trait is_nothrow_convertible<>
18.6.3 Type Trait is_layout_compatible<>
18.6.4 Type Trait is_layout_pointer_interconvertible_base_of<>
18.6.5 Type Trait remove_cvref<>
18.6.6 Type Traits unwrap_reference<> and unwrap_ref_decay
18.6.7 Type Trait common_reference<>
18.6.8 Type Trait type_identity<>
18.6.9 is_pointer_interconvertible_with_class<>() and is_corresponding_member<>()
18.7 Mathematical Constants
18.8 Utilities to Deal with Bits
18.8.1 Bit Operations
18.8.2 std::bit_cast<>()
18.8.3 std::endian
18.9
18.10 Afternotes
19 Modules
19.1 Motivation of Modules by a First Example
19.1.1 Implementing and Exporting a Module
19.1.2 Importing and Using a Module
19.1.3 Reachable versus Visible
19.1.4 Modules and Namespaces
19.1.5 Modules and Filenames
19.2 Modules with Multiple Files
19.2.1 Module Units
19.2.2 A Module with Multiple Implementation Units
19.2.3 Internal Module Partitions
19.2.4 Interface Partitions
19.2.5 How Modules Replace Traditional Code
19.2.6 Module Declaration/Export in Detail
19.2.7 Module Import in Detail
19.2.8 Dealing with Module Files in Compilers
19.3 Exporting
19.4 Importing
19.5 Private Module Fragments
19.6 Dealing with Header Files
19.7 Status of Modules in Practice
19.8 Afternotes
Glossary
A
C
F
G
I
L
P
R
S
U
V
X
Index
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
R
S
T
U
V
W
X
Y
Z