/Src/Dependencies/Boost/libs/iterator/doc/facade-and-adaptor.html

http://hadesmem.googlecode.com/ · HTML · 2716 lines · 2555 code · 55 blank · 106 comment · 0 complexity · 55d5116a1e6fa5204a1603aec838ef1e MD5 · raw file

Large files are truncated click here to view the full file

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" />
  7. <title>Iterator Facade and Adaptor</title>
  8. <meta name="author" content="David Abrahams, Jeremy Siek, Thomas Witt" />
  9. <meta name="organization" content="Boost Consulting, Indiana University Open Systems Lab, Zephyr Associates, Inc." />
  10. <meta name="date" content="2006-09-11" />
  11. <link rel="stylesheet" href="../../../rst.css" type="text/css" />
  12. </head>
  13. <body>
  14. <div class="document" id="iterator-facade-and-adaptor">
  15. <h1 class="title">Iterator Facade and Adaptor</h1>
  16. <table class="docinfo" frame="void" rules="none">
  17. <col class="docinfo-name" />
  18. <col class="docinfo-content" />
  19. <tbody valign="top">
  20. <tr><th class="docinfo-name">Author:</th>
  21. <td>David Abrahams, Jeremy Siek, Thomas Witt</td></tr>
  22. <tr><th class="docinfo-name">Contact:</th>
  23. <td><a class="first reference external" href="mailto:dave&#64;boost-consulting.com">dave&#64;boost-consulting.com</a>, <a class="reference external" href="mailto:jsiek&#64;osl.iu.edu">jsiek&#64;osl.iu.edu</a>, <a class="last reference external" href="mailto:witt&#64;styleadvisor.com">witt&#64;styleadvisor.com</a></td></tr>
  24. <tr><th class="docinfo-name">Organization:</th>
  25. <td><a class="first reference external" href="http://www.boost-consulting.com">Boost Consulting</a>, Indiana University <a class="reference external" href="http://www.osl.iu.edu">Open Systems
  26. Lab</a>, <a class="last reference external" href="http://www.styleadvisor.com">Zephyr Associates, Inc.</a></td></tr>
  27. <tr><th class="docinfo-name">Date:</th>
  28. <td>2006-09-11</td></tr>
  29. <tr class="field"><th class="docinfo-name">Number:</th><td class="field-body">This is a revised version of <a class="reference external" href="http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1530.html">N1530</a>=03-0113, which was
  30. accepted for Technical Report 1 by the C++ standard
  31. committee's library working group.</td>
  32. </tr>
  33. </tbody>
  34. </table>
  35. <!-- Distributed under the Boost -->
  36. <!-- Software License, Version 1.0. (See accompanying -->
  37. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  38. <!-- Version 1.9 of this ReStructuredText document corresponds to
  39. n1530_, the paper accepted by the LWG. -->
  40. <table class="docutils field-list" frame="void" rules="none">
  41. <col class="field-name" />
  42. <col class="field-body" />
  43. <tbody valign="top">
  44. <tr class="field"><th class="field-name">copyright:</th><td class="field-body">Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003.</td>
  45. </tr>
  46. </tbody>
  47. </table>
  48. <table class="docutils field-list" frame="void" rules="none">
  49. <col class="field-name" />
  50. <col class="field-body" />
  51. <tbody valign="top">
  52. <tr class="field"><th class="field-name">abstract:</th><td class="field-body">We propose a set of class templates that help programmers
  53. build standard-conforming iterators, both from scratch and
  54. by adapting other iterators.</td>
  55. </tr>
  56. </tbody>
  57. </table>
  58. <div class="contents topic" id="table-of-contents">
  59. <p class="topic-title first">Table of Contents</p>
  60. <ul class="simple">
  61. <li><a class="reference internal" href="#motivation" id="id15">Motivation</a></li>
  62. <li><a class="reference internal" href="#impact-on-the-standard" id="id16">Impact on the Standard</a></li>
  63. <li><a class="reference internal" href="#design" id="id17">Design</a><ul>
  64. <li><a class="reference internal" href="#iterator-concepts" id="id18">Iterator Concepts</a></li>
  65. <li><a class="reference internal" href="#interoperability" id="id19">Interoperability</a></li>
  66. <li><a class="reference internal" href="#iterator-facade" id="id20">Iterator Facade</a><ul>
  67. <li><a class="reference internal" href="#usage" id="id21">Usage</a></li>
  68. <li><a class="reference internal" href="#iterator-core-access" id="id22">Iterator Core Access</a></li>
  69. <li><a class="reference internal" href="#operator" id="id23"><tt class="docutils literal"><span class="pre">operator[]</span></tt></a></li>
  70. <li><a class="reference internal" href="#id6" id="id24"><tt class="docutils literal"><span class="pre">operator-&gt;</span></tt></a></li>
  71. </ul>
  72. </li>
  73. <li><a class="reference internal" href="#iterator-adaptor" id="id25">Iterator Adaptor</a></li>
  74. <li><a class="reference internal" href="#specialized-adaptors" id="id26">Specialized Adaptors</a></li>
  75. </ul>
  76. </li>
  77. <li><a class="reference internal" href="#proposed-text" id="id27">Proposed Text</a><ul>
  78. <li><a class="reference internal" href="#header-iterator-helper-synopsis-lib-iterator-helper-synopsis" id="id28">Header <tt class="docutils literal"><span class="pre">&lt;iterator_helper&gt;</span></tt> synopsis [lib.iterator.helper.synopsis]</a></li>
  79. <li><a class="reference internal" href="#iterator-facade-lib-iterator-facade" id="id29">Iterator facade [lib.iterator.facade]</a><ul>
  80. <li><a class="reference internal" href="#class-template-iterator-facade" id="id30">Class template <tt class="docutils literal"><span class="pre">iterator_facade</span></tt></a></li>
  81. <li><a class="reference internal" href="#iterator-facade-requirements" id="id31"><tt class="docutils literal"><span class="pre">iterator_facade</span></tt> Requirements</a></li>
  82. <li><a class="reference internal" href="#iterator-facade-operations" id="id32"><tt class="docutils literal"><span class="pre">iterator_facade</span></tt> operations</a></li>
  83. </ul>
  84. </li>
  85. <li><a class="reference internal" href="#iterator-adaptor-lib-iterator-adaptor" id="id33">Iterator adaptor [lib.iterator.adaptor]</a><ul>
  86. <li><a class="reference internal" href="#class-template-iterator-adaptor" id="id34">Class template <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt></a></li>
  87. <li><a class="reference internal" href="#iterator-adaptor-requirements" id="id35"><tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> requirements</a></li>
  88. <li><a class="reference internal" href="#iterator-adaptor-base-class-parameters" id="id36"><tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> base class parameters</a></li>
  89. <li><a class="reference internal" href="#iterator-adaptor-public-operations" id="id37"><tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> public operations</a></li>
  90. <li><a class="reference internal" href="#iterator-adaptor-protected-member-functions" id="id38"><tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> protected member functions</a></li>
  91. <li><a class="reference internal" href="#iterator-adaptor-private-member-functions" id="id39"><tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> private member functions</a></li>
  92. </ul>
  93. </li>
  94. <li><a class="reference internal" href="#specialized-adaptors-lib-iterator-special-adaptors" id="id40">Specialized adaptors [lib.iterator.special.adaptors]</a><ul>
  95. <li><a class="reference internal" href="#indirect-iterator" id="id41">Indirect iterator</a><ul>
  96. <li><a class="reference internal" href="#class-template-pointee" id="id42">Class template <tt class="docutils literal"><span class="pre">pointee</span></tt></a></li>
  97. <li><a class="reference internal" href="#class-template-indirect-reference" id="id43">Class template <tt class="docutils literal"><span class="pre">indirect_reference</span></tt></a></li>
  98. <li><a class="reference internal" href="#class-template-indirect-iterator" id="id44">Class template <tt class="docutils literal"><span class="pre">indirect_iterator</span></tt></a></li>
  99. <li><a class="reference internal" href="#indirect-iterator-requirements" id="id45"><tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> requirements</a></li>
  100. <li><a class="reference internal" href="#indirect-iterator-models" id="id46"><tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> models</a></li>
  101. <li><a class="reference internal" href="#indirect-iterator-operations" id="id47"><tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> operations</a></li>
  102. </ul>
  103. </li>
  104. <li><a class="reference internal" href="#reverse-iterator" id="id48">Reverse iterator</a><ul>
  105. <li><a class="reference internal" href="#class-template-reverse-iterator" id="id49">Class template <tt class="docutils literal"><span class="pre">reverse_iterator</span></tt></a></li>
  106. <li><a class="reference internal" href="#reverse-iterator-requirements" id="id50"><tt class="docutils literal"><span class="pre">reverse_iterator</span></tt> requirements</a></li>
  107. <li><a class="reference internal" href="#reverse-iterator-models" id="id51"><tt class="docutils literal"><span class="pre">reverse_iterator</span></tt> models</a></li>
  108. <li><a class="reference internal" href="#reverse-iterator-operations" id="id52"><tt class="docutils literal"><span class="pre">reverse_iterator</span></tt> operations</a></li>
  109. </ul>
  110. </li>
  111. <li><a class="reference internal" href="#transform-iterator" id="id53">Transform iterator</a><ul>
  112. <li><a class="reference internal" href="#class-template-transform-iterator" id="id54">Class template <tt class="docutils literal"><span class="pre">transform_iterator</span></tt></a></li>
  113. <li><a class="reference internal" href="#transform-iterator-requirements" id="id55"><tt class="docutils literal"><span class="pre">transform_iterator</span></tt> requirements</a></li>
  114. <li><a class="reference internal" href="#transform-iterator-models" id="id56"><tt class="docutils literal"><span class="pre">transform_iterator</span></tt> models</a></li>
  115. <li><a class="reference internal" href="#transform-iterator-operations" id="id57"><tt class="docutils literal"><span class="pre">transform_iterator</span></tt> operations</a></li>
  116. </ul>
  117. </li>
  118. <li><a class="reference internal" href="#filter-iterator" id="id58">Filter iterator</a><ul>
  119. <li><a class="reference internal" href="#class-template-filter-iterator" id="id59">Class template <tt class="docutils literal"><span class="pre">filter_iterator</span></tt></a></li>
  120. <li><a class="reference internal" href="#filter-iterator-requirements" id="id60"><tt class="docutils literal"><span class="pre">filter_iterator</span></tt> requirements</a></li>
  121. <li><a class="reference internal" href="#filter-iterator-models" id="id61"><tt class="docutils literal"><span class="pre">filter_iterator</span></tt> models</a></li>
  122. <li><a class="reference internal" href="#filter-iterator-operations" id="id62"><tt class="docutils literal"><span class="pre">filter_iterator</span></tt> operations</a></li>
  123. </ul>
  124. </li>
  125. <li><a class="reference internal" href="#counting-iterator" id="id63">Counting iterator</a><ul>
  126. <li><a class="reference internal" href="#class-template-counting-iterator" id="id64">Class template <tt class="docutils literal"><span class="pre">counting_iterator</span></tt></a></li>
  127. <li><a class="reference internal" href="#counting-iterator-requirements" id="id65"><tt class="docutils literal"><span class="pre">counting_iterator</span></tt> requirements</a></li>
  128. <li><a class="reference internal" href="#counting-iterator-models" id="id66"><tt class="docutils literal"><span class="pre">counting_iterator</span></tt> models</a></li>
  129. <li><a class="reference internal" href="#counting-iterator-operations" id="id67"><tt class="docutils literal"><span class="pre">counting_iterator</span></tt> operations</a></li>
  130. </ul>
  131. </li>
  132. <li><a class="reference internal" href="#function-output-iterator" id="id68">Function output iterator</a><ul>
  133. <li><a class="reference internal" href="#class-template-function-output-iterator" id="id69">Class template <tt class="docutils literal"><span class="pre">function_output_iterator</span></tt></a></li>
  134. <li><a class="reference internal" href="#header" id="id70">Header</a></li>
  135. <li><a class="reference internal" href="#function-output-iterator-requirements" id="id71"><tt class="docutils literal"><span class="pre">function_output_iterator</span></tt> requirements</a></li>
  136. <li><a class="reference internal" href="#function-output-iterator-models" id="id72"><tt class="docutils literal"><span class="pre">function_output_iterator</span></tt> models</a></li>
  137. <li><a class="reference internal" href="#function-output-iterator-operations" id="id73"><tt class="docutils literal"><span class="pre">function_output_iterator</span></tt> operations</a></li>
  138. </ul>
  139. </li>
  140. </ul>
  141. </li>
  142. </ul>
  143. </li>
  144. </ul>
  145. </div>
  146. <div class="section" id="motivation">
  147. <h1><a class="toc-backref" href="#id15">Motivation</a></h1>
  148. <p>Iterators play an important role in modern C++ programming. The
  149. iterator is the central abstraction of the algorithms of the Standard
  150. Library, allowing algorithms to be re-used in in a wide variety of
  151. contexts. The C++ Standard Library contains a wide variety of useful
  152. iterators. Every one of the standard containers comes with constant
  153. and mutable iterators<a class="footnote-reference" href="#mutable" id="id1"><sup>2</sup></a>, and also reverse versions of those
  154. same iterators which traverse the container in the opposite direction.
  155. The Standard also supplies <tt class="docutils literal"><span class="pre">istream_iterator</span></tt> and
  156. <tt class="docutils literal"><span class="pre">ostream_iterator</span></tt> for reading from and writing to streams,
  157. <tt class="docutils literal"><span class="pre">insert_iterator</span></tt>, <tt class="docutils literal"><span class="pre">front_insert_iterator</span></tt> and
  158. <tt class="docutils literal"><span class="pre">back_insert_iterator</span></tt> for inserting elements into containers, and
  159. <tt class="docutils literal"><span class="pre">raw_storage_iterator</span></tt> for initializing raw memory [7].</p>
  160. <p>Despite the many iterators supplied by the Standard Library, obvious
  161. and useful iterators are missing, and creating new iterator types is
  162. still a common task for C++ programmers. The literature documents
  163. several of these, for example line_iterator [3] and Constant_iterator
  164. [9]. The iterator abstraction is so powerful that we expect
  165. programmers will always need to invent new iterator types.</p>
  166. <p>Although it is easy to create iterators that <em>almost</em> conform to the
  167. standard, the iterator requirements contain subtleties which can make
  168. creating an iterator which <em>actually</em> conforms quite difficult.
  169. Further, the iterator interface is rich, containing many operators
  170. that are technically redundant and tedious to implement. To automate
  171. the repetitive work of constructing iterators, we propose
  172. <tt class="docutils literal"><span class="pre">iterator_facade</span></tt>, an iterator base class template which provides
  173. the rich interface of standard iterators and delegates its
  174. implementation to member functions of the derived class. In addition
  175. to reducing the amount of code necessary to create an iterator, the
  176. <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> also provides compile-time error detection.
  177. Iterator implementation mistakes that often go unnoticed are turned
  178. into compile-time errors because the derived class implementation must
  179. match the expectations of the <tt class="docutils literal"><span class="pre">iterator_facade</span></tt>.</p>
  180. <p>A common pattern of iterator construction is the adaptation of one
  181. iterator to form a new one. The functionality of an iterator is
  182. composed of four orthogonal aspects: traversal, indirection, equality
  183. comparison and distance measurement. Adapting an old iterator to
  184. create a new one often saves work because one can reuse one aspect of
  185. functionality while redefining the other. For example, the Standard
  186. provides <tt class="docutils literal"><span class="pre">reverse_iterator</span></tt>, which adapts any Bidirectional Iterator
  187. by inverting its direction of traversal. As with plain iterators,
  188. iterator adaptors defined outside the Standard have become commonplace
  189. in the literature:</p>
  190. <ul class="simple">
  191. <li>Checked iter[13] adds bounds-checking to an existing iterator.</li>
  192. <li>The iterators of the View Template Library[14], which adapts
  193. containers, are themselves adaptors over the underlying iterators.</li>
  194. <li>Smart iterators [5] adapt an iterator's dereferencing behavior by
  195. applying a function object to the object being referenced and
  196. returning the result.</li>
  197. <li>Custom iterators [4], in which a variety of adaptor types are enumerated.</li>
  198. <li>Compound iterators [1], which access a slice out of a container of containers.</li>
  199. <li>Several iterator adaptors from the MTL [12]. The MTL contains a
  200. strided iterator, where each call to <tt class="docutils literal"><span class="pre">operator++()</span></tt> moves the
  201. iterator ahead by some constant factor, and a scaled iterator, which
  202. multiplies the dereferenced value by some constant.</li>
  203. </ul>
  204. <table class="docutils footnote" frame="void" id="concept" rules="none">
  205. <colgroup><col class="label" /><col /></colgroup>
  206. <tbody valign="top">
  207. <tr><td class="label">[1]</td><td>We use the term concept to mean a set of requirements
  208. that a type must satisfy to be used with a particular template
  209. parameter.</td></tr>
  210. </tbody>
  211. </table>
  212. <table class="docutils footnote" frame="void" id="mutable" rules="none">
  213. <colgroup><col class="label" /><col /></colgroup>
  214. <tbody valign="top">
  215. <tr><td class="label"><a class="fn-backref" href="#id1">[2]</a></td><td>The term mutable iterator refers to iterators over objects that
  216. can be changed by assigning to the dereferenced iterator, while
  217. constant iterator refers to iterators over objects that cannot be
  218. modified.</td></tr>
  219. </tbody>
  220. </table>
  221. <p>To fulfill the need for constructing adaptors, we propose the
  222. <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> class template. Instantiations of
  223. <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> serve as a base classes for new iterators,
  224. providing the default behavior of forwarding all operations to the
  225. underlying iterator. The user can selectively replace these features
  226. in the derived iterator class. This proposal also includes a number
  227. of more specialized adaptors, such as the <tt class="docutils literal"><span class="pre">transform_iterator</span></tt> that
  228. applies some user-specified function during the dereference of the
  229. iterator.</p>
  230. </div>
  231. <div class="section" id="impact-on-the-standard">
  232. <h1><a class="toc-backref" href="#id16">Impact on the Standard</a></h1>
  233. <p>This proposal is purely an addition to the C++ standard library.
  234. However, note that this proposal relies on the proposal for New
  235. Iterator Concepts.</p>
  236. </div>
  237. <div class="section" id="design">
  238. <h1><a class="toc-backref" href="#id17">Design</a></h1>
  239. <div class="section" id="iterator-concepts">
  240. <h2><a class="toc-backref" href="#id18">Iterator Concepts</a></h2>
  241. <p>This proposal is formulated in terms of the new <tt class="docutils literal"><span class="pre">iterator</span> <span class="pre">concepts</span></tt>
  242. as proposed in <a class="reference external" href="http://anubis.dkuug.dk/JTC1/SC22/WG21/docs/papers/2003/n1550.html">n1550</a>, since user-defined and especially adapted
  243. iterators suffer from the well known categorization problems that are
  244. inherent to the current iterator categories.</p>
  245. <p>This proposal does not strictly depend on proposal <a class="reference external" href="http://anubis.dkuug.dk/JTC1/SC22/WG21/docs/papers/2003/n1550.html">n1550</a>, as there
  246. is a direct mapping between new and old categories. This proposal
  247. could be reformulated using this mapping if <a class="reference external" href="http://anubis.dkuug.dk/JTC1/SC22/WG21/docs/papers/2003/n1550.html">n1550</a> was not accepted.</p>
  248. </div>
  249. <div class="section" id="interoperability">
  250. <h2><a class="toc-backref" href="#id19">Interoperability</a></h2>
  251. <p>The question of iterator interoperability is poorly addressed in the
  252. current standard. There are currently two defect reports that are
  253. concerned with interoperability issues.</p>
  254. <p>Issue <a class="reference external" href="http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-defects.html#179">179</a> concerns the fact that mutable container iterator types
  255. are only required to be convertible to the corresponding constant
  256. iterator types, but objects of these types are not required to
  257. interoperate in comparison or subtraction expressions. This situation
  258. is tedious in practice and out of line with the way built in types
  259. work. This proposal implements the proposed resolution to issue
  260. <a class="reference external" href="http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-defects.html#179">179</a>, as most standard library implementations do nowadays. In other
  261. words, if an iterator type A has an implicit or user defined
  262. conversion to an iterator type B, the iterator types are interoperable
  263. and the usual set of operators are available.</p>
  264. <p>Issue <a class="reference external" href="http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-active.html#280">280</a> concerns the current lack of interoperability between
  265. reverse iterator types. The proposed new reverse_iterator template
  266. fixes the issues raised in 280. It provides the desired
  267. interoperability without introducing unwanted overloads.</p>
  268. </div>
  269. <div class="section" id="iterator-facade">
  270. <h2><a class="toc-backref" href="#id20">Iterator Facade</a></h2>
  271. <!-- Distributed under the Boost -->
  272. <!-- Software License, Version 1.0. (See accompanying -->
  273. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  274. <!-- Version 1.1 of this ReStructuredText document corresponds to
  275. n1530_, the paper accepted by the LWG for TR1. -->
  276. <!-- Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003. -->
  277. <p>While the iterator interface is rich, there is a core subset of the
  278. interface that is necessary for all the functionality. We have
  279. identified the following core behaviors for iterators:</p>
  280. <ul class="simple">
  281. <li>dereferencing</li>
  282. <li>incrementing</li>
  283. <li>decrementing</li>
  284. <li>equality comparison</li>
  285. <li>random-access motion</li>
  286. <li>distance measurement</li>
  287. </ul>
  288. <p>In addition to the behaviors listed above, the core interface elements
  289. include the associated types exposed through iterator traits:
  290. <tt class="docutils literal"><span class="pre">value_type</span></tt>, <tt class="docutils literal"><span class="pre">reference</span></tt>, <tt class="docutils literal"><span class="pre">difference_type</span></tt>, and
  291. <tt class="docutils literal"><span class="pre">iterator_category</span></tt>.</p>
  292. <p>Iterator facade uses the Curiously Recurring Template
  293. Pattern (CRTP) <a class="citation-reference" href="#cop95" id="id4">[Cop95]</a> so that the user can specify the behavior
  294. of <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> in a derived class. Former designs used
  295. policy objects to specify the behavior, but that approach was
  296. discarded for several reasons:</p>
  297. <blockquote>
  298. <ol class="arabic simple">
  299. <li>the creation and eventual copying of the policy object may create
  300. overhead that can be avoided with the current approach.</li>
  301. <li>The policy object approach does not allow for custom constructors
  302. on the created iterator types, an essential feature if
  303. <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> should be used in other library
  304. implementations.</li>
  305. <li>Without the use of CRTP, the standard requirement that an
  306. iterator's <tt class="docutils literal"><span class="pre">operator++</span></tt> returns the iterator type itself
  307. would mean that all iterators built with the library would
  308. have to be specializations of <tt class="docutils literal"><span class="pre">iterator_facade&lt;...&gt;</span></tt>, rather
  309. than something more descriptive like
  310. <tt class="docutils literal"><span class="pre">indirect_iterator&lt;T*&gt;</span></tt>. Cumbersome type generator
  311. metafunctions would be needed to build new parameterized
  312. iterators, and a separate <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> layer would be
  313. impossible.</li>
  314. </ol>
  315. </blockquote>
  316. <div class="section" id="usage">
  317. <h3><a class="toc-backref" href="#id21">Usage</a></h3>
  318. <p>The user of <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> derives his iterator class from a
  319. specialization of <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> and passes the derived
  320. iterator class as <tt class="docutils literal"><span class="pre">iterator_facade</span></tt>'s first template parameter.
  321. The order of the other template parameters have been carefully
  322. chosen to take advantage of useful defaults. For example, when
  323. defining a constant lvalue iterator, the user can pass a
  324. const-qualified version of the iterator's <tt class="docutils literal"><span class="pre">value_type</span></tt> as
  325. <tt class="docutils literal"><span class="pre">iterator_facade</span></tt>'s <tt class="docutils literal"><span class="pre">Value</span></tt> parameter and omit the
  326. <tt class="docutils literal"><span class="pre">Reference</span></tt> parameter which follows.</p>
  327. <p>The derived iterator class must define member functions implementing
  328. the iterator's core behaviors. The following table describes
  329. expressions which are required to be valid depending on the category
  330. of the derived iterator type. These member functions are described
  331. briefly below and in more detail in the iterator facade
  332. requirements.</p>
  333. <blockquote>
  334. <table border="1" class="docutils">
  335. <colgroup>
  336. <col width="44%" />
  337. <col width="56%" />
  338. </colgroup>
  339. <thead valign="bottom">
  340. <tr><th class="head">Expression</th>
  341. <th class="head">Effects</th>
  342. </tr>
  343. </thead>
  344. <tbody valign="top">
  345. <tr><td><tt class="docutils literal"><span class="pre">i.dereference()</span></tt></td>
  346. <td>Access the value referred to</td>
  347. </tr>
  348. <tr><td><tt class="docutils literal"><span class="pre">i.equal(j)</span></tt></td>
  349. <td>Compare for equality with <tt class="docutils literal"><span class="pre">j</span></tt></td>
  350. </tr>
  351. <tr><td><tt class="docutils literal"><span class="pre">i.increment()</span></tt></td>
  352. <td>Advance by one position</td>
  353. </tr>
  354. <tr><td><tt class="docutils literal"><span class="pre">i.decrement()</span></tt></td>
  355. <td>Retreat by one position</td>
  356. </tr>
  357. <tr><td><tt class="docutils literal"><span class="pre">i.advance(n)</span></tt></td>
  358. <td>Advance by <tt class="docutils literal"><span class="pre">n</span></tt> positions</td>
  359. </tr>
  360. <tr><td><tt class="docutils literal"><span class="pre">i.distance_to(j)</span></tt></td>
  361. <td>Measure the distance to <tt class="docutils literal"><span class="pre">j</span></tt></td>
  362. </tr>
  363. </tbody>
  364. </table>
  365. </blockquote>
  366. <!-- Should we add a comment that a zero overhead implementation of iterator_facade
  367. is possible with proper inlining? -->
  368. <p>In addition to implementing the core interface functions, an iterator
  369. derived from <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> typically defines several
  370. constructors. To model any of the standard iterator concepts, the
  371. iterator must at least have a copy constructor. Also, if the iterator
  372. type <tt class="docutils literal"><span class="pre">X</span></tt> is meant to be automatically interoperate with another
  373. iterator type <tt class="docutils literal"><span class="pre">Y</span></tt> (as with constant and mutable iterators) then
  374. there must be an implicit conversion from <tt class="docutils literal"><span class="pre">X</span></tt> to <tt class="docutils literal"><span class="pre">Y</span></tt> or from <tt class="docutils literal"><span class="pre">Y</span></tt>
  375. to <tt class="docutils literal"><span class="pre">X</span></tt> (but not both), typically implemented as a conversion
  376. constructor. Finally, if the iterator is to model Forward Traversal
  377. Iterator or a more-refined iterator concept, a default constructor is
  378. required.</p>
  379. </div>
  380. <div class="section" id="iterator-core-access">
  381. <h3><a class="toc-backref" href="#id22">Iterator Core Access</a></h3>
  382. <p><tt class="docutils literal"><span class="pre">iterator_facade</span></tt> and the operator implementations need to be able
  383. to access the core member functions in the derived class. Making the
  384. core member functions public would expose an implementation detail to
  385. the user. The design used here ensures that implementation details do
  386. not appear in the public interface of the derived iterator type.</p>
  387. <p>Preventing direct access to the core member functions has two
  388. advantages. First, there is no possibility for the user to accidently
  389. use a member function of the iterator when a member of the value_type
  390. was intended. This has been an issue with smart pointer
  391. implementations in the past. The second and main advantage is that
  392. library implementers can freely exchange a hand-rolled iterator
  393. implementation for one based on <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> without fear of
  394. breaking code that was accessing the public core member functions
  395. directly.</p>
  396. <p>In a naive implementation, keeping the derived class' core member
  397. functions private would require it to grant friendship to
  398. <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> and each of the seven operators. In order to
  399. reduce the burden of limiting access, <tt class="docutils literal"><span class="pre">iterator_core_access</span></tt> is
  400. provided, a class that acts as a gateway to the core member functions
  401. in the derived iterator class. The author of the derived class only
  402. needs to grant friendship to <tt class="docutils literal"><span class="pre">iterator_core_access</span></tt> to make his core
  403. member functions available to the library.</p>
  404. <!-- This is no long uptodate -thw -->
  405. <!-- Yes it is; I made sure of it! -DWA -->
  406. <p><tt class="docutils literal"><span class="pre">iterator_core_access</span></tt> will be typically implemented as an empty
  407. class containing only private static member functions which invoke the
  408. iterator core member functions. There is, however, no need to
  409. standardize the gateway protocol. Note that even if
  410. <tt class="docutils literal"><span class="pre">iterator_core_access</span></tt> used public member functions it would not
  411. open a safety loophole, as every core member function preserves the
  412. invariants of the iterator.</p>
  413. </div>
  414. <div class="section" id="operator">
  415. <h3><a class="toc-backref" href="#id23"><tt class="docutils literal"><span class="pre">operator[]</span></tt></a></h3>
  416. <p>The indexing operator for a generalized iterator presents special
  417. challenges. A random access iterator's <tt class="docutils literal"><span class="pre">operator[]</span></tt> is only
  418. required to return something convertible to its <tt class="docutils literal"><span class="pre">value_type</span></tt>.
  419. Requiring that it return an lvalue would rule out currently-legal
  420. random-access iterators which hold the referenced value in a data
  421. member (e.g. <a class="reference internal" href="#counting"><tt class="docutils literal"><span class="pre">counting_iterator</span></tt></a>), because <tt class="docutils literal"><span class="pre">*(p+n)</span></tt> is a reference
  422. into the temporary iterator <tt class="docutils literal"><span class="pre">p+n</span></tt>, which is destroyed when
  423. <tt class="docutils literal"><span class="pre">operator[]</span></tt> returns.</p>
  424. <p>Writable iterators built with <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> implement the
  425. semantics required by the preferred resolution to <a class="reference external" href="http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-active.html#299">issue 299</a> and
  426. adopted by proposal <a class="reference external" href="http://anubis.dkuug.dk/JTC1/SC22/WG21/docs/papers/2003/n1550.html">n1550</a>: the result of <tt class="docutils literal"><span class="pre">p[n]</span></tt> is an object
  427. convertible to the iterator's <tt class="docutils literal"><span class="pre">value_type</span></tt>, and <tt class="docutils literal"><span class="pre">p[n]</span> <span class="pre">=</span> <span class="pre">x</span></tt> is
  428. equivalent to <tt class="docutils literal"><span class="pre">*(p</span> <span class="pre">+</span> <span class="pre">n)</span> <span class="pre">=</span> <span class="pre">x</span></tt> (Note: This result object may be
  429. implemented as a proxy containing a copy of <tt class="docutils literal"><span class="pre">p+n</span></tt>). This approach
  430. will work properly for any random-access iterator regardless of the
  431. other details of its implementation. A user who knows more about
  432. the implementation of her iterator is free to implement an
  433. <tt class="docutils literal"><span class="pre">operator[]</span></tt> that returns an lvalue in the derived iterator
  434. class; it will hide the one supplied by <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> from
  435. clients of her iterator.</p>
  436. </div>
  437. <div class="section" id="id6">
  438. <span id="operator-arrow"></span><h3><a class="toc-backref" href="#id24"><tt class="docutils literal"><span class="pre">operator-&gt;</span></tt></a></h3>
  439. <p>The <tt class="docutils literal"><span class="pre">reference</span></tt> type of a readable iterator (and today's input
  440. iterator) need not in fact be a reference, so long as it is
  441. convertible to the iterator's <tt class="docutils literal"><span class="pre">value_type</span></tt>. When the <tt class="docutils literal"><span class="pre">value_type</span></tt>
  442. is a class, however, it must still be possible to access members
  443. through <tt class="docutils literal"><span class="pre">operator-&gt;</span></tt>. Therefore, an iterator whose <tt class="docutils literal"><span class="pre">reference</span></tt>
  444. type is not in fact a reference must return a proxy containing a copy
  445. of the referenced value from its <tt class="docutils literal"><span class="pre">operator-&gt;</span></tt>.</p>
  446. <p>The return types for <tt class="docutils literal"><span class="pre">iterator_facade</span></tt>'s <tt class="docutils literal"><span class="pre">operator-&gt;</span></tt> and
  447. <tt class="docutils literal"><span class="pre">operator[]</span></tt> are not explicitly specified. Instead, those types
  448. are described in terms of a set of requirements, which must be
  449. satisfied by the <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> implementation.</p>
  450. <table class="docutils citation" frame="void" id="cop95" rules="none">
  451. <colgroup><col class="label" /><col /></colgroup>
  452. <tbody valign="top">
  453. <tr><td class="label"><a class="fn-backref" href="#id4">[Cop95]</a></td><td>[Coplien, 1995] Coplien, J., Curiously Recurring Template
  454. Patterns, C++ Report, February 1995, pp. 24-27.</td></tr>
  455. </tbody>
  456. </table>
  457. </div>
  458. </div>
  459. <div class="section" id="iterator-adaptor">
  460. <h2><a class="toc-backref" href="#id25">Iterator Adaptor</a></h2>
  461. <!-- Distributed under the Boost -->
  462. <!-- Software License, Version 1.0. (See accompanying -->
  463. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  464. <!-- Version 1.2 of this ReStructuredText document corresponds to
  465. n1530_, the paper accepted by the LWG for TR1. -->
  466. <!-- Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003. -->
  467. <p>The <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> class template adapts some <tt class="docutils literal"><span class="pre">Base</span></tt><a class="footnote-reference" href="#base" id="id7"><sup>3</sup></a>
  468. type to create a new iterator. Instantiations of <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt>
  469. are derived from a corresponding instantiation of <tt class="docutils literal"><span class="pre">iterator_facade</span></tt>
  470. and implement the core behaviors in terms of the <tt class="docutils literal"><span class="pre">Base</span></tt> type. In
  471. essence, <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> merely forwards all operations to an
  472. instance of the <tt class="docutils literal"><span class="pre">Base</span></tt> type, which it stores as a member.</p>
  473. <table class="docutils footnote" frame="void" id="base" rules="none">
  474. <colgroup><col class="label" /><col /></colgroup>
  475. <tbody valign="top">
  476. <tr><td class="label"><a class="fn-backref" href="#id7">[3]</a></td><td>The term &quot;Base&quot; here does not refer to a base class and is
  477. not meant to imply the use of derivation. We have followed the lead
  478. of the standard library, which provides a base() function to access
  479. the underlying iterator object of a <tt class="docutils literal"><span class="pre">reverse_iterator</span></tt> adaptor.</td></tr>
  480. </tbody>
  481. </table>
  482. <p>The user of <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> creates a class derived from an
  483. instantiation of <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> and then selectively
  484. redefines some of the core member functions described in the
  485. <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> core requirements table. The <tt class="docutils literal"><span class="pre">Base</span></tt> type need
  486. not meet the full requirements for an iterator; it need only
  487. support the operations used by the core interface functions of
  488. <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> that have not been redefined in the user's
  489. derived class.</p>
  490. <p>Several of the template parameters of <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> default
  491. to <tt class="docutils literal"><span class="pre">use_default</span></tt>. This allows the
  492. user to make use of a default parameter even when she wants to
  493. specify a parameter later in the parameter list. Also, the
  494. defaults for the corresponding associated types are somewhat
  495. complicated, so metaprogramming is required to compute them, and
  496. <tt class="docutils literal"><span class="pre">use_default</span></tt> can help to simplify the implementation. Finally,
  497. the identity of the <tt class="docutils literal"><span class="pre">use_default</span></tt> type is not left unspecified
  498. because specification helps to highlight that the <tt class="docutils literal"><span class="pre">Reference</span></tt>
  499. template parameter may not always be identical to the iterator's
  500. <tt class="docutils literal"><span class="pre">reference</span></tt> type, and will keep users from making mistakes based on
  501. that assumption.</p>
  502. </div>
  503. <div class="section" id="specialized-adaptors">
  504. <h2><a class="toc-backref" href="#id26">Specialized Adaptors</a></h2>
  505. <p>This proposal also contains several examples of specialized adaptors
  506. which were easily implemented using <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt>:</p>
  507. <ul class="simple">
  508. <li><tt class="docutils literal"><span class="pre">indirect_iterator</span></tt>, which iterates over iterators, pointers,
  509. or smart pointers and applies an extra level of dereferencing.</li>
  510. <li>A new <tt class="docutils literal"><span class="pre">reverse_iterator</span></tt>, which inverts the direction of a Base
  511. iterator's motion, while allowing adapted constant and mutable
  512. iterators to interact in the expected ways (unlike those in most
  513. implementations of C++98).</li>
  514. <li><tt class="docutils literal"><span class="pre">transform_iterator</span></tt>, which applies a user-defined function object
  515. to the underlying values when dereferenced.</li>
  516. <li><tt class="docutils literal"><span class="pre">filter_iterator</span></tt>, which provides a view of an iterator range in
  517. which some elements of the underlying range are skipped.</li>
  518. </ul>
  519. <ul class="simple" id="counting">
  520. <li><tt class="docutils literal"><span class="pre">counting_iterator</span></tt>, which adapts any incrementable type
  521. (e.g. integers, iterators) so that incrementing/decrementing the
  522. adapted iterator and dereferencing it produces successive values of
  523. the Base type.</li>
  524. <li><tt class="docutils literal"><span class="pre">function_output_iterator</span></tt>, which makes it easier to create custom
  525. output iterators.</li>
  526. </ul>
  527. <p>Based on examples in the Boost library, users have generated many new
  528. adaptors, among them a permutation adaptor which applies some
  529. permutation to a random access iterator, and a strided adaptor, which
  530. adapts a random access iterator by multiplying its unit of motion by a
  531. constant factor. In addition, the Boost Graph Library (BGL) uses
  532. iterator adaptors to adapt other graph libraries, such as LEDA [10]
  533. and Stanford GraphBase [8], to the BGL interface (which requires C++
  534. Standard compliant iterators).</p>
  535. </div>
  536. </div>
  537. <div class="section" id="proposed-text">
  538. <h1><a class="toc-backref" href="#id27">Proposed Text</a></h1>
  539. <div class="section" id="header-iterator-helper-synopsis-lib-iterator-helper-synopsis">
  540. <h2><a class="toc-backref" href="#id28">Header <tt class="docutils literal"><span class="pre">&lt;iterator_helper&gt;</span></tt> synopsis [lib.iterator.helper.synopsis]</a></h2>
  541. <pre class="literal-block">
  542. struct use_default;
  543. struct iterator_core_access { /* implementation detail */ };
  544. template &lt;
  545. class Derived
  546. , class Value
  547. , class CategoryOrTraversal
  548. , class Reference = Value&amp;
  549. , class Difference = ptrdiff_t
  550. &gt;
  551. class iterator_facade;
  552. template &lt;
  553. class Derived
  554. , class Base
  555. , class Value = use_default
  556. , class CategoryOrTraversal = use_default
  557. , class Reference = use_default
  558. , class Difference = use_default
  559. &gt;
  560. class iterator_adaptor;
  561. template &lt;
  562. class Iterator
  563. , class Value = use_default
  564. , class CategoryOrTraversal = use_default
  565. , class Reference = use_default
  566. , class Difference = use_default
  567. &gt;
  568. class indirect_iterator;
  569. template &lt;class Dereferenceable&gt;
  570. struct pointee;
  571. template &lt;class Dereferenceable&gt;
  572. struct indirect_reference;
  573. template &lt;class Iterator&gt;
  574. class reverse_iterator;
  575. template &lt;
  576. class UnaryFunction
  577. , class Iterator
  578. , class Reference = use_default
  579. , class Value = use_default
  580. &gt;
  581. class transform_iterator;
  582. template &lt;class Predicate, class Iterator&gt;
  583. class filter_iterator;
  584. template &lt;
  585. class Incrementable
  586. , class CategoryOrTraversal = use_default
  587. , class Difference = use_default
  588. &gt;
  589. class counting_iterator;
  590. template &lt;class UnaryFunction&gt;
  591. class function_output_iterator;
  592. </pre>
  593. </div>
  594. <div class="section" id="iterator-facade-lib-iterator-facade">
  595. <h2><a class="toc-backref" href="#id29">Iterator facade [lib.iterator.facade]</a></h2>
  596. <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  597. <!-- Software License, Version 1.0. (See accompanying -->
  598. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  599. <p><tt class="docutils literal"><span class="pre">iterator_facade</span></tt> is a base class template that implements the
  600. interface of standard iterators in terms of a few core functions
  601. and associated types, to be supplied by a derived iterator class.</p>
  602. <div class="section" id="class-template-iterator-facade">
  603. <h3><a class="toc-backref" href="#id30">Class template <tt class="docutils literal"><span class="pre">iterator_facade</span></tt></a></h3>
  604. <!-- Distributed under the Boost -->
  605. <!-- Software License, Version 1.0. (See accompanying -->
  606. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  607. <!-- Version 1.3 of this ReStructuredText document corresponds to
  608. n1530_, the paper accepted by the LWG for TR1. -->
  609. <!-- Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003. -->
  610. <pre class="literal-block">
  611. template &lt;
  612. class Derived
  613. , class Value
  614. , class CategoryOrTraversal
  615. , class Reference = Value&amp;
  616. , class Difference = ptrdiff_t
  617. &gt;
  618. class iterator_facade {
  619. public:
  620. typedef remove_const&lt;Value&gt;::type value_type;
  621. typedef Reference reference;
  622. typedef Value* pointer;
  623. typedef Difference difference_type;
  624. typedef /* see <a class="reference internal" href="#iterator-category">below</a> */ iterator_category;
  625. reference operator*() const;
  626. /* see <a class="reference internal" href="#operator-arrow">below</a> */ operator-&gt;() const;
  627. /* see <a class="reference internal" href="#brackets">below</a> */ operator[](difference_type n) const;
  628. Derived&amp; operator++();
  629. Derived operator++(int);
  630. Derived&amp; operator--();
  631. Derived operator--(int);
  632. Derived&amp; operator+=(difference_type n);
  633. Derived&amp; operator-=(difference_type n);
  634. Derived operator-(difference_type n) const;
  635. protected:
  636. typedef iterator_facade iterator_facade_;
  637. };
  638. // Comparison operators
  639. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  640. class Dr2, class V2, class TC2, class R2, class D2&gt;
  641. typename enable_if_interoperable&lt;Dr1,Dr2,bool&gt;::type // exposition
  642. operator ==(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  643. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  644. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  645. class Dr2, class V2, class TC2, class R2, class D2&gt;
  646. typename enable_if_interoperable&lt;Dr1,Dr2,bool&gt;::type
  647. operator !=(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  648. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  649. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  650. class Dr2, class V2, class TC2, class R2, class D2&gt;
  651. typename enable_if_interoperable&lt;Dr1,Dr2,bool&gt;::type
  652. operator &lt;(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  653. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  654. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  655. class Dr2, class V2, class TC2, class R2, class D2&gt;
  656. typename enable_if_interoperable&lt;Dr1,Dr2,bool&gt;::type
  657. operator &lt;=(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  658. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  659. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  660. class Dr2, class V2, class TC2, class R2, class D2&gt;
  661. typename enable_if_interoperable&lt;Dr1,Dr2,bool&gt;::type
  662. operator &gt;(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  663. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  664. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  665. class Dr2, class V2, class TC2, class R2, class D2&gt;
  666. typename enable_if_interoperable&lt;Dr1,Dr2,bool&gt;::type
  667. operator &gt;=(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  668. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  669. // Iterator difference
  670. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  671. class Dr2, class V2, class TC2, class R2, class D2&gt;
  672. /* see <a class="reference internal" href="#minus">below</a> */
  673. operator-(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  674. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  675. // Iterator addition
  676. template &lt;class Dr, class V, class TC, class R, class D&gt;
  677. Derived operator+ (iterator_facade&lt;Dr,V,TC,R,D&gt; const&amp;,
  678. typename Derived::difference_type n);
  679. template &lt;class Dr, class V, class TC, class R, class D&gt;
  680. Derived operator+ (typename Derived::difference_type n,
  681. iterator_facade&lt;Dr,V,TC,R,D&gt; const&amp;);
  682. </pre>
  683. <p id="iterator-category">The <tt class="docutils literal"><span class="pre">iterator_category</span></tt> member of <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> is</p>
  684. <pre class="literal-block">
  685. <em>iterator-category</em>(CategoryOrTraversal, value_type, reference)
  686. </pre>
  687. <p>where <em>iterator-category</em> is defined as follows:</p>
  688. <pre class="literal-block" id="id12">
  689. <em>iterator-category</em>(C,R,V) :=
  690. if (C is convertible to std::input_iterator_tag
  691. || C is convertible to std::output_iterator_tag
  692. )
  693. return C
  694. else if (C is not convertible to incrementable_traversal_tag)
  695. <em>the program is ill-formed</em>
  696. else return a type X satisfying the following two constraints:
  697. 1. X is convertible to X1, and not to any more-derived
  698. type, where X1 is defined by:
  699. if (R is a reference type
  700. &amp;&amp; C is convertible to forward_traversal_tag)
  701. {
  702. if (C is convertible to random_access_traversal_tag)
  703. X1 = random_access_iterator_tag
  704. else if (C is convertible to bidirectional_traversal_tag)
  705. X1 = bidirectional_iterator_tag
  706. else
  707. X1 = forward_iterator_tag
  708. }
  709. else
  710. {
  711. if (C is convertible to single_pass_traversal_tag
  712. &amp;&amp; R is convertible to V)
  713. X1 = input_iterator_tag
  714. else
  715. X1 = C
  716. }
  717. 2. <a class="reference external" href="new-iter-concepts.html#category-to-traversal"><em>category-to-traversal</em></a>(X) is convertible to the most
  718. derived traversal tag type to which X is also
  719. convertible, and not to any more-derived traversal tag
  720. type.
  721. </pre>
  722. <p>[Note: the intention is to allow <tt class="docutils literal"><span class="pre">iterator_category</span></tt> to be one of
  723. the five original category tags when convertibility to one of the
  724. traversal tags would add no information]</p>
  725. <!-- Copyright David Abrahams 2004. Use, modification and distribution is -->
  726. <!-- subject to the Boost Software License, Version 1.0. (See accompanying -->
  727. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  728. <p>The <tt class="docutils literal"><span class="pre">enable_if_interoperable</span></tt> template used above is for exposition
  729. purposes. The member operators should only be in an overload set
  730. provided the derived types <tt class="docutils literal"><span class="pre">Dr1</span></tt> and <tt class="docutils literal"><span class="pre">Dr2</span></tt> are interoperable,
  731. meaning that at least one of the types is convertible to the other. The
  732. <tt class="docutils literal"><span class="pre">enable_if_intero