C++20
C++20
About the Book
My book C++20 is both: a tutorial and reference to the C++20 standard. It teaches you C++20 and provides you with the details of this new thrilling C++ standard. The thrilling factor is mainly due to the big four of C++20.
- Concepts change the way we think and program templates. They are semantic categories for the template parameters. They enable you to express your intention directly in the type system. If something goes wrong, you get a clear error message.
- The new ranges library enables it to perform algorithms directly on the container, compose the algorithm with the pipe symbol, and apply them onto infinite data streams.
- Thanks to coroutines asynchronous programming in C++ becomes mainstream. Coroutines are the base for cooperative tasks, event loops, infinite data streams, or pipelines.
- Modules overcome the restrictions of header files. They promise a lot. For example, the separation of header and source files becomes as obsolete as the preprocessor. In the end, we have faster built time and an easier way to build packages.
Table of Contents
-
-
Introduction
-
Conventions
- Special Fonts
- Special Boxes
-
Source Code
- Compilation of the Programs
- How should you read the Book?
-
Personal Notes
- Acknowledgements
- About Me
-
Conventions
-
Introduction
-
A Quick Overview of C++20
-
1. C++20
-
1.1 The Big Four
- 1.1.1 Concepts
- 1.1.2 Modules
- 1.1.3 The Ranges Library
- 1.1.4 Coroutines
-
1.2 Core Language
- 1.2.1 Three-Way Comparison Operator
- 1.2.2 Designated Initialization
-
1.2.3
consteval
andconstinit
- 1.2.4 Template Improvements
- 1.2.5 Lambda Improvements
- 1.2.6 New Attributes
-
1.3 The Standard Library
-
1.3.1
std::span
- 1.3.2 Container Improvements
- 1.3.3 Arithmetic Utilities
- 1.3.4 Calendar and Time Zones
- 1.3.5 Formatting Library
-
1.3.1
-
1.4 Concurrency
- 1.4.1 Atomics
- 1.4.2 Semaphores
- 1.4.3 Latches and Barriers
- 1.4.4 Cooperative Interruption
-
1.4.5
std::jthread
- 1.4.6 Synchronized Outputstreams
-
1.1 The Big Four
-
1. C++20
-
The Details
-
2. Historical Context
- 2.1 C++98
- 2.2 C++03
- 2.3 TR1
- 2.4 C++11
- 2.5 C++14
- 2.6 C++17
-
3. Core Language
-
3.1 Concepts
- 3.1.1 Two Wrong Approaches
- 3.1.2 Advantages of Concepts
- 3.1.3 The long, long History
- 3.1.4 Usage of Concepts
- 3.1.5 Constrained and Unconstrained Placeholders
- 3.1.6 Abbreviated Function Templates
- 3.1.7 Predefined Concepts
- 3.1.8 Define Concepts
- 3.1.9 Application
-
3.2 Modules
- 3.2.1 Why do we need Modules?
- 3.2.2 Advantages
- 3.2.3 A First Example
- 3.2.4 Compilation and Use
- 3.2.5 Export
- 3.2.6 Guidelines for a Module Structure
- 3.2.7 Module Interface Unit and Module Implementation Unit
- 3.2.8 Submodules and Module Partitions
- 3.2.9 Templates in Modules
- 3.2.10 Module Linkage
- 3.2.11 Header Units
-
3.3 Three-Way Comparison Operator
- 3.3.1 Ordering before C++20
- 3.3.2 Ordering since C++20
- 3.3.3 The Compiler-Generated Spaceship Operator
- 3.3.4 Rewriting Expressions
- 3.3.5 User-Defined and Auto-Generated Comparison Operators
-
3.4 Designated Initialization
- 3.4.1 Aggregate Initialization
- 3.4.2 Named Initialization of Class Members
-
3.5
consteval
andconstinit
-
3.5.1
consteval
-
3.5.2
constinit
- 3.5.3 Function Execution
- 3.5.4 Variable Initialization
- 3.5.5 Solving the Static Initialization Order Fiasco
-
3.5.1
-
3.6 Template Improvements
- 3.6.1 Conditionally Explicit Constructor
- 3.6.2 Non-Type Template Parameters
-
3.7 Lambda Improvements
- 3.7.1 Template Parameter for Lambdas
-
3.7.2 Detection of the Implicit Copy of the
this
Pointer - 3.7.3 Lambdas in an Unevaluated Context and Stateless Lambdas can be Default-Constructed and Copy-Assigned
-
3.8 New Attributes
-
3.8.1
[[nodiscard("reason")]]
-
3.8.2
[[likely]]
and[[unlikely]]
-
3.8.3
[[no_unique_address]]
-
3.8.1
-
3.9 Further Improvements
-
3.9.1
volatile
- 3.9.2 Range-based for-loop with Initializers
-
3.9.3 Virtual
constexpr
function -
3.9.4 The new Character Type of UTF-8 Strings:
char8_t
-
3.9.5
using
enum
in Local Scopes - 3.9.6 Default Member Initializers for Bit Fields
-
3.9.1
-
3.1 Concepts
-
4. The Standard Library
-
4.1 The Ranges Library
- 4.1.1 The Concepts Ranges and Views
- 4.1.2 Direct on the Container
- 4.1.3 Function Composition
- 4.1.4 Lazy Evaluation
- 4.1.5 A Flavor of Python
-
4.2
std::span
- 4.2.1 Static versus Dynamic Extent
- 4.2.2 Automatically Deduces the Size of a Contiguous Sequence of Objects
-
4.2.3 Create a
std::span
from a Pointer and a Size - 4.2.4 Modifying the Referenced Objects
-
4.2.5 Addressing
std::span
Elements - 4.2.6 A Constant Range of Modifiable Elements
-
4.3 Container Improvements
-
4.3.1
constexpr
Containers and Algorithms -
4.3.2
std::array
- 4.3.3 Consistent Container Erasure
-
4.3.4
contains
for Associative Containers - 4.3.5 String prefix and suffix checking
-
4.3.1
-
4.4 Arithmetic Utilities
- 4.4.1 Safe Comparison of Integers
- 4.4.2 Mathematical Constants
- 4.4.3 Midpoint and Linear Interpolation
- 4.4.4 Bit Manipulation
-
4.5 Calendar and Time Zones
- 4.5.1 Time of day
- 4.5.2 Calendar Dates
- 4.5.3 Time Zones
-
4.6 Formatting Library
- 4.6.1 Format String
- 4.6.2 User-Defined Types
-
4.7 Further Improvements
-
4.7.1
std::bind_front
-
4.7.2
std::is_constant_evaluated
-
4.7.3
std::source_location
-
4.7.1
-
4.1 The Ranges Library
-
5. Concurrency
-
5.1 Coroutines
- 5.1.1 A Generator Function
- 5.1.2 Details
- 5.1.3 The Framework
-
5.2 Atomics
-
5.2.1
std::atomic_ref
- 5.2.2 Atomic Smart Pointer
-
5.2.3
std::atomic_flag
Extensions -
5.2.4
std::atomic
Extensions
-
5.2.1
- 5.3 Semaphores
-
5.4 Latches and Barriers
-
5.4.1
std::latch
-
5.4.2
std::barrier
-
5.4.1
-
5.5 Cooperative Interruption
-
5.5.1
std::stop_token
,std::stop_source
, andstd::stop_callback
-
5.5.1
-
5.6
std::jthread
- 5.6.1 Automatically Joining
-
5.6.2 Cooperative Interruption of a
std::jthread
- 5.7 Synchronized Outputstreams
-
5.1 Coroutines
-
6. Case Studies
- 6.1 Fast Synchronization of Threads
-
2. Historical Context
- Epilogue
-
Further Information
-
7. C++23 and Beyond
-
7.1 C++23
- 7.1.1 The Coroutines Library
- 7.1.2 Modularized Standard Library for Modules
- 7.1.3 Executors
- 7.1.4 The Network Library
-
7.2 C++23 or Later
- 7.2.1 Contracts
- 7.2.2 Reflection
- 7.2.3 Pattern Matching
- 7.3 Further Information to C++23
-
7.1 C++23
- 8. Feature Testing
- Index
-
7. C++23 and Beyond
Authors have earned$9,883,643writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.
Learn more about writing on Leanpub
The Leanpub 45-day 100% Happiness Guarantee
Within 45 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers), EPUB (for phones and tablets) and MOBI (for Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them
Top Books
C++ Best Practices
Jason TurnerLevel up your C++, get the tools working for you, eliminate common problems, and move on to more exciting things!
OpenIntro Statistics
David Diez, Christopher Barr, Mine Cetinkaya-Rundel, and OpenIntroA complete foundation for Statistics, also serving as a foundation for Data Science.
Leanpub revenue supports OpenIntro (US-based nonprofit) so we can provide free desk copies to teachers interested in using OpenIntro Statistics in the classroom and expand the project to support free textbooks in other subjects.
More resources: openintro.org.
Functional Design and Architecture
Alexander GraninSoftware Design in Functional Programming, Design Patterns and Practices, Methodologies and Application Architectures. How to build real software in Haskell with less efforts and low risks. The first complete source of knowledge.
R Programming for Data Science
Roger D. PengThis book brings the fundamentals of R programming to you, using the same material developed as part of the industry-leading Johns Hopkins Data Science Specialization. The skills taught in this book will lay the foundation for you to begin your journey learning data science. Printed copies of this book are available through Lulu.
C++20
Rainer GrimmC++20 is the next big C++ standard after C++11. As C++11 did it, C++20 changes the way we program modern C++. This change is, in particular, due to the big four of C++20: ranges, coroutines, concepts, and modules.
I am a Software Engineer and I am in Charge
Alexis Monville and Michael DoyleI am a Software Engineer and I am in Charge is a real-world, practical book that helps you increase your impact and satisfaction at work no matter who you work with.
In the book, we will follow Sandrine, a fictional character who learns to think in a new way enabling her to take a different course of action.
Atomic Kotlin
Bruce Eckel and Svetlana IsakovaFor both beginning and experienced programmers! From the author of the multi-award-winning Thinking in C++ and Thinking in Java together with a member of the Kotlin language team comes a book that breaks the concepts into small, easy-to-digest "atoms," along with exercises supported by hints and solutions directly inside IntelliJ IDEA!
Invest In Digital Health - The Medical Futurist's Guide
Dr. Bertalan MeskoArtificial Intelligence and Digital Health are booming. In this book, we explain why now it's a good time to invest in Digital Health and give recommendations on where to invest by looking at the top 24 technological trends we find the most promising.
The Hundred-Page Machine Learning Book
Andriy BurkovEverything you really need to know in Machine Learning in a hundred pages.
Mastering STM32
Carmine NovielloWith more than 600 microcontrollers, STM32 is probably the most complete ARM Cortex-M platform on the market. This book aims to be the first guide around that introduces the reader to this exciting MCU portfolio from ST Microelectronics and its official CubeHAL.
Top Bundles
- #1
Software Architecture for Developers: Volumes 1 & 2 - Technical leadership and communication
2 Books
"Software Architecture for Developers" is a practical and pragmatic guide to modern, lightweight software architecture, specifically aimed at developers. You'll learn:The essence of software architecture.Why the software architecture role should include coding, coaching and collaboration.The things that you really need to think about before... - #2
CCIE Service Provider Ultimate Study Bundle
2 Books
Piotr Jablonski, Lukasz Bromirski, and Nick Russo have joined forces to deliver the only CCIE Service Provider training resource you'll ever need. This bundle contains a detailed and challenging collection of workbook labs, plus an extensively detailed technical reference guide. All of us have earned the CCIE Service Provider certification... - #3
The Future of Digital Health
6 Books
We put together the most popular books from The Medical Futurist to provide a clear picture about the major trends shaping the future of medicine and healthcare. Digital health technologies, artificial intelligence, the future of 20 medical specialties, big pharma, data privacy and how technology giants such as Amazon or Google want to conquer... - #4
Cisco CCNA 200-301 Complet
4 Books
Ce lot comprend les quatre volumes du guide préparation à l'examen de certification Cisco CCNA 200-301. - #5
CCDE Practical Studies (All labs)
3 Books
CCDE lab - #6
"The C++ Standard Library" and "Concurrency with Modern C++"
2 Books
Get my books "The C++ Standard Library" and "Concurrency with Modern C++" in a bundle. The first book gives you the details you should know about the C++ standard library; the second one dives deeper into concurrency with modern C++. In sum, you get more than 600 pages full of modern C++ and about 250 source files presenting the standard library... - #7
Modern Management Made Easy
3 Books
Read all three Modern Management Made Easy books. Learn to manage yourself, lead and serve others, and lead the organization. - #8
Linux Administration Complet
4 Books
Ce lot comprend les quatre volumes du Guide Linux Administration :Linux Administration, Volume 1, Administration fondamentale : Guide pratique de préparation aux examens de certification LPIC 1, Linux Essentials, RHCSA et LFCS. Administration fondamentale. Introduction à Linux. Le Shell. Traitement du texte. Arborescence de fichiers. Sécurité... - #9
Programming with Ease
3 Books
Alle drei Bände der Serie Programming with Ease in einem Paket. Darin findest du alles, was ich dir zu den wichtigsten Phasen der Softwareentwicklung im Hinblick auf Clean Code Development für langfristig hohe Produktivität sagen kann.Im Band Slicing findest du die Anforderungsanalyse im Rahmen eines iterativ-inkrementellen Vorgehensmodells aus... - #10
Vagrant Ansible
2 Books
Unveil the power of Ansible and Vagrant with this bundle at a special price. You'll have everything you need to get started with Vagrant - learn the basics and how to create your virtual development environments, using Ansible as provisioner! About Vagrant Cookbook Vagrant Cookbook is a complete guide to get started with Vagrant and create your...