Debian Codegraph @

yo/advancedtemplates/concepts/examples/memberconstraint.cc

raw
1 template <typename Type>
2 concept bool Addable =
3 requires(Type lh, Type rh)
4 {
5 lh + rh;
6 };
7
8
9 template <typename Type>
10 concept bool Constraint() // function concept
11 {
12 return
13 requires(Type lhs, Type rhs)
14 {
15 { lhs < rhs } -> bool; // multiple
16 lhs + rhs; // constraints
17 // typename Type::value_type;
18 };
19 }
20
21 template <typename Type>
22 concept bool Constraint2 = // variable concept
23 requires(Type lhs, Type rhs)
24 {
25 // { operator<(lhs, rhs) } -> bool; // both OK
26 { lhs < rhs } -> bool;
27
28 lhs + rhs;
29 typename Type::value_type;
30 };
31
32
33 // template <typename Type> concept bool Constraint = true ;
34
35 struct Combi
36 {
37 using value_type = int;
38 };
39
40 bool operator<(Combi const &lhs, Combi const &rhs);
41 Combi operator+(Combi const &lhs, Combi const &rhs);
42
43 template <typename Type>
44 struct Data
45 { // constraint applies only to a member
46 void process() requires Addable<Type>;
47
48 template <Addable Tp>
49 void add(Tp tp);
50 };
51
52 template <typename Typ> // formal type names may differ
53 void Data<Typ>::process() requires Addable<Typ> // from the class.
54 {}
55
56 template <typename Typ> // formal type names may differ
57 template <Addable Tp>
58 void Data<Typ>::add(Tp tp)
59 {}
60
61
62 int main()
63 {
64 Data<Combi>{}.process();
65 }