index.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. The Boost.Iterator Library |(logo)|__
  6. +++++++++++++++++++++++++++++++++++++++++++++++++
  7. .. |(logo)| image:: ../../../boost.png
  8. :alt: Boost
  9. __ ../../../index.htm
  10. -------------------------------------
  11. :Authors: David Abrahams, Jeremy Siek, Thomas Witt
  12. :Contact: dave@boost-consulting.com, jsiek@osl.iu.edu, witt@styleadvisor.com
  13. :organizations: `Boost Consulting`_, Indiana University `Open Systems
  14. Lab`_, `Zephyr Associates, Inc.`_
  15. :date: $Date$
  16. :copyright: Copyright David Abrahams, Jeremy Siek, Thomas Witt 2003.
  17. .. _`Boost Consulting`: http://www.boost-consulting.com
  18. .. _`Open Systems Lab`: http://www.osl.iu.edu
  19. .. _`Zephyr Associates, Inc.`: http://www.styleadvisor.com
  20. :Abstract: The Boost Iterator Library contains two parts. The first
  21. is a system of concepts_ which extend the C++ standard
  22. iterator requirements. The second is a framework of
  23. components for building iterators based on these
  24. extended concepts and includes several useful iterator
  25. adaptors. The extended iterator concepts have been
  26. carefully designed so that old-style iterators
  27. can fit in the new concepts and so that new-style
  28. iterators will be compatible with old-style algorithms,
  29. though algorithms may need to be updated if they want to
  30. take full advantage of the new-style iterator
  31. capabilities. Several components of this library have
  32. been accepted into the C++ standard technical report.
  33. The components of the Boost Iterator Library replace the
  34. older Boost Iterator Adaptor Library.
  35. .. _concepts: http://www.boost.org/more/generic_programming.html#concept
  36. .. contents:: **Table of Contents**
  37. -------------------------------------
  38. =====================
  39. New-Style Iterators
  40. =====================
  41. The iterator categories defined in C++98 are extremely limiting
  42. because they bind together two orthogonal concepts: traversal and
  43. element access. For example, because a random access iterator is
  44. required to return a reference (and not a proxy) when dereferenced,
  45. it is impossible to capture the capabilities of
  46. ``vector<bool>::iterator`` using the C++98 categories. This is the
  47. infamous "``vector<bool>`` is not a container, and its iterators
  48. aren't random access iterators", debacle about which Herb Sutter
  49. wrote two papers for the standards comittee (n1185_ and n1211_),
  50. and a `Guru of the Week`__. New-style iterators go well beyond
  51. patching up ``vector<bool>``, though: there are lots of other
  52. iterators already in use which can't be adequately represented by
  53. the existing concepts. For details about the new iterator
  54. concepts, see our
  55. .. _n1185: http://www.gotw.ca/publications/N1185.pdf
  56. .. _n1211: http://www.gotw.ca/publications/N1211.pdf
  57. __ http://www.gotw.ca/gotw/050.htm
  58. `Standard Proposal For New-Style Iterators`__ (PDF__)
  59. __ new-iter-concepts.html
  60. __ new-iter-concepts.pdf
  61. =============================
  62. Iterator Facade and Adaptor
  63. =============================
  64. Writing standard-conforming iterators is tricky, but the need comes
  65. up often. In order to ease the implementation of new iterators,
  66. the Boost.Iterator library provides the |facade| class template,
  67. which implements many useful defaults and compile-time checks
  68. designed to help the iterator author ensure that his iterator is
  69. correct.
  70. It is also common to define a new iterator that is similar to some
  71. underlying iterator or iterator-like type, but that modifies some
  72. aspect of the underlying type's behavior. For that purpose, the
  73. library supplies the |adaptor| class template, which is specially
  74. designed to take advantage of as much of the underlying type's
  75. behavior as possible.
  76. The documentation for these two classes can be found at the following
  77. web pages:
  78. * |facade|_ (PDF__)
  79. * |adaptor|_ (PDF__)
  80. .. |facade| replace:: ``iterator_facade``
  81. .. _facade: iterator_facade.html
  82. __ iterator_facade.pdf
  83. .. |adaptor| replace:: ``iterator_adaptor``
  84. .. _adaptor: iterator_adaptor.html
  85. __ iterator_adaptor.pdf
  86. Both |facade| and |adaptor| as well as many of the `specialized
  87. adaptors`_ mentioned below have been proposed for standardization;
  88. see our
  89. `Standard Proposal For Iterator Facade and Adaptor`__ (PDF__)
  90. for more details.
  91. __ facade-and-adaptor.html
  92. __ facade-and-adaptor.pdf
  93. ======================
  94. Specialized Adaptors
  95. ======================
  96. The iterator library supplies a useful suite of standard-conforming
  97. iterator templates based on the Boost `iterator facade and adaptor`_.
  98. * |counting|_ (PDF__): an iterator over a sequence of consecutive values.
  99. Implements a "lazy sequence"
  100. * |filter|_ (PDF__): an iterator over the subset of elements of some
  101. sequence which satisfy a given predicate
  102. * |function_input|_ (PDF__): an input iterator wrapping a generator (nullary
  103. function object); each time the iterator is dereferenced, the function object
  104. is called to get the value to return.
  105. * |function_output|_ (PDF__): an output iterator wrapping a unary function
  106. object; each time an element is written into the dereferenced
  107. iterator, it is passed as a parameter to the function object.
  108. * |generator|_: an input iterator wrapping a generator (nullary
  109. function object); each time the iterator is dereferenced, the function object
  110. is called to get the value to return. This is an outdated analogue of |function_input|_.
  111. * |indirect|_ (PDF__): an iterator over the objects *pointed-to* by the
  112. elements of some sequence.
  113. * |permutation|_ (PDF__): an iterator over the elements of some random-access
  114. sequence, rearranged according to some sequence of integer indices.
  115. * |reverse|_ (PDF__): an iterator which traverses the elements of some
  116. bidirectional sequence in reverse. Corrects many of the
  117. shortcomings of C++98's ``std::reverse_iterator``.
  118. * |shared|_: an iterator over elements of a container whose
  119. lifetime is maintained by a |shared_ptr|_ stored in the iterator.
  120. * |transform|_ (PDF__): an iterator over elements which are the result of
  121. applying some functional transformation to the elements of an
  122. underlying sequence. This component also replaces the old
  123. ``projection_iterator_adaptor``.
  124. * |zip|_ (PDF__): an iterator over tuples of the elements at corresponding
  125. positions of heterogeneous underlying iterators.
  126. .. |counting| replace:: ``counting_iterator``
  127. .. _counting: counting_iterator.html
  128. __ counting_iterator.pdf
  129. .. |filter| replace:: ``filter_iterator``
  130. .. _filter: filter_iterator.html
  131. __ filter_iterator.pdf
  132. .. |function_input| replace:: ``function_input_iterator``
  133. .. _function_input: function_input_iterator.html
  134. __ function_input_iterator.pdf
  135. .. |function_output| replace:: ``function_output_iterator``
  136. .. _function_output: function_output_iterator.html
  137. __ function_output_iterator.pdf
  138. .. |generator| replace:: ``generator_iterator``
  139. .. _generator: generator_iterator.htm
  140. .. |indirect| replace:: ``indirect_iterator``
  141. .. _indirect: indirect_iterator.html
  142. __ indirect_iterator.pdf
  143. .. |permutation| replace:: ``permutation_iterator``
  144. .. _permutation: permutation_iterator.html
  145. __ permutation_iterator.pdf
  146. .. |reverse| replace:: ``reverse_iterator``
  147. .. _reverse: reverse_iterator.html
  148. __ reverse_iterator.pdf
  149. .. |shared| replace:: ``shared_container_iterator``
  150. .. _shared: ../../utility/shared_container_iterator.html
  151. .. |transform| replace:: ``transform_iterator``
  152. .. _transform: transform_iterator.html
  153. __ transform_iterator.pdf
  154. .. |zip| replace:: ``zip_iterator``
  155. .. _zip: zip_iterator.html
  156. __ zip_iterator.pdf
  157. .. |shared_ptr| replace:: ``shared_ptr``
  158. .. _shared_ptr: ../../smart_ptr/shared_ptr.htm
  159. ====================
  160. Iterator Utilities
  161. ====================
  162. Operations
  163. ----------
  164. The standard library does not handle new-style iterators properly,
  165. because it knows nothing about the iterator traversal concepts.
  166. The Boost.Iterator library provides implementations that fully understand
  167. the new concepts for the two basic operations:
  168. - |advance|_
  169. - |distance|_
  170. .. |advance| replace:: ``advance``
  171. .. _advance: advance.html
  172. .. |distance| replace:: ``distance``
  173. .. _distance: distance.html
  174. Traits
  175. ------
  176. * |pointee|_ (PDF__): Provides the capability to deduce the referent types
  177. of pointers, smart pointers and iterators in generic code. Used
  178. in |indirect|.
  179. * |iterator_traits|_ (PDF__): Provides MPL_\ -compatible metafunctions which
  180. retrieve an iterator's traits. Also corrects for the deficiencies
  181. of broken implementations of ``std::iterator_traits``.
  182. .. * |interoperable|_ (PDF__): Provides an MPL_\ -compatible metafunction for
  183. testing iterator interoperability
  184. .. |pointee| replace:: ``pointee.hpp``
  185. .. _pointee: pointee.html
  186. __ pointee.pdf
  187. .. |iterator_traits| replace:: ``iterator_traits.hpp``
  188. .. _iterator_traits: iterator_traits.html
  189. __ iterator_traits.pdf
  190. .. |interoperable| replace:: ``interoperable.hpp``
  191. .. _interoperable: interoperable.html
  192. .. comment! __ interoperable.pdf
  193. .. _MPL: ../../mpl/doc/index.html
  194. Testing and Concept Checking
  195. ----------------------------
  196. * |iterator_concepts|_ (PDF__): Concept checking classes for the new iterator concepts.
  197. * |iterator_archetypes|_ (PDF__): Concept archetype classes for the new iterators concepts.
  198. .. |iterator_concepts| replace:: ``iterator_concepts.hpp``
  199. .. _iterator_concepts: iterator_concepts.html
  200. __ iterator_concepts.pdf
  201. .. |iterator_archetypes| replace:: ``iterator_archetypes.hpp``
  202. .. _iterator_archetypes: iterator_archetypes.html
  203. __ iterator_archetypes.pdf
  204. =======================================================
  205. Upgrading from the old Boost Iterator Adaptor Library
  206. =======================================================
  207. .. _Upgrading:
  208. If you have been using the old Boost Iterator Adaptor library to
  209. implement iterators, you probably wrote a ``Policies`` class which
  210. captures the core operations of your iterator. In the new library
  211. design, you'll move those same core operations into the body of the
  212. iterator class itself. If you were writing a family of iterators,
  213. you probably wrote a `type generator`_ to build the
  214. ``iterator_adaptor`` specialization you needed; in the new library
  215. design you don't need a type generator (though may want to keep it
  216. around as a compatibility aid for older code) because, due to the
  217. use of the Curiously Recurring Template Pattern (CRTP) [Cop95]_,
  218. you can now define the iterator class yourself and acquire
  219. functionality through inheritance from ``iterator_facade`` or
  220. ``iterator_adaptor``. As a result, you also get much finer control
  221. over how your iterator works: you can add additional constructors,
  222. or even override the iterator functionality provided by the
  223. library.
  224. .. _`type generator`: http://www.boost.org/more/generic_programming.html#type_generator
  225. If you're looking for the old ``projection_iterator`` component,
  226. its functionality has been merged into ``transform_iterator``: as
  227. long as the function object's ``result_type`` (or the ``Reference``
  228. template argument, if explicitly specified) is a true reference
  229. type, ``transform_iterator`` will behave like
  230. ``projection_iterator`` used to.
  231. =========
  232. History
  233. =========
  234. In 2000 Dave Abrahams was writing an iterator for a container of
  235. pointers, which would access the pointed-to elements when
  236. dereferenced. Naturally, being a library writer, he decided to
  237. generalize the idea and the Boost Iterator Adaptor library was born.
  238. Dave was inspired by some writings of Andrei Alexandrescu and chose a
  239. policy based design (though he probably didn't capture Andrei's idea
  240. very well - there was only one policy class for all the iterator's
  241. orthogonal properties). Soon Jeremy Siek realized he would need the
  242. library and they worked together to produce a "Boostified" version,
  243. which was reviewed and accepted into the library. They wrote a paper
  244. and made several important revisions of the code.
  245. Eventually, several shortcomings of the older library began to make
  246. the need for a rewrite apparent. Dave and Jeremy started working
  247. at the Santa Cruz C++ committee meeting in 2002, and had quickly
  248. generated a working prototype. At the urging of Mat Marcus, they
  249. decided to use the GenVoca/CRTP pattern approach, and moved the
  250. policies into the iterator class itself. Thomas Witt expressed
  251. interest and became the voice of strict compile-time checking for
  252. the project, adding uses of the SFINAE technique to eliminate false
  253. converting constructors and operators from the overload set. He
  254. also recognized the need for a separate ``iterator_facade``, and
  255. factored it out of ``iterator_adaptor``. Finally, after a
  256. near-complete rewrite of the prototype, they came up with the
  257. library you see today.
  258. .. [Cop95] [Coplien, 1995] Coplien, J., Curiously Recurring Template
  259. Patterns, C++ Report, February 1995, pp. 24-27.
  260. ..
  261. LocalWords: Abrahams Siek Witt const bool Sutter's WG int UL LI href Lvalue
  262. LocalWords: ReadableIterator WritableIterator SwappableIterator cv pre iter
  263. LocalWords: ConstantLvalueIterator MutableLvalueIterator CopyConstructible TR
  264. LocalWords: ForwardTraversalIterator BidirectionalTraversalIterator lvalue
  265. LocalWords: RandomAccessTraversalIterator dereferenceable Incrementable tmp
  266. LocalWords: incrementable xxx min prev inplace png oldeqnew AccessTag struct
  267. LocalWords: TraversalTag typename lvalues DWA Hmm JGS