new-iter-concepts.rst 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. .. Distributed under the Boost
  2. .. Software License, Version 1.0. (See accompanying
  3. .. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. ++++++++++++++++++++++
  5. New Iterator Concepts
  6. ++++++++++++++++++++++
  7. .. Version 1.25 of this ReStructuredText document is the same as
  8. n1550_, the paper accepted by the LWG.
  9. :Author: David Abrahams, Jeremy Siek, Thomas Witt
  10. :Contact: dave@boost-consulting.com, jsiek@osl.iu.edu, witt@styleadvisor.com
  11. :organization: `Boost Consulting`_, Indiana University `Open Systems
  12. Lab`_, `Zephyr Associates, Inc.`_
  13. :date: $Date$
  14. :Number: This is a revised version of n1550_\ =03-0133, which was
  15. accepted for Technical Report 1 by the C++ standard
  16. committee's library working group. This proposal is a
  17. revision of paper n1297_, n1477_, and n1531_.
  18. :copyright: Copyright David Abrahams, Jeremy Siek, and Thomas Witt
  19. 2003.
  20. .. _`Boost Consulting`: http://www.boost-consulting.com
  21. .. _`Open Systems Lab`: http://www.osl.iu.edu
  22. .. _`Zephyr Associates, Inc.`: http://www.styleadvisor.com
  23. .. _`Institute for Transport Railway Operation and Construction`:
  24. http://www.ive.uni-hannover.de
  25. :Abstract: We propose a new system of iterator concepts that treat
  26. access and positioning independently. This allows the
  27. concepts to more closely match the requirements
  28. of algorithms and provides better categorizations
  29. of iterators that are used in practice.
  30. .. contents:: Table of Contents
  31. .. _n1297: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2001/n1297.html
  32. .. _n1477: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1477.html
  33. .. _n1531: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1531.html
  34. .. _n1550: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1550.htm
  35. ============
  36. Motivation
  37. ============
  38. The standard iterator categories and requirements are flawed because
  39. they use a single hierarchy of concepts to address two orthogonal
  40. issues: *iterator traversal* and *value access*. As a result, many
  41. algorithms with requirements expressed in terms of the iterator
  42. categories are too strict. Also, many real-world iterators can not be
  43. accurately categorized. A proxy-based iterator with random-access
  44. traversal, for example, may only legally have a category of "input
  45. iterator", so generic algorithms are unable to take advantage of its
  46. random-access capabilities. The current iterator concept hierarchy is
  47. geared towards iterator traversal (hence the category names), while
  48. requirements that address value access sneak in at various places. The
  49. following table gives a summary of the current value access
  50. requirements in the iterator categories.
  51. +------------------------------------------------------------------------------+
  52. |Value Access Requirements in Existing Iterator Categories |
  53. +========================+=====================================================+
  54. |Output Iterator |``*i = a`` |
  55. +------------------------+-----------------------------------------------------+
  56. |Input Iterator |``*i`` is convertible to ``T`` |
  57. +------------------------+-----------------------------------------------------+
  58. |Forward Iterator |``*i`` is ``T&`` (or ``const T&`` once `issue 200`_ |
  59. | |is resolved) |
  60. +------------------------+-----------------------------------------------------+
  61. |Random Access Iterator |``i[n]`` is convertible to ``T`` (also ``i[n] = t`` |
  62. | |is required for mutable iterators once `issue 299`_ |
  63. | |is resolved) |
  64. +------------------------+-----------------------------------------------------+
  65. .. _issue 200: http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#200
  66. .. _issue 299: http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#299
  67. Because iterator traversal and value access are mixed together in a
  68. single hierarchy, many useful iterators can not be appropriately
  69. categorized. For example, ``vector<bool>::iterator`` is almost a
  70. random access iterator, but the return type is not ``bool&`` (see
  71. `issue 96`_ and Herb Sutter's paper J16/99-0008 = WG21
  72. N1185). Therefore, the iterators of ``vector<bool>`` only meet the
  73. requirements of input iterator and output iterator. This is so
  74. nonintuitive that the C++ standard contradicts itself on this point.
  75. In paragraph 23.2.4/1 it says that a ``vector`` is a sequence that
  76. supports random access iterators.
  77. .. _issue 96: http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#96
  78. Another difficult-to-categorize iterator is the transform iterator, an
  79. adaptor which applies a unary function object to the dereferenced
  80. value of the some underlying iterator (see `transform_iterator`_).
  81. For unary functions such as ``times``, the return type of
  82. ``operator*`` clearly needs to be the ``result_type`` of the function
  83. object, which is typically not a reference. Because random access
  84. iterators are required to return lvalues from ``operator*``, if you
  85. wrap ``int*`` with a transform iterator, you do not get a random
  86. access iterator as might be expected, but an input iterator.
  87. .. _`transform_iterator`: http://www.boost.org/libs/utility/transform_iterator.htm
  88. A third example is found in the vertex and edge iterators of the
  89. `Boost Graph Library`_. These iterators return vertex and edge
  90. descriptors, which are lightweight handles created on-the-fly. They
  91. must be returned by-value. As a result, their current standard
  92. iterator category is ``input_iterator_tag``, which means that,
  93. strictly speaking, you could not use these iterators with algorithms
  94. like ``min_element()``. As a temporary solution, the concept
  95. `Multi-Pass Input Iterator`_ was introduced to describe the vertex and
  96. edge descriptors, but as the design notes for the concept suggest, a
  97. better solution is needed.
  98. .. _Boost Graph Library: http://www.boost.org/libs/graph/doc/table_of_contents.html
  99. .. _Multi-Pass Input Iterator: http://www.boost.org/libs/utility/MultiPassInputIterator.html
  100. In short, there are many useful iterators that do not fit into the
  101. current standard iterator categories. As a result, the following bad
  102. things happen:
  103. - Iterators are often mis-categorized.
  104. - Algorithm requirements are more strict than necessary, because they
  105. cannot separate the need for random access or bidirectional
  106. traversal from the need for a true reference return type.
  107. ========================
  108. Impact on the Standard
  109. ========================
  110. This proposal for TR1 is a pure extension. Further, the new iterator
  111. concepts are backward-compatible with the old iterator requirements,
  112. and old iterators are forward-compatible with the new iterator
  113. concepts. That is to say, iterators that satisfy the old requirements
  114. also satisfy appropriate concepts in the new system, and iterators
  115. modeling the new concepts will automatically satisfy the appropriate
  116. old requirements.
  117. .. I think we need to say something about the resolution to allow
  118. convertibility to any of the old-style tags as a TR issue (hope it
  119. made it). -DWA
  120. .. Hmm, not sure I understand. Are you talking about whether a
  121. standards conforming input iterator is allowed to have
  122. a tag that is not input_iterator_tag but that
  123. is convertible to input_iterator_tag? -JGS
  124. Possible (but not proposed) Changes to the Working Paper
  125. ========================================================
  126. The extensions in this paper suggest several changes we might make
  127. to the working paper for the next standard. These changes are not
  128. a formal part of this proposal for TR1.
  129. Changes to Algorithm Requirements
  130. +++++++++++++++++++++++++++++++++
  131. The algorithms in the standard library could benefit from the new
  132. iterator concepts because the new concepts provide a more accurate way
  133. to express their type requirements. The result is algorithms that are
  134. usable in more situations and have fewer type requirements.
  135. For the next working paper (but not for TR1), the committee should
  136. consider the following changes to the type requirements of algorithms.
  137. These changes are phrased as textual substitutions, listing the
  138. algorithms to which each textual substitution applies.
  139. Forward Iterator -> Forward Traversal Iterator and Readable Iterator
  140. ``find_end, adjacent_find, search, search_n, rotate_copy,
  141. lower_bound, upper_bound, equal_range, binary_search,
  142. min_element, max_element``
  143. Forward Iterator (1) -> Single Pass Iterator and Readable Iterator,
  144. Forward Iterator (2) -> Forward Traversal Iterator and Readable Iterator
  145. ``find_first_of``
  146. Forward Iterator -> Readable Iterator and Writable Iterator
  147. ``iter_swap``
  148. Forward Iterator -> Single Pass Iterator and Writable Iterator
  149. ``fill, generate``
  150. Forward Iterator -> Forward Traversal Iterator and Swappable Iterator
  151. ``rotate``
  152. Forward Iterator (1) -> Swappable Iterator and Single Pass Iterator,
  153. Forward Iterator (2) -> Swappable Iterator and Incrementable Iterator
  154. ``swap_ranges``
  155. Forward Iterator -> Forward Traversal Iterator and Readable Iterator and Writable Iterator
  156. ``remove, remove_if, unique``
  157. Forward Iterator -> Single Pass Iterator and Readable Iterator and Writable Iterator
  158. ``replace, replace_if``
  159. Bidirectional Iterator -> Bidirectional Traversal Iterator and Swappable Iterator
  160. ``reverse``
  161. Bidirectional Iterator -> Bidirectional Traversal Iterator and Readable and Swappable Iterator
  162. ``partition``
  163. Bidirectional Iterator (1) -> Bidirectional Traversal Iterator and Readable Iterator,
  164. Bidirectional Iterator (2) -> Bidirectional Traversal Iterator and Writable Iterator
  165. ``copy_backwards``
  166. Bidirectional Iterator -> Bidirectional Traversal Iterator and Swappable Iterator and Readable Iterator
  167. ``next_permutation, prev_permutation``
  168. Bidirectional Iterator -> Bidirectional Traversal Iterator and Readable Iterator and Writable Iterator
  169. ``stable_partition, inplace_merge``
  170. Bidirectional Iterator -> Bidirectional Traversal Iterator and Readable Iterator
  171. ``reverse_copy``
  172. Random Access Iterator -> Random Access Traversal Iterator and Readable and Writable Iterator
  173. ``random_shuffle, sort, stable_sort, partial_sort, nth_element, push_heap, pop_heap
  174. make_heap, sort_heap``
  175. Input Iterator (2) -> Incrementable Iterator and Readable Iterator
  176. ``equal, mismatch``
  177. Input Iterator (2) -> Incrementable Iterator and Readable Iterator
  178. ``transform``
  179. Deprecations
  180. ++++++++++++
  181. For the next working paper (but not for TR1), the committee should
  182. consider deprecating the old iterator tags, and
  183. std::iterator_traits, since it will be superceded by individual
  184. traits metafunctions.
  185. ``vector<bool>``
  186. ++++++++++++++++
  187. For the next working paper (but not for TR1), the committee should
  188. consider reclassifying ``vector<bool>::iterator`` as a Random
  189. Access Traversal Iterator and Readable Iterator and Writable
  190. Iterator.
  191. ========
  192. Design
  193. ========
  194. The iterator requirements are to be separated into two groups. One set
  195. of concepts handles the syntax and semantics of value access:
  196. - Readable Iterator
  197. - Writable Iterator
  198. - Swappable Iterator
  199. - Lvalue Iterator
  200. The access concepts describe requirements related to ``operator*`` and
  201. ``operator->``, including the ``value_type``, ``reference``, and
  202. ``pointer`` associated types.
  203. The other set of concepts handles traversal:
  204. - Incrementable Iterator
  205. - Single Pass Iterator
  206. - Forward Traversal Iterator
  207. - Bidirectional Traversal Iterator
  208. - Random Access Traversal Iterator
  209. The refinement relationships for the traversal concepts are in the
  210. following diagram.
  211. .. image:: traversal.png
  212. In addition to the iterator movement operators, such as
  213. ``operator++``, the traversal concepts also include requirements on
  214. position comparison such as ``operator==`` and ``operator<``. The
  215. reason for the fine grain slicing of the concepts into the
  216. Incrementable and Single Pass is to provide concepts that are exact
  217. matches with the original input and output iterator requirements.
  218. This proposal also includes a concept for specifying when an iterator
  219. is interoperable with another iterator, in the sense that ``int*`` is
  220. interoperable with ``int const*``.
  221. - Interoperable Iterators
  222. The relationship between the new iterator concepts and the old are
  223. given in the following diagram.
  224. .. image:: oldeqnew.png
  225. Like the old iterator requirements, we provide tags for purposes of
  226. dispatching based on the traversal concepts. The tags are related via
  227. inheritance so that a tag is convertible to another tag if the concept
  228. associated with the first tag is a refinement of the second tag.
  229. Our design reuses ``iterator_traits<Iter>::iterator_category`` to
  230. indicate an iterator's traversal capability. To specify
  231. capabilities not captured by any old-style iterator category, an
  232. iterator designer can use an ``iterator_category`` type that is
  233. convertible to both the the most-derived old iterator category tag
  234. which fits, and the appropriate new iterator traversal tag.
  235. .. dwa2003/1/2: Note that we are not *requiring* convertibility to
  236. a new-style traversal tag in order to meet new concepts.
  237. Old-style iterators still fit, after all.
  238. We do not provide tags for the purposes of dispatching based on the
  239. access concepts, in part because we could not find a way to
  240. automatically infer the right access tags for old-style iterators.
  241. An iterator's writability may be dependent on the assignability of
  242. its ``value_type`` and there's no known way to detect whether an
  243. arbitrary type is assignable. Fortunately, the need for
  244. dispatching based on access capability is not as great as the need
  245. for dispatching based on traversal capability.
  246. A difficult design decision concerned the ``operator[]``. The direct
  247. approach for specifying ``operator[]`` would have a return type of
  248. ``reference``; the same as ``operator*``. However, going in this
  249. direction would mean that an iterator satisfying the old Random Access
  250. Iterator requirements would not necessarily be a model of Readable or
  251. Writable Lvalue Iterator. Instead we have chosen a design that
  252. matches the preferred resolution of `issue 299`_: ``operator[]`` is
  253. only required to return something convertible to the ``value_type``
  254. (for a Readable Iterator), and is required to support assignment
  255. ``i[n] = t`` (for a Writable Iterator).
  256. ===============
  257. Proposed Text
  258. ===============
  259. Addition to [lib.iterator.requirements]
  260. =======================================
  261. Iterator Value Access Concepts [lib.iterator.value.access]
  262. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  263. In the tables below, ``X`` is an iterator type, ``a`` is a constant
  264. object of type ``X``, ``R`` is
  265. ``std::iterator_traits<X>::reference``, ``T`` is
  266. ``std::iterator_traits<X>::value_type``, and ``v`` is a constant
  267. object of type ``T``.
  268. .. _Readable Iterator:
  269. Readable Iterators [lib.readable.iterators]
  270. -------------------------------------------
  271. A class or built-in type ``X`` models the *Readable Iterator* concept
  272. for value type ``T`` if, in addition to ``X`` being Assignable and
  273. Copy Constructible, the following expressions are valid and respect
  274. the stated semantics. ``U`` is the type of any specified member of
  275. type ``T``.
  276. +-----------------------------------------------------------------------------------------------------------------------------+
  277. |Readable Iterator Requirements (in addition to Assignable and Copy Constructible) |
  278. +-----------------------------------+------------------------+----------------------------------------------------------------+
  279. |Expression |Return Type |Note/Precondition |
  280. +===================================+========================+================================================================+
  281. |``iterator_traits<X>::value_type`` |``T`` |Any non-reference, |
  282. | | |non-cv-qualified type |
  283. +-----------------------------------+------------------------+----------------------------------------------------------------+
  284. |``*a`` | Convertible to ``T`` |pre: ``a`` is dereferenceable. If ``a == b`` then ``*a`` |
  285. | | | is equivalent to ``*b``. |
  286. +-----------------------------------+------------------------+----------------------------------------------------------------+
  287. |``a->m`` |``U&`` |pre: ``pre: (*a).m`` is well-defined. Equivalent to ``(*a).m``. |
  288. +-----------------------------------+------------------------+----------------------------------------------------------------+
  289. .. We won't say anything about iterator_traits<X>::reference until the DR is resolved. -JGS
  290. .. _Writable Iterator:
  291. Writable Iterators [lib.writable.iterators]
  292. -------------------------------------------
  293. A class or built-in type ``X`` models the *Writable Iterator* concept
  294. if, in addition to ``X`` being Copy Constructible, the following
  295. expressions are valid and respect the stated semantics. Writable
  296. Iterators have an associated *set of value types*.
  297. +---------------------------------------------------------------------+
  298. |Writable Iterator Requirements (in addition to Copy Constructible) |
  299. +-------------------------+--------------+----------------------------+
  300. |Expression |Return Type |Precondition |
  301. +=========================+==============+============================+
  302. |``*a = o`` | | pre: The type of ``o`` |
  303. | | | is in the set of |
  304. | | | value types of ``X`` |
  305. +-------------------------+--------------+----------------------------+
  306. Swappable Iterators [lib.swappable.iterators]
  307. ---------------------------------------------
  308. A class or built-in type ``X`` models the *Swappable Iterator* concept
  309. if, in addition to ``X`` being Copy Constructible, the following
  310. expressions are valid and respect the stated semantics.
  311. +---------------------------------------------------------------------+
  312. |Swappable Iterator Requirements (in addition to Copy Constructible) |
  313. +-------------------------+-------------+-----------------------------+
  314. |Expression |Return Type |Postcondition |
  315. +=========================+=============+=============================+
  316. |``iter_swap(a, b)`` |``void`` |the pointed to values are |
  317. | | |exchanged |
  318. +-------------------------+-------------+-----------------------------+
  319. [*Note:* An iterator that is a model of the `Readable Iterator`_ and
  320. `Writable Iterator`_ concepts is also a model of *Swappable
  321. Iterator*. *--end note*]
  322. Lvalue Iterators [lib.lvalue.iterators]
  323. ---------------------------------------
  324. The *Lvalue Iterator* concept adds the requirement that the return
  325. type of ``operator*`` type be a reference to the value type of the
  326. iterator.
  327. +-------------------------------------------------------------+
  328. | Lvalue Iterator Requirements |
  329. +-------------+-----------+-----------------------------------+
  330. |Expression |Return Type|Note/Assertion |
  331. +=============+===========+===================================+
  332. |``*a`` | ``T&`` |``T`` is *cv* |
  333. | | |``iterator_traits<X>::value_type`` |
  334. | | |where *cv* is an optional |
  335. | | |cv-qualification. pre: ``a`` is |
  336. | | |dereferenceable. |
  337. +-------------+-----------+-----------------------------------+
  338. If ``X`` is a `Writable Iterator`_ then ``a == b`` if and only if
  339. ``*a`` is the same object as ``*b``. If ``X`` is a `Readable
  340. Iterator`_ then ``a == b`` implies ``*a`` is the same object as
  341. ``*b``.
  342. Iterator Traversal Concepts [lib.iterator.traversal]
  343. ++++++++++++++++++++++++++++++++++++++++++++++++++++
  344. In the tables below, ``X`` is an iterator type, ``a`` and ``b`` are
  345. constant objects of type ``X``, ``r`` and ``s`` are mutable objects of
  346. type ``X``, ``T`` is ``std::iterator_traits<X>::value_type``, and
  347. ``v`` is a constant object of type ``T``.
  348. Incrementable Iterators [lib.incrementable.iterators]
  349. -----------------------------------------------------
  350. A class or built-in type ``X`` models the *Incrementable Iterator*
  351. concept if, in addition to ``X`` being Assignable and Copy
  352. Constructible, the following expressions are valid and respect the
  353. stated semantics.
  354. +------------------------------------------------------------------------------------+
  355. |Incrementable Iterator Requirements (in addition to Assignable, Copy Constructible) |
  356. | |
  357. +--------------------------------+-------------------------------+-------------------+
  358. |Expression |Return Type |Assertion |
  359. +================================+===============================+===================+
  360. |``++r`` |``X&`` |``&r == &++r`` |
  361. +--------------------------------+-------------------------------+-------------------+
  362. |``r++`` | | |
  363. +--------------------------------+-------------------------------+-------------------+
  364. |``*r++`` | | |
  365. +--------------------------------+-------------------------------+-------------------+
  366. |``iterator_traversal<X>::type`` |Convertible to | |
  367. | |``incrementable_traversal_tag``| |
  368. +--------------------------------+-------------------------------+-------------------+
  369. If ``X`` is a `Writable Iterator`_ then ``X a(r++);`` is equivalent
  370. to ``X a(r); ++r;`` and ``*r++ = o`` is equivalent
  371. to ``*r = o; ++r``.
  372. If ``X`` is a `Readable Iterator`_ then ``T z(*r++);`` is equivalent
  373. to ``T z(*r); ++r;``.
  374. .. TR1: incrementable_iterator_tag changed to
  375. incrementable_traversal_tag for consistency.
  376. Single Pass Iterators [lib.single.pass.iterators]
  377. -------------------------------------------------
  378. A class or built-in type ``X`` models the *Single Pass Iterator*
  379. concept if the following expressions are valid and respect the stated
  380. semantics.
  381. +----------------------------------------------------------------------------------------------------------------+
  382. |Single Pass Iterator Requirements (in addition to Incrementable Iterator and Equality Comparable) |
  383. | |
  384. +----------------------------------------+-----------------------------+-------------+---------------------------+
  385. |Expression |Return Type | Operational |Assertion/ |
  386. | | | Semantics |Pre-/Post-condition |
  387. +========================================+=============================+=============+===========================+
  388. |``++r`` |``X&`` | |pre: ``r`` is |
  389. | | | |dereferenceable; post: |
  390. | | | |``r`` is dereferenceable or|
  391. | | | |``r`` is past-the-end |
  392. +----------------------------------------+-----------------------------+-------------+---------------------------+
  393. |``a == b`` |convertible to ``bool`` | |``==`` is an equivalence |
  394. | | | |relation over its domain |
  395. +----------------------------------------+-----------------------------+-------------+---------------------------+
  396. |``a != b`` |convertible to ``bool`` |``!(a == b)``| |
  397. +----------------------------------------+-----------------------------+-------------+---------------------------+
  398. |``iterator_traits<X>::difference_type`` |A signed integral type | | |
  399. | |representing the distance | | |
  400. | |between iterators | | |
  401. +----------------------------------------+-----------------------------+-------------+---------------------------+
  402. |``iterator_traversal<X>::type`` |Convertible to | | |
  403. | |``single_pass_traversal_tag``| | |
  404. +----------------------------------------+-----------------------------+-------------+---------------------------+
  405. .. TR1: single_pass_iterator_tag changed to
  406. single_pass_traversal_tag for consistency
  407. Forward Traversal Iterators [lib.forward.traversal.iterators]
  408. -------------------------------------------------------------
  409. A class or built-in type ``X`` models the *Forward Traversal Iterator*
  410. concept if, in addition to ``X`` meeting the requirements of Default
  411. Constructible and Single Pass Iterator, the following expressions are
  412. valid and respect the stated semantics.
  413. +--------------------------------------------------------------------------------------------------------+
  414. |Forward Traversal Iterator Requirements (in addition to Default Constructible and Single Pass Iterator) |
  415. +---------------------------------------+-----------------------------------+----------------------------+
  416. |Expression |Return Type |Assertion/Note |
  417. +=======================================+===================================+============================+
  418. |``X u;`` |``X&`` |note: ``u`` may have a |
  419. | | |singular value. |
  420. +---------------------------------------+-----------------------------------+----------------------------+
  421. |``++r`` |``X&`` |``r == s`` and ``r`` is |
  422. | | |dereferenceable implies |
  423. | | |``++r == ++s.`` |
  424. +---------------------------------------+-----------------------------------+----------------------------+
  425. |``iterator_traversal<X>::type`` |Convertible to | |
  426. | |``forward_traversal_tag`` | |
  427. +---------------------------------------+-----------------------------------+----------------------------+
  428. .. TR1: forward_traversal_iterator_tag changed to
  429. forward_traversal_tag for consistency
  430. Bidirectional Traversal Iterators [lib.bidirectional.traversal.iterators]
  431. -------------------------------------------------------------------------
  432. A class or built-in type ``X`` models the *Bidirectional Traversal
  433. Iterator* concept if, in addition to ``X`` meeting the requirements of
  434. Forward Traversal Iterator, the following expressions are valid and
  435. respect the stated semantics.
  436. +-----------------------------------------------------------------------------------------------------+
  437. |Bidirectional Traversal Iterator Requirements (in addition to Forward Traversal |
  438. |Iterator) |
  439. +--------------------------------+-------------------------------+--------------+---------------------+
  440. |Expression |Return Type | Operational |Assertion/ |
  441. | | | Semantics |Pre-/Post-condition |
  442. +================================+===============================+==============+=====================+
  443. |``--r`` |``X&`` | |pre: there exists |
  444. | | | |``s`` such that ``r |
  445. | | | |== ++s``. post: |
  446. | | | |``s`` is |
  447. | | | |dereferenceable. |
  448. | | | | |
  449. | | | |``++(--r) == r``. |
  450. | | | |``--r == --s`` |
  451. | | | |implies ``r == |
  452. | | | |s``. ``&r == &--r``. |
  453. +--------------------------------+-------------------------------+--------------+---------------------+
  454. |``r--`` |convertible to ``const X&`` |:: | |
  455. | | | | |
  456. | | | { | |
  457. | | | X tmp = r; | |
  458. | | | --r; | |
  459. | | | return tmp;| |
  460. | | | } | |
  461. +--------------------------------+-------------------------------+--------------+---------------------+
  462. |``iterator_traversal<X>::type`` |Convertible to | | |
  463. | |``bidirectional_traversal_tag``| | |
  464. | | | | |
  465. +--------------------------------+-------------------------------+--------------+---------------------+
  466. .. TR1: bidirectional_traversal_iterator_tag changed to
  467. bidirectional_traversal_tag for consistency
  468. Random Access Traversal Iterators [lib.random.access.traversal.iterators]
  469. -------------------------------------------------------------------------
  470. A class or built-in type ``X`` models the *Random Access Traversal
  471. Iterator* concept if the following expressions are valid and respect
  472. the stated semantics. In the table below, ``Distance`` is
  473. ``iterator_traits<X>::difference_type`` and ``n`` represents a
  474. constant object of type ``Distance``.
  475. +------------------------------------------------------------------------------------------------------------------+
  476. |Random Access Traversal Iterator Requirements (in addition to Bidirectional Traversal Iterator) |
  477. +-------------------------------+---------------------------------+-------------------------+----------------------+
  478. |Expression |Return Type |Operational Semantics |Assertion/ |
  479. | | | |Precondition |
  480. +===============================+=================================+=========================+======================+
  481. |``r += n`` |``X&`` |:: | |
  482. | | | | |
  483. | | | { | |
  484. | | | Distance m = n; | |
  485. | | | if (m >= 0) | |
  486. | | | while (m--) | |
  487. | | | ++r; | |
  488. | | | else | |
  489. | | | while (m++) | |
  490. | | | --r; | |
  491. | | | return r; | |
  492. | | | } | |
  493. +-------------------------------+---------------------------------+-------------------------+----------------------+
  494. |``a + n``, ``n + a`` |``X`` |``{ X tmp = a; return tmp| |
  495. | | |+= n; }`` | |
  496. | | | | |
  497. +-------------------------------+---------------------------------+-------------------------+----------------------+
  498. |``r -= n`` |``X&`` |``return r += -n`` | |
  499. +-------------------------------+---------------------------------+-------------------------+----------------------+
  500. |``a - n`` |``X`` |``{ X tmp = a; return tmp| |
  501. | | |-= n; }`` | |
  502. | | | | |
  503. +-------------------------------+---------------------------------+-------------------------+----------------------+
  504. |``b - a`` |``Distance`` |``a < b ? distance(a,b) |pre: there exists a |
  505. | | |: -distance(b,a)`` |value ``n`` of |
  506. | | | |``Distance`` such that|
  507. | | | |``a + n == b``. ``b |
  508. | | | |== a + (b - a)``. |
  509. +-------------------------------+---------------------------------+-------------------------+----------------------+
  510. |``a[n]`` |convertible to T |``*(a + n)`` |pre: a is a `Readable |
  511. | | | |Iterator`_ |
  512. +-------------------------------+---------------------------------+-------------------------+----------------------+
  513. |``a[n] = v`` |convertible to T |``*(a + n) = v`` |pre: a is a `Writable |
  514. | | | |Iterator`_ |
  515. +-------------------------------+---------------------------------+-------------------------+----------------------+
  516. |``a < b`` |convertible to ``bool`` |``b - a > 0`` |``<`` is a total |
  517. | | | |ordering relation |
  518. +-------------------------------+---------------------------------+-------------------------+----------------------+
  519. |``a > b`` |convertible to ``bool`` |``b < a`` |``>`` is a total |
  520. | | | |ordering relation |
  521. +-------------------------------+---------------------------------+-------------------------+----------------------+
  522. |``a >= b`` |convertible to ``bool`` |``!(a < b)`` | |
  523. +-------------------------------+---------------------------------+-------------------------+----------------------+
  524. |``a <= b`` |convertible to ``bool`` |``!(a > b)`` | |
  525. +-------------------------------+---------------------------------+-------------------------+----------------------+
  526. |``iterator_traversal<X>::type``|Convertible to | | |
  527. | |``random_access_traversal_tag`` | | |
  528. +-------------------------------+---------------------------------+-------------------------+----------------------+
  529. .. TR1: random_access_traversal_iterator_tag changed to
  530. random_access_traversal_tag for consistency
  531. Interoperable Iterators [lib.interoperable.iterators]
  532. -----------------------------------------------------
  533. A class or built-in type ``X`` that models Single Pass Iterator is
  534. *interoperable with* a class or built-in type ``Y`` that also models
  535. Single Pass Iterator if the following expressions are valid and
  536. respect the stated semantics. In the tables below, ``x`` is an object
  537. of type ``X``, ``y`` is an object of type ``Y``, ``Distance`` is
  538. ``iterator_traits<Y>::difference_type``, and ``n`` represents a
  539. constant object of type ``Distance``.
  540. +-----------+-----------------------+---------------------------------------------------+
  541. |Expression |Return Type |Assertion/Precondition/Postcondition |
  542. +===========+=======================+===================================================+
  543. |``y = x`` |``Y`` |post: ``y == x`` |
  544. +-----------+-----------------------+---------------------------------------------------+
  545. |``Y(x)`` |``Y`` |post: ``Y(x) == x`` |
  546. +-----------+-----------------------+---------------------------------------------------+
  547. |``x == y`` |convertible to ``bool``|``==`` is an equivalence relation over its domain. |
  548. +-----------+-----------------------+---------------------------------------------------+
  549. |``y == x`` |convertible to ``bool``|``==`` is an equivalence relation over its domain. |
  550. +-----------+-----------------------+---------------------------------------------------+
  551. |``x != y`` |convertible to ``bool``|``bool(a==b) != bool(a!=b)`` over its domain. |
  552. +-----------+-----------------------+---------------------------------------------------+
  553. |``y != x`` |convertible to ``bool``|``bool(a==b) != bool(a!=b)`` over its domain. |
  554. +-----------+-----------------------+---------------------------------------------------+
  555. If ``X`` and ``Y`` both model Random Access Traversal Iterator then
  556. the following additional requirements must be met.
  557. +-----------+-----------------------+---------------------+--------------------------------------+
  558. |Expression |Return Type |Operational Semantics|Assertion/ Precondition |
  559. +===========+=======================+=====================+======================================+
  560. |``x < y`` |convertible to ``bool``|``y - x > 0`` |``<`` is a total ordering relation |
  561. +-----------+-----------------------+---------------------+--------------------------------------+
  562. |``y < x`` |convertible to ``bool``|``x - y > 0`` |``<`` is a total ordering relation |
  563. +-----------+-----------------------+---------------------+--------------------------------------+
  564. |``x > y`` |convertible to ``bool``|``y < x`` |``>`` is a total ordering relation |
  565. +-----------+-----------------------+---------------------+--------------------------------------+
  566. |``y > x`` |convertible to ``bool``|``x < y`` |``>`` is a total ordering relation |
  567. +-----------+-----------------------+---------------------+--------------------------------------+
  568. |``x >= y`` |convertible to ``bool``|``!(x < y)`` | |
  569. +-----------+-----------------------+---------------------+--------------------------------------+
  570. |``y >= x`` |convertible to ``bool``|``!(y < x)`` | |
  571. +-----------+-----------------------+---------------------+--------------------------------------+
  572. |``x <= y`` |convertible to ``bool``|``!(x > y)`` | |
  573. +-----------+-----------------------+---------------------+--------------------------------------+
  574. |``y <= x`` |convertible to ``bool``|``!(y > x)`` | |
  575. +-----------+-----------------------+---------------------+--------------------------------------+
  576. |``y - x`` |``Distance`` |``distance(Y(x),y)`` |pre: there exists a value ``n`` of |
  577. | | | |``Distance`` such that ``x + n == y``.|
  578. | | | |``y == x + (y - x)``. |
  579. +-----------+-----------------------+---------------------+--------------------------------------+
  580. |``x - y`` |``Distance`` |``distance(y,Y(x))`` |pre: there exists a value ``n`` of |
  581. | | | |``Distance`` such that ``y + n == x``.|
  582. | | | |``x == y + (x - y)``. |
  583. +-----------+-----------------------+---------------------+--------------------------------------+
  584. Addition to [lib.iterator.synopsis]
  585. ===================================
  586. ::
  587. // lib.iterator.traits, traits and tags
  588. template <class Iterator> struct is_readable_iterator;
  589. template <class Iterator> struct iterator_traversal;
  590. struct incrementable_traversal_tag { };
  591. struct single_pass_traversal_tag : incrementable_traversal_tag { };
  592. struct forward_traversal_tag : single_pass_traversal_tag { };
  593. struct bidirectional_traversal_tag : forward_traversal_tag { };
  594. struct random_access_traversal_tag : bidirectional_traversal_tag { };
  595. Addition to [lib.iterator.traits]
  596. =================================
  597. The ``is_readable_iterator`` class
  598. template satisfies the UnaryTypeTrait_ requirements.
  599. Given an iterator type ``X``, ``is_readable_iterator<X>::value``
  600. yields ``true`` if, for an object ``a`` of type ``X``, ``*a`` is
  601. convertible to ``iterator_traits<X>::value_type``, and ``false``
  602. otherwise.
  603. ``iterator_traversal<X>::type`` is
  604. .. parsed-literal::
  605. *category-to-traversal*\ (iterator_traits<X>::iterator_category)
  606. where *category-to-traversal* is defined as follows
  607. .. _`category-to-traversal`:
  608. .. parsed-literal::
  609. *category-to-traversal*\ (C) =
  610. if (C is convertible to incrementable_traversal_tag)
  611. return C;
  612. else if (C is convertible to random_access_iterator_tag)
  613. return random_access_traversal_tag;
  614. else if (C is convertible to bidirectional_iterator_tag)
  615. return bidirectional_traversal_tag;
  616. else if (C is convertible to forward_iterator_tag)
  617. return forward_traversal_tag;
  618. else if (C is convertible to input_iterator_tag)
  619. return single_pass_traversal_tag;
  620. else if (C is convertible to output_iterator_tag)
  621. return incrementable_traversal_tag;
  622. else
  623. *the program is ill-formed*
  624. ===========
  625. Footnotes
  626. ===========
  627. .. _UnaryTypeTrait: n1519_
  628. The UnaryTypeTrait concept is defined in n1519_; the LWG is
  629. considering adding the requirement that specializations are derived
  630. from their nested ``::type``.
  631. .. _n1519: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1519.htm
  632. ..
  633. LocalWords: Abrahams Siek Witt const bool Sutter's WG int UL LI href Lvalue
  634. LocalWords: ReadableIterator WritableIterator SwappableIterator cv pre iter
  635. LocalWords: ConstantLvalueIterator MutableLvalueIterator CopyConstructible TR
  636. LocalWords: ForwardTraversalIterator BidirectionalTraversalIterator lvalue
  637. LocalWords: RandomAccessTraversalIterator dereferenceable Incrementable tmp
  638. LocalWords: incrementable xxx min prev inplace png oldeqnew AccessTag struct
  639. LocalWords: TraversalTag typename lvalues DWA Hmm JGS mis enum