Debian Codegraph @

yo/advancedtemplates/concepts/examples/constrainedclass.cc

raw
1
2 template <typename Type>
3 concept bool Constraint() // function concept
4 {
5 return
6 requires(Type lhs, Type rhs)
7 {
8 { lhs < rhs } -> bool; // multiple
9 lhs + rhs; // constraints
10 // typename Type::value_type;
11 };
12 }
13
14 template <typename Type>
15 concept bool Constraint2 = // variable concept
16 requires(Type lhs, Type rhs)
17 {
18 // { operator<(lhs, rhs) } -> bool; // both OK
19 { lhs < rhs } -> bool;
20
21 lhs + rhs;
22 typename Type::value_type;
23 };
24
25
26 // template <typename Type> concept bool Constraint = true ;
27
28 struct Combi
29 {
30 using value_type = int;
31 };
32
33 bool operator<(Combi const &lhs, Combi const &rhs);
34 Combi operator+(Combi const &lhs, Combi const &rhs);
35
36 template <Constraint Type>
37 struct Data
38 {
39 void process();
40 };
41
42 // template <Constraint X> // Constraint may or may not be specified
43 template <typename X> // as the constraints are implied by Data
44 void Data<X>::process() // the formal typename may also be different
45 {}
46
47 template <Constraint2 Type>
48 struct Data2
49 {
50 void process();
51 };
52
53 template <Constraint2 X> // Constraint must be specified,
54 void Data2<X>::process() // but the formal type name
55 { // doesn't have to be Type
56 int x = 0;
57 int y = x;
58 x = y << 1;
59 }
60
61 template <Constraint2 Type>
62 struct Data3
63 {
64 void process();
65 };
66
67 template <Constraint2 X> // Constraint must be specified,
68 void Data3<X>::process() // but the formal type name
69 { // doesn't have to be Type
70 int x = 0;
71 int y = x;
72 x = y << 1;
73 }
74
75
76 int main()
77 {
78 Data<Combi>{}.process();
79 Data2<Combi>{}.process();
80 }