This week (2021) I'm at the C++Now conference. The conference is normally in Aspen, Colorado (I was there more than 10 years ago), but this year it's online, for obvious reasons.
Yesterday there were two excellent interesting talks from Jeff Garland about concepts in C++20, how it works, what you can do with it, how you can use them and how you can write them.
Some compilers (notably clang) don't yet support concepts, but you can already play with toy projects online using https://godbolt.org, where your code is compiled and run on the fly.
So here is my toy project, which I wrote during the talks (there were two consecutive talks):
So this code is using C++20 concepts. You can see it clearly at and around the keywords concept and requires. It is really cool, especially compared with what we had to do with C++03 (Boost.Geometry was written in C++03 and only a few releases ago went to C++14) to achieve this.
Some things could earlier be done with static_assert too (such as checking the number of dimensions), but now the compiler neatly warns that that is better written using a concept.
Other things needed SFINAE earlier (such as the enabling / disabling the functions for the Z coordinate).
If the last declarations in main are uncommented, you get a neat compiler error report (for instance for p2):
<source>: In function 'int main()':40 | mypoint<double, 4> p2; // Fails because 4 coordinates are not allowed by the concept| ^6 | template <int D> concept ValidDimension = D >= 2 and D <= 3;| ~~^~~~
Wow, it's only a few lines! And so clear! In the past we got hundreds of hard to interpret error messages about templates...
And if you uncomment the line streaming z for a 2d coordinate, you get:
<source>: In function 'int main()':43 | std::cout << two.z();| ^23 | auto z() const requires HasZ<D>| ^<source>: In instantiation of 'auto mypoint<C, D>::z() const requires HasZ<D> [with C = double; int D = 2]':7 | template <int D> concept HasZ = D >= 3;| ~~^~~~