counting_iterator_ref.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. .. Copyright David Abrahams 2006. 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. template <
  6. class Incrementable
  7. , class CategoryOrTraversal = use_default
  8. , class Difference = use_default
  9. >
  10. class counting_iterator
  11. {
  12. public:
  13. typedef Incrementable value_type;
  14. typedef const Incrementable& reference;
  15. typedef const Incrementable* pointer;
  16. typedef /* see below */ difference_type;
  17. typedef /* see below */ iterator_category;
  18. counting_iterator();
  19. counting_iterator(counting_iterator const& rhs);
  20. explicit counting_iterator(Incrementable x);
  21. Incrementable const& base() const;
  22. reference operator*() const;
  23. counting_iterator& operator++();
  24. counting_iterator& operator--();
  25. private:
  26. Incrementable m_inc; // exposition
  27. };
  28. If the ``Difference`` argument is ``use_default`` then
  29. ``difference_type`` is an unspecified signed integral
  30. type. Otherwise ``difference_type`` is ``Difference``.
  31. ``iterator_category`` is determined according to the following
  32. algorithm:
  33. .. parsed-literal::
  34. if (CategoryOrTraversal is not use_default)
  35. return CategoryOrTraversal
  36. else if (numeric_limits<Incrementable>::is_specialized)
  37. return |iterator-category|_\ (
  38. random_access_traversal_tag, Incrementable, const Incrementable&)
  39. else
  40. return |iterator-category|_\ (
  41. iterator_traversal<Incrementable>::type,
  42. Incrementable, const Incrementable&)
  43. [*Note:* implementers are encouraged to provide an implementation of
  44. ``operator-`` and a ``difference_type`` that avoids overflows in
  45. the cases where ``std::numeric_limits<Incrementable>::is_specialized``
  46. is true.]
  47. ``counting_iterator`` requirements
  48. ..................................
  49. The ``Incrementable`` argument shall be Copy Constructible and Assignable.
  50. If ``iterator_category`` is convertible to ``forward_iterator_tag``
  51. or ``forward_traversal_tag``, the following must be well-formed::
  52. Incrementable i, j;
  53. ++i; // pre-increment
  54. i == j; // operator equal
  55. If ``iterator_category`` is convertible to
  56. ``bidirectional_iterator_tag`` or ``bidirectional_traversal_tag``,
  57. the following expression must also be well-formed::
  58. --i
  59. If ``iterator_category`` is convertible to
  60. ``random_access_iterator_tag`` or ``random_access_traversal_tag``,
  61. the following must must also be valid::
  62. counting_iterator::difference_type n;
  63. i += n;
  64. n = i - j;
  65. i < j;
  66. ``counting_iterator`` models
  67. ............................
  68. Specializations of ``counting_iterator`` model Readable Lvalue
  69. Iterator. In addition, they model the concepts corresponding to the
  70. iterator tags to which their ``iterator_category`` is convertible.
  71. Also, if ``CategoryOrTraversal`` is not ``use_default`` then
  72. ``counting_iterator`` models the concept corresponding to the iterator
  73. tag ``CategoryOrTraversal``. Otherwise, if
  74. ``numeric_limits<Incrementable>::is_specialized``, then
  75. ``counting_iterator`` models Random Access Traversal Iterator.
  76. Otherwise, ``counting_iterator`` models the same iterator traversal
  77. concepts modeled by ``Incrementable``.
  78. ``counting_iterator<X,C1,D1>`` is interoperable with
  79. ``counting_iterator<Y,C2,D2>`` if and only if ``X`` is
  80. interoperable with ``Y``.
  81. ``counting_iterator`` operations
  82. ................................
  83. In addition to the operations required by the concepts modeled by
  84. ``counting_iterator``, ``counting_iterator`` provides the following
  85. operations.
  86. ``counting_iterator();``
  87. :Requires: ``Incrementable`` is Default Constructible.
  88. :Effects: Default construct the member ``m_inc``.
  89. ``counting_iterator(counting_iterator const& rhs);``
  90. :Effects: Construct member ``m_inc`` from ``rhs.m_inc``.
  91. ``explicit counting_iterator(Incrementable x);``
  92. :Effects: Construct member ``m_inc`` from ``x``.
  93. ``reference operator*() const;``
  94. :Returns: ``m_inc``
  95. ``counting_iterator& operator++();``
  96. :Effects: ``++m_inc``
  97. :Returns: ``*this``
  98. ``counting_iterator& operator--();``
  99. :Effects: ``--m_inc``
  100. :Returns: ``*this``
  101. ``Incrementable const& base() const;``
  102. :Returns: ``m_inc``