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
8 changes: 7 additions & 1 deletion include/boost/iterator/distance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#define BOOST_ITERATOR_DISTANCE_HPP

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

Expand Down Expand Up @@ -40,7 +42,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
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();
}
Loading