/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

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</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_lis…