using_concept_check.htm 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <!-- Copyright (c) Jeremy Siek and Andrew Lumsdaine 2000 -->
  5. <!-- Distributed under the Boost -->
  6. <!-- Software License, Version 1.0. (See accompanying -->
  7. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  8. <head>
  9. <meta name="generator" content=
  10. "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org" />
  11. <meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
  12. <title>Using Concept Checks</title>
  13. <link rel="stylesheet" href="../../rst.css" type="text/css" />
  14. </head>
  15. <body bgcolor="#FFFFFF" link="#0000EE" text="#000000" vlink="#551A8B" alink=
  16. "#FF0000">
  17. <img src="../../boost.png" alt="C++ Boost" width="277" height=
  18. "86" /><br clear="none" />
  19. <h2><a name="using-concept-checks" id="using-concept-checks">Using Concept
  20. Checks</a></h2>
  21. <p>For each concept there is a concept checking class template that can be
  22. used to make sure that a given type (or set of types) models the concept.
  23. The Boost Concept Checking Library (BCCL) includes concept checking class
  24. templates for all of the concepts used in the C++ standard library and a
  25. few more. See the <a href="./reference.htm">Reference</a> section for a
  26. complete list. In addition, other boost libraries come with concept
  27. checking classes for the concepts that are particular to those libraries.
  28. For example, there are <a href="../graph/doc/graph_concepts.html">graph
  29. concepts</a> and <a href="../property_map/doc/property_map.html">property map
  30. concepts</a>. Also, whenever <b>anyone</b> writing function templates needs
  31. to express requirements that are not yet stated by an existing concept, a
  32. new concept checking class should be created. How to do this is explained
  33. in <a href="./creating_concepts.htm">Creating Concept Checking
  34. Classes</a>.</p>
  35. <p>An example of a concept checking class from the BCCL is the
  36. <tt>EqualityComparableConcept</tt> class. The class corresponds to the
  37. EqualityComparable requirements described in 20.1.1 of the C++ Standard,
  38. and to the <a href=
  39. "http://www.boost.org/sgi/stl/EqualityComparable.html">EqualityComparable</a>
  40. concept documented in the SGI STL.</p>
  41. <pre>
  42. template &lt;class T&gt;
  43. struct EqualityComparable;
  44. </pre>
  45. <p>The template argument is the type to be checked. That is, the purpose of
  46. <tt>EqualityComparable&lt;<em>T</em>&gt;</tt> is to make sure that
  47. <tt><em>T</em></tt> models the EqualityComparable concept.</p>
  48. <h4><tt>BOOST_CONCEPT_ASSERT()</tt></h4>
  49. <p>The most versatile way of checking concept requirements is to use the
  50. <code>BOOST_CONCEPT_ASSERT()</code> macro. You can use this macro at any
  51. scope, by passing a concept checking template specialization enclosed in
  52. parentheses. <strong>Note:</strong> that means invocations of
  53. <code>BOOST_CONCEPT_ASSERT</code> will appear to use <strong>double
  54. parentheses</strong>.</p>
  55. <pre>
  56. <font color="green">// In my library:</font>
  57. template &lt;class T&gt;
  58. void generic_library_function(T x)
  59. {
  60. BOOST_CONCEPT_ASSERT<strong>((</strong>EqualityComparable&lt;T&gt;<strong>))</strong>;
  61. <font color="green">// ...</font>
  62. };
  63. template &lt;class It&gt;
  64. class generic_library_class
  65. {
  66. BOOST_CONCEPT_ASSERT<strong>((</strong>RandomAccessIterator&lt;It&gt;<strong>))</strong>;
  67. <font color="green">// ...</font>
  68. };
  69. <font color="green">// In the user's code:</font>
  70. class foo {
  71. <font color="green">//... </font>
  72. };
  73. int main() {
  74. foo x;
  75. generic_library_function(x);
  76. generic_library_class&lt;std::vector&lt;char&gt;::iterator&gt; y;
  77. <font color="green">//...</font>
  78. }
  79. </pre>
  80. <h4><tt>BOOST_CONCEPT_REQUIRES</tt></h4>
  81. <p>One of the nice things about the proposed C++0x <a href=
  82. "http://www.generic-programming.org/languages/conceptcpp/tutorial">syntax
  83. for declaring concept constrained function templates</a> is the way that
  84. constraints are part of the function <em>declaration</em>, so clients will
  85. see them. <code>BOOST_CONCEPT_ASSERT</code> can only express constraints
  86. within the function template definition, which hides the constraint in the
  87. function body. Aside from the loss of a self-documenting interface,
  88. asserting conformance only in the function body can undesirably delay
  89. checking if the function is explicitly instantiated in a different
  90. translation unit from the one in which it is called, or if the compiler
  91. does link-time instantiation.</p>
  92. <p>The <tt>BOOST_CONCEPT_REQUIRES</tt> macro can be used in a function
  93. template declaration to check whether some type models a concept. It
  94. accepts two arguments, a <strong>list of constraints</strong>, and the
  95. function template's return type. The list of constraints takes the form of
  96. a sequence of adjacent concept checking template specializations,
  97. <strong>in double parentheses</strong>, and the function's return type must
  98. also be parenthesized. For example, the standard <code>stable_sort</code>
  99. algorithm might be declared as follows: </p>
  100. <pre>
  101. template &lt;class RanIter&gt;
  102. BOOST_CONCEPT_REQUIRES(
  103. ((Mutable_RandomAccessIterator&lt;RanIter&gt;))
  104. ((LessThanComparable&lt;typename Mutable_RandomAccessIterator&lt;RanIter&gt;::value_type&gt;)),
  105. (void)) <font color="green">// return type</font>
  106. stable_sort(RanIter,RanIter);
  107. </pre>
  108. <p>Note that the algorithm requires that the value type of the iterator be
  109. LessThanComparable, and it accesses that value type through the
  110. <code>Mutable_RandomAccessIterator</code> concept checking template. In
  111. general, the Boost concept checking classes expose associated types as
  112. nested member typedefs so that you can use this syntax, which mimics the
  113. approach used in the concept support proposed for the next version of
  114. C++.</p>
  115. <h4>Multi-Type Concepts</h4>
  116. <p>Some concepts deal with more than one type. In this case the
  117. corresponding concept checking class will have multiple template
  118. parameters. The following example shows how <tt>BOOST_CONCEPT_REQUIRES</tt>
  119. is used with the <a href=
  120. "../property_map/doc/ReadWritePropertyMap.html">ReadWritePropertyMap</a>
  121. concept, which takes two type parameters: a property map and the key type
  122. for the map.</p>
  123. <pre>
  124. template &lt;class G, class Buffer, class BFSVisitor,
  125. class ColorMap&gt;
  126. BOOST_CONCEPT_REQUIRES(
  127. ((ReadWritePropertyMap&lt;ColorMap, typename IncidenceGraph&lt;G&gt;::vertex_descriptor&gt;)),
  128. (void)) <font color="green">// return type</font>
  129. breadth_first_search(G&amp; g,
  130. typename graph_traits&lt;IncidenceGraph&gt;::vertex_descriptor s,
  131. Buffer&amp; Q, BFSVisitor vis, ColorMap color)
  132. {
  133. typedef typename IncidenceGraph&lt;G&gt;::vertex_descriptor Vertex;
  134. ...
  135. }
  136. </pre>
  137. <p>Although concept checks are designed for use by generic library
  138. implementors, they can also be useful to end users. Sometimes one may not
  139. be sure whether some type models a particular concept. The syntactic
  140. requirements, at least, can easily be checked by creating a small program
  141. and using <tt>BOOST_CONCEPT_ASSERT</tt> with the type and concept in
  142. question. For example:</p>
  143. <pre>
  144. <font color=
  145. "green">// Make sure list&lt;int&gt; has bidirectional iterators.</font>
  146. BOOST_CONCEPT_ASSERT((BidirectionalIterator&lt;std::list&lt;int&gt;::iterator&gt;));
  147. </pre>
  148. <p><a href="./concept_check.htm">Prev: Concept Checking
  149. Introduction</a><br />
  150. <a href="./creating_concepts.htm">Next: Creating Concept Checking
  151. Classes</a><br /></p>
  152. <hr />
  153. <table>
  154. <tr valign="top">
  155. <td nowrap="nowrap">Copyright &copy; 2000</td>
  156. <td><a href="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</a>(<a href=
  157. "mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</a>) Andrew
  158. Lumsdaine(<a href="mailto:lums@osl.iu.edu">lums@osl.iu.edu</a>), 2007
  159. <a href="mailto:dave@boost-consulting.com">David Abrahams</a>.</td>
  160. </tr>
  161. </table>
  162. </body>
  163. </html>