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