| 1 |
#include <iostream> |
| 2 |
|
| 3 |
template <typename Type> |
| 4 |
concept Addable = |
| 5 |
requires(Type lh, Type rh) |
| 6 |
{ |
| 7 |
lh + rh; |
| 8 |
}; |
| 9 |
|
| 10 |
template <typename Type> |
| 11 |
concept Subtractable = |
| 12 |
true; |
| 13 |
|
| 14 |
//requires |
| 15 |
template<Addable Type> |
| 16 |
Type add2(Type const &x, Type const &y) |
| 17 |
{ |
| 18 |
return x + y; |
| 19 |
} |
| 20 |
|
| 21 |
template<typename Type> |
| 22 |
requires Addable<Type> |
| 23 |
Type add(Type const &x, Type const &y) |
| 24 |
{ |
| 25 |
return x + y; |
| 26 |
} |
| 27 |
//= |
| 28 |
|
| 29 |
//multiple |
| 30 |
template<typename Type> |
| 31 |
requires |
| 32 |
Addable<Type> and Subtractable<Type> |
| 33 |
Type addsub(Type const &x, Type const &y, Type const &z) |
| 34 |
{ |
| 35 |
return x + y - z; |
| 36 |
} |
| 37 |
//= |
| 38 |
|
| 39 |
//basicmath |
| 40 |
template <typename Type> |
| 41 |
concept BasicMath = |
| 42 |
Addable<Type> and Subtractable<Type> and |
| 43 |
requires(Type x, Type y) |
| 44 |
{ |
| 45 |
x * y; |
| 46 |
}; |
| 47 |
//= |
| 48 |
|
| 49 |
//variadic |
| 50 |
template <typename ...Types> |
| 51 |
concept Variadic = |
| 52 |
requires(Types ...arguments) |
| 53 |
{ |
| 54 |
true; |
| 55 |
}; |
| 56 |
|
| 57 |
//= |
| 58 |
|
| 59 |
|
| 60 |
template<Addable Type> |
| 61 |
requires Subtractable<Type> |
| 62 |
Type addsub2(Type const &x, Type const &y, Type const &z) |
| 63 |
{ |
| 64 |
return x + y - z; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
using namespace std; |
| 69 |
|
| 70 |
int main() |
| 71 |
{ |
| 72 |
add("one"s, "two"s); |
| 73 |
add2("one"s, "two"s); |
| 74 |
} |