concept_covering.htm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. <title>Concept Covering and Archetypes</title>
  12. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  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="concept-covering" id="concept-covering">Concept Covering and
  20. Archetypes</a></h2>
  21. <p>We have discussed how it is important to select the minimal requirements
  22. (concepts) for the inputs to a component, but it is equally important to
  23. verify that the chosen concepts <i>cover</i> the algorithm. That is, any
  24. possible user error should be caught by the concept checks and not let slip
  25. through. Concept coverage can be verified through the use of <i>archetype
  26. classes</i>. An archetype class is an exact implementation of the interface
  27. associated with a particular concept. The run-time behavior of the
  28. archetype class is not important, the functions can be left empty. A simple
  29. test program can then be compiled with the archetype classes as the inputs
  30. to the component. If the program compiles then one can be sure that the
  31. concepts cover the component. The following code shows the archetype class
  32. for the <a href="http://www.boost.org/sgi/stl/InputIterator.html">Input
  33. Iterator</a> concept. Some care must be taken to ensure that the archetype
  34. is an exact match to the concept. For example, the concept states that the
  35. return type of <tt>operator*()</tt> must be convertible to the value type.
  36. It does not state the more stringent requirement that the return type be
  37. <tt>T&amp;</tt> or <tt>const T&amp;</tt>. That means it would be a mistake
  38. to use <tt>T&amp;</tt> or <tt>const T&amp;</tt> for the return type of the
  39. archetype class. The correct approach is to create an artificial return
  40. type that is convertible to <tt>T</tt>, as we have done here with
  41. <tt>reference</tt>. The validity of the archetype class test is completely
  42. dependent on it being an exact match with the concept, which must be
  43. verified by careful (manual) inspection.</p>
  44. <pre>
  45. template &lt;class T&gt;
  46. class input_iterator_archetype
  47. {
  48. private:
  49. typedef input_iterator_archetype self;
  50. public:
  51. typedef std::input_iterator_tag iterator_category;
  52. typedef T value_type;
  53. struct reference {
  54. operator const value_type&amp;() const { return static_object&lt;T&gt;::get(); }
  55. };
  56. typedef const T* pointer;
  57. typedef std::ptrdiff_t difference_type;
  58. self&amp; operator=(const self&amp;) { return *this; }
  59. bool operator==(const self&amp;) const { return true; }
  60. bool operator!=(const self&amp;) const { return true; }
  61. reference operator*() const { return reference(); }
  62. self&amp; operator++() { return *this; }
  63. self operator++(int) { return *this; }
  64. };
  65. </pre>
  66. <p>Generic algorithms are often tested by being instantiated with a number
  67. of common input types. For example, one might apply
  68. <tt>std::stable_sort()</tt> with basic pointer types as the iterators.
  69. Though appropriate for testing the run-time behavior of the algorithm, this
  70. is not helpful for ensuring concept coverage because C++ types never match
  71. particular concepts exactly. Instead, they often provide more than the
  72. minimal functionality required by any one concept. Even though the function
  73. template has concept checks, and compiles with a given type, the checks may
  74. still fall short of covering all the functionality that is actually used.
  75. This is why it is important to compile with archetype classes in addition
  76. to testing with common input types.</p>
  77. <p>The following is an excerpt from <a href=
  78. "./stl_concept_covering.cpp"><tt>stl_concept_covering.cpp</tt></a> that
  79. shows how archetypes can be used to check the requirement documentation for
  80. <a href=
  81. "http://www.boost.org/sgi/stl/stable_sort.html"><tt>std::stable_sort()</tt></a>.
  82. In this case, it looks like the <a href=
  83. "../utility/CopyConstructible.html">CopyConstructible</a> and <a href=
  84. "../utility/Assignable.html">Assignable</a> requirements were forgotten in
  85. the SGI STL documentation (try removing those archetypes). The Boost
  86. archetype classes have been designed so that they can be layered. In this
  87. example the value type of the iterator is composed out of three archetypes.
  88. In the <a href="reference.htm#basic-archetype">archetype class
  89. reference</a>, template parameters named <tt>Base</tt> indicate where the
  90. layered archetype paradigm can be used.</p>
  91. <pre>
  92. {
  93. typedef less_than_comparable_archetype&lt;
  94. sgi_assignable_archetype&lt;&gt; &gt; ValueType;
  95. random_access_iterator_archetype&lt;ValueType&gt; ri;
  96. std::stable_sort(ri, ri);
  97. }
  98. </pre>
  99. <p><a href="./prog_with_concepts.htm">Next: Programming with
  100. Concepts</a><br />
  101. <a href="./creating_concepts.htm">Prev: Creating Concept Checking
  102. Classes</a><br />
  103. <hr />
  104. <table>
  105. <tr valign="top">
  106. <td nowrap="nowrap">Copyright &copy; 2000</td>
  107. <td><a href="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</a>(<a href=
  108. "mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</a>) Andrew
  109. Lumsdaine(<a href="mailto:lums@osl.iu.edu">lums@osl.iu.edu</a>),
  110. 2007 <a href="mailto:dave@boost-consulting.com">David Abrahams</a>.
  111. </tr>
  112. </table>
  113. </body>
  114. </html>