C++20: The Big Four
This post is a cross-post from www.ModernesCpp.com.
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 to the big four, the timeline gives you 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
The easiest way to get used to the new features is to play with 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 compiler 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 for the library. GCC has the best support for the library, followed by the 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, there are many features which are not supported by any compiler.
Often, you find workarounds to play with the new features. Here are two examples:
- Concepts: GCC support a previous version of concepts.
- std::jthread: There is a draft implementation on Github maintained by Nicolai Josuttis.
To make my story short. The situation is not so bad. With a little 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 it 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 can be checked by the compiler. Concepts revolutionise 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 specialisation of class templates can be based on concepts.
- We get 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 bool Integral(){ return 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 which 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. For the curious ones - and you should be curious- here are my posts to the type-traits library.
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 kind of function templates which puts requirements on its arguments and return value. When I remove the syntactic sugar, maybe you can see the real 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 real nature of gcd, you have to wait for my posts to concepts which will come in a few weeks.
Ranges Library
The ranges library is the first customer of concepts. It supports algorithms which
- 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 support 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 which 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 on each element of ints the filter even and map each remaining element to its square. If you are familiar with functional programming, this reads like prose.
Coroutines
Coroutines are generalised 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 which 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. Thi piece is only a code-snippet. The function getNext is a coroutine because it uses the keyword co_yield. getNext has an infinite loop which returns the value after co_yield. A call to next() (line 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 next 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 Wandbox online compiler, I can show you the output of the program.
Modules
For modules, I make it quite 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 an 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.
Building Products & Service towards better future
5 年These are some significant improvements for which we used to create custom libraries.
Author/trainer/mentor in computational finance: maths (pure, applied, numerical), ODE/PDE/FDM, C++11/C++20, Python, C#, modern software design
5 年When can we expect modules to be really shipping and not just on paper?