1 | <HTML> |
---|
2 | <!-- |
---|
3 | -- Copyright (c) Jeremy Siek and Andrew Lumsdaine 2000 |
---|
4 | -- |
---|
5 | -- Permission to use, copy, modify, distribute and sell this software |
---|
6 | -- and its documentation for any purpose is hereby granted without fee, |
---|
7 | -- provided that the above copyright notice appears in all copies and |
---|
8 | -- that both that copyright notice and this permission notice appear |
---|
9 | -- in supporting documentation. We make no |
---|
10 | -- representations about the suitability of this software for any |
---|
11 | -- purpose. It is provided "as is" without express or implied warranty. |
---|
12 | --> |
---|
13 | <Head> |
---|
14 | <Title>Using Concept Checks</Title> |
---|
15 | <BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b" |
---|
16 | ALINK="#ff0000"> |
---|
17 | <IMG SRC="../../boost.png" |
---|
18 | ALT="C++ Boost" width="277" height="86"> |
---|
19 | |
---|
20 | <BR Clear> |
---|
21 | |
---|
22 | |
---|
23 | <h2><a name="using-concept-checks">Using Concept Checks</a></h2> |
---|
24 | |
---|
25 | For each concept there is a concept checking class which can be used |
---|
26 | to make sure that a given type (or set of types) models the concept. |
---|
27 | The Boost Concept Checking Library (BCCL) includes concept checking classes |
---|
28 | for all of the concepts used in the C++ standard library and a few |
---|
29 | more. The <a href="./reference.htm">Reference</a> section lists these |
---|
30 | concept checking classes. In addition, other boost libraries come with |
---|
31 | concept checking classes for the concepts that are particular to those |
---|
32 | libraries. For example, there are <a |
---|
33 | href="../graph/doc/graph_concepts.html">graph concepts</a> and <a |
---|
34 | href="../property_map/property_map.html">property map concepts</a>. |
---|
35 | Also, whenever <b>anyone</b> writing a class of function template |
---|
36 | needs to express requirements that are not yet stated by an existing |
---|
37 | concept, a new concept checking class should be created. How |
---|
38 | to do this is explained in <a href="./creating_concepts.htm">Creating |
---|
39 | Concept Checking Classes</a>. |
---|
40 | |
---|
41 | <p> |
---|
42 | An example of a concept checking class from the BCCL is the |
---|
43 | <tt>EqualityComparableConcept</tt> class. The class corresponds to the |
---|
44 | EqualityComparable requirements described in 20.1.1 of the C++ |
---|
45 | Standard, and to the <a |
---|
46 | href="http://www.sgi.com/tech/stl/EqualityComparable.html">EqualityComparable</a> |
---|
47 | concept documented in the SGI STL. |
---|
48 | |
---|
49 | <pre> |
---|
50 | template <class T> |
---|
51 | struct EqualityComparableConcept; |
---|
52 | </pre> |
---|
53 | |
---|
54 | The template argument <tt>T</tt> will the type to be checked. That is, |
---|
55 | the purpose of <tt>EqualityComparableConcept</tt> is to make sure that |
---|
56 | the template argument given for <tt>T</tt> models the |
---|
57 | EqualityComparable concept. |
---|
58 | |
---|
59 | <p> |
---|
60 | Each concept checking class has a member function named |
---|
61 | <tt>constraints()</tt> which contains the valid expressions for the |
---|
62 | concept. To check whether some type is EqualityComparable we need to |
---|
63 | instantiate the concept checking class with the type and then find a |
---|
64 | way to get the compiler to compile the <tt>constraints()</tt> function |
---|
65 | without actually executing the function. The Boost Concept Checking |
---|
66 | Library defines two utilities that make this easy: |
---|
67 | <tt>function_requires()</tt> and <tt>BOOST_CLASS_REQUIRE</tt>. |
---|
68 | |
---|
69 | <h4><tt>function_requires()</tt></h4> |
---|
70 | |
---|
71 | The <tt>function_requires()</tt> function can be used in function bodies |
---|
72 | and the <tt>BOOST_CLASS_REQUIRE</tt> macro can be used inside class |
---|
73 | bodies. The <tt>function_requires()</tt> function takes no arguments, |
---|
74 | but has a template parameter for the concept checking class. This |
---|
75 | means that the instantiated concept checking class must be given as an |
---|
76 | explicit template argument, as shown below. |
---|
77 | |
---|
78 | <pre> |
---|
79 | // In my library: |
---|
80 | template <class T> |
---|
81 | void generic_library_function(T x) |
---|
82 | { |
---|
83 | function_requires< EqualityComparableConcept<T> >(); |
---|
84 | // ... |
---|
85 | }; |
---|
86 | |
---|
87 | // In the user's code: |
---|
88 | class foo { |
---|
89 | //... |
---|
90 | }; |
---|
91 | |
---|
92 | int main() { |
---|
93 | foo f; |
---|
94 | generic_library_function(f); |
---|
95 | return 0; |
---|
96 | } |
---|
97 | </pre> |
---|
98 | |
---|
99 | |
---|
100 | <h4><tt>BOOST_CLASS_REQUIRE</tt></h4> |
---|
101 | |
---|
102 | The <tt>BOOST_CLASS_REQUIRE</tt> macro can be used inside a class |
---|
103 | definition to check whether some type models a concept. Make sure |
---|
104 | that the arguments to this macro are simply identifiers. You may need |
---|
105 | to use typedef to get your types into this form. |
---|
106 | |
---|
107 | <pre> |
---|
108 | // In my library: |
---|
109 | template <class T> |
---|
110 | struct generic_library_class |
---|
111 | { |
---|
112 | BOOST_CLASS_REQUIRE(T, boost, EqualityComparableConcept); |
---|
113 | // ... |
---|
114 | }; |
---|
115 | |
---|
116 | // In the user's code: |
---|
117 | class foo { |
---|
118 | //... |
---|
119 | }; |
---|
120 | |
---|
121 | int main() { |
---|
122 | generic_library_class<foo> glc; |
---|
123 | // ... |
---|
124 | return 0; |
---|
125 | } |
---|
126 | </pre> |
---|
127 | |
---|
128 | |
---|
129 | <h4>Example</h4> |
---|
130 | |
---|
131 | <p> |
---|
132 | Getting back to the earlier <a |
---|
133 | href="./concept_check.htm#motivating-example">motivating example</a>, |
---|
134 | one good application of concept checks would be to insert |
---|
135 | <tt>function_requires()</tt> at the top of <tt>std::stable_sort()</tt> |
---|
136 | to make sure the template parameter type models <a |
---|
137 | href="http://www.sgi.com/tech/stl/RandomAccessIterator.html"> |
---|
138 | RandomAccessIterator</a>. In addition, <tt>std::stable_sort()</tt> |
---|
139 | requires that the <tt>value_type</tt> of the iterators be |
---|
140 | <a href="http://www.sgi.com/tech/stl/LessThanComparable.html"> |
---|
141 | LessThanComparable</a>, so we also use <tt>function_requires()</tt> to |
---|
142 | check this. |
---|
143 | |
---|
144 | <pre> |
---|
145 | template <class RandomAccessIter> |
---|
146 | void stable_sort(RandomAccessIter first, RandomAccessIter last) |
---|
147 | { |
---|
148 | function_requires< RandomAccessIteratorConcept<RandomAccessIter> >(); |
---|
149 | typedef typename std::iterator_traits<RandomAccessIter>::value_type value_type; |
---|
150 | function_requires< LessThanComparableConcept<value_type> >(); |
---|
151 | ... |
---|
152 | } |
---|
153 | </pre> |
---|
154 | |
---|
155 | |
---|
156 | |
---|
157 | <!-- There are a few places where the SGI STL documentation differs |
---|
158 | from the corresponding requirements described in the C++ Standard. In |
---|
159 | these cases we use the definition from the C++ Standard. --> |
---|
160 | |
---|
161 | <p> |
---|
162 | Some concepts deal with more than one type. In this case the |
---|
163 | corresponding concept checking class will have multiple template |
---|
164 | parameters. The following example shows how |
---|
165 | <tt>function_requires()</tt> is used with the <a |
---|
166 | href="../property_map/ReadWritePropertyMap.html">ReadWritePropertyMap</a> |
---|
167 | concept which takes two type parameters: a property map and the key |
---|
168 | type for the map. |
---|
169 | |
---|
170 | <pre> |
---|
171 | template <class IncidenceGraph, class Buffer, class BFSVisitor, |
---|
172 | class ColorMap> |
---|
173 | void breadth_first_search(IncidenceGraph& g, |
---|
174 | typename graph_traits<IncidenceGraph>::vertex_descriptor s, |
---|
175 | Buffer& Q, BFSVisitor vis, ColorMap color) |
---|
176 | { |
---|
177 | typedef typename graph_traits<IncidenceGraph>::vertex_descriptor Vertex; |
---|
178 | function_requires< ReadWritePropertyMap<ColorMap, Vertex> >(); |
---|
179 | ... |
---|
180 | } |
---|
181 | </pre> |
---|
182 | |
---|
183 | |
---|
184 | As an example of using <tt>BOOST_CLASS_REQUIRE</tt> we look at a concept |
---|
185 | check that could be added to <tt>std::vector</tt>. One requirement |
---|
186 | that is placed on the element type is that it must be <a |
---|
187 | href="http://www.sgi.com/tech/stl/Assignable.html">Assignable</a>. |
---|
188 | We can check this by inserting |
---|
189 | <tt>class_requires<AssignableConcept<T> ></tt> at the top |
---|
190 | of the definition for <tt>std::vector</tt>. |
---|
191 | |
---|
192 | <pre> |
---|
193 | namespace std { |
---|
194 | template <class T> |
---|
195 | struct vector { |
---|
196 | BOOST_CLASS_REQUIRE(T, boost, AssignableConcept); |
---|
197 | ... |
---|
198 | }; |
---|
199 | } |
---|
200 | </pre> |
---|
201 | |
---|
202 | |
---|
203 | Although the concept checks are designed for use by generic library |
---|
204 | implementors, they can also be useful to end users. Sometimes one may |
---|
205 | not be sure whether some type models a particular concept. This can |
---|
206 | easily be checked by creating a small program and using |
---|
207 | <tt>function_requires()</tt> with the type and concept in question. |
---|
208 | The file <a |
---|
209 | href="./stl_concept_check.cpp"><tt>stl_concept_checks.cpp</tt></a> |
---|
210 | gives and example of applying the concept checks to STL containers. |
---|
211 | |
---|
212 | <p> |
---|
213 | <a href="./concept_check.htm">Prev: Concept Checking Introduction</a> <br> |
---|
214 | <a href="./creating_concepts.htm">Next: Creating Concept Checking Classes</a> |
---|
215 | |
---|
216 | <br> |
---|
217 | <HR> |
---|
218 | <TABLE> |
---|
219 | <TR valign=top> |
---|
220 | <TD nowrap>Copyright © 2000</TD><TD> |
---|
221 | <A HREF="../../people/jeremy_siek.htm">Jeremy Siek</A>(<A |
---|
222 | HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>) |
---|
223 | Andrew Lumsdaine</A>(<A HREF="mailto:lums@osl.iu.edu">lums@osl.iu.edu</A>) |
---|
224 | </TD></TR></TABLE> |
---|
225 | |
---|
226 | </BODY> |
---|
227 | </HTML> |
---|