PageRenderTime 57ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/Src/Dependencies/Boost/libs/iterator/doc/iterator_facade.html

http://hadesmem.googlecode.com/
HTML | 1348 lines | 1259 code | 62 blank | 27 comment | 0 complexity | 52ca6966e55f88bc79ecded6ddd4ccb4 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0, Apache-2.0, LGPL-3.0
  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</title>
  8. <meta name="author" content="David Abrahams, Jeremy Siek, Thomas Witt" />
  9. <meta name="organization" content="Boost Consulting, Indiana University Open Systems Lab, University of Hanover Institute for Transport Railway Operation and Construction" />
  10. <meta name="date" content="2006-09-11" />
  11. <meta name="copyright" content="Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003." />
  12. <link rel="stylesheet" href="../../../rst.css" type="text/css" />
  13. </head>
  14. <body>
  15. <div class="document" id="iterator-facade">
  16. <h1 class="title">Iterator Facade</h1>
  17. <table class="docinfo" frame="void" rules="none">
  18. <col class="docinfo-name" />
  19. <col class="docinfo-content" />
  20. <tbody valign="top">
  21. <tr><th class="docinfo-name">Author:</th>
  22. <td>David Abrahams, Jeremy Siek, Thomas Witt</td></tr>
  23. <tr><th class="docinfo-name">Contact:</th>
  24. <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;ive.uni-hannover.de">witt&#64;ive.uni-hannover.de</a></td></tr>
  25. <tr><th class="docinfo-name">Organization:</th>
  26. <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
  27. Lab</a>, University of Hanover <a class="last reference external" href="http://www.ive.uni-hannover.de">Institute for Transport
  28. Railway Operation and Construction</a></td></tr>
  29. <tr><th class="docinfo-name">Date:</th>
  30. <td>2006-09-11</td></tr>
  31. <tr><th class="docinfo-name">Copyright:</th>
  32. <td>Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003.</td></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. <table class="docutils field-list" frame="void" rules="none">
  39. <col class="field-name" />
  40. <col class="field-body" />
  41. <tbody valign="top">
  42. <tr class="field"><th class="field-name">abstract:</th><td class="field-body"><!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  43. <!-- Software License, Version 1.0. (See accompanying -->
  44. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  45. <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> is a base class template that implements the
  46. interface of standard iterators in terms of a few core functions
  47. and associated types, to be supplied by a derived iterator class.</td>
  48. </tr>
  49. </tbody>
  50. </table>
  51. <div class="contents topic" id="table-of-contents">
  52. <p class="topic-title first">Table of Contents</p>
  53. <ul class="simple">
  54. <li><a class="reference internal" href="#overview" id="id23">Overview</a><ul>
  55. <li><a class="reference internal" href="#usage" id="id24">Usage</a></li>
  56. <li><a class="reference internal" href="#iterator-core-access" id="id25">Iterator Core Access</a></li>
  57. <li><a class="reference internal" href="#operator" id="id26"><tt class="docutils literal"><span class="pre">operator[]</span></tt></a></li>
  58. <li><a class="reference internal" href="#id2" id="id27"><tt class="docutils literal"><span class="pre">operator-&gt;</span></tt></a></li>
  59. </ul>
  60. </li>
  61. <li><a class="reference internal" href="#reference" id="id28">Reference</a><ul>
  62. <li><a class="reference internal" href="#iterator-facade-requirements" id="id29"><tt class="docutils literal"><span class="pre">iterator_facade</span></tt> Requirements</a></li>
  63. <li><a class="reference internal" href="#iterator-facade-operations" id="id30"><tt class="docutils literal"><span class="pre">iterator_facade</span></tt> operations</a></li>
  64. </ul>
  65. </li>
  66. <li><a class="reference internal" href="#tutorial-example" id="id31">Tutorial Example</a><ul>
  67. <li><a class="reference internal" href="#the-problem" id="id32">The Problem</a></li>
  68. <li><a class="reference internal" href="#a-basic-iterator-using-iterator-facade" id="id33">A Basic Iterator Using <tt class="docutils literal"><span class="pre">iterator_facade</span></tt></a><ul>
  69. <li><a class="reference internal" href="#template-arguments-for-iterator-facade" id="id34">Template Arguments for <tt class="docutils literal"><span class="pre">iterator_facade</span></tt></a><ul>
  70. <li><a class="reference internal" href="#derived" id="id35"><tt class="docutils literal"><span class="pre">Derived</span></tt></a></li>
  71. <li><a class="reference internal" href="#value" id="id36"><tt class="docutils literal"><span class="pre">Value</span></tt></a></li>
  72. <li><a class="reference internal" href="#categoryortraversal" id="id37"><tt class="docutils literal"><span class="pre">CategoryOrTraversal</span></tt></a></li>
  73. <li><a class="reference internal" href="#id12" id="id38"><tt class="docutils literal"><span class="pre">Reference</span></tt></a></li>
  74. <li><a class="reference internal" href="#difference" id="id39"><tt class="docutils literal"><span class="pre">Difference</span></tt></a></li>
  75. </ul>
  76. </li>
  77. <li><a class="reference internal" href="#constructors-and-data-members" id="id40">Constructors and Data Members</a></li>
  78. <li><a class="reference internal" href="#implementing-the-core-operations" id="id41">Implementing the Core Operations</a></li>
  79. </ul>
  80. </li>
  81. <li><a class="reference internal" href="#a-constant-node-iterator" id="id42">A constant <tt class="docutils literal"><span class="pre">node_iterator</span></tt></a></li>
  82. <li><a class="reference internal" href="#interoperability" id="id43">Interoperability</a></li>
  83. <li><a class="reference internal" href="#telling-the-truth" id="id44">Telling the Truth</a></li>
  84. <li><a class="reference internal" href="#wrap-up" id="id45">Wrap Up</a></li>
  85. </ul>
  86. </li>
  87. </ul>
  88. </div>
  89. <div class="section" id="overview">
  90. <h1><a class="toc-backref" href="#id23">Overview</a></h1>
  91. <!-- Distributed under the Boost -->
  92. <!-- Software License, Version 1.0. (See accompanying -->
  93. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  94. <!-- Version 1.1 of this ReStructuredText document corresponds to
  95. n1530_, the paper accepted by the LWG for TR1. -->
  96. <!-- Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003. -->
  97. <p>While the iterator interface is rich, there is a core subset of the
  98. interface that is necessary for all the functionality. We have
  99. identified the following core behaviors for iterators:</p>
  100. <ul class="simple">
  101. <li>dereferencing</li>
  102. <li>incrementing</li>
  103. <li>decrementing</li>
  104. <li>equality comparison</li>
  105. <li>random-access motion</li>
  106. <li>distance measurement</li>
  107. </ul>
  108. <p>In addition to the behaviors listed above, the core interface elements
  109. include the associated types exposed through iterator traits:
  110. <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
  111. <tt class="docutils literal"><span class="pre">iterator_category</span></tt>.</p>
  112. <p>Iterator facade uses the Curiously Recurring Template
  113. Pattern (CRTP) <a class="citation-reference" href="#cop95" id="id1">[Cop95]</a> so that the user can specify the behavior
  114. of <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> in a derived class. Former designs used
  115. policy objects to specify the behavior, but that approach was
  116. discarded for several reasons:</p>
  117. <blockquote>
  118. <ol class="arabic simple">
  119. <li>the creation and eventual copying of the policy object may create
  120. overhead that can be avoided with the current approach.</li>
  121. <li>The policy object approach does not allow for custom constructors
  122. on the created iterator types, an essential feature if
  123. <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> should be used in other library
  124. implementations.</li>
  125. <li>Without the use of CRTP, the standard requirement that an
  126. iterator's <tt class="docutils literal"><span class="pre">operator++</span></tt> returns the iterator type itself
  127. would mean that all iterators built with the library would
  128. have to be specializations of <tt class="docutils literal"><span class="pre">iterator_facade&lt;...&gt;</span></tt>, rather
  129. than something more descriptive like
  130. <tt class="docutils literal"><span class="pre">indirect_iterator&lt;T*&gt;</span></tt>. Cumbersome type generator
  131. metafunctions would be needed to build new parameterized
  132. iterators, and a separate <tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt> layer would be
  133. impossible.</li>
  134. </ol>
  135. </blockquote>
  136. <div class="section" id="usage">
  137. <h2><a class="toc-backref" href="#id24">Usage</a></h2>
  138. <p>The user of <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> derives his iterator class from a
  139. specialization of <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> and passes the derived
  140. iterator class as <tt class="docutils literal"><span class="pre">iterator_facade</span></tt>'s first template parameter.
  141. The order of the other template parameters have been carefully
  142. chosen to take advantage of useful defaults. For example, when
  143. defining a constant lvalue iterator, the user can pass a
  144. const-qualified version of the iterator's <tt class="docutils literal"><span class="pre">value_type</span></tt> as
  145. <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
  146. <tt class="docutils literal"><span class="pre">Reference</span></tt> parameter which follows.</p>
  147. <p>The derived iterator class must define member functions implementing
  148. the iterator's core behaviors. The following table describes
  149. expressions which are required to be valid depending on the category
  150. of the derived iterator type. These member functions are described
  151. briefly below and in more detail in the iterator facade
  152. requirements.</p>
  153. <blockquote>
  154. <table border="1" class="docutils">
  155. <colgroup>
  156. <col width="44%" />
  157. <col width="56%" />
  158. </colgroup>
  159. <thead valign="bottom">
  160. <tr><th class="head">Expression</th>
  161. <th class="head">Effects</th>
  162. </tr>
  163. </thead>
  164. <tbody valign="top">
  165. <tr><td><tt class="docutils literal"><span class="pre">i.dereference()</span></tt></td>
  166. <td>Access the value referred to</td>
  167. </tr>
  168. <tr><td><tt class="docutils literal"><span class="pre">i.equal(j)</span></tt></td>
  169. <td>Compare for equality with <tt class="docutils literal"><span class="pre">j</span></tt></td>
  170. </tr>
  171. <tr><td><tt class="docutils literal"><span class="pre">i.increment()</span></tt></td>
  172. <td>Advance by one position</td>
  173. </tr>
  174. <tr><td><tt class="docutils literal"><span class="pre">i.decrement()</span></tt></td>
  175. <td>Retreat by one position</td>
  176. </tr>
  177. <tr><td><tt class="docutils literal"><span class="pre">i.advance(n)</span></tt></td>
  178. <td>Advance by <tt class="docutils literal"><span class="pre">n</span></tt> positions</td>
  179. </tr>
  180. <tr><td><tt class="docutils literal"><span class="pre">i.distance_to(j)</span></tt></td>
  181. <td>Measure the distance to <tt class="docutils literal"><span class="pre">j</span></tt></td>
  182. </tr>
  183. </tbody>
  184. </table>
  185. </blockquote>
  186. <!-- Should we add a comment that a zero overhead implementation of iterator_facade
  187. is possible with proper inlining? -->
  188. <p>In addition to implementing the core interface functions, an iterator
  189. derived from <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> typically defines several
  190. constructors. To model any of the standard iterator concepts, the
  191. iterator must at least have a copy constructor. Also, if the iterator
  192. type <tt class="docutils literal"><span class="pre">X</span></tt> is meant to be automatically interoperate with another
  193. iterator type <tt class="docutils literal"><span class="pre">Y</span></tt> (as with constant and mutable iterators) then
  194. 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>
  195. to <tt class="docutils literal"><span class="pre">X</span></tt> (but not both), typically implemented as a conversion
  196. constructor. Finally, if the iterator is to model Forward Traversal
  197. Iterator or a more-refined iterator concept, a default constructor is
  198. required.</p>
  199. </div>
  200. <div class="section" id="iterator-core-access">
  201. <h2><a class="toc-backref" href="#id25">Iterator Core Access</a></h2>
  202. <p><tt class="docutils literal"><span class="pre">iterator_facade</span></tt> and the operator implementations need to be able
  203. to access the core member functions in the derived class. Making the
  204. core member functions public would expose an implementation detail to
  205. the user. The design used here ensures that implementation details do
  206. not appear in the public interface of the derived iterator type.</p>
  207. <p>Preventing direct access to the core member functions has two
  208. advantages. First, there is no possibility for the user to accidently
  209. use a member function of the iterator when a member of the value_type
  210. was intended. This has been an issue with smart pointer
  211. implementations in the past. The second and main advantage is that
  212. library implementers can freely exchange a hand-rolled iterator
  213. implementation for one based on <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> without fear of
  214. breaking code that was accessing the public core member functions
  215. directly.</p>
  216. <p>In a naive implementation, keeping the derived class' core member
  217. functions private would require it to grant friendship to
  218. <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> and each of the seven operators. In order to
  219. reduce the burden of limiting access, <tt class="docutils literal"><span class="pre">iterator_core_access</span></tt> is
  220. provided, a class that acts as a gateway to the core member functions
  221. in the derived iterator class. The author of the derived class only
  222. needs to grant friendship to <tt class="docutils literal"><span class="pre">iterator_core_access</span></tt> to make his core
  223. member functions available to the library.</p>
  224. <!-- This is no long uptodate -thw -->
  225. <!-- Yes it is; I made sure of it! -DWA -->
  226. <p><tt class="docutils literal"><span class="pre">iterator_core_access</span></tt> will be typically implemented as an empty
  227. class containing only private static member functions which invoke the
  228. iterator core member functions. There is, however, no need to
  229. standardize the gateway protocol. Note that even if
  230. <tt class="docutils literal"><span class="pre">iterator_core_access</span></tt> used public member functions it would not
  231. open a safety loophole, as every core member function preserves the
  232. invariants of the iterator.</p>
  233. </div>
  234. <div class="section" id="operator">
  235. <h2><a class="toc-backref" href="#id26"><tt class="docutils literal"><span class="pre">operator[]</span></tt></a></h2>
  236. <p>The indexing operator for a generalized iterator presents special
  237. challenges. A random access iterator's <tt class="docutils literal"><span class="pre">operator[]</span></tt> is only
  238. required to return something convertible to its <tt class="docutils literal"><span class="pre">value_type</span></tt>.
  239. Requiring that it return an lvalue would rule out currently-legal
  240. random-access iterators which hold the referenced value in a data
  241. member (e.g. <a class="reference external" href="counting_iterator.html"><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
  242. into the temporary iterator <tt class="docutils literal"><span class="pre">p+n</span></tt>, which is destroyed when
  243. <tt class="docutils literal"><span class="pre">operator[]</span></tt> returns.</p>
  244. <p>Writable iterators built with <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> implement the
  245. 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
  246. 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
  247. 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
  248. 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
  249. implemented as a proxy containing a copy of <tt class="docutils literal"><span class="pre">p+n</span></tt>). This approach
  250. will work properly for any random-access iterator regardless of the
  251. other details of its implementation. A user who knows more about
  252. the implementation of her iterator is free to implement an
  253. <tt class="docutils literal"><span class="pre">operator[]</span></tt> that returns an lvalue in the derived iterator
  254. class; it will hide the one supplied by <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> from
  255. clients of her iterator.</p>
  256. </div>
  257. <div class="section" id="id2">
  258. <span id="operator-arrow"></span><h2><a class="toc-backref" href="#id27"><tt class="docutils literal"><span class="pre">operator-&gt;</span></tt></a></h2>
  259. <p>The <tt class="docutils literal"><span class="pre">reference</span></tt> type of a readable iterator (and today's input
  260. iterator) need not in fact be a reference, so long as it is
  261. 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>
  262. is a class, however, it must still be possible to access members
  263. 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>
  264. type is not in fact a reference must return a proxy containing a copy
  265. of the referenced value from its <tt class="docutils literal"><span class="pre">operator-&gt;</span></tt>.</p>
  266. <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
  267. <tt class="docutils literal"><span class="pre">operator[]</span></tt> are not explicitly specified. Instead, those types
  268. are described in terms of a set of requirements, which must be
  269. satisfied by the <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> implementation.</p>
  270. <table class="docutils citation" frame="void" id="cop95" rules="none">
  271. <colgroup><col class="label" /><col /></colgroup>
  272. <tbody valign="top">
  273. <tr><td class="label">[Cop95]</td><td><em>(<a class="fn-backref" href="#id1">1</a>, <a class="fn-backref" href="#id10">2</a>)</em> [Coplien, 1995] Coplien, J., Curiously Recurring Template
  274. Patterns, C++ Report, February 1995, pp. 24-27.</td></tr>
  275. </tbody>
  276. </table>
  277. </div>
  278. </div>
  279. <div class="section" id="reference">
  280. <h1><a class="toc-backref" href="#id28">Reference</a></h1>
  281. <!-- Distributed under the Boost -->
  282. <!-- Software License, Version 1.0. (See accompanying -->
  283. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  284. <!-- Version 1.3 of this ReStructuredText document corresponds to
  285. n1530_, the paper accepted by the LWG for TR1. -->
  286. <!-- Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003. -->
  287. <pre class="literal-block">
  288. template &lt;
  289. class Derived
  290. , class Value
  291. , class CategoryOrTraversal
  292. , class Reference = Value&amp;
  293. , class Difference = ptrdiff_t
  294. &gt;
  295. class iterator_facade {
  296. public:
  297. typedef remove_const&lt;Value&gt;::type value_type;
  298. typedef Reference reference;
  299. typedef Value* pointer;
  300. typedef Difference difference_type;
  301. typedef /* see <a class="reference internal" href="#iterator-category">below</a> */ iterator_category;
  302. reference operator*() const;
  303. /* see <a class="reference internal" href="#operator-arrow">below</a> */ operator-&gt;() const;
  304. /* see <a class="reference internal" href="#brackets">below</a> */ operator[](difference_type n) const;
  305. Derived&amp; operator++();
  306. Derived operator++(int);
  307. Derived&amp; operator--();
  308. Derived operator--(int);
  309. Derived&amp; operator+=(difference_type n);
  310. Derived&amp; operator-=(difference_type n);
  311. Derived operator-(difference_type n) const;
  312. protected:
  313. typedef iterator_facade iterator_facade_;
  314. };
  315. // Comparison operators
  316. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  317. class Dr2, class V2, class TC2, class R2, class D2&gt;
  318. typename enable_if_interoperable&lt;Dr1,Dr2,bool&gt;::type // exposition
  319. operator ==(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  320. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  321. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  322. class Dr2, class V2, class TC2, class R2, class D2&gt;
  323. typename enable_if_interoperable&lt;Dr1,Dr2,bool&gt;::type
  324. operator !=(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  325. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  326. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  327. class Dr2, class V2, class TC2, class R2, class D2&gt;
  328. typename enable_if_interoperable&lt;Dr1,Dr2,bool&gt;::type
  329. operator &lt;(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  330. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  331. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  332. class Dr2, class V2, class TC2, class R2, class D2&gt;
  333. typename enable_if_interoperable&lt;Dr1,Dr2,bool&gt;::type
  334. operator &lt;=(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  335. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  336. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  337. class Dr2, class V2, class TC2, class R2, class D2&gt;
  338. typename enable_if_interoperable&lt;Dr1,Dr2,bool&gt;::type
  339. operator &gt;(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  340. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  341. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  342. class Dr2, class V2, class TC2, class R2, class D2&gt;
  343. typename enable_if_interoperable&lt;Dr1,Dr2,bool&gt;::type
  344. operator &gt;=(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  345. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  346. // Iterator difference
  347. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  348. class Dr2, class V2, class TC2, class R2, class D2&gt;
  349. /* see <a class="reference internal" href="#minus">below</a> */
  350. operator-(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  351. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  352. // Iterator addition
  353. template &lt;class Dr, class V, class TC, class R, class D&gt;
  354. Derived operator+ (iterator_facade&lt;Dr,V,TC,R,D&gt; const&amp;,
  355. typename Derived::difference_type n);
  356. template &lt;class Dr, class V, class TC, class R, class D&gt;
  357. Derived operator+ (typename Derived::difference_type n,
  358. iterator_facade&lt;Dr,V,TC,R,D&gt; const&amp;);
  359. </pre>
  360. <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>
  361. <pre class="literal-block">
  362. <em>iterator-category</em>(CategoryOrTraversal, value_type, reference)
  363. </pre>
  364. <p>where <em>iterator-category</em> is defined as follows:</p>
  365. <pre class="literal-block" id="id7">
  366. <em>iterator-category</em>(C,R,V) :=
  367. if (C is convertible to std::input_iterator_tag
  368. || C is convertible to std::output_iterator_tag
  369. )
  370. return C
  371. else if (C is not convertible to incrementable_traversal_tag)
  372. <em>the program is ill-formed</em>
  373. else return a type X satisfying the following two constraints:
  374. 1. X is convertible to X1, and not to any more-derived
  375. type, where X1 is defined by:
  376. if (R is a reference type
  377. &amp;&amp; C is convertible to forward_traversal_tag)
  378. {
  379. if (C is convertible to random_access_traversal_tag)
  380. X1 = random_access_iterator_tag
  381. else if (C is convertible to bidirectional_traversal_tag)
  382. X1 = bidirectional_iterator_tag
  383. else
  384. X1 = forward_iterator_tag
  385. }
  386. else
  387. {
  388. if (C is convertible to single_pass_traversal_tag
  389. &amp;&amp; R is convertible to V)
  390. X1 = input_iterator_tag
  391. else
  392. X1 = C
  393. }
  394. 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
  395. derived traversal tag type to which X is also
  396. convertible, and not to any more-derived traversal tag
  397. type.
  398. </pre>
  399. <p>[Note: the intention is to allow <tt class="docutils literal"><span class="pre">iterator_category</span></tt> to be one of
  400. the five original category tags when convertibility to one of the
  401. traversal tags would add no information]</p>
  402. <!-- Copyright David Abrahams 2004. Use, modification and distribution is -->
  403. <!-- subject to the Boost Software License, Version 1.0. (See accompanying -->
  404. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  405. <p>The <tt class="docutils literal"><span class="pre">enable_if_interoperable</span></tt> template used above is for exposition
  406. purposes. The member operators should only be in an overload set
  407. 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,
  408. meaning that at least one of the types is convertible to the other. The
  409. <tt class="docutils literal"><span class="pre">enable_if_interoperable</span></tt> approach uses SFINAE to take the operators
  410. out of the overload set when the types are not interoperable.
  411. The operators should behave <em>as-if</em> <tt class="docutils literal"><span class="pre">enable_if_interoperable</span></tt>
  412. were defined to be:</p>
  413. <pre class="literal-block">
  414. template &lt;bool, typename&gt; enable_if_interoperable_impl
  415. {};
  416. template &lt;typename T&gt; enable_if_interoperable_impl&lt;true,T&gt;
  417. { typedef T type; };
  418. template&lt;typename Dr1, typename Dr2, typename T&gt;
  419. struct enable_if_interoperable
  420. : enable_if_interoperable_impl&lt;
  421. is_convertible&lt;Dr1,Dr2&gt;::value || is_convertible&lt;Dr2,Dr1&gt;::value
  422. , T
  423. &gt;
  424. {};
  425. </pre>
  426. <div class="section" id="iterator-facade-requirements">
  427. <h2><a class="toc-backref" href="#id29"><tt class="docutils literal"><span class="pre">iterator_facade</span></tt> Requirements</a></h2>
  428. <p>The following table describes the typical valid expressions on
  429. <tt class="docutils literal"><span class="pre">iterator_facade</span></tt>'s <tt class="docutils literal"><span class="pre">Derived</span></tt> parameter, depending on the
  430. iterator concept(s) it will model. The operations in the first
  431. column must be made accessible to member functions of class
  432. <tt class="docutils literal"><span class="pre">iterator_core_access</span></tt>. In addition,
  433. <tt class="docutils literal"><span class="pre">static_cast&lt;Derived*&gt;(iterator_facade*)</span></tt> shall be well-formed.</p>
  434. <p>In the table below, <tt class="docutils literal"><span class="pre">F</span></tt> is <tt class="docutils literal"><span class="pre">iterator_facade&lt;X,V,C,R,D&gt;</span></tt>, <tt class="docutils literal"><span class="pre">a</span></tt> is an
  435. object of type <tt class="docutils literal"><span class="pre">X</span></tt>, <tt class="docutils literal"><span class="pre">b</span></tt> and <tt class="docutils literal"><span class="pre">c</span></tt> are objects of type <tt class="docutils literal"><span class="pre">const</span> <span class="pre">X</span></tt>,
  436. <tt class="docutils literal"><span class="pre">n</span></tt> is an object of <tt class="docutils literal"><span class="pre">F::difference_type</span></tt>, <tt class="docutils literal"><span class="pre">y</span></tt> is a constant
  437. object of a single pass iterator type interoperable with <tt class="docutils literal"><span class="pre">X</span></tt>, and <tt class="docutils literal"><span class="pre">z</span></tt>
  438. is a constant object of a random access traversal iterator type
  439. interoperable with <tt class="docutils literal"><span class="pre">X</span></tt>.</p>
  440. <div class="topic" id="core-operations">
  441. <p class="topic-title first"><tt class="docutils literal"><span class="pre">iterator_facade</span></tt> Core Operations</p>
  442. <table border="1" class="docutils">
  443. <colgroup>
  444. <col width="21%" />
  445. <col width="23%" />
  446. <col width="27%" />
  447. <col width="29%" />
  448. </colgroup>
  449. <thead valign="bottom">
  450. <tr><th class="head">Expression</th>
  451. <th class="head">Return Type</th>
  452. <th class="head">Assertion/Note</th>
  453. <th class="head">Used to implement Iterator
  454. Concept(s)</th>
  455. </tr>
  456. </thead>
  457. <tbody valign="top">
  458. <tr><td><tt class="docutils literal"><span class="pre">c.dereference()</span></tt></td>
  459. <td><tt class="docutils literal"><span class="pre">F::reference</span></tt></td>
  460. <td>&nbsp;</td>
  461. <td>Readable Iterator, Writable
  462. Iterator</td>
  463. </tr>
  464. <tr><td><tt class="docutils literal"><span class="pre">c.equal(y)</span></tt></td>
  465. <td>convertible to bool</td>
  466. <td>true iff <tt class="docutils literal"><span class="pre">c</span></tt> and <tt class="docutils literal"><span class="pre">y</span></tt>
  467. refer to the same
  468. position.</td>
  469. <td>Single Pass Iterator</td>
  470. </tr>
  471. <tr><td><tt class="docutils literal"><span class="pre">a.increment()</span></tt></td>
  472. <td>unused</td>
  473. <td>&nbsp;</td>
  474. <td>Incrementable Iterator</td>
  475. </tr>
  476. <tr><td><tt class="docutils literal"><span class="pre">a.decrement()</span></tt></td>
  477. <td>unused</td>
  478. <td>&nbsp;</td>
  479. <td>Bidirectional Traversal
  480. Iterator</td>
  481. </tr>
  482. <tr><td><tt class="docutils literal"><span class="pre">a.advance(n)</span></tt></td>
  483. <td>unused</td>
  484. <td>&nbsp;</td>
  485. <td>Random Access Traversal
  486. Iterator</td>
  487. </tr>
  488. <tr><td><tt class="docutils literal"><span class="pre">c.distance_to(z)</span></tt></td>
  489. <td>convertible to
  490. <tt class="docutils literal"><span class="pre">F::difference_type</span></tt></td>
  491. <td>equivalent to
  492. <tt class="docutils literal"><span class="pre">distance(c,</span> <span class="pre">X(z))</span></tt>.</td>
  493. <td>Random Access Traversal
  494. Iterator</td>
  495. </tr>
  496. </tbody>
  497. </table>
  498. </div>
  499. </div>
  500. <div class="section" id="iterator-facade-operations">
  501. <h2><a class="toc-backref" href="#id30"><tt class="docutils literal"><span class="pre">iterator_facade</span></tt> operations</a></h2>
  502. <p>The operations in this section are described in terms of operations on
  503. the core interface of <tt class="docutils literal"><span class="pre">Derived</span></tt> which may be inaccessible
  504. (i.e. private). The implementation should access these operations
  505. through member functions of class <tt class="docutils literal"><span class="pre">iterator_core_access</span></tt>.</p>
  506. <p><tt class="docutils literal"><span class="pre">reference</span> <span class="pre">operator*()</span> <span class="pre">const;</span></tt></p>
  507. <table class="docutils field-list" frame="void" rules="none">
  508. <col class="field-name" />
  509. <col class="field-body" />
  510. <tbody valign="top">
  511. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">static_cast&lt;Derived</span> <span class="pre">const*&gt;(this)-&gt;dereference()</span></tt></td>
  512. </tr>
  513. </tbody>
  514. </table>
  515. <p><tt class="docutils literal"><span class="pre">operator-&gt;()</span> <span class="pre">const;</span></tt> (see <a class="reference internal" href="#operator-arrow">below</a>)</p>
  516. <table class="docutils field-list" frame="void" rules="none">
  517. <col class="field-name" />
  518. <col class="field-body" />
  519. <tbody valign="top">
  520. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">If <tt class="docutils literal"><span class="pre">reference</span></tt> is a reference type, an object
  521. of type <tt class="docutils literal"><span class="pre">pointer</span></tt> equal to:</p>
  522. <pre class="literal-block">
  523. &amp;static_cast&lt;Derived const*&gt;(this)-&gt;dereference()
  524. </pre>
  525. <p class="last">Otherwise returns an object of unspecified type such that,
  526. <tt class="docutils literal"><span class="pre">(*static_cast&lt;Derived</span> <span class="pre">const*&gt;(this))-&gt;m</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">(w</span> <span class="pre">=</span> <span class="pre">**static_cast&lt;Derived</span> <span class="pre">const*&gt;(this),</span>
  527. <span class="pre">w.m)</span></tt> for some temporary object <tt class="docutils literal"><span class="pre">w</span></tt> of type <tt class="docutils literal"><span class="pre">value_type</span></tt>.</p>
  528. </td>
  529. </tr>
  530. </tbody>
  531. </table>
  532. <p id="brackets"><em>unspecified</em> <tt class="docutils literal"><span class="pre">operator[](difference_type</span> <span class="pre">n)</span> <span class="pre">const;</span></tt></p>
  533. <table class="docutils field-list" frame="void" rules="none">
  534. <col class="field-name" />
  535. <col class="field-body" />
  536. <tbody valign="top">
  537. <tr class="field"><th class="field-name">Returns:</th><td class="field-body">an object convertible to <tt class="docutils literal"><span class="pre">value_type</span></tt>. For constant
  538. objects <tt class="docutils literal"><span class="pre">v</span></tt> of type <tt class="docutils literal"><span class="pre">value_type</span></tt>, and <tt class="docutils literal"><span class="pre">n</span></tt> of type
  539. <tt class="docutils literal"><span class="pre">difference_type</span></tt>, <tt class="docutils literal"><span class="pre">(*this)[n]</span> <span class="pre">=</span> <span class="pre">v</span></tt> is equivalent to
  540. <tt class="docutils literal"><span class="pre">*(*this</span> <span class="pre">+</span> <span class="pre">n)</span> <span class="pre">=</span> <span class="pre">v</span></tt>, and <tt class="docutils literal"><span class="pre">static_cast&lt;value_type</span>
  541. <span class="pre">const&amp;&gt;((*this)[n])</span></tt> is equivalent to
  542. <tt class="docutils literal"><span class="pre">static_cast&lt;value_type</span> <span class="pre">const&amp;&gt;(*(*this</span> <span class="pre">+</span> <span class="pre">n))</span></tt></td>
  543. </tr>
  544. </tbody>
  545. </table>
  546. <p><tt class="docutils literal"><span class="pre">Derived&amp;</span> <span class="pre">operator++();</span></tt></p>
  547. <table class="docutils field-list" frame="void" rules="none">
  548. <col class="field-name" />
  549. <col class="field-body" />
  550. <tbody valign="top">
  551. <tr class="field"><th class="field-name">Effects:</th><td class="field-body"><pre class="first last literal-block">
  552. static_cast&lt;Derived*&gt;(this)-&gt;increment();
  553. return *static_cast&lt;Derived*&gt;(this);
  554. </pre>
  555. </td>
  556. </tr>
  557. </tbody>
  558. </table>
  559. <p><tt class="docutils literal"><span class="pre">Derived</span> <span class="pre">operator++(int);</span></tt></p>
  560. <table class="docutils field-list" frame="void" rules="none">
  561. <col class="field-name" />
  562. <col class="field-body" />
  563. <tbody valign="top">
  564. <tr class="field"><th class="field-name">Effects:</th><td class="field-body"><pre class="first last literal-block">
  565. Derived tmp(static_cast&lt;Derived const*&gt;(this));
  566. ++*this;
  567. return tmp;
  568. </pre>
  569. </td>
  570. </tr>
  571. </tbody>
  572. </table>
  573. <p><tt class="docutils literal"><span class="pre">Derived&amp;</span> <span class="pre">operator--();</span></tt></p>
  574. <table class="docutils field-list" frame="void" rules="none">
  575. <col class="field-name" />
  576. <col class="field-body" />
  577. <tbody valign="top">
  578. <tr class="field"><th class="field-name">Effects:</th><td class="field-body"><pre class="first last literal-block">
  579. static_cast&lt;Derived*&gt;(this)-&gt;decrement();
  580. return *static_cast&lt;Derived*&gt;(this);
  581. </pre>
  582. </td>
  583. </tr>
  584. </tbody>
  585. </table>
  586. <p><tt class="docutils literal"><span class="pre">Derived</span> <span class="pre">operator--(int);</span></tt></p>
  587. <table class="docutils field-list" frame="void" rules="none">
  588. <col class="field-name" />
  589. <col class="field-body" />
  590. <tbody valign="top">
  591. <tr class="field"><th class="field-name">Effects:</th><td class="field-body"><pre class="first last literal-block">
  592. Derived tmp(static_cast&lt;Derived const*&gt;(this));
  593. --*this;
  594. return tmp;
  595. </pre>
  596. </td>
  597. </tr>
  598. </tbody>
  599. </table>
  600. <p><tt class="docutils literal"><span class="pre">Derived&amp;</span> <span class="pre">operator+=(difference_type</span> <span class="pre">n);</span></tt></p>
  601. <table class="docutils field-list" frame="void" rules="none">
  602. <col class="field-name" />
  603. <col class="field-body" />
  604. <tbody valign="top">
  605. <tr class="field"><th class="field-name">Effects:</th><td class="field-body"><pre class="first last literal-block">
  606. static_cast&lt;Derived*&gt;(this)-&gt;advance(n);
  607. return *static_cast&lt;Derived*&gt;(this);
  608. </pre>
  609. </td>
  610. </tr>
  611. </tbody>
  612. </table>
  613. <p><tt class="docutils literal"><span class="pre">Derived&amp;</span> <span class="pre">operator-=(difference_type</span> <span class="pre">n);</span></tt></p>
  614. <table class="docutils field-list" frame="void" rules="none">
  615. <col class="field-name" />
  616. <col class="field-body" />
  617. <tbody valign="top">
  618. <tr class="field"><th class="field-name">Effects:</th><td class="field-body"><pre class="first last literal-block">
  619. static_cast&lt;Derived*&gt;(this)-&gt;advance(-n);
  620. return *static_cast&lt;Derived*&gt;(this);
  621. </pre>
  622. </td>
  623. </tr>
  624. </tbody>
  625. </table>
  626. <p><tt class="docutils literal"><span class="pre">Derived</span> <span class="pre">operator-(difference_type</span> <span class="pre">n)</span> <span class="pre">const;</span></tt></p>
  627. <table class="docutils field-list" frame="void" rules="none">
  628. <col class="field-name" />
  629. <col class="field-body" />
  630. <tbody valign="top">
  631. <tr class="field"><th class="field-name">Effects:</th><td class="field-body"><pre class="first last literal-block">
  632. Derived tmp(static_cast&lt;Derived const*&gt;(this));
  633. return tmp -= n;
  634. </pre>
  635. </td>
  636. </tr>
  637. </tbody>
  638. </table>
  639. <pre class="literal-block">
  640. template &lt;class Dr, class V, class TC, class R, class D&gt;
  641. Derived operator+ (iterator_facade&lt;Dr,V,TC,R,D&gt; const&amp;,
  642. typename Derived::difference_type n);
  643. template &lt;class Dr, class V, class TC, class R, class D&gt;
  644. Derived operator+ (typename Derived::difference_type n,
  645. iterator_facade&lt;Dr,V,TC,R,D&gt; const&amp;);
  646. </pre>
  647. <table class="docutils field-list" frame="void" rules="none">
  648. <col class="field-name" />
  649. <col class="field-body" />
  650. <tbody valign="top">
  651. <tr class="field"><th class="field-name">Effects:</th><td class="field-body"><pre class="first last literal-block">
  652. Derived tmp(static_cast&lt;Derived const*&gt;(this));
  653. return tmp += n;
  654. </pre>
  655. </td>
  656. </tr>
  657. </tbody>
  658. </table>
  659. <pre class="literal-block">
  660. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  661. class Dr2, class V2, class TC2, class R2, class D2&gt;
  662. typename enable_if_interoperable&lt;Dr1,Dr2,bool&gt;::type
  663. operator ==(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  664. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  665. </pre>
  666. <table class="docutils field-list" frame="void" rules="none">
  667. <col class="field-name" />
  668. <col class="field-body" />
  669. <tbody valign="top">
  670. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">if <tt class="docutils literal"><span class="pre">is_convertible&lt;Dr2,Dr1&gt;::value</span></tt></p>
  671. <dl class="last docutils">
  672. <dt>then</dt>
  673. <dd><p class="first last"><tt class="docutils literal"><span class="pre">((Dr1</span> <span class="pre">const&amp;)lhs).equal((Dr2</span> <span class="pre">const&amp;)rhs)</span></tt>.</p>
  674. </dd>
  675. <dt>Otherwise,</dt>
  676. <dd><p class="first last"><tt class="docutils literal"><span class="pre">((Dr2</span> <span class="pre">const&amp;)rhs).equal((Dr1</span> <span class="pre">const&amp;)lhs)</span></tt>.</p>
  677. </dd>
  678. </dl>
  679. </td>
  680. </tr>
  681. </tbody>
  682. </table>
  683. <pre class="literal-block">
  684. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  685. class Dr2, class V2, class TC2, class R2, class D2&gt;
  686. typename enable_if_interoperable&lt;Dr1,Dr2,bool&gt;::type
  687. operator !=(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  688. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  689. </pre>
  690. <table class="docutils field-list" frame="void" rules="none">
  691. <col class="field-name" />
  692. <col class="field-body" />
  693. <tbody valign="top">
  694. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">if <tt class="docutils literal"><span class="pre">is_convertible&lt;Dr2,Dr1&gt;::value</span></tt></p>
  695. <dl class="last docutils">
  696. <dt>then</dt>
  697. <dd><p class="first last"><tt class="docutils literal"><span class="pre">!((Dr1</span> <span class="pre">const&amp;)lhs).equal((Dr2</span> <span class="pre">const&amp;)rhs)</span></tt>.</p>
  698. </dd>
  699. <dt>Otherwise,</dt>
  700. <dd><p class="first last"><tt class="docutils literal"><span class="pre">!((Dr2</span> <span class="pre">const&amp;)rhs).equal((Dr1</span> <span class="pre">const&amp;)lhs)</span></tt>.</p>
  701. </dd>
  702. </dl>
  703. </td>
  704. </tr>
  705. </tbody>
  706. </table>
  707. <pre class="literal-block">
  708. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  709. class Dr2, class V2, class TC2, class R2, class D2&gt;
  710. typename enable_if_interoperable&lt;Dr1,Dr2,bool&gt;::type
  711. operator &lt;(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  712. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  713. </pre>
  714. <table class="docutils field-list" frame="void" rules="none">
  715. <col class="field-name" />
  716. <col class="field-body" />
  717. <tbody valign="top">
  718. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">if <tt class="docutils literal"><span class="pre">is_convertible&lt;Dr2,Dr1&gt;::value</span></tt></p>
  719. <dl class="last docutils">
  720. <dt>then</dt>
  721. <dd><p class="first last"><tt class="docutils literal"><span class="pre">((Dr1</span> <span class="pre">const&amp;)lhs).distance_to((Dr2</span> <span class="pre">const&amp;)rhs)</span> <span class="pre">&lt;</span> <span class="pre">0</span></tt>.</p>
  722. </dd>
  723. <dt>Otherwise,</dt>
  724. <dd><p class="first last"><tt class="docutils literal"><span class="pre">((Dr2</span> <span class="pre">const&amp;)rhs).distance_to((Dr1</span> <span class="pre">const&amp;)lhs)</span> <span class="pre">&gt;</span> <span class="pre">0</span></tt>.</p>
  725. </dd>
  726. </dl>
  727. </td>
  728. </tr>
  729. </tbody>
  730. </table>
  731. <pre class="literal-block">
  732. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  733. class Dr2, class V2, class TC2, class R2, class D2&gt;
  734. typename enable_if_interoperable&lt;Dr1,Dr2,bool&gt;::type
  735. operator &lt;=(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  736. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  737. </pre>
  738. <table class="docutils field-list" frame="void" rules="none">
  739. <col class="field-name" />
  740. <col class="field-body" />
  741. <tbody valign="top">
  742. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">if <tt class="docutils literal"><span class="pre">is_convertible&lt;Dr2,Dr1&gt;::value</span></tt></p>
  743. <dl class="last docutils">
  744. <dt>then</dt>
  745. <dd><p class="first last"><tt class="docutils literal"><span class="pre">((Dr1</span> <span class="pre">const&amp;)lhs).distance_to((Dr2</span> <span class="pre">const&amp;)rhs)</span> <span class="pre">&lt;=</span> <span class="pre">0</span></tt>.</p>
  746. </dd>
  747. <dt>Otherwise,</dt>
  748. <dd><p class="first last"><tt class="docutils literal"><span class="pre">((Dr2</span> <span class="pre">const&amp;)rhs).distance_to((Dr1</span> <span class="pre">const&amp;)lhs)</span> <span class="pre">&gt;=</span> <span class="pre">0</span></tt>.</p>
  749. </dd>
  750. </dl>
  751. </td>
  752. </tr>
  753. </tbody>
  754. </table>
  755. <pre class="literal-block">
  756. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  757. class Dr2, class V2, class TC2, class R2, class D2&gt;
  758. typename enable_if_interoperable&lt;Dr1,Dr2,bool&gt;::type
  759. operator &gt;(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  760. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  761. </pre>
  762. <table class="docutils field-list" frame="void" rules="none">
  763. <col class="field-name" />
  764. <col class="field-body" />
  765. <tbody valign="top">
  766. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">if <tt class="docutils literal"><span class="pre">is_convertible&lt;Dr2,Dr1&gt;::value</span></tt></p>
  767. <dl class="last docutils">
  768. <dt>then</dt>
  769. <dd><p class="first last"><tt class="docutils literal"><span class="pre">((Dr1</span> <span class="pre">const&amp;)lhs).distance_to((Dr2</span> <span class="pre">const&amp;)rhs)</span> <span class="pre">&gt;</span> <span class="pre">0</span></tt>.</p>
  770. </dd>
  771. <dt>Otherwise,</dt>
  772. <dd><p class="first last"><tt class="docutils literal"><span class="pre">((Dr2</span> <span class="pre">const&amp;)rhs).distance_to((Dr1</span> <span class="pre">const&amp;)lhs)</span> <span class="pre">&lt;</span> <span class="pre">0</span></tt>.</p>
  773. </dd>
  774. </dl>
  775. </td>
  776. </tr>
  777. </tbody>
  778. </table>
  779. <pre class="literal-block">
  780. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  781. class Dr2, class V2, class TC2, class R2, class D2&gt;
  782. typename enable_if_interoperable&lt;Dr1,Dr2,bool&gt;::type
  783. operator &gt;=(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  784. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  785. </pre>
  786. <table class="docutils field-list" frame="void" rules="none">
  787. <col class="field-name" />
  788. <col class="field-body" />
  789. <tbody valign="top">
  790. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">if <tt class="docutils literal"><span class="pre">is_convertible&lt;Dr2,Dr1&gt;::value</span></tt></p>
  791. <dl class="last docutils">
  792. <dt>then</dt>
  793. <dd><p class="first last"><tt class="docutils literal"><span class="pre">((Dr1</span> <span class="pre">const&amp;)lhs).distance_to((Dr2</span> <span class="pre">const&amp;)rhs)</span> <span class="pre">&gt;=</span> <span class="pre">0</span></tt>.</p>
  794. </dd>
  795. <dt>Otherwise,</dt>
  796. <dd><p class="first last"><tt class="docutils literal"><span class="pre">((Dr2</span> <span class="pre">const&amp;)rhs).distance_to((Dr1</span> <span class="pre">const&amp;)lhs)</span> <span class="pre">&lt;=</span> <span class="pre">0</span></tt>.</p>
  797. </dd>
  798. </dl>
  799. </td>
  800. </tr>
  801. </tbody>
  802. </table>
  803. <pre class="literal-block" id="minus">
  804. template &lt;class Dr1, class V1, class TC1, class R1, class D1,
  805. class Dr2, class V2, class TC2, class R2, class D2&gt;
  806. typename enable_if_interoperable&lt;Dr1,Dr2,difference&gt;::type
  807. operator -(iterator_facade&lt;Dr1,V1,TC1,R1,D1&gt; const&amp; lhs,
  808. iterator_facade&lt;Dr2,V2,TC2,R2,D2&gt; const&amp; rhs);
  809. </pre>
  810. <table class="docutils field-list" frame="void" rules="none">
  811. <col class="field-name" />
  812. <col class="field-body" />
  813. <tbody valign="top">
  814. <tr class="field"><th class="field-name">Return Type:</th><td class="field-body"><p class="first">if <tt class="docutils literal"><span class="pre">is_convertible&lt;Dr2,Dr1&gt;::value</span></tt></p>
  815. <blockquote>
  816. <dl class="docutils">
  817. <dt>then</dt>
  818. <dd><p class="first last"><tt class="docutils literal"><span class="pre">difference</span></tt> shall be
  819. <tt class="docutils literal"><span class="pre">iterator_traits&lt;Dr1&gt;::difference_type</span></tt>.</p>
  820. </dd>
  821. <dt>Otherwise</dt>
  822. <dd><p class="first last"><tt class="docutils literal"><span class="pre">difference</span></tt> shall be <tt class="docutils literal"><span class="pre">iterator_traits&lt;Dr2&gt;::difference_type</span></tt></p>
  823. </dd>
  824. </dl>
  825. </blockquote>
  826. </td>
  827. </tr>
  828. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">if <tt class="docutils literal"><span class="pre">is_convertible&lt;Dr2,Dr1&gt;::value</span></tt></p>
  829. <dl class="last docutils">
  830. <dt>then</dt>
  831. <dd><p class="first last"><tt class="docutils literal"><span class="pre">-((Dr1</span> <span class="pre">const&amp;)lhs).distance_to((Dr2</span> <span class="pre">const&amp;)rhs)</span></tt>.</p>
  832. </dd>
  833. <dt>Otherwise,</dt>
  834. <dd><p class="first last"><tt class="docutils literal"><span class="pre">((Dr2</span> <span class="pre">const&amp;)rhs).distance_to((Dr1</span> <span class="pre">const&amp;)lhs)</span></tt>.</p>
  835. </dd>
  836. </dl>
  837. </td>
  838. </tr>
  839. </tbody>
  840. </table>
  841. </div>
  842. </div>
  843. <div class="section" id="tutorial-example">
  844. <h1><a class="toc-backref" href="#id31">Tutorial Example</a></h1>
  845. <!-- Copyright David Abrahams 2004. Use, modification and distribution is -->
  846. <!-- subject to the Boost Software License, Version 1.0. (See accompanying -->
  847. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  848. <p>In this section we'll walk through the implementation of a few
  849. iterators using <tt class="docutils literal"><span class="pre">iterator_facade</span></tt>, based around the simple
  850. example of a linked list of polymorphic objects. This example was
  851. inspired by a <a class="reference external" href="http://thread.gmane.org/gmane.comp.lib.boost.user/5100">posting</a> by Keith Macdonald on the <a class="reference external" href="http://www.boost.org/more/mailing_lists.htm#users">Boost-Users</a>
  852. mailing list.</p>
  853. <div class="section" id="the-problem">
  854. <h2><a class="toc-backref" href="#id32">The Problem</a></h2>
  855. <p>Say we've written a polymorphic linked list node base class:</p>
  856. <pre class="literal-block">
  857. # include &lt;iostream&gt;
  858. struct node_base
  859. {
  860. node_base() : m_next(0) {}
  861. // Each node manages all of its tail nodes
  862. virtual ~node_base() { delete m_next; }
  863. // Access the rest of the list
  864. node_base* next() const { return m_next; }
  865. // print to the stream
  866. virtual void print(std::ostream&amp; s) const = 0;
  867. // double the value
  868. virtual void double_me() = 0;
  869. void append(node_base* p)
  870. {
  871. if (m_next)
  872. m_next-&gt;append(p);
  873. else
  874. m_next = p;
  875. }
  876. private:
  877. node_base* m_next;
  878. };
  879. </pre>
  880. <p>Lists can hold objects of different types by linking together
  881. specializations of the following template:</p>
  882. <pre class="literal-block">
  883. template &lt;class T&gt;
  884. struct node : node_base
  885. {
  886. node(T x)
  887. : m_value(x)
  888. {}
  889. void print(std::ostream&amp; s) const { s &lt;&lt; this-&gt;m_value; }
  890. void double_me() { m_value += m_value; }
  891. private:
  892. T m_value;
  893. };
  894. </pre>
  895. <p>And we can print any node using the following streaming operator:</p>
  896. <pre class="literal-block">
  897. inline std::ostream&amp; operator&lt;&lt;(std::ostream&amp; s, node_base const&amp; n)
  898. {
  899. n.print(s);
  900. return s;
  901. }
  902. </pre>
  903. <p>Our first challenge is to build an appropriate iterator over these
  904. lists.</p>
  905. </div>
  906. <div class="section" id="a-basic-iterator-using-iterator-facade">
  907. <h2><a class="toc-backref" href="#id33">A Basic Iterator Using <tt class="docutils literal"><span class="pre">iterator_facade</span></tt></a></h2>
  908. <p>We will construct a <tt class="docutils literal"><span class="pre">node_iterator</span></tt> class using inheritance from
  909. <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> to implement most of the iterator's operations.</p>
  910. <pre class="literal-block">
  911. # include &quot;node.hpp&quot;
  912. # include &lt;boost/iterator/iterator_facade.hpp&gt;
  913. class node_iterator
  914. : public boost::iterator_facade&lt;...&gt;
  915. {
  916. ...
  917. };
  918. </pre>
  919. <div class="section" id="template-arguments-for-iterator-facade">
  920. <h3><a class="toc-backref" href="#id34">Template Arguments for <tt class="docutils literal"><span class="pre">iterator_facade</span></tt></a></h3>
  921. <p><tt class="docutils literal"><span class="pre">iterator_facade</span></tt> has several template parameters, so we must decide
  922. what types to use for the arguments. The parameters are <tt class="docutils literal"><span class="pre">Derived</span></tt>,
  923. <tt class="docutils literal"><span class="pre">Value</span></tt>, <tt class="docutils literal"><span class="pre">CategoryOrTraversal</span></tt>, <tt class="docutils literal"><span class="pre">Reference</span></tt>, and <tt class="docutils literal"><span class="pre">Difference</span></tt>.</p>
  924. <div class="section" id="derived">
  925. <h4><a class="toc-backref" href="#id35"><tt class="docutils literal"><span class="pre">Derived</span></tt></a></h4>
  926. <p>Because <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> is meant to be used with the CRTP
  927. <a class="citation-reference" href="#cop95" id="id10">[Cop95]</a> the first parameter is the iterator class name itself,
  928. <tt class="docutils literal"><span class="pre">node_iterator</span></tt>.</p>
  929. </div>
  930. <div class="section" id="value">
  931. <h4><a class="toc-backref" href="#id36"><tt class="docutils literal"><span class="pre">Value</span></tt></a></h4>
  932. <p>The <tt class="docutils literal"><span class="pre">Value</span></tt> parameter determines the <tt class="docutils literal"><span class="pre">node_iterator</span></tt>'s
  933. <tt class="docutils literal"><span class="pre">value_type</span></tt>. In this case, we are iterating over <tt class="docutils literal"><span class="pre">node_base</span></tt>
  934. objects, so <tt class="docutils literal"><span class="pre">Value</span></tt> will be <tt class="docutils literal"><span class="pre">node_base</span></tt>.</p>
  935. </div>
  936. <div class="section" id="categoryortraversal">
  937. <h4><a class="toc-backref" href="#id37"><tt class="docutils literal"><span class="pre">CategoryOrTraversal</span></tt></a></h4>
  938. <p>Now we have to determine which <a class="reference external" href="new-iter-concepts.html#iterator-traversal-concepts-lib-iterator-traversal">iterator traversal concept</a> our
  939. <tt class="docutils literal"><span class="pre">node_iterator</span></tt> is going to model. Singly-linked lists only have
  940. forward links, so our iterator can't can't be a <a class="reference external" href="new-iter-concepts.html#bidirectional-traversal-iterators-lib-bidirectional-traversal-iterators">bidirectional
  941. traversal iterator</a>. Our iterator should be able to make multiple
  942. passes over the same linked list (unlike, say, an
  943. <tt class="docutils literal"><span class="pre">istream_iterator</span></tt> which consumes the stream it traverses), so it
  944. must be a <a class="reference external" href="new-iter-concepts.html#forward-traversal-iterators-lib-forward-traversal-iterators">forward traversal iterator</a>. Therefore, we'll pass
  945. <tt class="docutils literal"><span class="pre">boost::forward_traversal_tag</span></tt> in this position<a class="footnote-reference" href="#category" id="id11"><sup>1</sup></a>.</p>
  946. <table class="docutils footnote" frame="void" id="category" rules="none">
  947. <colgroup><col class="label" /><col /></colgroup>
  948. <tbody valign="top">
  949. <tr><td class="label"><a class="fn-backref" href="#id11">[1]</a></td><td><tt class="docutils literal"><span class="pre">iterator_facade</span></tt> also supports old-style category
  950. tags, so we could have passed <tt class="docutils literal"><span class="pre">std::forward_iterator_tag</span></tt> here;
  951. either way, the resulting iterator's <tt class="docutils literal"><span class="pre">iterator_category</span></tt> will
  952. end up being <tt class="docutils literal"><span class="pre">std::forward_iterator_tag</span></tt>.</td></tr>
  953. </tbody>
  954. </table>
  955. </div>
  956. <div class="section" id="id12">
  957. <h4><a class="toc-backref" href="#id38"><tt class="docutils literal"><span class="pre">Reference</span></tt></a></h4>
  958. <p>The <tt class="docutils literal"><span class="pre">Reference</span></tt> argument becomes the type returned by
  959. <tt class="docutils literal"><span class="pre">node_iterator</span></tt>'s dereference operation, and will also be the
  960. same as <tt class="docutils literal"><span class="pre">std::iterator_traits&lt;node_iterator&gt;::reference</span></tt>. The
  961. library's default for this parameter is <tt class="docutils literal"><span class="pre">Value&amp;</span></tt>; since
  962. <tt class="docutils literal"><span class="pre">node_base&amp;</span></tt> is a good choice for the iterator's <tt class="docutils literal"><span class="pre">reference</span></tt>
  963. type, we can omit this argument, or pass <tt class="docutils literal"><span class="pre">use_default</span></tt>.</p>
  964. </div>
  965. <div class="section" id="difference">
  966. <h4><a class="toc-backref" href="#id39"><tt class="docutils literal"><span class="pre">Difference</span></tt></a></h4>
  967. <p>The <tt class="docutils literal"><span class="pre">Difference</span></tt> argument determines how the distance between
  968. two <tt class="docutils literal"><span class="pre">node_iterator</span></tt>s will be measured and will also be the
  969. same as <tt class="docutils literal"><span class="pre">std::iterator_traits&lt;node_iterator&gt;::difference_type</span></tt>.
  970. The library's default for <tt class="docutils literal"><span class="pre">Difference</span></tt> is <tt class="docutils literal"><span class="pre">std::ptrdiff_t</span></tt>, an
  971. appropriate type for measuring the distance between any two
  972. addresses in memory, and one that works for almost any iterator,
  973. so we can omit this argument, too.</p>
  974. <p>The declaration of <tt class="docutils literal"><span class="pre">node_iterator</span></tt> will therefore look something
  975. like:</p>
  976. <pre class="literal-block">
  977. # include &quot;node.hpp&quot;
  978. # include &lt;boost/iterator/iterator_facade.hpp&gt;
  979. class node_iterator
  980. : public boost::iterator_facade&lt;
  981. node_iterator
  982. , node_base
  983. , boost::forward_traversal_tag
  984. &gt;
  985. {
  986. ...
  987. };
  988. </pre>
  989. </div>
  990. </div>
  991. <div class="section" id="constructors-and-data-members">
  992. <h3><a class="toc-backref" href="#id40">Constructors and Data Members</a></h3>
  993. <p>Next we need to decide how to represent the iterator's position.
  994. This representation will take the form of data members, so we'll
  995. also need to write constructors to initialize them. The
  996. <tt class="docutils literal"><span class="pre">node_iterator</span></tt>'s position is quite naturally represented using
  997. a pointer to a <tt class="docutils literal"><span class="pre">node_base</span></tt>. We'll need a constructor to build an
  998. iterator from a <tt class="docutils literal"><span class="pre">node_base*</span></tt>, and a default constructor to
  999. satisfy the <a class="reference external" href="new-iter-concepts.html#forward-traversal-iterators-lib-forward-traversal-iterators">forward traversal iterator</a> requirements<a class="footnote-reference" href="#default" id="id13"><sup>2</sup></a>.
  1000. Our <tt class="docutils literal"><span class="pre">node_iterator</span></tt> then becomes:</p>
  1001. <pre class="literal-block">
  1002. # include &quot;node.hpp&quot;
  1003. # include &lt;boost/iterator/iterator_facade.hpp&gt;
  1004. class node_iterator
  1005. : public boost::iterator_facade&lt;
  1006. node_iterator
  1007. , node_base
  1008. , boost::forward_traversal_tag
  1009. &gt;
  1010. {
  1011. public:
  1012. node_iterator()
  1013. : m_node(0)
  1014. {}
  1015. explicit node_iterator(node_base* p)
  1016. : m_node(p)
  1017. {}
  1018. private:
  1019. ...
  1020. node_base* m_node;
  1021. };
  1022. </pre>
  1023. <table class="docutils footnote" frame="void" id="default" rules="none">
  1024. <colgroup><col class="label" /><col /></colgroup>
  1025. <tbody valign="top">
  1026. <tr><td class="label"><a class="fn-backref" href="#id13">[2]</a></td><td>Technically, the C++ standard places almost no
  1027. requirements on a default-constructed iterator, so if we were
  1028. really concerned with efficiency, we could've written the
  1029. default constructor to leave <tt class="docutils literal"><span class="pre">m_node</span></tt> uninitialized.</td></tr>
  1030. </tbody>
  1031. </table>
  1032. </div>
  1033. <div class="section" id="implementing-the-core-operations">
  1034. <h3><a class="toc-backref" href="#id41">Implementing the Core Operations</a></h3>
  1035. <p>The last step is to implement the <a class="reference internal" href="#core-operations">core operations</a> required by
  1036. the concepts we want our iterator to model. Referring to the
  1037. <a class="reference internal" href="#core-operations">table</a>, we can see that the first three rows are applicable
  1038. because <tt class="docutils literal"><span class="pre">node_iterator</span></tt> needs to satisfy the requirements for
  1039. <a class="reference external" href="new-iter-concepts.html#readable-iterators-lib-readable-iterators">readable iterator</a>, <a class="reference external" href="new-iter-concepts.html#single-pass-iterators-lib-single-pass-iterators">single pass iterator</a>, and <a class="reference external" href="new-iter-concepts.html#incrementable-iterators-lib-incrementable-iterators">incrementable
  1040. iterator</a>.</p>
  1041. <p>We therefore need to supply <tt class="docutils literal"><span class="pre">dereference</span></tt>,
  1042. <tt class="docutils literal"><span class="pre">equal</span></tt>, and <tt class="docutils literal"><span class="pre">increment</span></tt> members. We don't want these members
  1043. to become part of <tt class="docutils literal"><span class="pre">node_iterator</span></tt>'s public interface, so we can
  1044. make them private and grant friendship to
  1045. <tt class="docutils literal"><span class="pre">boost::iterator_core_access</span></tt>, a &quot;back-door&quot; that
  1046. <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> uses to get access to the core operations:</p>
  1047. <pre class="literal-block">
  1048. # include &quot;node.hpp&quot;
  1049. # include &lt;boost/iterator/iterator_facade.hpp&gt;
  1050. class node_iterator
  1051. : public boost::iterator_facade&lt;
  1052. node_iterator
  1053. , node_base
  1054. , boost::forward_traversal_tag
  1055. &gt;
  1056. {
  1057. public:
  1058. node_iterator()
  1059. : m_node(0) {}
  1060. explicit node_iterator(node_base* p)
  1061. : m_node(p) {}
  1062. private:
  1063. friend class boost::iterator_core_access;
  1064. void increment() { m_node = m_node-&gt;next(); }
  1065. bool equal(node_iterator const&amp; other) const
  1066. {
  1067. return this-&gt;m_node == other.m_node;
  1068. }
  1069. node_base&amp; dereference() const { return *m_node; }
  1070. node_base* m_node;
  1071. };
  1072. </pre>
  1073. <p>Voilà; a complete and conforming readable, forward-traversal
  1074. iterator! For a working example of its use, see <a class="reference external" href="../example/node_iterator1.cpp">this program</a>.</p>
  1075. </div>
  1076. </div>
  1077. <div class="section" id="a-constant-node-iterator">
  1078. <h2><a class="toc-backref" href="#id42">A constant <tt class="docutils literal"><span class="pre">node_iterator</span></tt></a></h2>
  1079. <div class="sidebar">
  1080. <p class="first sidebar-title">Constant and Mutable iterators</p>
  1081. <p>The term <strong>mutable iterator</strong> means an iterator through which
  1082. the object it references (its &quot;referent&quot;) can be modified. A
  1083. <strong>constant iterator</strong> is one which doesn't allow modification of
  1084. its referent.</p>
  1085. <p>The words <em>constant</em> and <em>mutable</em> don't refer to the ability to
  1086. modify the iterator itself. For example, an <tt class="docutils literal"><span class="pre">int</span> <span class="pre">const*</span></tt> is a
  1087. non-<tt class="docutils literal"><span class="pre">const</span></tt> <em>constant iterator</em>, which can be incremented
  1088. but doesn't allow modification of its referent, and <tt class="docutils literal"><span class="pre">int*</span>
  1089. <span class="pre">const</span></tt> is a <tt class="docutils literal"><span class="pre">const</span></tt> <em>mutable iterator</em>, which cannot be
  1090. modified but which allows modification of its referent.</p>
  1091. <p class="last">Confusing? We agree, but those are the standard terms. It
  1092. probably doesn't help much that a container's constant iterator
  1093. is called <tt class="docutils literal"><span class="pre">const_iterator</span></tt>.</p>
  1094. </div>
  1095. <p>Now, our <tt class="docutils literal"><span class="pre">node_iterator</span></tt> gives clients access to both <tt class="docutils literal"><span class="pre">node</span></tt>'s <tt class="docutils literal"><span class="pre">print(std::ostream&amp;)</span> <span class="pre">const</span></tt> member function, but also its
  1096. mutating <tt class="docutils literal"><span class="pre">double_me()</span></tt> member. If we wanted to build a
  1097. <em>constant</em> <tt class="docutils literal"><span class="pre">node_iterator</span></tt>, we'd only have to make three
  1098. changes:</p>
  1099. <pre class="literal-block">
  1100. class const_node_iterator
  1101. : public boost::iterator_facade&lt;
  1102. const_node_iterator
  1103. , node_base <strong>const</strong>
  1104. , boost::forward_traversal_tag
  1105. &gt;
  1106. {
  1107. public:
  1108. const_node_iterator()
  1109. : m_node(0) {}
  1110. explicit const_node_iterator(node_base* p)
  1111. : m_node(p) {}
  1112. private:
  1113. friend class boost::iterator_core_access;
  1114. void increment() { m_node = m_node-&gt;next(); }
  1115. bool equal(const_node_iterator const&amp; other) const
  1116. {
  1117. return this-&gt;m_node == other.m_node;
  1118. }
  1119. node_base <strong>const</strong>&amp; dereference() const { return *m_node; }
  1120. node_base <strong>const</strong>* m_node;
  1121. };
  1122. </pre>
  1123. <div class="sidebar">
  1124. <p class="first sidebar-title"><tt class="docutils literal"><span class="pre">const</span></tt> and an iterator's <tt class="docutils literal"><span class="pre">value_type</span></tt></p>
  1125. <p class="last">The C++ standard requires an iterator's <tt class="docutils literal"><span class="pre">value_type</span></tt> <em>not</em> be
  1126. <tt class="docutils literal"><span class="pre">const</span></tt>-qualified, so <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> strips the
  1127. <tt class="docutils literal"><span class="pre">const</span></tt> from its <tt class="docutils literal"><span class="pre">Value</span></tt> parameter in order to produce the
  1128. iterator's <tt class="docutils literal"><span class="pre">value_type</span></tt>. Making the <tt class="docutils literal"><span class="pre">Value</span></tt> argument
  1129. <tt class="docutils literal"><span class="pre">const</span></tt> provides a useful hint to <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> that the
  1130. iterator is a <em>constant iterator</em>, and the default <tt class="docutils literal"><span class="pre">Reference</span></tt>
  1131. argument will be correct for all lvalue iterators.</p>
  1132. </div>
  1133. <p>As a matter of fact, <tt class="docutils literal"><span class="pre">node_iterator</span></tt> and <tt class="docutils literal"><span class="pre">const_node_iterator</span></tt>
  1134. are so similar that it makes sense to factor the common code out
  1135. into a template as follows:</p>
  1136. <pre class="literal-block">
  1137. template &lt;class Value&gt;
  1138. class node_iter
  1139. : public boost::iterator_facade&lt;
  1140. node_iter&lt;Value&gt;
  1141. , Value
  1142. , boost::forward_traversal_tag
  1143. &gt;
  1144. {
  1145. public:
  1146. node_iter()
  1147. : m_node(0) {}
  1148. explicit node_iter(Value* p)
  1149. : m_node(p) {}
  1150. private:
  1151. friend class boost::iterator_core_access;
  1152. bool equal(node_iter&lt;Value&gt; const&amp; other) const
  1153. {
  1154. return this-&gt;m_node == other.m_node;
  1155. }
  1156. void increment()
  1157. { m_node = m_node-&gt;next(); }
  1158. Value&amp; dereference() const
  1159. { return *m_node; }
  1160. Value* m_node;
  1161. };
  1162. typedef node_iter&lt;node_base&gt; node_iterator;
  1163. typedef node_iter&lt;node_base const&gt; node_const_iterator;
  1164. </pre>
  1165. </div>
  1166. <div class="section" id="interoperability">
  1167. <h2><a class="toc-backref" href="#id43">Interoperability</a></h2>
  1168. <p>Our <tt class="docutils literal"><span class="pre">const_node_iterator</span></tt> works perfectly well on its own, but
  1169. taken together with <tt class="docutils literal"><span class="pre">node_iterator</span></tt> it doesn't quite meet
  1170. expectations. For example, we'd like to be able to pass a
  1171. <tt class="docutils literal"><span class="pre">node_iterator</span></tt> where a <tt class="docutils literal"><span class="pre">node_const_iterator</span></tt> was expected,
  1172. just as you can with <tt class="docutils literal"><span class="pre">std::list&lt;int&gt;</span></tt>'s <tt class="docutils literal"><span class="pre">iterator</span></tt> and
  1173. <tt class="docutils literal"><span class="pre">const_iterator</span></tt>. Furthermore, given a <tt class="docutils literal"><span class="pre">node_iterator</span></tt> and a
  1174. <tt class="docutils literal"><span class="pre">node_const_iterator</span></tt> into the same list, we should be able to
  1175. compare them for equality.</p>
  1176. <p>This expected ability to use two different iterator types together
  1177. is known as <a class="reference external" href="new-iter-concepts.html#interoperable-iterators-lib-interoperable-iterators"><strong>interoperability</strong></a>. Achieving interoperability in
  1178. our case is as simple as templatizing the <tt class="docutils literal"><span class="pre">equal</span></tt> function and
  1179. adding a templatized converting constructor<a class="footnote-reference" href="#broken" id="id16"><sup>3</sup></a><a class="footnote-reference" href="#random" id="id17"><sup>4</sup></a>:</p>
  1180. <pre class="literal-block">
  1181. template &lt;class Value&gt;
  1182. class node_iter
  1183. : public boost::iterator_facade&lt;
  1184. node_iter&lt;Value&gt;
  1185. , Value
  1186. , boost::forward_traversal_tag
  1187. &gt;
  1188. {
  1189. public:
  1190. node_iter()
  1191. : m_node(0) {}
  1192. explicit node_iter(Value* p)
  1193. : m_node(p) {}
  1194. template &lt;class OtherValue&gt;
  1195. node_iter(node_iter&lt;OtherValue&gt; const&amp; other)
  1196. : m_node(other.m_node) {}
  1197. private:
  1198. friend class boost::iterator_core_access;
  1199. template &lt;class&gt; friend class node_iter;
  1200. template &lt;class OtherValue&gt;
  1201. bool equal(node_iter&lt;OtherValue&gt; const&amp; other) const
  1202. {
  1203. return this-&gt;m_node == other.m_node;
  1204. }
  1205. void increment()
  1206. { m_node = m_node-&gt;next(); }
  1207. Value&amp; dereference() const
  1208. { return *m_node; }
  1209. Value* m_node;
  1210. };
  1211. typedef impl::node_iterator&lt;node_base&gt; node_iterator;
  1212. typedef impl::node_iterator&lt;node_base const&gt; node_const_iterator;
  1213. </pre>
  1214. <table class="docutils footnote" frame="void" id="broken" rules="none">
  1215. <colgroup><col class="label" /><col /></colgroup>
  1216. <tbody valign="top">
  1217. <tr><td class="label"><a class="fn-backref" href="#id16">[3]</a></td><td>If you're using an older compiler and it can't handle
  1218. this example, see the <a class="reference external" href="../example/node_iterator2.hpp">example code</a> for workarounds.</td></tr>
  1219. </tbody>
  1220. </table>
  1221. <table class="docutils footnote" frame="void" id="random" rules="none">
  1222. <colgroup><col class="label" /><col /></colgroup>
  1223. <tbody valign="top">
  1224. <tr><td class="label"><a class="fn-backref" href="#id17">[4]</a></td><td>If <tt class="docutils literal"><span class="pre">node_iterator</span></tt> had been a <a class="reference external" href="new-iter-concepts.html#random-access-traversal-iterators-lib-random-access-traversal-iterators">random access
  1225. traversal iterator</a>, we'd have had to templatize its
  1226. <tt class="docutils literal"><span class="pre">distance_to</span></tt> function as well.</td></tr>
  1227. </tbody>
  1228. </table>
  1229. <p>You can see an example program which exercises our interoperable
  1230. iterators <a class="reference external" href="../example/node_iterator2.cpp">here</a>.</p>
  1231. </div>
  1232. <div class="section" id="telling-the-truth">
  1233. <h2><a class="toc-backref" href="#id44">Telling the Truth</a></h2>
  1234. <p>Now <tt class="docutils literal"><span class="pre">node_iterator</span></tt> and <tt class="docutils literal"><span class="pre">node_const_iterator</span></tt> behave exactly as
  1235. you'd expect... almost. We can compare them and we can convert in
  1236. one direction: from <tt class="docutils literal"><span class="pre">node_iterator</span></tt> to <tt class="docutils literal"><span class="pre">node_const_iterator</span></tt>.
  1237. If we try to convert from <tt class="docutils literal"><span class="pre">node_const_iterator</span></tt> to
  1238. <tt class="docutils literal"><span class="pre">node_iterator</span></tt>, we'll get an error when the converting
  1239. constructor tries to initialize <tt class="docutils literal"><span class="pre">node_iterator</span></tt>'s <tt class="docutils literal"><span class="pre">m_node</span></tt>, a
  1240. <tt class="docutils literal"><span class="pre">node*</span></tt> with a <tt class="docutils literal"><span class="pre">node</span> <span class="pre">const*</span></tt>. So what's the problem?</p>
  1241. <p>The problem is that
  1242. <tt class="docutils literal"><span class="pre">boost::</span></tt><a class="reference external" href="../../type_traits/index.html#relationships"><tt class="docutils literal"><span class="pre">is_convertible</span></tt></a><tt class="docutils literal"><span class="pre">&lt;node_const_iterator,node_iterator&gt;::value</span></tt>
  1243. will be <tt class="docutils literal"><span class="pre">true</span></tt>, but it should be <tt class="docutils literal"><span class="pre">false</span></tt>. <a class="reference external" href="../../type_traits/index.html#relationships"><tt class="docutils literal"><span class="pre">is_convertible</span></tt></a>
  1244. lies because it can only see as far as the <em>declaration</em> of
  1245. <tt class="docutils literal"><span class="pre">node_iter</span></tt>'s converting constructor, but can't look inside at
  1246. the <em>definition</em> to make sure it will compile. A perfect solution
  1247. would make <tt class="docutils literal"><span class="pre">node_iter</span></tt>'s converting constructor disappear when
  1248. the <tt class="docutils literal"><span class="pre">m_node</span></tt> conversion would fail.</p>
  1249. <p>In fact, that sort of magic is possible using
  1250. <a class="reference external" href="../../utility/enable_if.html"><tt class="docutils literal"><span class="pre">boost::enable_if</span></tt></a>. By rewriting the converting constructor as
  1251. follows, we can remove it from the overload set when it's not
  1252. appropriate:</p>
  1253. <pre class="literal-block">
  1254. #include &lt;boost/type_traits/is_convertible.hpp&gt;
  1255. #include &lt;boost/utility/enable_if.hpp&gt;
  1256. ...
  1257. private:
  1258. struct enabler {};
  1259. public:
  1260. template &lt;class OtherValue&gt;
  1261. node_iter(
  1262. node_iter&lt;OtherValue&gt; const&amp; other
  1263. , typename boost::enable_if&lt;
  1264. boost::is_convertible&lt;OtherValue*,Value*&gt;
  1265. , enabler
  1266. &gt;::type = enabler()
  1267. )
  1268. : m_node(other.m_node) {}
  1269. </pre>
  1270. </div>
  1271. <div class="section" id="wrap-up">
  1272. <h2><a class="toc-backref" href="#id45">Wrap Up</a></h2>
  1273. <p>This concludes our <tt class="docutils literal"><span class="pre">iterator_facade</span></tt> tutorial, but before you
  1274. stop reading we urge you to take a look at <a class="reference external" href="iterator_adaptor.html"><tt class="docutils literal"><span class="pre">iterator_adaptor</span></tt></a>.
  1275. There's another way to approach writing these iterators which might
  1276. even be superior.</p>
  1277. </div>
  1278. </div>
  1279. </div>
  1280. <div class="footer">
  1281. <hr class="footer" />
  1282. <a class="reference external" href="iterator_facade.rst">View document source</a>.
  1283. Generated by <a class="reference external" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference external" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.
  1284. </div>
  1285. </body>
  1286. </html>