| 1 |
Simple: |
| 2 |
======= |
| 3 |
template <typename Type> |
| 4 |
concept BasicMath = |
| 5 |
requires(Type lhs, Type rhs) |
| 6 |
{ |
| 7 |
lhs + rhs; // addition must be available |
| 8 |
}; |
| 9 |
|
| 10 |
also calling a function may be used as simple requirement: |
| 11 |
|
| 12 |
template< class T > |
| 13 |
concept swappable = |
| 14 |
requires(T &a, T &b) |
| 15 |
{ |
| 16 |
ranges::swap(a, b); |
| 17 |
}; |
| 18 |
|
| 19 |
Type: |
| 20 |
===== |
| 21 |
template <typename Type> |
| 22 |
concept HasValueType = |
| 23 |
requires() |
| 24 |
{ |
| 25 |
typename Type::value_type; |
| 26 |
}; |
| 27 |
|
| 28 |
Compound: |
| 29 |
========= |
| 30 |
concept ReturnType = |
| 31 |
requires(Type par) |
| 32 |
{ |
| 33 |
{ par[0] } -> std::same_as<Ret>; // par[..] returns a Ret |
| 34 |
} |
| 35 |
|
| 36 |
Nested: |
| 37 |
======= |
| 38 |
template <typename Type> |
| 39 |
concept BiIterator = |
| 40 |
FwdIterator<Type> |
| 41 |
and |
| 42 |
requires(Type type) |
| 43 |
{ |
| 44 |
--type; |
| 45 |
}; |
| 46 |
|
| 47 |
templates using concepts: |
| 48 |
========================= |
| 49 |
template <BiIterator Type> |
| 50 |
void biFun(Type tp) |
| 51 |
{} |
| 52 |
--------------------------- |
| 53 |
template <typename Type> |
| 54 |
void biFun(Type tp) |
| 55 |
requires BiIterator<Type> |
| 56 |
{} |
| 57 |
|