| 1 |
#include <iostream> |
| 2 |
#include <vector> |
| 3 |
|
| 4 |
template <typename Type> // make constraints available |
| 5 |
concept bool C1 = true; |
| 6 |
template <typename Type> |
| 7 |
concept bool C2 = true; |
| 8 |
|
| 9 |
|
| 10 |
template <typename Type> |
| 11 |
concept bool Addition = |
| 12 |
requires(Type lhs, Type rhs) |
| 13 |
{ |
| 14 |
lhs + rhs; |
| 15 |
}; |
| 16 |
|
| 17 |
template <typename Type> |
| 18 |
concept bool Subtraction = |
| 19 |
requires(Type lhs, Type rhs) |
| 20 |
{ |
| 21 |
lhs - rhs; |
| 22 |
}; |
| 23 |
|
| 24 |
template <typename Type> // 3rd form: 1st requires introduces |
| 25 |
requires requires(Type lhs, Type rhs) // the requires-clause, followed |
| 26 |
{ // by the requires expression |
| 27 |
lhs + rhs; // (which may consist of and/or |
| 28 |
} // concatenated |
| 29 |
Type adder(Type lhs, Type rhs) // requires-expressions) |
| 30 |
{ |
| 31 |
return lhs + rhs; |
| 32 |
} |
| 33 |
|
| 34 |
void fun(int arg) requires requires(int x) { false; }; |
| 35 |
|
| 36 |
template <typename ...Types> concept bool Pack = true; |
| 37 |
|
| 38 |
template <Pack ...Types> // multiple |
| 39 |
void fun2(); |
| 40 |
|
| 41 |
template <typename Type> concept bool Constraint = true; |
| 42 |
|
| 43 |
template <Constraint Type> |
| 44 |
class Data |
| 45 |
{ |
| 46 |
void process(); |
| 47 |
}; |
| 48 |
|
| 49 |
template <typename Type> |
| 50 |
requires Constraint<Type> class Data; |
| 51 |
|
| 52 |
template <Constraint Type> |
| 53 |
class Data; |
| 54 |
|
| 55 |
template <typename Type> concept bool Concept = true; |
| 56 |
|
| 57 |
template <typename Tp> |
| 58 |
struct Struct |
| 59 |
{ |
| 60 |
Struct() |
| 61 |
{ |
| 62 |
std::cout << "Generic class template\n"; |
| 63 |
} |
| 64 |
Struct(Struct const &other) |
| 65 |
{ |
| 66 |
std::cout << "Generic class CC\n"; |
| 67 |
} |
| 68 |
|
| 69 |
Struct &operator=(Struct const &other) |
| 70 |
{ |
| 71 |
std::cout << "Generic operator=\n"; |
| 72 |
return *this; |
| 73 |
} |
| 74 |
|
| 75 |
Struct &operator+=(Struct const &other) |
| 76 |
{ |
| 77 |
std::cout << "Generic operator+=\n"; |
| 78 |
return *this; |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
template <Subtraction Tp> |
| 83 |
struct S2: public Struct<Tp> |
| 84 |
{ |
| 85 |
}; |
| 86 |
|
| 87 |
template <Addition Tp> |
| 88 |
struct Struct<Tp>; |
| 89 |
|
| 90 |
template <Subtraction Tp> |
| 91 |
struct S2; |
| 92 |
|
| 93 |
|
| 94 |
template <Addition Type> |
| 95 |
void fun(); |
| 96 |
|
| 97 |
template <typename Type> |
| 98 |
void fun() requires Addition<Type>; |
| 99 |
|
| 100 |
template <typename Type> |
| 101 |
requires Addition<Type> void fun(); |
| 102 |
|
| 103 |
template <C1 Type> // declares a multiply |
| 104 |
requires C2<Type> void fun(); // constrained function template |
| 105 |
|
| 106 |
template <typename Type> // same, using 'and' |
| 107 |
requires C1<Type> and C2<Type> void fun(); |
| 108 |
|
| 109 |
template <typename Type> // same |
| 110 |
void fun() requires C1<Type> and C2<Type>; |
| 111 |
|
| 112 |
int main() |
| 113 |
{ |
| 114 |
// fun(4); |
| 115 |
// adder(4, 6); |
| 116 |
|
| 117 |
// fun2<int>(); |
| 118 |
// fun2<int, double>(); |
| 119 |
|
| 120 |
// Struct<int>{}; |
| 121 |
// Struct<std::vector<int>>{}; |
| 122 |
|
| 123 |
S2<int> s1{}; |
| 124 |
S2<int> s2{s1}; |
| 125 |
|
| 126 |
s1 = s2; |
| 127 |
s1 += s2; |
| 128 |
} |