From e25d85446ead83024a1873687ac337293287026d Mon Sep 17 00:00:00 2001 From: Andrey Semashev Date: Wed, 12 Jul 2017 21:08:13 +0300 Subject: [PATCH] Use Boost.Iterator to advance iterators. By using Boost.Iterator we rely on the separate traversal category instead of the standard iterator category to advance iterators efficiently. For instance, this allows to advance transform iterators over a random access sequence in constant time, despite that they are formally input iterators. Also, std::reverse_iterator formally requires at least bidirectional iterator as the underlying iterator type. Transform iterators from the example above don't qualify, so potentially std::reverse_iterator could fail to compile. --- include/boost/next_prior.hpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/include/boost/next_prior.hpp b/include/boost/next_prior.hpp index 58da590..bad46ed 100644 --- a/include/boost/next_prior.hpp +++ b/include/boost/next_prior.hpp @@ -22,6 +22,8 @@ #include #include #include +#include +#include namespace boost { @@ -101,7 +103,7 @@ struct next_advance_impl< T, Distance, true > { static T call(T x, Distance n) { - std::advance(x, n); + boost::iterators::advance(x, n); return x; } }; @@ -147,8 +149,8 @@ struct prior_advance_impl< T, Distance, true > static T call(T x, Distance n) { // Avoid negating n to sidestep possible integer overflow - std::reverse_iterator< T > rx(x); - std::advance(rx, n); + boost::iterators::reverse_iterator< T > rx(x); + boost::iterators::advance(rx, n); return rx.base(); } };