Programming with C++20 teaches programmers with C++ experience the new features of C++20 and how to apply them. It does so by assuming C++11 knowledge. Elements of the standards between C++11 and C++20 will be briefly introduced, if necessary. However, the focus is on teaching the features of C++20.
You will start with learning about the so-called big four Concepts, Coroutines, std::ranges, and modules. The big four a followed by smaller yet not less important features. You will learn about std::format, the new way to format a string in C++. In chapter 6, you will learn about a new operator, the so-called spaceship operator, which makes you write less code.
You then will look at various improvements of the language, ensuring more consistency and reducing surprises. You will learn how lambdas improved in C++20 and what new elements you can now pass as non-type template parameters. Your next stop is the improvements to the STL.
Of course, you will not end this book without learning about what happened in the constexpr-world.
Author(s): Andreas Fertig
Edition: 1
Publisher: Fertig Publications
Year: 2021
Language: English
Pages: 333
Tags: C++; C++20; Concepts; Coroutines; Ranges; Modules; Lambdas; STL;
Using Code Examples
About the Author
About the Book
Table of Contents
1 Concepts: Predicates for strongly typed generic code
1.1 Programming before Concepts
1.2 Start using Concepts
1.3 Application areas for Concepts
1.4 The requires-expression: The runway for Concepts
1.5 Requirement kinds in a requires-expression
1.6 Ad hoc constraints
1.7 Defining a concept
1.8 Testing requirements
1.9 Abbreviated function template with auto as a generic parameter
1.10 Using a constexpr function in a concept
1.11 Concepts and constrained auto types
1.12 The power of Concepts: requires instead of enable_if
1.13 Concepts ordering
1.14 Improved error message
1.15 Existing Concepts
2 Coroutines: Suspending functions
2.1 Regular functions and their control flow
2.2 What are Coroutines
2.3 The elements of Coroutines in C++
2.4 Writing a byte-stream parser the old way
2.5 A byte-stream parser with Coroutines
2.6 A different strategy of the Parse generator
2.7 Using a coroutine with custom new / delete
2.8 Using a coroutine with a custom allocator
2.9 Exceptions in coroutines
3 Ranges: The next generation STL
3.1 Motivation
3.2 The who is who of ranges
3.3 A range
3.4 A range algorithm
3.5 A view into a range
3.6 A range adaptor
3.7 The new ranges namespaces
3.8 Ranges Concepts
3.9 Views
3.10 Creating a custom range
4 Modules: The superior way of includes
4.1 Background about the need for modules
4.2 Creating modules
4.3 Applying modules to an existing code base
5 std::format: Modern & type-safe text formatting
5.1 Formatting a string before C++20
5.2 Formatting a string using [columns=fullflexible,breaklines=true,prebreak=,postbreak=,keywordstyle=]std::format
5.3 Formatting a custom type
5.4 Referring to a format argument
5.5 Using a custom buffer
5.6 Writing our own logging function
6 Three-way comparisons: Simplify your comparisons
6.1 Writing a class with equal comparison
6.2 Writing a class with ordering comparison, pre C++20
6.3 Writing a class with ordering comparison in C++20
6.4 The different comparison categories
6.5 Converting between comparison categories
6.6 New operator abilities: reverse and rewrite
6.7 The power of the default spaceship
6.8 Applying a custom sort order
6.9 Spaceship-operation interaction with existing code
7 Lambdas in C++20: New features
7.1 [=, this] as a lambda capture
7.2 Default-constructible lambdas
7.3 Captureless lambdas in unevaluated contexts
7.4 Lambdas in generic code
7.5 Pack expansions in lambda init-captures
7.6 Restricting lambdas with Concepts
8 Aggregates: Designated initializers and more
8.1 What is an aggregate
8.2 Designated initializers
8.3 Direct-initialization for aggregates
8.4 Class Template Argument Deduction for aggregates
9 Class-types as non-type template parameters
9.1 What are non-type template parameters again
9.2 The requirements for class types as non-type template parameters
9.3 Class types as non-type template parameters
9.4 Building a format function with specifier count check
10 New STL elements
10.1 bit_cast: Reinterpreting your objects
10.2 endian: Endianness detection at compile time
10.3 to_array
10.4 span: A view of continuous memory
10.5 source_location: The modern way for __FUNCTION__
10.6 contains for all associative containers
10.7 starts_with and ends_with for std::string
11 Language Updates
11.1 Range-based for-loops with initializers
11.2 New Attributes
11.3 using enums
11.4 conditional explicit
12 Doing (more) things at compile-time
12.1 The two worlds: compile- vs. run-time
12.2 is_constant_evaluated: Is this a constexpr-context?
12.3 Less restrictive constexpr-function requirements
12.4 Utilizing the new compile-time world: Sketching a car racing game
12.5 consteval: Do things guaranteed at compile-time
12.6 constinit: Initialize a non-const object at compile-time
Acronyms
Bibliography
Index