iterator.qbk 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. [library Boost.Iterator
  2. [/ version 1.0.1]
  3. [quickbook 1.6]
  4. [authors [Abrahams, David], [Siek, Jeremy], [Witt, Thomas]]
  5. [copyright 2003 2005 David Abrahams Jeremy Siek Thomas Witt]
  6. [category iterator]
  7. [id iterator]
  8. [dirname iterator]
  9. [purpose
  10. ]
  11. [license
  12. Distributed under the Boost Software License, Version 1.0.
  13. (See accompanying file LICENSE_1_0.txt or copy at
  14. <ulink url="http://www.boost.org/LICENSE_1_0.txt">
  15. http://www.boost.org/LICENSE_1_0.txt
  16. </ulink>)
  17. ]
  18. ]
  19. [/ QuickBook Document version 1.0 ]
  20. [/ Images ]
  21. [def _note_ [$images/note.png]]
  22. [def _alert_ [$images/caution.png]]
  23. [def _detail_ [$images/note.png]]
  24. [def _tip_ [$images/tip.png]]
  25. [/ Links ]
  26. [def _iterator_ [@../../../iterator/doc/index.html Boost.Iterator]]
  27. [def _concept_check_ [@../../../concept_check/index.html Boost.ConceptCheck]]
  28. [template sub[x]'''<subscript>'''[x]'''</subscript>''']
  29. [section:intro Introduction]
  30. [def _concepts_ [@http://www.boost.org/more/generic_programming.html#concept concepts]]
  31. The Boost Iterator Library contains two parts. The first
  32. is a system of _concepts_ which extend the C++ standard
  33. iterator requirements. The second is a framework of
  34. components for building iterators based on these
  35. extended concepts and includes several useful iterator
  36. adaptors. The extended iterator concepts have been
  37. carefully designed so that old-style iterators
  38. can fit in the new concepts and so that new-style
  39. iterators will be compatible with old-style algorithms,
  40. though algorithms may need to be updated if they want to
  41. take full advantage of the new-style iterator
  42. capabilities. Several components of this library have
  43. been accepted into the C++ standard technical report.
  44. The components of the Boost Iterator Library replace the
  45. older Boost Iterator Adaptor Library.
  46. [h2 New-Style Iterators]
  47. [def _N1185_ [@http://www.gotw.ca/publications/N1185.pdf N1185]]
  48. [def _N1211_ [@http://www.gotw.ca/publications/N1211.pdf N1211]]
  49. [def _GOTW_50_ [@http://www.gotw.ca/gotw/050.htm Guru of the Week]]
  50. The iterator categories defined in C++98 are extremely limiting
  51. because they bind together two orthogonal concepts: traversal and
  52. element access. For example, because a random access iterator is
  53. required to return a reference (and not a proxy) when dereferenced,
  54. it is impossible to capture the capabilities of
  55. `vector<bool>::iterator` using the C++98 categories. This is the
  56. infamous "`vector<bool>` is not a container, and its iterators
  57. aren't random access iterators", debacle about which Herb Sutter
  58. wrote two papers for the standards comittee (_N1185_ and _N1211_),
  59. and a _GOTW_50_. New-style iterators go well beyond
  60. patching up `vector<bool>`, though: there are lots of other
  61. iterators already in use which can't be adequately represented by
  62. the existing concepts. For details about the new iterator
  63. concepts, see our [@../new-iter-concepts.html Standard Proposal for New-Style Iterators].
  64. [h2 Iterator Facade and Adaptor]
  65. [/
  66. [def _facade_ [link iterator.generic.facade facade]]
  67. [def _adaptor_ [link iterator.generic.adaptor adaptor]]
  68. ]
  69. [def _facade_ [@../iterator_facade.html facade]]
  70. [def _adaptor_ [@../iterator_adaptor.html adaptor]]
  71. Writing standard-conforming iterators is tricky, but the need comes
  72. up often. In order to ease the implementation of new iterators,
  73. the Boost.Iterator library provides the _facade_ class template,
  74. which implements many useful defaults and compile-time checks
  75. designed to help the iterator author ensure that his iterator is
  76. correct.
  77. It is also common to define a new iterator that is similar to some
  78. underlying iterator or iterator-like type, but that modifies some
  79. aspect of the underlying type's behavior. For that purpose, the
  80. library supplies the _adaptor_ class template, which is specially
  81. designed to take advantage of as much of the underlying type's
  82. behavior as possible.
  83. Both _facade_ and _adaptor_ as well as many of the [link iterator.specialized specialized
  84. adaptors] mentioned below have been proposed for standardization
  85. ([@../facade-and-adaptor.html Standard Proposal For Iterator Facade and Adaptor]).
  86. [h2 Specialized Adaptors]
  87. The iterator library supplies a useful suite of standard-conforming
  88. iterator templates based on the Boost [link
  89. iterator.intro.iterator_facade_and_adaptor iterator facade and adaptor]
  90. templates.
  91. [def _counting_ [link iterator.specialized.counting `counting_iterator`]]
  92. [def _filter_ [link iterator.specialized.filter `filter_iterator`]]
  93. [def _function_input_ [@../function_input_iterator.html `function_input_iterator`]]
  94. [def _function_output_ [link iterator.specialized.function_output `function_output_iterator`]]
  95. [def _generator_ [@../generator_iterator.htm `generator_iterator`]]
  96. [def _indirect_ [link iterator.specialized.indirect `indirect_iterator`]]
  97. [def _permutation_ [link iterator.specialized.permutation `permutation_iterator`]]
  98. [def _reverse_ [link iterator.specialized.reverse `reverse_iterator`]]
  99. [def _shared_ [link iterator.specialized.shared_container `shared_container_iterator`]]
  100. [def _transform_ [link iterator.specialized.transform `transform_iterator`]]
  101. [def _zip_ [link iterator.specialized.zip `zip_iterator`]]
  102. [def _shared_ptr_ [@../../smart_ptr/shared_ptr.htm `shared_ptr`]]
  103. * _counting_: an iterator over a sequence of consecutive values.
  104. Implements a "lazy sequence"
  105. * _filter_: an iterator over the subset of elements of some
  106. sequence which satisfy a given predicate
  107. * _function_input_: an input iterator wrapping a generator (nullary
  108. function object); each time the iterator is dereferenced, the function object
  109. is called to get the value to return.
  110. * _function_output_: an output iterator wrapping a unary function
  111. object; each time an element is written into the dereferenced
  112. iterator, it is passed as a parameter to the function object.
  113. * _generator_: an input iterator wrapping a generator (nullary
  114. function object); each time the iterator is dereferenced, the function object
  115. is called to get the value to return. An outdated analogue of _function_input_.
  116. * _indirect_: an iterator over the objects *pointed-to* by the
  117. elements of some sequence.
  118. * _permutation_: an iterator over the elements of some random-access
  119. sequence, rearranged according to some sequence of integer indices.
  120. * _reverse_: an iterator which traverses the elements of some
  121. bidirectional sequence in reverse. Corrects many of the
  122. shortcomings of C++98's `std::reverse_iterator`.
  123. * _shared_: an iterator over elements of a container whose
  124. lifetime is maintained by a _shared_ptr_ stored in the iterator.
  125. * _transform_: an iterator over elements which are the result of
  126. applying some functional transformation to the elements of an
  127. underlying sequence. This component also replaces the old
  128. `projection_iterator_adaptor`.
  129. * _zip_: an iterator over tuples of the elements at corresponding
  130. positions of heterogeneous underlying iterators.
  131. [h2 Iterator Utilities]
  132. [h3 Traits]
  133. [def _pointee_ [link iterator.utilities.traits `pointee.hpp`]]
  134. [def _iterator_traits_ [link iterator.utilities.iterator_traits `iterator_traits.hpp`]]
  135. [def _interoperable_ [@../interoperable.html `interoperable.hpp`]]
  136. [def _MPL_ [@../../mpl/doc/index.html [*MPL]]]
  137. * _pointee_: Provides the capability to deduce the referent types
  138. of pointers, smart pointers and iterators in generic code. Used
  139. in _indirect_.
  140. * _iterator_traits_: Provides _MPL_ compatible metafunctions which
  141. retrieve an iterator's traits. Also corrects for the deficiencies
  142. of broken implementations of `std::iterator_traits`.
  143. [/
  144. * _interoperable_: Provides an _MPL_ compatible metafunction for
  145. testing iterator interoperability
  146. ]
  147. [h3 Testing and Concept Checking]
  148. [def _iterator_concepts_ [link iterator.concepts `iterator_concepts.hpp`]]
  149. [def _iterator_archetypes_ [link iterator.utilities.archetypes `iterator_archetypes.hpp`]]
  150. * _iterator_concepts_: Concept checking classes for the new iterator concepts.
  151. * _iterator_archetypes_: Concept archetype classes for the new iterators concepts.
  152. [h2 Iterator Algorithms]
  153. The library provides a number of generic algorithms for use with iterators. These
  154. algorithms take advantage of the new concepts defined by the library to provide
  155. better performance and functionality.
  156. [def _advance_ [link iterator.algorithms.advance `advance.hpp`]]
  157. [def _distance_ [link iterator.algorithms.distance `distance.hpp`]]
  158. [def _next_prior_ [link iterator.algorithms.next_prior `next_prior.hpp`]]
  159. * _advance_: Provides `advance()` function for advancing an iterator a given number
  160. of positions forward or backward.
  161. * _distance_: Provides `distance()` function for computing distance between two
  162. iterators.
  163. * _next_prior_: Provides `next()` and `prior()` functions for obtaining
  164. next and prior iterators to a given iterator. The functions are also compatible
  165. with non-iterator types.
  166. [endsect]
  167. [include concepts.qbk]
  168. [section:generic Generic Iterators]
  169. [include facade.qbk]
  170. [include adaptor.qbk]
  171. [endsect]
  172. [include specialized_adaptors.qbk]
  173. [section:utilities Utilities]
  174. [include archetypes.qbk]
  175. [include concept_checking.qbk]
  176. [include iterator_traits.qbk]
  177. [include type_traits.qbk]
  178. [endsect]
  179. [include algorithms.qbk]
  180. [section:upgrading Upgrading from the old Boost Iterator Adaptor Library]
  181. [def _type_generator_ [@http://www.boost.org/more/generic_programming.html#type_generator type generator]]
  182. If you have been using the old Boost Iterator Adaptor library to
  183. implement iterators, you probably wrote a `Policies` class which
  184. captures the core operations of your iterator. In the new library
  185. design, you'll move those same core operations into the body of the
  186. iterator class itself. If you were writing a family of iterators,
  187. you probably wrote a _type_generator_ to build the
  188. `iterator_adaptor` specialization you needed; in the new library
  189. design you don't need a type generator (though may want to keep it
  190. around as a compatibility aid for older code) because, due to the
  191. use of the Curiously Recurring Template Pattern (CRTP) [Cop95]_,
  192. you can now define the iterator class yourself and acquire
  193. functionality through inheritance from `iterator_facade` or
  194. `iterator_adaptor`. As a result, you also get much finer control
  195. over how your iterator works: you can add additional constructors,
  196. or even override the iterator functionality provided by the
  197. library.
  198. If you're looking for the old `projection_iterator` component,
  199. its functionality has been merged into _transform_iterator_: as
  200. long as the function object's `result_type` (or the `Reference`
  201. template argument, if explicitly specified) is a true reference
  202. type, _transform_iterator_ will behave like
  203. `projection_iterator` used to.
  204. [endsect]
  205. [section:history History]
  206. In 2000 Dave Abrahams was writing an iterator for a container of
  207. pointers, which would access the pointed-to elements when
  208. dereferenced. Naturally, being a library writer, he decided to
  209. generalize the idea and the Boost Iterator Adaptor library was born.
  210. Dave was inspired by some writings of Andrei Alexandrescu and chose a
  211. policy based design (though he probably didn't capture Andrei's idea
  212. very well - there was only one policy class for all the iterator's
  213. orthogonal properties). Soon Jeremy Siek realized he would need the
  214. library and they worked together to produce a "Boostified" version,
  215. which was reviewed and accepted into the library. They wrote a paper
  216. and made several important revisions of the code.
  217. Eventually, several shortcomings of the older library began to make
  218. the need for a rewrite apparent. Dave and Jeremy started working
  219. at the Santa Cruz C++ committee meeting in 2002, and had quickly
  220. generated a working prototype. At the urging of Mat Marcus, they
  221. decided to use the GenVoca/CRTP pattern approach, and moved the
  222. policies into the iterator class itself. Thomas Witt expressed
  223. interest and became the voice of strict compile-time checking for
  224. the project, adding uses of the SFINAE technique to eliminate false
  225. converting constructors and operators from the overload set. He
  226. also recognized the need for a separate `iterator_facade`, and
  227. factored it out of `iterator_adaptor`. Finally, after a
  228. near-complete rewrite of the prototype, they came up with the
  229. library you see today.
  230. [:\[Coplien, 1995\] Coplien, J., Curiously Recurring Template
  231. Patterns, C++ Report, February 1995, pp. 24-27.]
  232. [endsect]