Debian Codegraph @

yo/advancedtemplates/concepts/examples/relation.cc

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