Functions and Classes
Explore how concepts enhance C++ generic programming by constraining template parameters in functions and classes. Understand the syntax for implicit and explicit concept usage, enforcing type requirements like Sortable and Copyable, applying multiple constraints, overloading template functions, and specializing class templates based on concepts.
We'll cover the following...
Concepts are part of the template declaration.
Functions
Using the concept Sortable.
Implicit
template<Sortable Cont>
void sort(Cont& container){
...
}
The container has to be Sortable.
The implicit version from the left is syntactic sugar to the explicit version:
Explicit
template<typename Cont>
requires Sortable<Cont>()
void sort(Cont& container){
...
}
Sortable has to be a constant expression that is a predicate. That means that the expression has to be evaluable at compile-time and has to return a boolean.
If you invoke the sort algorithm with a container lst that is not sortable, you will get a ...