Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion include/boost/iterator/advance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
#define BOOST_ITERATOR_ADVANCE_HPP

#include <boost/config.hpp>
#include <boost/iterator/is_iterator.hpp>
#include <boost/iterator/iterator_categories.hpp>

#include <type_traits>

namespace boost {
namespace iterators {
namespace detail {
Expand Down Expand Up @@ -56,7 +59,9 @@ inline BOOST_CXX14_CONSTEXPR void advance_impl(RandomAccessIterator& it, Distanc
namespace advance_adl_barrier {

template< typename InputIterator, typename Distance >
inline BOOST_CXX14_CONSTEXPR void advance(InputIterator& it, Distance n)
inline BOOST_CXX14_CONSTEXPR
typename std::enable_if< boost::is_iterator< InputIterator >::value, void >::type
advance(InputIterator& it, Distance n)
{
detail::advance_impl(it, n, typename iterator_traversal< InputIterator >::type());
}
Expand Down
10 changes: 9 additions & 1 deletion include/boost/iterator/distance.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (C) 2017 Michel Morin.
// Copyright (C) 2026 Jeremy W. Murphy
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
Expand All @@ -8,9 +9,12 @@
#define BOOST_ITERATOR_DISTANCE_HPP

#include <boost/config.hpp>
#include <boost/iterator/is_iterator.hpp>
#include <boost/iterator/iterator_categories.hpp>
#include <boost/iterator/iterator_traits.hpp>

#include <type_traits>

namespace boost {
namespace iterators {
namespace detail {
Expand Down Expand Up @@ -40,7 +44,11 @@ distance_impl(RandomAccessIterator first, RandomAccessIterator last, random_acce
namespace distance_adl_barrier {

template< typename SinglePassIterator >
inline BOOST_CXX14_CONSTEXPR typename iterator_difference< SinglePassIterator >::type
inline BOOST_CXX14_CONSTEXPR
typename std::enable_if<
boost::is_iterator< SinglePassIterator >::value,
iterator_difference< SinglePassIterator >
>::type::type
distance(SinglePassIterator first, SinglePassIterator last)
{
return detail::distance_impl(first, last, typename iterator_traversal< SinglePassIterator >::type());
Expand Down
19 changes: 19 additions & 0 deletions test/advance_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ void test_advance(Iterator it_from, Iterator it_to, int n)
BOOST_TEST(it_from == it_to);
}

// Definitely not an iterator
struct Foo
{
int x = 0;

friend
void advance(Foo &value, int n)
{
value.x += 10 * n;
}
};

int main()
{
int array[3] = {1, 2, 3};
Expand Down Expand Up @@ -87,5 +99,12 @@ int main()
);
}

{
using boost::advance;
Foo bar;
advance(bar, 3);
BOOST_TEST(bar.x == 30);
}
Comment on lines +102 to +107
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is not quite as convincing as the one for distance, because it still passes if I take away the constraint on advance. Any advice on how to make it fail without the change to advance?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this question is directed at you, @Lastique.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regardless of the constraint on boost::advance, the friend advance is the more preferable overload as it is not a template and the arguments don't require conversions. Make it a less preferable overload so that boost::advance is picked in overload resolution, e.g. make the second argument long or something.


return boost::report_errors();
}
14 changes: 14 additions & 0 deletions test/distance_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <cstddef>
#include <vector>
#include <list>
#include <boost/container/slist.hpp>
Expand All @@ -19,6 +20,13 @@ void test_distance(Iterator it_from, Iterator it_to, int n)
BOOST_TEST(boost::distance(it_from, it_to) == n);
}

// Definitely not an iterator.
struct Foo
{
constexpr friend
std::ptrdiff_t distance(Foo const &, Foo const &) { return -1; }
};

int main()
{
int array[3] = {1, 2, 3};
Expand Down Expand Up @@ -80,5 +88,11 @@ int main()
);
}

{
// Make boost::distance visible since we're not actually in the boost namespace here.
using boost::distance;
auto result = distance(Foo{}, Foo{});
BOOST_TEST(result == -1);
}
return boost::report_errors();
}