Debian Codegraph @

yo/advancedtemplates/concepts/examples/multirequirements.cc

raw
1 template <typename Type>
2 concept Comparable = // function concept
3 requires(Type lhs, Type rhs)
4 {
5 // { operator<(lhs, rhs) } -> bool; // multiple
6 lhs + rhs; // constraints
7 typename Type::value_type;
8 };
9
10 template <typename Type>
11 concept Comparable2 = // variable concept
12 requires(Type lhs, Type rhs)
13 {
14 // { operator<(lhs, rhs) } -> bool;
15 lhs + rhs;
16 typename Type::value_type;
17 };
18
19
20 template<Comparable Type> // concept names can be function
21 Type const &min1(Type const &x, Type const &y) // and variable concepts
22 {
23 return y < x ? y : x;
24 }
25
26 template<Comparable2 Type>
27 Type const &min2(Type const &x, Type const &y)
28 {
29 return y < x ? y : x;
30 }
31
32 template<typename Type> requires Comparable2<Type> // cannot be fun. con.
33 Type const &min3(Type const &x, Type const &y)
34 {
35 return y < x ? y : x;
36 }
37
38 struct Combi
39 {
40 using value_type = int;
41 };
42
43 bool operator<(Combi const &lhs, Combi const &rhs);
44 Combi operator+(Combi const &lhs, Combi const &rhs);
45
46 int main()
47 {
48 min1(Combi{}, Combi());
49 min2(Combi{}, Combi());
50 min3(Combi{}, Combi());
51 }