Appendix B

Concepts

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 ...

Get C++ Core Guidelines Explained: Best Practices for Modern C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.