This post presents you the big four: concepts, ranges, coroutines, and modules.
C++20 has a lot to offer. Before I give you a first impression of the big four, here is an overview of C++20. Besides the big four, many features affect the core language, the library, and the concurrency capabilities of C++20.
Compiler Support for C++20
Playing with the new features is the easiest way to get used to them. Okay. This approach arises immediately the question: Which C++20 features are supported by which compiler? As so often, cppreference.com/compiler_support gives you the answer to the core language and the library.
To make it simple, the brand-new GCC, Clang, and EDG compilers give the best support to the core language. Additionally, the MSVC and Apple Clang compiler support also many C++20 features.
The story is similar to the library. GCC has the best support for the library, followed by Clang and the MSVC compiler.
The screenshots show only the beginning of the tables, but they also give you an answer which is not so satisfying. Even if you use all brand-new compilers, many features are not supported by any compiler.
Often, you find workarounds to play with the new features. Here are two examples:
- Concepts: GCC supports a previous version of concepts.
- std::jthread: Nicolai Josuttis maintain a draft implementation on GitHub.
To make my story short. The situation is not so bad. With a bit of tinkering, many new features can be tried out. I will mention this little tinkering if necessary.
But now, let me give you a bird-eyes view of the new features. Of course, we should start with the big four.
The Big Four
The key idea of generic programming with templates is to define functions and classes which can be used with various types. Often it happens that you instantiate a template with the wrong type. The result is typically a few pages of cryptic error messages. This sad story ends with concepts. Concepts empower you to write requirements for your templates which the compiler can check. Concepts revolutionize the way we think about and write generic code. Here is why:
- Requirements for templates are part of the interface.
- The overloading of functions or specialization of class templates can be based on concepts.
- We get an improved error message because the compiler compares the requirements of the template parameter with the actual template arguments.
However, this is not the end of the story.
- You can use predefined concepts or define your own.
- The usage of auto and concepts is unified. Instead of auto, you can use a concept.
- If a function declaration uses a concept, it automatically becomes a function template. Writing function templates is, therefore, as easy as writing a function.
The following code snippet shows you the definition and the usage of the straightforward concept Integral:
Integral is the concept that requires from it type-parameter T that std::is_integral<T>::value holds. std::is_integral<T>::value is a function from the type-traits library which checks at compile-time if T is integral. If std::is_integral<T>::value evaluates to true, all is fine. If not, you get a compile-time error. Here are my posts to the type-traits library for the curious ones – and you should be curious.
The gcd algorithm determines the greatest common divisor based on the Euclidean algorithm. I used the so-called abbreviated function template syntax to define gcd. gcd requires, from its arguments and return type, that they support the concept Integral. gcd is a function template that puts requirements on its arguments and return value. When I remove the syntactic sugar, maybe you can see the fundamental nature of gcd.
Here is the semantically equivalent gcd algorithm.
If you don’t see the fundamental nature of gcd, you have to wait for my posts to concepts in a few weeks.
The ranges library is the first customer of concepts. It supports algorithms that
- can operate directly on the container; you don’t need iterators to specify a range
- can be evaluated lazily
- can be composed
To make it short: The ranges library supports functional patterns.
Okay, code may help more than words. The following functions show function composition with the pipe symbol.
even is a lambda function that returns if a i is even, and the lambda function square maps i to its square. The rest ist function composition, which you have to read from left to right: for (int i : ints | std::view::filter(even) | std::view::transform(square)). Apply the filter even on each element of ints and map each remaining element to its square. If you are familiar with functional programming, this reads like prose.
Coroutines are generalized functions that can be suspended and resumed while keeping their state. Coroutines are the usual way to write event-driven applications. An event-driven application can be simulations, games, servers, user interfaces, or even algorithms. Coroutines are also typically used for cooperative multitasking.
We don’t get with C++20 concrete coroutines; we will get a framework for writing our coroutines. The framework for writing coroutines consists of more than 20 functions that you partially have to implement and partially could overwrite. Therefore, you can tailor the coroutine to your needs.
Let me show you the usage of a special coroutine. The following program uses a generator for an infinite data stream.
Okay, I have to add a few words. This piece is only a code snippet. The function getNext is a coroutine because it uses the keyword co_yield. getNext has an infinite loop that returns the value after co_yield. A call to next() (lines 2 and 3) resumes the coroutine, and the following getValue call gets the value. After the getNext call, the coroutine pauses once more. It pauses until the following next() call. There is one big unknown in my example. This unknown is the return value Generator<int> of the getNext function. Here the complicated stuff starts, which will be part of detailed posts to coroutines.
Thanks to the Wandbox online compiler, I can show you the program’s output.
I make modules relatively short because the post is already too long.
- Faster compile times
- Isolation of macros
- Express the logical structure of the code
- Make header files superfluous
- Get rid of ugly macro workarounds
After the high-level overview of the big four, I will continue in my next post with the core language features, as shown in my image.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, moon, and Philipp Lenk.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
|My special thanks to SHAVEDYAKS|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,