1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#include <concepts>
#include <iostream>
#include <string>
template <typename LHS, typename RHS>
requires std::totally_ordered_with<LHS, RHS> // multiple ? combine in
auto pred = // a separate concept
[](LHS lhs, RHS rhs)
{
return lhs < rhs;
};
template <typename LHS, typename RHS>
bool smaller(LHS lhs, RHS rhs)
requires std::totally_ordered_with<LHS, RHS>
{
return lhs < rhs;
}
template <typename Fun,
typename LHS, typename RHS>
bool cmp3(Fun fun, LHS lhs, RHS rhs)
requires std::relation<Fun, LHS, RHS>
{
return fun(lhs, rhs);
}
template <typename T1, typename T2>
T1 fun(T2)
{
return T1{};
}
template <typename LHS, typename RHS>
bool cmp2(LHS lhs, RHS rhs)
requires std::relation<decltype(pred<LHS, RHS>), LHS, RHS>
{
return pred<LHS, RHS>(lhs, rhs);
}
//relation
template <typename LHS, typename RHS>
struct Less
{
bool operator()(LHS lhs, RHS rhs) const
{
return lhs < rhs;
}
};
template <template<typename LHS, typename RHS> typename Pred,
typename LHS, typename RHS>
bool cmp(LHS lhs, RHS rhs)
requires std::relation<Pred<LHS, RHS>, LHS, RHS>
{
return Pred<LHS, RHS>{}(lhs, rhs);
}
int main()
{
std::cout << cmp <Less>(5, 4.9) << '\n';
std::cout << cmp <Less>(5, "hello world") << '\n';
}
//=
//
//int main()
//{
// std::cout <<
// cmp2(3, 4) << ' ' <<
// cmp2(3.2, 4) << ' ' <<
// cmp2(3, 4.2) << ' ' <<
// cmp2(3.3, 4.4) << ' ' <<
// cmp < Less >(5, 4) << ' ' <<
// cmp3( smaller<int, int>, 5, 4) << ' ' <<
// '\n';
//
// int x = fun<int>(std::string{});
// x += 12;
//}
//
|