generic_programming.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
  2. <meta name="generator" content="HTML Tidy, see www.w3.org">
  3. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  4. <meta name="GENERATOR" content="Microsoft FrontPage 4.0">
  5. <meta name="ProgId" content="FrontPage.Editor.Document">
  6. <title>Generic Programming Techniques</title>
  7. <img src="../c++boost.gif" alt="c++boost.gif (8819 bytes)" align="center"
  8. width="277" height="86">
  9. <h1>Generic Programming Techniques</h1>
  10. <p>This is an incomplete survey of some of the generic programming
  11. techniques used in the <a href="../index.htm">boost</a> libraries.
  12. <h2>Table of Contents</h2>
  13. <ul>
  14. <li><a href="#introduction">Introduction</a>
  15. <li><a href="#concept">The Anatomy of a Concept</a>
  16. <li><a href="#traits">Traits</a>
  17. <li><a href="#tag_dispatching">Tag Dispatching</a>
  18. <li><a href="#adaptors">Adaptors</a>
  19. <li><a href="#type_generator">Type Generators</a>
  20. <li><a href="#object_generator">Object Generators</a>
  21. <li><a href="#policy">Policy Classes</a>
  22. </ul>
  23. <h2><a name="introduction">Introduction</a></h2>
  24. <p>Generic programming is about generalizing software components so that
  25. they can be easily reused in a wide variety of situations. In C++, class
  26. and function templates are particularly effective mechanisms for generic
  27. programming because they make the generalization possible without
  28. sacrificing efficiency.
  29. <p>As a simple example of generic programming, we will look at how one
  30. might generalize the <tt>memcpy()</tt> function of the C standard library.
  31. An implementation of <tt>memcpy()</tt> might look like the following:
  32. <br>
  33. <br>
  34. <blockquote>
  35. <pre>
  36. void* memcpy(void* region1, const void* region2, size_t n)
  37. {
  38. const char* first = (const char*)region2;
  39. const char* last = ((const char*)region2) + n;
  40. char* result = (char*)region1;
  41. while (first != last)
  42. *result++ = *first++;
  43. return result;
  44. }
  45. </pre>
  46. </blockquote>
  47. The <tt>memcpy()</tt> function is already generalized to some extent by the
  48. use of <tt>void*</tt> so that the function can be used to copy arrays of
  49. different kinds of data. But what if the data we would like to copy is not
  50. in an array? Perhaps it is in a linked list. Can we generalize the notion
  51. of copy to any sequence of elements? Looking at the body of
  52. <tt>memcpy()</tt>, the function's <b><i>minimal requirements</i></b> are
  53. that it needs to to <i>traverse</i> through the sequence using some sort of
  54. pointer, <i>access</i> elements pointed to, <i>write</i> the elements to
  55. the destination, and <i>compare</i> pointers to know when to stop. The C++
  56. standard library groups requirements such as these into
  57. <b><i>concepts</i></b>, in this case the <a href=
  58. "http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a> concept
  59. (for <tt>region2</tt>) and the <a href=
  60. "http://www.sgi.com/tech/stl/OutputIterator.html">Output Iterator</a>
  61. concept (for <tt>region1</tt>).
  62. <p>If we rewrite the <tt>memcpy()</tt> as a function template, and use the
  63. <a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>
  64. and <a href="http://www.sgi.com/tech/stl/OutputIterator.html">Output
  65. Iterator</a> concepts to describe the requirements on the template
  66. parameters, we can implement a highly reusable <tt>copy()</tt> function in
  67. the following way:
  68. <br>
  69. <br>
  70. <blockquote>
  71. <pre>
  72. template &lt;typename InputIterator, typename OutputIterator&gt;
  73. OutputIterator
  74. copy(InputIterator first, InputIterator last, OutputIterator result)
  75. {
  76. while (first != last)
  77. *result++ = *first++;
  78. return result;
  79. }
  80. </pre>
  81. </blockquote>
  82. <p>Using the generic <tt>copy()</tt> function, we can now copy elements
  83. from any kind of sequence, including a linked list that exports iterators
  84. such as <tt>std::<a href=
  85. "http://www.sgi.com/tech/stl/List.html">list</a></tt>.
  86. <br>
  87. <br>
  88. <blockquote>
  89. <pre>
  90. #include &lt;list&gt;
  91. #include &lt;vector&gt;
  92. #include &lt;iostream&gt;
  93. int main()
  94. {
  95. const int N = 3;
  96. std::vector&lt;int&gt; region1(N);
  97. std::list&lt;int&gt; region2;
  98. region2.push_back(1);
  99. region2.push_back(0);
  100. region2.push_back(3);
  101. std::copy(region2.begin(), region2.end(), region1.begin());
  102. for (int i = 0; i &lt; N; ++i)
  103. std::cout &lt;&lt; region1[i] &lt;&lt; " ";
  104. std::cout &lt;&lt; std::endl;
  105. }
  106. </pre>
  107. </blockquote>
  108. <h2><a name="concept">Anatomy of a Concept</a></h2>
  109. A <b><i>concept</i></b> is a set requirements, where the requirements
  110. consist of valid expressions, associated types, invariants, and complexity
  111. guarantees. A type that satisfies the set of requirements is said to
  112. <b><i>model</i></b> the concept. A concept can extend the requirements of
  113. another concept, which is called <b><i>refinement</i></b>.
  114. <ul>
  115. <li><a name="valid_expression"><b>Valid Expressions</b></a> are C++
  116. expressions which must compile successfully for the objects involved in
  117. the expression to be considered <i>models</i> of the concept.
  118. <li><a name="associated_type"><b>Associated Types</b></a> are types that
  119. are related to the modeling type in that they participate in one or more
  120. of the valid expressions. Typically associated types can be accessed
  121. either through typedefs nested within a class definition for the modeling
  122. type, or they are accessed through a <a href="#traits">traits class</a>.
  123. <li><b>Invariants</b> are run-time characteristics of the objects that
  124. must always be true, that is, the functions involving the objects must
  125. preserve these characteristics. The invariants often take the form of
  126. pre-conditions and post-conditions.
  127. <li><b>Complexity Guarantees</b> are maximum limits on how long the
  128. execution of one of the valid expressions will take, or how much of
  129. various resources its computation will use.
  130. </ul>
  131. <p>The concepts used in the C++ Standard Library are documented at the <a
  132. href="http://www.sgi.com/tech/stl/table_of_contents.html">SGI STL site</a>.
  133. <h2><a name="traits">Traits</a></h2>
  134. <p>A traits class provides a way of associating information with a
  135. compile-time entity (a type, integral constant, or address). For example,
  136. the class template <tt><a href=
  137. "http://www.sgi.com/tech/stl/iterator_traits.html">std::iterator_traits&lt;T&gt;</a></tt>
  138. looks something like this:
  139. <blockquote>
  140. <pre>
  141. template &lt;class Iterator&gt;
  142. struct iterator_traits {
  143. typedef ... iterator_category;
  144. typedef ... value_type;
  145. typedef ... difference_type;
  146. typedef ... pointer;
  147. typedef ... reference;
  148. };
  149. </pre>
  150. </blockquote>
  151. The traits' <tt>value_type</tt> gives generic code the type which the
  152. iterator is "pointing at", while the <tt>iterator_category</tt> can be used
  153. to select more efficient algorithms depending on the iterator's
  154. capabilities.
  155. <p>A key feature of traits templates is that they're <i>non-intrusive</i>:
  156. they allow us to associate information with arbitrary types, including
  157. built-in types and types defined in third-party libraries, Normally, traits
  158. are specified for a particular type by (partially) specializing the traits
  159. template.
  160. <p>For an in-depth description of <tt>std::iterator_traits</tt>, see <a
  161. href="http://www.sgi.com/tech/stl/iterator_traits.html">this page</a>
  162. provided by SGI. Another very different expression of the traits idiom in
  163. the standard is <tt>std::numeric_limits&lt;T&gt;</tt> which provides
  164. constants describing the range and capabilities of numeric types.
  165. <h2><a name="tag_dispatching">Tag Dispatching</a></h2>
  166. <p>A technique that often goes hand in hand with traits classes is tag
  167. dispatching, which is a way of using function overloading to dispatch based
  168. on properties of a type. A good example of this is the implementation of
  169. the <a href=
  170. "http://www.sgi.com/tech/stl/advance.html"><tt>std::advance()</tt></a>
  171. function in the C++ Standard Library, which increments an iterator
  172. <tt>n</tt> times. Depending on the kind of iterator, there are different
  173. optimizations that can be applied in the implementation. If the iterator is
  174. <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">random
  175. access</a> (can jump forward and backward arbitrary distances), then the
  176. <tt>advance()</tt> function can simply be implemented with <tt>i += n</tt>,
  177. and is very efficient: constant time. If the iterator is <a href=
  178. "http://www.sgi.com/tech/stl/BidirectionalIterator.html">bidirectional</a>,
  179. then it makes sense for <tt>n</tt> to be negative, we can decrement the
  180. iterator <tt>n</tt> times.
  181. <p>The relation between tag dispatching and traits classes is that the
  182. property used for dispatching (in this case the <tt>iterator_category</tt>)
  183. is accessed through a traits class. The main <tt>advance()</tt> function
  184. uses the <a href=
  185. "http://www.sgi.com/tech/stl/iterator_traits.html"><tt>iterator_traits</tt></a>
  186. class to get the <tt>iterator_category</tt>. It then makes a call the the
  187. overloaded <tt>advance_dispatch()</tt> function. The appropriate
  188. <tt>advance_dispatch()</tt> is selected by the compiler based on whatever
  189. type the <tt>iterator_category</tt> resolves to, either <a href=
  190. "http://www.sgi.com/tech/stl/input_iterator_tag.html"><tt>input_iterator_tag</tt></a>,
  191. <a href=
  192. "http://www.sgi.com/tech/stl/bidirectional_iterator_tag.html"><tt>bidirectional_iterator_tag</tt></a>,
  193. or <a href=
  194. "http://www.sgi.com/tech/stl/random_access_iterator_tag.html"><tt>random_access_iterator_tag</tt></a>.
  195. A <b><i>tag</i></b> is simply a class whose only purpose is to convey some
  196. property for use in tag dispatching and similar techniques. Refer to <a
  197. href="http://www.sgi.com/tech/stl/iterator_tags.html">this page</a> for a
  198. more detailed description of iterator tags.
  199. <blockquote>
  200. <pre>
  201. namespace std {
  202. struct input_iterator_tag { };
  203. struct bidirectional_iterator_tag { };
  204. struct random_access_iterator_tag { };
  205. namespace detail {
  206. template &lt;class InputIterator, class Distance&gt;
  207. void advance_dispatch(InputIterator&amp; i, Distance n, <b>input_iterator_tag</b>) {
  208. while (n--) ++i;
  209. }
  210. template &lt;class BidirectionalIterator, class Distance&gt;
  211. void advance_dispatch(BidirectionalIterator&amp; i, Distance n,
  212. <b>bidirectional_iterator_tag</b>) {
  213. if (n &gt;= 0)
  214. while (n--) ++i;
  215. else
  216. while (n++) --i;
  217. }
  218. template &lt;class RandomAccessIterator, class Distance&gt;
  219. void advance_dispatch(RandomAccessIterator&amp; i, Distance n,
  220. <b>random_access_iterator_tag</b>) {
  221. i += n;
  222. }
  223. }
  224. template &lt;class InputIterator, class Distance&gt;
  225. void advance(InputIterator&amp; i, Distance n) {
  226. typename <b>iterator_traits&lt;InputIterator&gt;::iterator_category</b> category;
  227. detail::advance_dispatch(i, n, <b>category</b>);
  228. }
  229. }
  230. </pre>
  231. </blockquote>
  232. <h2><a name="adaptors">Adaptors</a></h2>
  233. <p>An <i>adaptor</i> is a class template which builds on another type or
  234. types to provide a new interface or behavioral variant. Examples of
  235. standard adaptors are <a href=
  236. "http://www.sgi.com/tech/stl/ReverseIterator.html">std::reverse_iterator</a>,
  237. which adapts an iterator type by reversing its motion upon
  238. increment/decrement, and <a href=
  239. "http://www.sgi.com/tech/stl/stack.html">std::stack</a>, which adapts a
  240. container to provide a simple stack interface.
  241. <p>A more comprehensive review of the adaptors in the standard can be found
  242. <a href=
  243. "http://www.cs.rpi.edu/~wiseb/xrds/ovp2-3b.html#SECTION00015000000000000000">
  244. here</a>.
  245. <h2><a name="type_generator">Type Generators</a></h2>
  246. <p>A <i>type generator</i> is a template whose only purpose is to
  247. synthesize a new type or types based on its template argument(s)<a href=
  248. "#1">[1]</a>. The generated type is usually expressed as a nested typedef
  249. named, appropriately <tt>type</tt>. A type generator is usually used to
  250. consolidate a complicated type expression into a simple one, as in
  251. <tt>boost::<a href=
  252. "../libs/utility/filter_iterator.hpp">filter_iterator_generator</a></tt>,
  253. which looks something like this:
  254. <blockquote>
  255. <pre>
  256. template &lt;class Predicate, class Iterator,
  257. class Value = <i>complicated default</i>,
  258. class Reference = <i>complicated default</i>,
  259. class Pointer = <i>complicated default</i>,
  260. class Category = <i>complicated default</i>,
  261. class Distance = <i>complicated default</i>
  262. &gt;
  263. struct filter_iterator_generator {
  264. typedef iterator_adaptor&lt;
  265. Iterator,filter_iterator_policies&lt;Predicate,Iterator&gt;,
  266. Value,Reference,Pointer,Category,Distance&gt; <b>type</b>;
  267. };
  268. </pre>
  269. </blockquote>
  270. <p>Now, that's complicated, but producing an adapted filter iterator is
  271. much easier. You can usually just write:
  272. <blockquote>
  273. <pre>
  274. boost::filter_iterator_generator&lt;my_predicate,my_base_iterator&gt;::type
  275. </pre>
  276. </blockquote>
  277. <h2><a name="object_generator">Object Generators</a></h2>
  278. <p>An <i>object generator</i> is a function template whose only purpose is
  279. to construct a new object out of its arguments. Think of it as a kind of
  280. generic constructor. An object generator may be more useful than a plain
  281. constructor when the exact type to be generated is difficult or impossible
  282. to express and the result of the generator can be passed directly to a
  283. function rather than stored in a variable. Most Boost object generators are
  284. named with the prefix "<tt>make_</tt>", after <tt>std::<a href=
  285. "http://www.sgi.com/tech/stl/pair.html">make_pair</a>(const T&amp;, const U&amp;)</tt>.
  286. <p>For example, given:
  287. <blockquote>
  288. <pre>
  289. struct widget {
  290. void tweak(int);
  291. };
  292. std::vector&lt;widget *&gt; widget_ptrs;
  293. </pre>
  294. </blockquote>
  295. By chaining two standard object generators, <tt>std::<a href=
  296. "http://www.dinkumware.com/htm_cpl/functio2.html#bind2nd">bind2nd</a>()</tt>
  297. and <tt>std::<a href=
  298. "http://www.dinkumware.com/htm_cpl/functio2.html#mem_fun">mem_fun</a>()</tt>,
  299. we can easily tweak all widgets:
  300. <blockquote>
  301. <pre>
  302. void tweak_all_widgets1(int arg)
  303. {
  304. for_each(widget_ptrs.begin(), widget_ptrs.end(),
  305. <b>bind2nd</b>(std::<b>mem_fun</b>(&amp;widget::tweak), arg));
  306. }
  307. </pre>
  308. </blockquote>
  309. <p>Without using object generators the example above would look like this:
  310. <blockquote>
  311. <pre>
  312. void tweak_all_widgets2(int arg)
  313. {
  314. for_each(struct_ptrs.begin(), struct_ptrs.end(),
  315. <b>std::binder2nd&lt;std::mem_fun1_t&lt;void, widget, int&gt; &gt;</b>(
  316. std::<b>mem_fun1_t&lt;void, widget, int&gt;</b>(&amp;widget::tweak), arg));
  317. }
  318. </pre>
  319. </blockquote>
  320. <p>As expressions get more complicated the need to reduce the verbosity of
  321. type specification gets more compelling.
  322. <h2><a name="policy">Policy Classes</a></h2>
  323. <p>A policy class is a template parameter used to transmit behavior. An
  324. example from the standard library is <tt>std::<a href=
  325. "http://www.dinkumware.com/htm_cpl/memory.html#allocator">allocator</a></tt>,
  326. which supplies memory management behaviors to standard <a href=
  327. "http://www.sgi.com/tech/stl/Container.html">containers</a>.
  328. <p>Policy classes have been explored in detail by <a href=
  329. "mailto:andrewalex@hotmail.com">Andrei Alexandrescu</a> in <a href=
  330. "http://www.cs.ualberta.ca/~hoover/cmput401/XP-Notes/xp-conf/Papers/7_3_Alexandrescu.pdf">
  331. this paper</a>. He writes:
  332. <blockquote>
  333. <p>Policy classes are implementations of punctual design choices. They
  334. are inherited from, or contained within, other classes. They provide
  335. different strategies under the same syntactic interface. A class using
  336. policies is templated having one template parameter for each policy it
  337. uses. This allows the user to select the policies needed.
  338. <p>The power of policy classes comes from their ability to combine
  339. freely. By combining several policy classes in a template class with
  340. multiple parameters, one achieves combinatorial behaviors with a linear
  341. amount of code.
  342. </blockquote>
  343. <p>Andrei's description of policy classes describe their power as being
  344. derived from their granularity and orthogonality. Boost has probably
  345. diluted the distinction in the <a href=
  346. "../libs/utility/iterator_adaptors.htm">Iterator Adaptors</a> library,
  347. where we transmit all of an adapted iterator's behavior in a single policy
  348. class. There is precedent for this, however: <tt><a href=
  349. "http://www.dinkumware.com/htm_cpl/string2.html#char_traits">std::char_traits</a></tt>,
  350. despite its name, acts as a policies class that determines the behaviors of
  351. <a href=
  352. "http://www.dinkumware.com/htm_cpl/string2.html#basic_string">std::basic_string</a>.
  353. <h2>Notes</h2>
  354. <a name="1">[1]</a> Type generators are a workaround for the lack of
  355. ``templated typedefs'' in C++.
  356. <hr>
  357. <p>Revised
  358. <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %b %Y" startspan -->15
  359. Feb 2001<!--webbot bot="Timestamp" endspan i-checksum="14377" -->
  360. <p>&copy; Copyright David Abrahams 2001. Permission to copy, use, modify,
  361. sell and distribute this document is granted provided this copyright notice
  362. appears in all copies. This document is provided "as is" without express or
  363. implied warranty, and with no claim as to its suitability for any purpose.
  364. <!-- LocalWords: HTML html charset gif alt htm struct SGI namespace std libs
  365. -->
  366. <!-- LocalWords: InputIterator BidirectionalIterator RandomAccessIterator pdf
  367. -->
  368. <!-- LocalWords: typename Alexandrescu templated Andrei's Abrahams memcpy int
  369. -->
  370. <!-- LocalWords: const OutputIterator iostream pre cpl
  371. -->
粤ICP备19079148号