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: Meeting C++, Matt Braun, Roman Postanciuc, Venkata Ramesh Gudpati, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Richard Ohnemus, Frank Grimm, Sakib, Broeserl, António Pina, Markus Falkner, Darshan Mody, Sergey Agafyin, Андрей Бурмистров, Jake, GS, and Lawton Shoemake.
|Thanks in particular to:
Get your e-book at Leanpub:
The C++ Standard Library
Concurrency With Modern C++
Get Both as one Bundle
|With C++11, C++14, and C++17 we got a lot of new C++ libraries. In addition, the existing ones are greatly improved. The key idea of my book is to give you the necessary information to the current C++ libraries in about 200 pages. I also included more than 120 source files.
C++11 is the first C++ standard that deals with concurrency. The story goes on with C++17 and will continue with C++20.
I'll give you a detailed insight in the current and the upcoming concurrency in C++. This insight includes the theory and a lot of practice with more than 140 source files.
Get my books "The C++ Standard Library" (including C++17) and "Concurrency with Modern C++" in a bundle.
In sum, you get more than 700 pages full of modern C++ and more than 260 source files presenting concurrency in practice.
Get your interactive course
Modern C++ Concurrency in Practice
C++ Standard Library including C++14 & C++17
Based on my book "Concurrency with Modern C++" educative.io created an interactive course.
- 140 lessons
- 110 code playgrounds => Runs in the browser
- 78 code snippets
- 55 illustrations
Based on my book "The C++ Standard Library" educative.io created an interactive course.
- 149 lessons
- 111 code playgrounds => Runs in the browser
- 164 code snippets
- 25 illustrations