1 // Boost next_prior.hpp header file ---------------------------------------//
3 // (C) Copyright Dave Abrahams and Daniel Walker 1999-2003. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/utility for documentation.
10 // 13 Dec 2003 Added next(x, n) and prior(x, n) (Daniel Walker)
12 #ifndef BOOST_NEXT_PRIOR_HPP_INCLUDED
13 #define BOOST_NEXT_PRIOR_HPP_INCLUDED
16 #if defined(_MSC_VER) && _MSC_VER <= 1310
17 #include <boost/mpl/and.hpp>
18 #include <boost/type_traits/is_integral.hpp>
20 #include <boost/type_traits/is_unsigned.hpp>
21 #include <boost/type_traits/integral_promotion.hpp>
22 #include <boost/type_traits/make_signed.hpp>
23 #include <boost/type_traits/has_plus.hpp>
24 #include <boost/type_traits/has_plus_assign.hpp>
25 #include <boost/type_traits/has_minus.hpp>
26 #include <boost/type_traits/has_minus_assign.hpp>
30 // Helper functions for classes like bidirectional iterators not supporting
31 // operator+ and operator-
34 // const std::list<T>::iterator p = get_some_iterator();
35 // const std::list<T>::iterator prev = boost::prior(p);
36 // const std::list<T>::iterator next = boost::next(prev, 2);
38 // Contributed by Dave Abrahams
40 namespace next_prior_detail {
42 template< typename T, typename Distance, bool HasPlus = has_plus< T, Distance >::value >
45 static T call(T x, Distance n)
52 template< typename T, typename Distance >
53 struct next_impl2< T, Distance, true >
55 static T call(T x, Distance n)
62 template< typename T, typename Distance, bool HasPlusAssign = has_plus_assign< T, Distance >::value >
64 public next_impl2< T, Distance >
68 template< typename T, typename Distance >
69 struct next_impl1< T, Distance, true >
71 static T call(T x, Distance n)
82 typename PromotedDistance = typename integral_promotion< Distance >::type,
83 #if !defined(_MSC_VER) || _MSC_VER > 1310
84 bool IsUInt = is_unsigned< PromotedDistance >::value
86 // MSVC 7.1 has problems with applying is_unsigned to non-integral types
87 bool IsUInt = mpl::and_< is_integral< PromotedDistance >, is_unsigned< PromotedDistance > >::value
92 static T call(T x, Distance n)
99 template< typename T, typename Distance, typename PromotedDistance >
100 struct prior_impl3< T, Distance, PromotedDistance, true >
102 static T call(T x, Distance n)
104 typedef typename make_signed< PromotedDistance >::type signed_distance;
105 std::advance(x, -static_cast< signed_distance >(static_cast< PromotedDistance >(n)));
111 template< typename T, typename Distance, bool HasMinus = has_minus< T, Distance >::value >
113 public prior_impl3< T, Distance >
117 template< typename T, typename Distance >
118 struct prior_impl2< T, Distance, true >
120 static T call(T x, Distance n)
127 template< typename T, typename Distance, bool HasMinusAssign = has_minus_assign< T, Distance >::value >
129 public prior_impl2< T, Distance >
133 template< typename T, typename Distance >
134 struct prior_impl1< T, Distance, true >
136 static T call(T x, Distance n)
143 } // namespace next_prior_detail
146 inline T next(T x) { return ++x; }
148 template <class T, class Distance>
149 inline T next(T x, Distance n)
151 return next_prior_detail::next_impl1< T, Distance >::call(x, n);
155 inline T prior(T x) { return --x; }
157 template <class T, class Distance>
158 inline T prior(T x, Distance n)
160 return next_prior_detail::prior_impl1< T, Distance >::call(x, n);
165 #endif // BOOST_NEXT_PRIOR_HPP_INCLUDED