C++20 has many predefined concepts. Consequentially, before you define your concept, you should know the existing ones. This post gives you an overview of the predefined concepts. To get the general idea of concepts, read my previous posts.
Don't reinvent the wheel. This golden rule also applies to concepts. By the way. The C++ Core Guidelines is very clear about this rule: T.11: Whenever possible use standard concepts.
My information is from the newest C++20 draft: N4842. To find all predefined concepts is quite a job. Most of the concepts are in chapter 18 (concepts library) and chapter 24 (ranges library). Additionally, a few concepts are in chapter 17 (language support library), chapter 20 (general utilities library), chapter 23 (iterators library), and chapter 26 (numerics library). The document also shows how the concepts are implemented.
Honestly, I was astonished that I didn't find any concept to concurrency such as Lockable. Originally, the concepts were written in CamelCase or wiki syntax. Now, they are written with underscores. E.g.: DerivedFrom became derived_from.
Here are the concepts according to their ordering in the C++20 draft. I ignore in this presentation the special or auxiliary concepts. I also ignore the concepts from the ranges library. To understand them, you first have to understand the new ideas of the ranges library. I write about them when I write about the ranges library. The ranges library will follow the concepts immediately.
I have to warn you: if you don't like technical posts, you probably don't like this post.
Language support library
This section has the one interesting concept three_way_comparable to support the three-way comparison operator.
If you want it more formal. Let a and b values of type T. They are essential three_way_comparable only if:
- (a <=> b == 0) == bool(a == b) is true,
- (a <=> b != 0) == bool(a != b) is true,
- ((a <=> b) <=> 0) and (0 <=> (b <=> a)) are equal,
- (a <=> b < 0) == bool(a < b) is true,
- (a <=> b > 0) == bool(a > b) is true,
- (a <=> b <= 0) == bool(a <= b) is true,
- (a <=> b >= 0) == bool(a >= b) is true, and
I assume, these are the concepts we mainly use.
This section has around 15 concepts which should be self-explanatory. These concepts express relationships between types, type classifications, and fundamental type properties. Their implementation is often directly based on the corresponding function from the type-traits library. I rearranged them and add a few words if necessary.
- common_reference_with: common_reference_with<T, U> must be well-formed and T and U must be convertible to a reference type C
- common_with: similar to common_reference_with but the common type C has not to be a reference type
Their definition is straight forward:
concept integral = is_integral_v<T>;
concept signed_integral = integral<T> && is_signed_v<T>;
concept unsigned_integral = integral<T> && !signed_integral<T>;
concept floating_point = is_floating_point_v<T>;
- boolean: specifies if a type T is usable as a truth value; interestingly, pointers, smart pointers, types with an explicit conversion operator to bool are not boolean types.
Maybe you know it from your math. For values a, b, and c of type T, T models totally_ordered only if
- Exactly one of bool(a < b), bool(a > b), or bool(a == b) is true.
- If bool(a < b) and bool(b < c), then bool(a < c).
- bool(a > b) == bool(b < a).
- bool(a <= b) == !bool(b < a).
- bool(a >= b) == !bool(a < b).
Here is the definition of the four concepts:
concept movable = is_object_v<T> && move_constructible<T> && assignable_from<T&, T> && swappable<T>;
concept copyable = copy_constructible<T> && movable<T> && assignable_from<T&, const T&>;
concept semiregular = copyable<T> && default_constructible<T>;
concept regular = semiregular<T> && equality_comparable<T>;
I have to add a few words. The concept movable requires for T that is_object_v<T> holds. This means under the hood, that T is either a scalar, an array, a union, or a class.
I implement the concept of semiregular and regular in future posts. Informally, a semiregular type behaves similar to an int, and a regular type behaves similar to an int and is comparable with ==.
- regular_invocable: F models invocable and equality-preserving, and does not modify the function arguments; equality-preserving means the F produces the same output when giving the same input
- predicate: F models a predicate if F models invocable and returns a boolean
General utilities library
This chapter has only special memory concepts; therefore, I skip it.
The iterators library has many important concepts. Here are the iterator categories:
The six categories of iterators correspond to the iterator concepts. The image shows the three most prominent iterator categories and the containers of the Standard Template Library, respectively.
The following relation holds. A random access iterator is a bidirectional iterator, and a bidirectional iterator is a forward iterator. A contiguous iterator is a random access iterator and requires that the elements of the container are stored contiguously in memory. This means std::array, std::vector, and std::string support contiguous iterator, but not std::deque.
- permutable: reordering of elements in place is possible
- mergeable: merging sorted sequences into an output sequence is possible
- sortable: permuting a sequence into an ordered sequence is possible
The numeric library has the concept of a uniform_random_bit_generator. A uniform_random_bit_generator g of type G has to return unsigned integers so that each value is equally probable. Additionally, the uniform random bit generator g of type G has to support the member functions G::min, and G::max.
With the next posts, it becomes more practical. I write about the definition of concepts such as integral, regular, and semiregular. Defining concepts is more than just putting constraints on type parameters.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Wolfgang Gärtner, Louis St-Amour, Stephan Roslen, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Avi Kohn, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Tobi Heideman, Daniel Hufschläger, Red Trip, Alexander Schwarz, and Tornike Porchxidze.
Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, and Richard Sargeant.
My special thanks to Embarcadero
I'm happy to give online-seminars or face-to-face seminars world-wide. Please call me if you have any questions.
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- Tel.: +49 7472 917441
- Mobil: +49 152 31965939
- German Seminar Page: www.ModernesCpp.de
- English Seminar Page: www.ModernesCpp.net