2 [section:utilities Iterator Utilities]
4 [section:utilities_traits Traits]
8 Have you ever wanted to write a generic function that can operate
9 on any kind of dereferenceable object? If you have, you've
10 probably run into the problem of how to determine the type that the
13 template <class Dereferenceable>
14 void f(Dereferenceable p)
16 *what-goes-here?* value = \*p;
23 It turns out to be impossible to come up with a fully-general
24 algorithm to do determine *what-goes-here* directly, but it is
25 possible to require that `pointee<Dereferenceable>::type` is
26 correct. Naturally, `pointee` has the same difficulty: it can't
27 determine the appropriate `::type` reliably for all
28 `Dereferenceable`\ s, but it makes very good guesses (it works
29 for all pointers, standard and boost smart pointers, and
30 iterators), and when it guesses wrongly, it can be specialized as
36 struct pointee<third_party_lib::smart_pointer<T> >
42 [h2 `indirect_reference`]
44 `indirect_reference<T>::type` is rather more specialized than
45 `pointee`, and is meant to be used to forward the result of
46 dereferencing an object of its argument type. Most dereferenceable
47 types just return a reference to their pointee, but some return
48 proxy references or return the pointee by value. When that
49 information is needed, call on `indirect_reference`.
51 Both of these templates are essential to the correct functioning of
52 [link boost_iterator.indirect `indirect_iterator`].
54 [h2 `minimum_category`]
56 `minimum_category` takes two iterator categories or two iterator traversal tags
57 and returns the one that is the weakest (i.e. least advanced). For example:
62 std::forward_iterator_tag,
63 std::random_access_iterator_tag
65 std::forward_iterator_tag
67 "Unexpected minimum_category result"
70 [h2 Iterator category and traversal tags manipulation]
72 The library provides several utilities to simplify conversions between iterator categories
75 * `iterator_category_to_traversal<C>::type` - the metafunction takes an iterator category `C` and returns
76 the corresponding traversal tag.
77 * `iterator_traversal<T>::type` - a shorthand for `iterator_category_to_traversal<iterator_category<T>::type>::type`.
78 * `pure_traversal_tag<T>::type` - the metafunction takes a tag `T` which derives from one of the iterator traversal tags
79 and returns that traversal tag. `T` may also derive from other tags describing the iterator (e.g. whether this is a `const`-iterator
80 or not), these additional tags are not considered.
81 * `pure_iterator_traversal<T>::type` - a shorthand for `pure_traversal_tag<iterator_traversal<T>::type>::type`.
87 template <class Dereferenceable>
90 typedef /* see below */ type;
93 [*Requires:] For an object `x` of type `Dereferenceable`, `*x`
94 is well-formed. If `++x` is ill-formed it shall neither be
95 ambiguous nor shall it violate access control, and
96 `Dereferenceable::element_type` shall be an accessible type.
97 Otherwise `iterator_traits<Dereferenceable>::value_type` shall
98 be well formed. \[Note: These requirements need not apply to
99 explicit or partial specializations of `pointee`\]
101 `type` is determined according to the following algorithm, where
102 `x` is an object of type `Dereferenceable`:
104 if ( ++x is ill-formed )
106 return `Dereferenceable::element_type`
108 else if (`*x` is a mutable reference to
109 std::iterator_traits<Dereferenceable>::value_type)
111 return iterator_traits<Dereferenceable>::value_type
115 return iterator_traits<Dereferenceable>::value_type const
118 [h3 `indirect_reference`]
120 template <class Dereferenceable>
121 struct indirect_reference
123 typedef /* see below */ type;
126 [*Requires:] For an object `x` of type `Dereferenceable`, `*x`
127 is well-formed. If `++x` is ill-formed it shall neither be
128 ambiguous nor shall it violate access control, and
129 `pointee<Dereferenceable>::type&` shall be well-formed.
130 Otherwise `iterator_traits<Dereferenceable>::reference` shall
131 be well formed. \[Note: These requirements need not apply to
132 explicit or partial specializations of `indirect_reference`\]
134 `type` is determined according to the following algorithm, where
135 `x` is an object of type `Dereferenceable`:
137 if ( ++x is ill-formed )
138 return `pointee<Dereferenceable>::type&`
140 std::iterator_traits<Dereferenceable>::reference
142 [h3 `minimum_category`]
144 template <typename C1, typename C2>
145 struct minimum_category
147 typedef /* see below */ type;
150 [*Requires:] Both `C1` and `C2` shall be standard iterator categories or
151 iterator traversal tags.
153 `type` is determined according to the following algorithm, where `c1` is an
154 object of type `C1` and `c2` is an object of type `C2`:
156 if (c1 is convertible to c2)
161 [note The above definition relies on the fact that the more restricting categories
162 and traversal tags are convertible to the less restricting ones.]
164 [h3 `iterator_category_to_traversal`]
166 template <typename C>
167 struct iterator_category_to_traversal
169 typedef /* see below */ type;
172 [*Requires:] `C` shall be a standard iterator category or an
173 iterator traversal tag.
175 If `C` is an iterator traversal tag or convertible to one, `type` equivalent to `C`.
176 Otherwise, `type` is defined to the closest iterator traversal tag matching `C`.
178 [h3 `iterator_traversal`]
180 template <typename Iterator>
181 struct iterator_traversal
183 typedef typename iterator_category_to_traversal<
184 typename iterator_category<Iterator>::type
188 [*Requires:] `Iterator` shall be an iterator.
190 [h3 `pure_traversal_tag`]
192 template <typename T>
193 struct pure_traversal_tag
195 typedef /* see below */ type;
198 [*Requires:] `T` shall be convertible to an iterator traversal tag.
200 `type` is defined to be the most advanced traversal tag `Tag` so that `T` is convertible to `Tag`.
202 [h3 `pure_iterator_traversal`]
204 template <typename Iterator>
205 struct pure_iterator_traversal
207 typedef typename pure_traversal_tag<
208 typename iterator_traversal<Iterator>::type
212 [*Requires:] `Iterator` shall be an iterator.
216 [section:utilities_testing Testing and Concept Checking]
218 The iterator concept checking classes provide a mechanism for a
219 template to report better error messages when a user instantiates
220 the template with a type that does not meet the requirements of the
223 For an introduction to using concept checking classes, see
224 the documentation for the
225 [@../../concept_check/index.html `boost::concept_check`] library.
230 [h3 Iterator Access Concepts]
237 [/ .. |Readable| replace:: *Readable Iterator* ]
238 [/ .. _Readable: ReadableIterator.html ]
240 [/ .. |Writable| replace:: *Writable Iterator* ]
241 [/ .. _Writable: WritableIterator.html ]
243 [/ .. |Swappable| replace:: *Swappable Iterator* ]
244 [/ .. _Swappable: SwappableIterator.html ]
246 [/ .. |Lvalue| replace:: *Lvalue Iterator* ]
247 [/ .. _Lvalue: LvalueIterator.html ]
250 Iterator Traversal Concepts
251 ...........................
260 [/ .. |Incrementable| replace:: *Incrementable Iterator* ]
261 [/ .. _Incrementable: IncrementableIterator.html ]
263 [/ .. |SinglePass| replace:: *Single Pass Iterator* ]
264 [/ .. _SinglePass: SinglePassIterator.html ]
266 [/ .. |Forward| replace:: *Forward Traversal* ]
267 [/ .. _Forward: ForwardTraversal.html ]
269 [/ .. |Bidir| replace:: *Bidirectional Traversal* ]
270 [/ .. _Bidir: BidirectionalTraversal.html ]
272 [/ .. |Random| replace:: *Random Access Traversal* ]
273 [/ .. _Random: RandomAccessTraversal.html ]
277 [h3 `iterator_concepts.hpp` Synopsis]
279 namespace boost_concepts {
281 // Iterator Access Concepts
283 template <typename Iterator>
284 class ReadableIteratorConcept;
288 , typename ValueType = std::iterator_traits<Iterator>::value_type
290 class WritableIteratorConcept;
292 template <typename Iterator>
293 class SwappableIteratorConcept;
295 template <typename Iterator>
296 class LvalueIteratorConcept;
298 // Iterator Traversal Concepts
300 template <typename Iterator>
301 class IncrementableIteratorConcept;
303 template <typename Iterator>
304 class SinglePassIteratorConcept;
306 template <typename Iterator>
307 class ForwardTraversalConcept;
309 template <typename Iterator>
310 class BidirectionalTraversalConcept;
312 template <typename Iterator>
313 class RandomAccessTraversalConcept;
317 template <typename Iterator, typename ConstIterator>
318 class InteroperableIteratorConcept;