generic_programming.html 18 KB

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