| 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. |
|---|
| 104 | |
|---|
| 105 | <pre> |
|---|
| 106 | // In my library: |
|---|
| 107 | template <class T> |
|---|
| 108 | struct generic_library_class |
|---|
| 109 | { |
|---|
| 110 | BOOST_CLASS_REQUIRE(T, boost, EqualityComparableConcept); |
|---|
| 111 | // ... |
|---|
| 112 | }; |
|---|
| 113 | |
|---|
| 114 | // In the user's code: |
|---|
| 115 | class foo { |
|---|
| 116 | //... |
|---|
| 117 | }; |
|---|
| 118 | |
|---|
| 119 | int main() { |
|---|
| 120 | generic_library_class<foo> glc; |
|---|
| 121 | // ... |
|---|
| 122 | return 0; |
|---|
| 123 | } |
|---|
| 124 | </pre> |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | <h4>Example</h4> |
|---|
| 128 | |
|---|
| 129 | <p> |
|---|
| 130 | Getting back to the earlier <a |
|---|
| 131 | href="./concept_check.htm#motivating-example">motivating example</a>, |
|---|
| 132 | one good application of concept checks would be to insert |
|---|
| 133 | <tt>function_requires()</tt> at the top of <tt>std::stable_sort()</tt> |
|---|
| 134 | to make sure the template parameter type models <a |
|---|
| 135 | href="http://www.sgi.com/tech/stl/RandomAccessIterator.html"> |
|---|
| 136 | RandomAccessIterator</a>. In addition, <tt>std::stable_sort()</tt> |
|---|
| 137 | requires that the <tt>value_type</tt> of the iterators be |
|---|
| 138 | <a href="http://www.sgi.com/tech/stl/LessThanComparable.html"> |
|---|
| 139 | LessThanComparable</a>, so we also use <tt>function_requires()</tt> to |
|---|
| 140 | check this. |
|---|
| 141 | |
|---|
| 142 | <pre> |
|---|
| 143 | template <class RandomAccessIter> |
|---|
| 144 | void stable_sort(RandomAccessIter first, RandomAccessIter last) |
|---|
| 145 | { |
|---|
| 146 | function_requires< RandomAccessIteratorConcept<RandomAccessIter> >(); |
|---|
| 147 | typedef typename std::iterator_traits<RandomAccessIter>::value_type value_type; |
|---|
| 148 | function_requires< LessThanComparableConcept<value_type> >(); |
|---|
| 149 | ... |
|---|
| 150 | } |
|---|
| 151 | </pre> |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | <!-- There are a few places where the SGI STL documentation differs |
|---|
| 156 | from the corresponding requirements described in the C++ Standard. In |
|---|
| 157 | these cases we use the definition from the C++ Standard. --> |
|---|
| 158 | |
|---|
| 159 | <p> |
|---|
| 160 | Some concepts deal with more than one type. In this case the |
|---|
| 161 | corresponding concept checking class will have multiple template |
|---|
| 162 | parameters. The following example shows how |
|---|
| 163 | <tt>function_requires()</tt> is used with the <a |
|---|
| 164 | href="../property_map/ReadWritePropertyMap.html">ReadWritePropertyMap</a> |
|---|
| 165 | concept which takes two type parameters: a property map and the key |
|---|
| 166 | type for the map. |
|---|
| 167 | |
|---|
| 168 | <pre> |
|---|
| 169 | template <class IncidenceGraph, class Buffer, class BFSVisitor, |
|---|
| 170 | class ColorMap> |
|---|
| 171 | void breadth_first_search(IncidenceGraph& g, |
|---|
| 172 | typename graph_traits<IncidenceGraph>::vertex_descriptor s, |
|---|
| 173 | Buffer& Q, BFSVisitor vis, ColorMap color) |
|---|
| 174 | { |
|---|
| 175 | typedef typename graph_traits<IncidenceGraph>::vertex_descriptor Vertex; |
|---|
| 176 | function_requires< ReadWritePropertyMap<ColorMap, Vertex> >(); |
|---|
| 177 | ... |
|---|
| 178 | } |
|---|
| 179 | </pre> |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | As an example of using <tt>BOOST_CLASS_REQUIRE</tt> we look at a concept |
|---|
| 183 | check that could be added to <tt>std::vector</tt>. One requirement |
|---|
| 184 | that is placed on the element type is that it must be <a |
|---|
| 185 | href="http://www.sgi.com/tech/stl/Assignable.html">Assignable</a>. |
|---|
| 186 | We can check this by inserting |
|---|
| 187 | <tt>class_requires<AssignableConcept<T> ></tt> at the top |
|---|
| 188 | of the definition for <tt>std::vector</tt>. |
|---|
| 189 | |
|---|
| 190 | <pre> |
|---|
| 191 | namespace std { |
|---|
| 192 | template <class T> |
|---|
| 193 | struct vector { |
|---|
| 194 | BOOST_CLASS_REQUIRE(T, boost, AssignableConcept); |
|---|
| 195 | ... |
|---|
| 196 | }; |
|---|
| 197 | } |
|---|
| 198 | </pre> |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | Although the concept checks are designed for use by generic library |
|---|
| 202 | implementors, they can also be useful to end users. Sometimes one may |
|---|
| 203 | not be sure whether some type models a particular concept. This can |
|---|
| 204 | easily be checked by creating a small program and using |
|---|
| 205 | <tt>function_requires()</tt> with the type and concept in question. |
|---|
| 206 | The file <a |
|---|
| 207 | href="./stl_concept_check.cpp"><tt>stl_concept_checks.cpp</tt></a> |
|---|
| 208 | gives and example of applying the concept checks to STL containers. |
|---|
| 209 | |
|---|
| 210 | <p> |
|---|
| 211 | <a href="./concept_check.htm">Prev: Concept Checking Introduction</a> <br> |
|---|
| 212 | <a href="./creating_concepts.htm">Next: Creating Concept Checking Classes</a> |
|---|
| 213 | |
|---|
| 214 | <br> |
|---|
| 215 | <HR> |
|---|
| 216 | <TABLE> |
|---|
| 217 | <TR valign=top> |
|---|
| 218 | <TD nowrap>Copyright © 2000</TD><TD> |
|---|
| 219 | <A HREF="../../people/jeremy_siek.htm">Jeremy Siek</A>(<A |
|---|
| 220 | HREF="mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</A>) |
|---|
| 221 | Andrew Lumsdaine</A>(<A HREF="mailto:lums@osl.iu.edu">lums@osl.iu.edu</A>) |
|---|
| 222 | </TD></TR></TABLE> |
|---|
| 223 | |
|---|
| 224 | </BODY> |
|---|
| 225 | </HTML> |
|---|