April 2022
Intermediate to advanced
528 pages
13h 34m
English
The C++20 feature “concepts” (also known as “named requirements”) allows you to express the template parameter requirements as part of the interface. Before I dive deeper, here is the first example:
template<typename Cont>
requires Sortable<Cont> // Sortable is a user-defined concept
void sort(Cont& container);
template<typename Cont>
void sort(Cont& container) requires Sortable<Cont>; // Trailing
// requires clause
template<Sortable Cont> // Constrained template parameters
void sort(Cont& container);
The first version of the generic function sort requires that its argument supports the concept Sortable. The second and the third variants of the function sort are semantically identical. The second ...
Read now
Unlock full access