Templates and generic programming
T.11
Whenever possible use standard concepts
Reason
"Standard" concepts (as provided by the GSL and the ISO standard itself) save us the work of thinking up our own concepts, are better thought out than we can manage to do in a hurry, and improve interoperability.
Note
Unless you are creating a new generic library, most of the concepts you need will already be defined by the standard library.
Example
template<typename T>
// don't define this: sortable is in <iterator>
concept Ordered_container = Sequence<T> && Random_access<Iterator<T>> && Ordered<Value_type<T>>;
void sort(Ordered_container auto& s);
This Ordered_container is quite plausible, but it is very similar to the sortable concept in the standard library. Is it better? Is it right? Does it accurately reflect the standard's requirements for sort? It is better and simpler just to use sortable:
void sort(sortable auto& s); // better
Note
The set of "standard" concepts is evolving as we approach an ISO standard including concepts.
Note
Designing a useful concept is challenging.
Enforcement
Hard.
- Look for unconstrained arguments, templates that use "unusual"/non-standard concepts, templates that use "homebrew" concepts without axioms.
- Develop a concept-discovery tool (e.g., see an early experiment).