| 1 |
#include <utility> |
| 2 |
#include <concepts> |
| 3 |
|
| 4 |
//comparable |
| 5 |
template <typename Type> |
| 6 |
concept Comparable = |
| 7 |
requires (Type lhs, Type rhs) |
| 8 |
{ |
| 9 |
lhs == rhs; |
| 10 |
lhs != rhs; |
| 11 |
}; |
| 12 |
|
| 13 |
template <typename Type> |
| 14 |
concept Incrementable = |
| 15 |
requires (Type type) |
| 16 |
{ |
| 17 |
++type; |
| 18 |
type++; |
| 19 |
}; |
| 20 |
//= |
| 21 |
|
| 22 |
//dereference |
| 23 |
template <typename Type> |
| 24 |
concept Dereferenceable = |
| 25 |
requires(Type type) |
| 26 |
{ |
| 27 |
{ *type } -> std::same_as<typename Type::value_type &>; |
| 28 |
}; |
| 29 |
//= |
| 30 |
|
| 31 |
//constderef |
| 32 |
template <typename Type> |
| 33 |
concept ConstDereferenceable = |
| 34 |
requires(Type const &type) |
| 35 |
{ |
| 36 |
{ *type } -> std::same_as<typename Type::value_type const &>; |
| 37 |
}; |
| 38 |
//= |
| 39 |
|
| 40 |
struct InIt |
| 41 |
{ |
| 42 |
using value_type = int; |
| 43 |
|
| 44 |
InIt &operator++(); |
| 45 |
InIt operator++(int); |
| 46 |
|
| 47 |
int const &operator*() const; |
| 48 |
}; |
| 49 |
|
| 50 |
bool operator==(InIt const &lh, InIt const &rh); |
| 51 |
bool operator!=(InIt const &lh, InIt const &rh); |
| 52 |
int operator-(InIt const &lh, InIt const &rh); |
| 53 |
|
| 54 |
|
| 55 |
//initerator |
| 56 |
template <typename Type> |
| 57 |
concept InIterator = |
| 58 |
Comparable<Type> and Incrementable<Type> and |
| 59 |
ConstDereferenceable<Type>; |
| 60 |
|
| 61 |
template <InIterator Type> |
| 62 |
void inFun(Type tp) |
| 63 |
{} |
| 64 |
//= |
| 65 |
|
| 66 |
|
| 67 |
struct OutIt: public InIt |
| 68 |
{ |
| 69 |
int &operator*(); |
| 70 |
}; |
| 71 |
|
| 72 |
//outiterator |
| 73 |
template <typename Type> |
| 74 |
concept OutIterator = |
| 75 |
Comparable<Type> and Incrementable<Type> and |
| 76 |
Dereferenceable<Type>; |
| 77 |
|
| 78 |
template <OutIterator Type> |
| 79 |
void outFun(Type tp) |
| 80 |
{} |
| 81 |
|
| 82 |
//= |
| 83 |
|
| 84 |
//fwditerator |
| 85 |
template <typename Type> |
| 86 |
concept FwdIterator = |
| 87 |
InIterator<Type> and OutIterator<Type>; |
| 88 |
|
| 89 |
struct FwdIt: public OutIt |
| 90 |
{ |
| 91 |
using OutIt::operator*; |
| 92 |
using InIt::operator*; |
| 93 |
}; |
| 94 |
|
| 95 |
template <FwdIterator Type> |
| 96 |
void fwdFun(Type tp) |
| 97 |
{} |
| 98 |
|
| 99 |
//= |
| 100 |
|
| 101 |
//iterable |
| 102 |
struct Iterable |
| 103 |
{ |
| 104 |
using value_type = int; |
| 105 |
|
| 106 |
Iterable &operator++(); |
| 107 |
Iterable operator++(int); |
| 108 |
|
| 109 |
int const &operator*() const; |
| 110 |
int &operator*(); |
| 111 |
}; |
| 112 |
|
| 113 |
bool operator==(Iterable const &lh, Iterable const &rh); |
| 114 |
bool operator!=(Iterable const &lh, Iterable const &rh); |
| 115 |
int operator-(Iterable const &lh, Iterable const &rh); |
| 116 |
//= |
| 117 |
|
| 118 |
//bidir |
| 119 |
template <typename Type> |
| 120 |
concept BiIterator = |
| 121 |
FwdIterator<Type> and |
| 122 |
requires(Type type) |
| 123 |
{ |
| 124 |
--type; |
| 125 |
type--; |
| 126 |
}; |
| 127 |
|
| 128 |
template <typename Type> |
| 129 |
concept RndIterator = |
| 130 |
BiIterator<Type> |
| 131 |
and |
| 132 |
requires(Type lhs, Type rhs) |
| 133 |
{ |
| 134 |
lhs += 0; |
| 135 |
lhs -= 0; |
| 136 |
lhs + 0; |
| 137 |
lhs - 0; |
| 138 |
{ lhs - rhs } -> std::same_as<int>; |
| 139 |
}; |
| 140 |
//= |
| 141 |
|
| 142 |
struct BiIt: public FwdIt |
| 143 |
{ |
| 144 |
InIt &operator--(); |
| 145 |
InIt operator--(int); |
| 146 |
}; |
| 147 |
|
| 148 |
template <BiIterator Type> |
| 149 |
void biFun(Type tp) |
| 150 |
{} |
| 151 |
|
| 152 |
//randomit |
| 153 |
struct RndIt: public BiIt |
| 154 |
{ |
| 155 |
RndIt &operator+=(int); |
| 156 |
RndIt &operator-=(int); |
| 157 |
}; |
| 158 |
|
| 159 |
RndIt &operator+(RndIt const &lh, int); |
| 160 |
RndIt &operator-(RndIt const &lh, int); |
| 161 |
|
| 162 |
template <RndIterator Type> |
| 163 |
void rndFun(Type tp) |
| 164 |
{} |
| 165 |
|
| 166 |
int main() |
| 167 |
{ |
| 168 |
inFun(InIt{}); |
| 169 |
// outFun(InIt{}); // WC: unsatisfied constraint |
| 170 |
outFun(OutIt{}); |
| 171 |
// fwdFun(OutIt{}); // WC |
| 172 |
fwdFun(FwdIt{}); |
| 173 |
|
| 174 |
inFun(Iterable{}); |
| 175 |
outFun(Iterable{}); |
| 176 |
fwdFun(Iterable{}); |
| 177 |
|
| 178 |
biFun(BiIt{}); |
| 179 |
// biFun(InIt{}); // WC |
| 180 |
// biFun(OutIt{}); // WC |
| 181 |
|
| 182 |
rndFun(RndIt{}); |
| 183 |
biFun(RndIt{}); // OK: extra functions are fine |
| 184 |
} |