creating_concepts.htm 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
  10. <meta name="generator" content=
  11. "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org" />
  12. <title>Creating Concept Checking Classes</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="creating-concept-checks" id="creating-concept-checks">Creating
  20. Concept Checking Classes</a></h2>
  21. <p>As an example of how to create a concept checking class template, we
  22. look at how to create the corresponding checks for the <a href=
  23. "http://www.boost.org/sgi/stl/InputIterator.html">InputIterator</a> concept.
  24. The complete definition is here:</p>
  25. <pre>
  26. template &lt;class X&gt;
  27. struct InputIterator
  28. : Assignable&lt;X&gt;, EqualityComparable&lt;X&gt;
  29. {
  30. private:
  31. typedef std::iterator_traits&lt;X&gt; t;
  32. public:
  33. typedef typename t::value_type value_type;
  34. typedef typename t::difference_type difference_type;
  35. typedef typename t::reference reference;
  36. typedef typename t::pointer pointer;
  37. typedef typename t::iterator_category iterator_category;
  38. BOOST_CONCEPT_ASSERT((SignedInteger&lt;difference_type&gt;));
  39. BOOST_CONCEPT_ASSERT((Convertible&lt;iterator_category, std::input_iterator_tag&gt;));
  40. BOOST_CONCEPT_USAGE(InputIterator)
  41. {
  42. X j(i); <font color=
  43. "green">// require copy construction</font>
  44. same_type(*i++,v); <font color=
  45. "green">// require postincrement-dereference returning value_type</font>
  46. X&amp; x = ++j; <font color=
  47. "green">// require preincrement returning X&amp;</font>
  48. }
  49. private:
  50. X i;
  51. value_type v;
  52. <font color=
  53. "green">// Type deduction will fail unless the arguments have the same type.</font>
  54. template &lt;typename T&gt;
  55. void same_type(T const&amp;, T const&amp;);
  56. };
  57. </pre>
  58. <h3>Walkthrough</h3>
  59. <p>First, as a convention we name the concept checking class after the
  60. concept. Next, since InputIterator is a refinement of Assignable and
  61. EqualityComparable, we derive its concept checking class from the checking
  62. classes for those other concepts. The library will automatically check for
  63. conformance to Assignable and EqualityComparable whenever it checks the
  64. InputIterator concept.</p>
  65. <p>Next, we declare the concept's <a href=
  66. "http://www.boost.org/more/generic_programming.html#associated_type">associated types</a>
  67. as member typedefs. The associated difference type is required to be a
  68. signed integer, and the iterator category has to be convertible to
  69. std::input_iterator_tag, so we assert those relationships. The syntax for
  70. accessing associated types through the concept-checking template mirrors
  71. the <a href=
  72. "http://www.generic-programming.org/languages/conceptcpp/">proposed</a>
  73. syntax for associated type access in C++0x Finally, we use the
  74. <code>BOOST_CONCEPT_USAGE</code> macro to declare the function that
  75. exercises all the concept's valid expressions. Note that at this point you
  76. may sometimes need to be a little creative: for example, to check that
  77. <code>*i++</code> returns the iterator's value type, we pass both values to
  78. the <code>same_type</code> member function template, which requires both
  79. arguments to have the same type, modulo references and cv-qualification.
  80. It's an imperfect check, but it's better than nothing.</p>
  81. <h3>Values for Usage Patterns Should Be Data Members</h3>
  82. <p>You may be wondering why we declared <code>i</code> and <code>v</code>
  83. as data members in the example above. Why didn't we simply write the
  84. following?</p>
  85. <pre>
  86. BOOST_CONCEPT_USAGE(InputIterator)
  87. {
  88. X i; <font color=
  89. "green">// create the values we need</font>
  90. value_type v;
  91. X j(i); <font color=
  92. "green">// require copy construction</font>
  93. same_type(*i++,v); <font color=
  94. "green">// require postincrement-dereference returning value_type</font>
  95. X&amp; x = ++j; <font color=
  96. "green">// require preincrement returning X&amp;</font>
  97. }
  98. </pre>
  99. <p>Unfortunately, that code wouldn't have worked out so well, because it
  100. unintentionally imposes the requirement that <code>X</code> and its value
  101. type are both default-constructible. On the other hand, since instances of
  102. the <code>InputIterator</code> template will never be constructed, the
  103. compiler never has to check how its data members will be constructed (C++
  104. Standard Section 14.7.1 9). For that reason you should <strong>always
  105. declare values needed for usage patterns as data members</strong>.</p>
  106. <p>These sorts of errors in concept definitions can be detected by the use
  107. of <a href="concept_covering.htm">Concept Archetypes</a>, but it's always
  108. better to avoid them pre-emptively.</p>
  109. <h3>Similarity to Proposed C++0x Language Support for Concepts</h3>
  110. <p>This library's syntaxes for concept refinement and for access of
  111. associated types mirrors the corresponding <a href=
  112. "http://www.generic-programming.org/languages/conceptcpp/">proposed</a>
  113. syntaxes in C++0x. However, C++0x will use
  114. “signatures” rather than usage patterns to
  115. describe the valid operations on types participating in a concept, so when
  116. converting your concept checking classes into language-supported concepts,
  117. you'll need to translate your usage function into a series of
  118. signatures.</p>
  119. <p><a href="./concept_covering.htm">Next: Concept Covering and
  120. Archetypes</a><br />
  121. <a href="./using_concept_check.htm">Prev: Using Concept
  122. Checks</a><br /></p>
  123. <hr />
  124. <table>
  125. <tr valign="top">
  126. <td nowrap="nowrap">Copyright &copy; 2000</td>
  127. <td><a href="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</a>(<a href=
  128. "mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</a>) Andrew
  129. Lumsdaine(<a href="mailto:lums@osl.iu.edu">lums@osl.iu.edu</a>),
  130. 2007 <a href="mailto:dave@boost-consulting.com">David Abrahams</a>.
  131. </tr>
  132. </table>
  133. </body>
  134. </html>