1 // (C) Copyright David Abrahams 2002. Permission to copy, use, modify,
2 // sell and distribute this software is granted provided this
3 // copyright notice appears in all copies. This software is provided
4 // "as is" without express or implied warranty, and with no claim as
5 // to its suitability for any purpose.
9 // std::iterator_traits<>::iterator_category
10 // std::iterator_traits<>::difference_type
13 // ...for all compilers and iterators
15 // Additionally, if X is a pointer
16 // std::iterator_traits<X>::pointer
18 // Otherwise, if partial specialization is supported or X is not a pointer
19 // std::iterator_traits<X>::value_type
20 // std::iterator_traits<X>::pointer
21 // std::iterator_traits<X>::reference
23 // See http://www.boost.org for most recent version including documentation.
26 // 04 Mar 2001 - More attempted fixes for Intel C++ (David Abrahams)
27 // 03 Mar 2001 - Put all implementation into namespace
28 // boost::detail::iterator_traits_. Some progress made on fixes
29 // for Intel compiler. (David Abrahams)
30 // 02 Mar 2001 - Changed BOOST_MSVC to BOOST_MSVC_STD_ITERATOR in a few
31 // places. (Jeremy Siek)
32 // 19 Feb 2001 - Improved workarounds for stock MSVC6; use yes_type and
33 // no_type from type_traits.hpp; stopped trying to remove_cv
34 // before detecting is_pointer, in honor of the new type_traits
35 // semantics. (David Abrahams)
36 // 13 Feb 2001 - Make it work with nearly all standard-conforming iterators
37 // under raw VC6. The one category remaining which will fail is
38 // that of iterators derived from std::iterator but not
39 // boost::iterator and which redefine difference_type.
40 // 11 Feb 2001 - Clean away code which can never be used (David Abrahams)
41 // 09 Feb 2001 - Always have a definition for each traits member, even if it
42 // can't be properly deduced. These will be incomplete types in
43 // some cases (undefined<void>), but it helps suppress MSVC errors
44 // elsewhere (David Abrahams)
45 // 07 Feb 2001 - Support for more of the traits members where possible, making
46 // this useful as a replacement for std::iterator_traits<T> when
47 // used as a default template parameter.
48 // 06 Feb 2001 - Removed useless #includes of standard library headers
51 #ifndef ITERATOR_DWA122600_HPP_
52 # define ITERATOR_DWA122600_HPP_
54 # include <boost/config.hpp>
57 // STLPort 4.0 and betas have a bug when debugging is enabled and there is no
58 // partial specialization: instead of an iterator_category typedef, the standard
59 // container iterators have _Iterator_category.
61 // Also, whether debugging is enabled or not, there is a broken specialization
62 // of std::iterator<output_iterator_tag,void,void,void,void> which has no
63 // typedefs but iterator_category.
64 # if defined(__SGI_STL_PORT)
66 # if (__SGI_STL_PORT <= 0x410) && !defined(__STL_CLASS_PARTIAL_SPECIALIZATION) && defined(__STL_DEBUG)
67 # define BOOST_BAD_CONTAINER_ITERATOR_CATEGORY_TYPEDEF
70 # define BOOST_BAD_OUTPUT_ITERATOR_SPECIALIZATION
72 # endif // STLPort <= 4.1b4 && no partial specialization
74 # if !defined(BOOST_NO_STD_ITERATOR_TRAITS) \
75 && !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) \
76 && !defined(BOOST_MSVC_STD_ITERATOR)
78 namespace boost { namespace detail {
80 // Define a new template so it can be specialized
81 template <class Iterator>
82 struct iterator_traits
83 : std::iterator_traits<Iterator>
87 }} // namespace boost::detail
91 # if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) \
92 && !defined(BOOST_MSVC_STD_ITERATOR)
94 // This is the case where everything conforms except BOOST_NO_STD_ITERATOR_TRAITS
96 namespace boost { namespace detail {
98 // Rogue Wave Standard Library fools itself into thinking partial
99 // specialization is missing on some platforms (e.g. Sun), so fails to
100 // supply iterator_traits!
101 template <class Iterator>
102 struct iterator_traits
104 typedef typename Iterator::value_type value_type;
105 typedef typename Iterator::reference reference;
106 typedef typename Iterator::pointer pointer;
107 typedef typename Iterator::difference_type difference_type;
108 typedef typename Iterator::iterator_category iterator_category;
112 struct iterator_traits<T*>
114 typedef T value_type;
115 typedef T& reference;
117 typedef std::ptrdiff_t difference_type;
118 typedef std::random_access_iterator_tag iterator_category;
122 struct iterator_traits<T const*>
124 typedef T value_type;
125 typedef T const& reference;
126 typedef T const* pointer;
127 typedef std::ptrdiff_t difference_type;
128 typedef std::random_access_iterator_tag iterator_category;
131 }} // namespace boost::detail
135 # include <boost/type_traits/remove_const.hpp>
136 # include <boost/type_traits/detail/yes_no_type.hpp>
137 # include <boost/type_traits/is_pointer.hpp>
139 # ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
140 # include <boost/type_traits/is_same.hpp>
141 # include <boost/type_traits/remove_pointer.hpp>
143 # ifdef BOOST_BAD_OUTPUT_ITERATOR_SPECIALIZATION
144 # include <boost/type_traits/is_base_and_derived.hpp>
147 # include <boost/mpl/if.hpp>
148 # include <boost/mpl/aux_/has_xxx.hpp>
151 // should be the last #include
152 # include "boost/type_traits/detail/bool_trait_def.hpp"
154 namespace boost { namespace detail {
156 BOOST_MPL_HAS_XXX_TRAIT_DEF(value_type)
157 BOOST_MPL_HAS_XXX_TRAIT_DEF(reference)
158 BOOST_MPL_HAS_XXX_TRAIT_DEF(pointer)
159 BOOST_MPL_HAS_XXX_TRAIT_DEF(difference_type)
160 BOOST_MPL_HAS_XXX_TRAIT_DEF(iterator_category)
162 // is_mutable_iterator --
164 // A metafunction returning true iff T is a mutable iterator type
165 // with a nested value_type. Will only work portably with iterators
166 // whose operator* returns a reference, but that seems to be OK for
167 // the iterators supplied by Dinkumware. Some input iterators may
168 // compile-time if they arrive here, and if the compiler is strict
169 // about not taking the address of an rvalue.
171 // This one detects ordinary mutable iterators - the result of
172 // operator* is convertible to the value_type.
174 type_traits::yes_type is_mutable_iterator_helper(T const*, BOOST_DEDUCED_TYPENAME T::value_type*);
176 // Since you can't take the address of an rvalue, the guts of
177 // is_mutable_iterator_impl will fail if we use &*t directly. This
178 // makes sure we can still work with non-lvalue iterators.
179 template <class T> T* mutable_iterator_lvalue_helper(T& x);
180 int mutable_iterator_lvalue_helper(...);
183 // This one detects output iterators such as ostream_iterator which
184 // return references to themselves.
186 type_traits::yes_type is_mutable_iterator_helper(T const*, T const*);
188 type_traits::no_type is_mutable_iterator_helper(...);
191 struct is_mutable_iterator_impl
195 BOOST_STATIC_CONSTANT(
196 bool, value = sizeof(
197 detail::is_mutable_iterator_helper(
199 , mutable_iterator_lvalue_helper(*t) // like &*t
201 == sizeof(type_traits::yes_type)
205 BOOST_TT_AUX_BOOL_TRAIT_DEF1(
206 is_mutable_iterator,T,::boost::detail::is_mutable_iterator_impl<T>::value)
209 // is_full_iterator_traits --
211 // A metafunction returning true iff T has all the requisite nested
212 // types to satisfy the requirements for a fully-conforming
213 // iterator_traits implementation.
215 struct is_full_iterator_traits_impl
218 has_value_type<T>::value
219 & has_reference<T>::value
220 & has_pointer<T>::value
221 & has_difference_type<T>::value
222 & has_iterator_category<T>::value
226 BOOST_TT_AUX_BOOL_TRAIT_DEF1(
227 is_full_iterator_traits,T,::boost::detail::is_full_iterator_traits_impl<T>::value)
230 # ifdef BOOST_BAD_CONTAINER_ITERATOR_CATEGORY_TYPEDEF
231 BOOST_MPL_HAS_XXX_TRAIT_DEF(_Iterator_category)
233 // is_stlport_40_debug_iterator --
235 // A metafunction returning true iff T has all the requisite nested
236 // types to satisfy the requirements of an STLPort 4.0 debug iterator
237 // iterator_traits implementation.
239 struct is_stlport_40_debug_iterator_impl
242 has_value_type<T>::value
243 & has_reference<T>::value
244 & has_pointer<T>::value
245 & has_difference_type<T>::value
246 & has__Iterator_category<T>::value
250 BOOST_TT_AUX_BOOL_TRAIT_DEF1(
251 is_stlport_40_debug_iterator,T,::boost::detail::is_stlport_40_debug_iterator_impl<T>::value)
254 struct stlport_40_debug_iterator_traits
256 typedef typename T::value_type value_type;
257 typedef typename T::reference reference;
258 typedef typename T::pointer pointer;
259 typedef typename T::difference_type difference_type;
260 typedef typename T::_Iterator_category iterator_category;
262 # endif // BOOST_BAD_CONTAINER_ITERATOR_CATEGORY_TYPEDEF
264 template <class T> struct pointer_iterator_traits;
266 # ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
268 struct pointer_iterator_traits<T*>
270 typedef remove_const<T>::type value_type;
272 typedef T& reference;
273 typedef std::random_access_iterator_tag iterator_category;
274 typedef std::ptrdiff_t difference_type;
278 // In case of no template partial specialization, and if T is a
279 // pointer, iterator_traits<T>::value_type can still be computed. For
280 // some basic types, remove_pointer is manually defined in
281 // type_traits/broken_compiler_spec.hpp. For others, do it yourself.
283 template<class P> class please_invoke_BOOST_TT_BROKEN_COMPILER_SPEC_on_cv_unqualified_pointee;
286 struct pointer_value_type
288 is_same<P, typename remove_pointer<P>::type>
289 , please_invoke_BOOST_TT_BROKEN_COMPILER_SPEC_on_cv_unqualified_pointee<P>
290 , typename remove_const<
291 typename remove_pointer<P>::type
299 struct pointer_reference
301 is_same<P, typename remove_pointer<P>::type>
302 , please_invoke_BOOST_TT_BROKEN_COMPILER_SPEC_on_cv_unqualified_pointee<P>
303 , typename remove_pointer<P>::type&
309 struct pointer_iterator_traits
312 typedef std::random_access_iterator_tag iterator_category;
313 typedef std::ptrdiff_t difference_type;
315 typedef typename pointer_value_type<T>::type value_type;
316 typedef typename pointer_reference<T>::type reference;
319 # endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
321 // We'll sort iterator types into one of these classifications, from which we
322 // can determine the difference_type, pointer, reference, and value_type
323 template <class Iterator>
324 struct standard_iterator_traits
326 typedef typename Iterator::difference_type difference_type;
327 typedef typename Iterator::value_type value_type;
328 typedef typename Iterator::pointer pointer;
329 typedef typename Iterator::reference reference;
330 typedef typename Iterator::iterator_category iterator_category;
333 template <class Iterator>
334 struct msvc_stdlib_mutable_traits
335 : std::iterator_traits<Iterator>
337 typedef typename std::iterator_traits<Iterator>::distance_type difference_type;
338 typedef value_type* pointer;
339 typedef value_type& reference;
342 template <class Iterator>
343 struct msvc_stdlib_const_traits
344 : std::iterator_traits<Iterator>
346 typedef typename std::iterator_traits<Iterator>::distance_type difference_type;
347 typedef const value_type* pointer;
348 typedef const value_type& reference;
351 # ifdef BOOST_BAD_OUTPUT_ITERATOR_SPECIALIZATION
352 template <class Iterator>
353 struct is_bad_output_iterator
354 : is_base_and_derived<
355 std::iterator<std::output_iterator_tag,void,void,void,void>
360 struct bad_output_iterator_traits
362 typedef void value_type;
363 typedef void difference_type;
364 typedef std::output_iterator_tag iterator_category;
365 typedef void pointer;
366 typedef void reference;
370 // If we're looking at an MSVC6 (old Dinkumware) ``standard''
371 // iterator, this will generate an appropriate traits class.
372 template <class Iterator>
373 struct msvc_stdlib_iterator_traits
375 is_mutable_iterator<Iterator>
376 , msvc_stdlib_mutable_traits<Iterator>
377 , msvc_stdlib_const_traits<Iterator>
381 template <class Iterator>
382 struct non_pointer_iterator_traits
384 // if the iterator contains all the right nested types...
385 is_full_iterator_traits<Iterator>
386 // Use a standard iterator_traits implementation
387 , standard_iterator_traits<Iterator>
388 # ifdef BOOST_BAD_CONTAINER_ITERATOR_CATEGORY_TYPEDEF
389 // Check for STLPort 4.0 broken _Iterator_category type
391 is_stlport_40_debug_iterator<Iterator>
392 , stlport_40_debug_iterator_traits<Iterator>
394 // Otherwise, assume it's a Dinkum iterator
395 , msvc_stdlib_iterator_traits<Iterator>
396 # ifdef BOOST_BAD_CONTAINER_ITERATOR_CATEGORY_TYPEDEF
403 template <class Iterator>
404 struct iterator_traits_aux
407 , pointer_iterator_traits<Iterator>
408 , non_pointer_iterator_traits<Iterator>
413 template <class Iterator>
414 struct iterator_traits
416 // Explicit forwarding from base class needed to keep MSVC6 happy
417 // under some circumstances.
419 # ifdef BOOST_BAD_OUTPUT_ITERATOR_SPECIALIZATION
422 is_bad_output_iterator<Iterator>
423 , bad_output_iterator_traits
424 , iterator_traits_aux<Iterator>
427 typedef iterator_traits_aux<Iterator> base;
430 typedef typename base::value_type value_type;
431 typedef typename base::pointer pointer;
432 typedef typename base::reference reference;
433 typedef typename base::difference_type difference_type;
434 typedef typename base::iterator_category iterator_category;
437 // This specialization cuts off ETI (Early Template Instantiation) for MSVC.
438 template <> struct iterator_traits<int>
440 typedef int value_type;
442 typedef int reference;
443 typedef int difference_type;
444 typedef int iterator_category;
447 }} // namespace boost::detail
449 # endif // workarounds
451 namespace boost { namespace detail {
453 namespace iterator_traits_
455 template <class Iterator, class Difference>
456 struct distance_select
458 static Difference execute(Iterator i1, const Iterator i2, ...)
460 Difference result = 0;
469 static Difference execute(Iterator i1, const Iterator i2, std::random_access_iterator_tag*)
474 } // namespace boost::detail::iterator_traits_
476 template <class Iterator>
477 inline typename iterator_traits<Iterator>::difference_type
478 distance(Iterator first, Iterator last)
480 typedef typename iterator_traits<Iterator>::difference_type diff_t;
481 typedef typename ::boost::detail::iterator_traits<Iterator>::iterator_category iterator_category;
483 return iterator_traits_::distance_select<Iterator,diff_t>::execute(
484 first, last, (iterator_category*)0);
492 # undef BOOST_BAD_CONTAINER_ITERATOR_CATEGORY_TYPEDEF
493 # undef BOOST_BAD_OUTPUT_ITERATOR_SPECIALIZATION
495 #endif // ITERATOR_DWA122600_HPP_