| 1 |
#include <string> |
| 2 |
#include <iostream> |
| 3 |
#include <concepts> |
| 4 |
//#include <type_traits> |
| 5 |
|
| 6 |
template<typename Compound, typename Specified> |
| 7 |
struct isEqual |
| 8 |
{ |
| 9 |
enum { equal = false }; |
| 10 |
}; |
| 11 |
|
| 12 |
template<typename Specified> |
| 13 |
struct isEqual<Specified, Specified> |
| 14 |
{ |
| 15 |
enum { equal = true }; |
| 16 |
}; |
| 17 |
|
| 18 |
template<typename Compound, typename Specified> |
| 19 |
concept same = isEqual<Compound, Specified>::equal; |
| 20 |
|
| 21 |
////////////////////////////////// |
| 22 |
|
| 23 |
|
| 24 |
template <typename Type, typename Ret> |
| 25 |
concept ReturnType = |
| 26 |
requires(Type par) |
| 27 |
{ |
| 28 |
{ par[0] } -> std::same_as<Ret>; // par[..] returns a Ret |
| 29 |
|
| 30 |
// no type constraints: |
| 31 |
// { par } -> isEqual<Ret>::equal; // par returns a Ret |
| 32 |
// { par << 1 } -> isEqual<decltype(par << 1), Ret>::equal; |
| 33 |
|
| 34 |
// { par } -> same<Ret>; |
| 35 |
// { par } noexcept; |
| 36 |
|
| 37 |
{++par}; |
| 38 |
// same as: |
| 39 |
// ++par; |
| 40 |
}; |
| 41 |
|
| 42 |
/////////////////////////////// |
| 43 |
|
| 44 |
struct XX |
| 45 |
{ |
| 46 |
void operator++() |
| 47 |
{}; |
| 48 |
|
| 49 |
std::string operator[](int idx) const |
| 50 |
{ |
| 51 |
return std::string{"a"}; |
| 52 |
} |
| 53 |
}; |
| 54 |
|
| 55 |
template <typename Type, typename Ret> |
| 56 |
requires ReturnType<Type, Ret> |
| 57 |
Ret ret(Type tp) |
| 58 |
{ |
| 59 |
return tp[0]; |
| 60 |
} |
| 61 |
|
| 62 |
using namespace std; |
| 63 |
|
| 64 |
|
| 65 |
int main() |
| 66 |
{ |
| 67 |
ret<XX, string>(XX{}); |
| 68 |
} |