Debian Codegraph @

yo/advancedtemplates/concepts/examples/membertemplateconstraint.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 <typename Type>
37 struct Data
38 {
39 template <Constraint Member>
40 void process(Member par); // a member template is constrained
41
42 template <typename Tp> // also, now via a variable constr.
43 void another(Tp par) requires Constraint2<Tp>;
44 };
45
46 template <typename T1>
47 template <Constraint T2>
48 void Data<T1>::process(T2 par)
49 {}
50
51 template <typename T1>
52 template <typename T2>
53 void Data<T1>::another(T2 par) requires Constraint2<T2>
54 {}
55
56 int main()
57 {
58 Data<int>{}.process(Combi{});
59 }