C++20: The Big Four
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.
Modernes C++ Mentoring
Do you want to stay informed: Subscribe.
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
Concepts
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:
template<typename T> concept Integral = std::is_integral<T>::value; Integral auto gcd(Integral auto a, Integral auto b){ if( b == 0 ) return a; else return gcd(b, a % b); }
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.
template<typename T> requires Integral<T>() T gcd(T a, T b){ if( b == 0 ) return a; else return gcd(b, a % b); }
If you don’t see the fundamental nature of gcd, you have to wait for my posts to concepts in a few weeks.
Ranges Library
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.
#include <vector> #include <ranges> #include <iostream> int main(){ std::vector<int> ints{0, 1, 2, 3, 4, 5}; auto even = [](int i){ return 0 == i % 2; }; auto square = [](int i) { return i * i; }; for (int i : ints | std::view::filter(even) | std::view::transform(square)) { std::cout << i << ' '; // 0 4 16 } }
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
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.
Generator<int> getNext(int start = 0, int step = 1){ auto value = start; for (int i = 0;; ++i){ co_yield value; // 1 value += step; } } int main() { std::cout << std::endl; std::cout << "getNext():"; auto gen = getNext(); for (int i = 0; i <= 10; ++i) { gen.next(); // 2 std::cout << " " << gen.getValue(); } std::cout << "\n\n"; std::cout << "getNext(100, -10):"; auto gen2 = getNext(100, -10); for (int i = 0; i <= 20; ++i) { gen2.next(); // 3 std::cout << " " << gen2.getValue(); } std::cout << std::endl; }
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.
Modules
I make modules relatively short because the post is already too long.
Modules promise:
- Faster compile times
- Isolation of macros
- Express the logical structure of the code
- Make header files superfluous
- Get rid of ugly macro workarounds
What’s next?
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, 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, Stephen Kelley, Kyle Dean, Tusar Palauri, 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, Philipp Lenk, Charles-Jianye Chen, Keith Jeffery,and Matt Godbolt.
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 |
Seminars
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
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++
- C++20
Online Seminars (German)
- Embedded
Programmierung mit modernem C++ (24. Sep. 2024 bis 26.
Sep. 2024)
Contact Me
- Mobil: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,
Leave a Reply
Want to join the discussion?Feel free to contribute!