Debian Codegraph @

yo/advancedtemplates/concepts/intro.txt

raw
1
2 Alternatively the following syntax can be used:
3 verb(
4 template< typename Type> requires ComparableBySmaller<Type>
5 Type const &min(Type const &x, Type const &y)
6 {
7 return y < x ? y : x;
8 }
9 )
10 The advantage of the former syntax is that it allows a list of
11 requirements, e.g., linebreak() tt(ComparableBySmaller<Type>,
12 Ordered<Type>). Moreover, these requirements may be combined using the
13 logical negation (! (tt(not))) and conjunction (&& (tt(and))) operators.
14
15 To define a concept hi(concept: definition) the following syntax will be
16 provided:
17 verb(
18 auto concept ComparableBySmaller <typename Type>
19 {
20 bool operator<(Type, Type);
21 }
22 )
23 This concept specifies that a type supporting an tt(operator<) accepting
24 two tt(Type) objects and returning a tt(bool) is a tt(ComparableBySmaller)
25 type. Note that this therefore becomes an implied characteristic of an actual
26 type. Any type defining this operator by implication also becomes a
27 tt(ComparableBySmaller) type. Any such type may therefore be used to
28 instantiate a template with that requires the tt(ComparableBySmaller) concept.
29
30 The C++ standard introduces emi(axiom)em(s) as a feature of concepts. Axioms
31 can be used to define properties of concepts, such as their
32 associativity. Here is a preliminary example of how an axiom could be used in
33 the definition of a concept:
34 verb(
35 concept Semigroup< typename Op, typename Type> : CopyConstructible<Type>
36 {
37 Type operator()(Op, Type, Type);
38
39 axiom Associativity(Op op, Type x, Type y, Type z)
40 {
41 op(x, op(y, z)) == op(op(x, y), z);
42 }
43 };
44 )
45
46 As remarked, concepts and axioms are currently still unavailable in the
47 tt(g++) compiler. It is likely that this section will be vastly expanded by
48 the time the compiler has fully implemented the C2a standard. Concepts, axioms
49 and related constructs may eventually even be covered in a separate chapter.
50
51
52
53
54 COMMENT(
55 =================================================================
56 The
57 operator need not be a free-function; it could be a member function of the
58 type T.
59
60 Concepts can involve multiple objects as well. For example, concepts can
61 express that a type is convertible from one type to another:
62
63 auto concept Convertible< typename T, typename U>
64 {
65 operator U(const T&);
66 }
67
68 In order to use this in a template, it must use a generalized form of concept
69 usage:
70
71 template< typename U, typename T> requires Convertible<T, U>
72 U convert(const T& t)
73 {
74 return t;
75 }
76
77
78 Concepts may be composed. For example, given a concept named Regular:
79
80 concept InputIterator< typename Iter, typename Value>
81 {
82 requires Regular<Iter>;
83 Value operator*(const Iter&);
84 Iter& operator++(Iter&);
85 Iter operator++(Iter&, int);
86 }
87
88 The first template parameter to the InputIterator concept must conform to the
89 Regular concept.
90
91 Concepts can also be derived from one another, like inheritance. Like in class
92 inheritance, types that meet the requirements of the derived concept also meet
93 the requirements of the base concept. It is defined as per class derivation:
94
95 concept ForwardIterator< typename Iter, typename Value> : InputIterator<Iter,
96 Value> {
97 //Add other requirements here.
98 }
99
100 Typenames can also be associated with a concept. These impose the requirement
101 that, in templates that use those concepts, these typenames are available:
102
103 concept InputIterator< typename Iter>
104 {
105 typename value_type;
106 typename reference;
107 typename pointer;
108 typename difference_type;
109 requires Regular<Iter>;
110 requires Convertible<reference, value_type>;
111 reference operator*(const Iter&); // dereference
112 Iter& operator++(Iter&); // pre-increment
113 Iter operator++(Iter&, int); // post-increment
114 //...
115 }
116
117 Concept maps allow types to be explicitly bound to a concept. They also allow
118 types to, where possible, adopt the syntax of a concept without changing the
119 definition of the type. As an example:
120
121 concept_map InputIterator<char*>
122 {
123 using value_type = char;
124 using reference = char &;
125 using pointer = char *;
126 using difference_type = std::ptrdiff_t;
127 };
128
129 This map fills in the required typenames for the InputIterator concept when
130 applied to char* types.
131
132 As an added degree of flexibility, concept maps themselves can be
133 templated. The above example can be extended to all pointer types:
134
135 template< typename T> concept_map InputIterator<T*>
136 {
137 using value_type = T;
138 using reference = T &;
139 using pointer = T *;
140 using difference_type = std::ptrdiff_t;
141 };
142
143 Further, concept maps can act as mini-types, with function definitions and
144 other constructs commonly associated with classes:
145
146 concept Stack< typename X>
147 {
148 typename value_type;
149 void push(X&, const value_type&);
150 void pop(X&);
151 value_type top(const X&);
152 bool empty(const X&);
153 };
154
155 template< typename T> concept_map Stack<std::vector<T> >
156 {
157 using value_type = T;
158 void push(std::vector<T>& v, const T& x) { v.push_back(x); }
159 void pop(std::vector<T>& v) { v.pop_back(); }
160 T top(const std::vector<T>& v) { return v.back(); }
161 bool empty(const std::vector<T>& v) { return v.empty(); }
162 };
163
164 This concept map allows templates that take types that implement the concept
165 Stack to take a std::vector, remapping the function calls directly to the
166 std::vector calls. Ultimately, this allows a pre-existing object to be
167 converted, without touching the definition of the object, into an interface
168 that a template function can utilize.
169
170 Finally, it should be noted that some requirements can be checked using static
171 assertions. These can verify some requirements that templates need, but are
172 really aimed at a different problem.
173
174 ================================================================
175
176 Compilers are allowed, but not required, to take advantage of the semantics
177 specified by axioms to perform optimizations that possibly have side-effects
178 on the observable behavior of the program, which are typically prohibited
179 (with few exceptions such as copy constructor elision). In the above example,
180 compilers may reassociate nested calls to operator() of type Op on several
181 values of type Type provided that there is a concept map for types Op and Type
182 to the concept Semigroup.
183
184 Axioms can also assist in software verification, software testing, and other
185 program analyses and transformations.
186 ===========================================================
187 END)
188