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.
Reader Testimonials
Sandor Dargo
Senior Software Development Engineer at Amadeus
"'C++ 20: Get the details' is exactly the book you need right now if you want to immerse yourself in the latest version of C++. It's a complete guide, Rainer doesn't only discuss the flagship features of C++20, but also every minor addition to the language. Luckily, the book includes tons of example code, so even if you don't have direct access yet to the latest compilers, you will have a very good idea of what you can expect from the different features. A highly recommended read!"
Adrian Tam
Director of Data Science, Synechron Inc.
"C++ has evolved a lot from its birth. With C++20, it is like a new language now. Surely this book is not a primer to teach you inheritance or overloading, but if you need to bring your C++ knowledge up to date, this is the right book. You will be surprised about the new features C++20 brought into C++. This book gives you clear explanations with concise examples. Its organization allows you to use it as a reference later. It can help you unleash the old language into its powerful future."
Introduction
My book C++20 is both a tutorial and a reference. It teaches you C++20 and provides you with the details of this new thrilling C++ standard. The thrill factor is mainly due to the big four of C++20:
- Concepts change the way we think about and program with templates. They are semantic categories for template parameters. They enable you to express your intention directly in the type system. If something goes wrong, the compiler gives you a clear error message.
- 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 build times and an easier way to build packages.
- The new ranges library supports performing algorithms directly on the containers, composing algorithms with the pipe symbol, and applying algorithms lazily on infinite data streams.
- Thanks to coroutines, asynchronous programming in C++ becomes mainstream. Coroutines are the basis for cooperative tasks, event loops, infinite data streams, or pipelines.
Of course, this is not the end of the story. Here are more C++20 features:
- Auto-generated comparison operators
- Calendar and time-zone libraries
- Format library
- Views on contiguous memory blocks
- Improved, interruptible threads
- Atomic smart pointers
- Semaphores
- Coordination primitives such as latches and barriers
Conventions
Here are only a few conventions.
Special Fonts
Italic: I use Italic to emphasize a quote.
Bold: I use Bold to emphasize a name.
Monospace
: I use Monospace
for code, instructions, keywords, and names of types, variables, functions, and classes.
Special Boxes
Boxes contain background tips, warnings, and distilled information.
Tip Headline
This box provides additional information about the presented material and tips for compiling the programs.
Warning Headline
Warning boxes should help you to avoid pitfalls.
Distilled Information
This box summarizes at the end of each main section the important things to remember.
Source Code
The source code examplesstarting with the details partshown in the book are complete. That means, assuming you have a conforming compiler, you can compile and run them. I put the name of the source file in the title of each source code example. The source code uses four whitespaces for indentation. Only for layout reasons, I sometimes use two whitespaces.
Furthermore, Im not a fan of namespace directives such as using namespace std
because they make the code more difficult to read and pollute namespaces. Consequently, I use them only when it improves the codes readability (e.g.: using namespaces std::chrono_literals
or using namespace std::chrono
).
When necessary for layout reasons, I indent two characters instead of four, and I apply using directives such as using std::chrono::Monday
. Using directives allows it to use the names unqualified: constexpr auto monday = Monday
instead of constexpr auto monday = std::chrono::Monday
.
Compilation of the Programs
As the C++20 standard is brand-new, many examples can only be compiled and executed with a specific compiler. I use the newest GCC, Clang, and MSVC compilers. When you compile the program, you must specify the applied C++ standard. This means, with GCC or Clang you must provide the flag -std=c++20
, and with MSVC /std:c++latest
. When using concurrency features, unlike with MSVC, the GCC and Clang compilers require that you link the pthread library using -pthread
.
If you dont have an appropriate C++ compiler at your disposal, use an online compiler such as Wandbox or Compiler Explorer. When you use Compiler Explorer with GCC or Clang, you can also execute the program. First, you have to enable Run the compiled output
(1) and, second, open the Output
window (2).
Run code in the Compiler Explorer
You can get more details about the C++20 conformity of various C++ compilers at cppreference.com.
How should you read the Book?
If you are not familiar with C++20, start at the very beginning with to get the big picture.
Once you get the big picture, you can proceed with the chapter.
Personal Notes
Acknowledgments
I started a request for proofreading on my English blog: ModernesCpp Cpp and received more responses than I expected. Special thanks to all of you. Here are the names of the proofreaders in alphabetic order: Bob Bird, Nicola Bombace, Dave Burchill, Sandor Dargo, James Drobina, Frank Grimm, Kilian Henneberger, Ivan espkk Kondakov, Pter Kardos, Rakesh Mane, Jonathan OConnor, John Plaice, Iwan Smith, Paul Targosz, Steve Vinoski, and Greg Wagner.