prog_with_concepts.htm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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>Programming With Concepts</title>
  12. <meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
  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="programming-with-concepts" id=
  20. "programming-with-concepts">Programming with Concepts</a></h2>
  21. <p>The process of deciding how to group requirements into concepts and
  22. deciding which concepts to use in each algorithm is perhaps the most
  23. difficult (yet most important) part of building a generic library. A
  24. guiding principle to use during this process is one we call the
  25. <i>requirement minimization principle</i>.</p>
  26. <p><b>Requirement Minimization Principle:</b> Minimize the requirements on
  27. the input parameters of a component to increase its reusability.</p>
  28. <p>There is natural tension in this statement. By definition, the input
  29. parameters must be used by the component in order for the component to
  30. accomplish its task (by ``component'' we mean a function or class
  31. template). The challenge then is to implement the component in such a way
  32. that makes the fewest assumptions (the minimum requirements) about the
  33. inputs while still accomplishing the task.</p>
  34. <p>The traditional notions of <i>abstraction</i> tie in directly to the
  35. idea of minimal requirements. The more abstract the input, the fewer the
  36. requirements. Thus, concepts are simply the embodiment of generic abstract
  37. data types in C++ template programming.</p>
  38. <p>When designing the concepts for some problem domain it is important to
  39. keep in mind their purpose, namely to express the requirements for the
  40. input to the components. With respect to the requirement minimization
  41. principle, this means we want to minimize concepts.
  42. <!-- the following discussion does not match the Standard definition
  43. of LessThanComparable and needs to be changed -Jeremy
  44. <p>
  45. It is important to note, however, that
  46. minimizing concepts does not mean simply
  47. reducing the number of valid expressions
  48. in the concept.
  49. For example, the
  50. <tt>std::stable_sort()</tt> function requires that the value type of
  51. the iterator be <a
  52. href="http://www.boost.org/sgi/stl/LessThanComparable.html">
  53. LessThanComparable</a>, which not only
  54. includes <tt>operator&lt;()</tt>, but also <tt>operator&gt;()</tt>,
  55. <tt>operator&lt;=()</tt>, and <tt>operator&gt;=()</tt>.
  56. It turns out that <tt>std::stable_sort()</tt> only uses
  57. <tt>operator&lt;()</tt>. The question then arises: should
  58. <tt>std::stable_sort()</tt> be specified in terms of the concept
  59. <a
  60. href="http://www.boost.org/sgi/stl/LessThanComparable.html">
  61. LessThanComparable</a> or in terms of a concept that only
  62. requires <tt>operator&lt;()</tt>?
  63. <p>
  64. We remark first that the use of <a
  65. href="http://www.boost.org/sgi/stl/LessThanComparable.html">
  66. LessThanComparable</a> does not really violate the requirement
  67. minimization principle because all of the other operators can be
  68. trivially implemented in terms of <tt>operator&lt;()</tt>. By
  69. ``trivial'' we mean one line of code and a constant run-time cost.
  70. More fundamentally, however, the use of <a
  71. href="http://www.boost.org/sgi/stl/LessThanComparable.html">
  72. LessThanComparable</a> does not violate the requirement minimization
  73. principle because all of the comparison operators (<tt>&lt;</tt>,
  74. <tt>></tt>, <tt><=</tt>, <tt>>=</tt>) are conceptually equivalent (in
  75. a mathematical sense). Adding conceptually equivalent valid
  76. expressions is not a violation of the requirement minimization
  77. principle because no new semantics are being added === only new
  78. syntax. The added syntax increases re-usability.
  79. <p>
  80. For example,
  81. the
  82. maintainer of the <tt>std::stable_sort()</tt> may some day change the
  83. implementation in places to use <tt>operator>()</tt> instead of
  84. <tt>operator<()</tt>, since, after all, they are equivalent. Since the
  85. requirements are part of the public interface, such a change could
  86. potentially break client code. If instead
  87. <a
  88. href="http://www.boost.org/sgi/stl/LessThanComparable.html">
  89. LessThanComparable</a> is given as the requirement for
  90. <tt>std::stable_sort()</tt>, then the maintainer is given a reasonable
  91. amount of flexibility within which to work.
  92. --></p>
  93. <p>Minimality in concepts is a property associated with the underlying
  94. semantics of the problem domain being represented. In the problem domain of
  95. basic containers, requiring traversal in a single direction is a smaller
  96. requirement than requiring traversal in both directions (hence the
  97. distinction between <a href=
  98. "http://www.boost.org/sgi/stl/ForwardIterator.html">ForwardIterator</a> and
  99. <a href=
  100. "http://www.boost.org/sgi/stl/BidirectionalIterator.html">BidirectionalIterator</a>).
  101. The semantic difference can be easily seen in the difference between the
  102. set of concrete data structures that have forward iterators versus the set
  103. that has bidirectional iterators. For example, singly-linked lists would
  104. fall in the set of data structures having forward iterators, but not
  105. bidirectional iterators. In addition, the set of algorithms that one can
  106. implement using only forward iterators is quite different than the set that
  107. can be implemented with bidirectional iterators. Because of this, it is
  108. important to factor families of requirements into rather fine-grained
  109. concepts. For example, the requirements for iterators are factored into the
  110. six STL iterator concepts (trivial, output, input, forward, bidirectional,
  111. and random access).</p>
  112. <p><a href="./implementation.htm">Next: Implementation</a><br />
  113. <a href="./concept_covering.htm">Prev: Concept Covering and
  114. Archetypes</a><br /></p>
  115. <hr />
  116. <table>
  117. <tr valign="top">
  118. <td nowrap="nowrap">Copyright &copy; 2000</td>
  119. <td><a href="http://www.boost.org/people/jeremy_siek.htm">Jeremy Siek</a>(<a href=
  120. "mailto:jsiek@osl.iu.edu">jsiek@osl.iu.edu</a>) Andrew
  121. Lumsdaine(<a href="mailto:lums@osl.iu.edu">lums@osl.iu.edu</a>),
  122. 2007 <a href="mailto:dave@boost-consulting.com">David Abrahams</a>.
  123. </tr>
  124. </table>
  125. </body>
  126. </html>