mirror of
https://github.com/boostorg/iterator.git
synced 2026-07-05 16:10:48 +02:00
286c9885d6
* Constrain distance function using is_iterator type trait * Add enable_if include * Fix template syntax in distance function * Fix syntax issue in distance enable_if * Test which overload of distance is called Added a test for custom distance function using Foo struct. * Fix wrong spelling of std::ptrdiff_t * Move Foo struct to global scope * Andrey magic * Fix headers, add copyright * Constrain advance to iterators * Test contraint of advance function with int overload Added overload for advance function to handle integers. * Fix enable_if condition for advance function * Fix template syntax in advance constraint * Fix advance test * Remove constexpr specifier * Make advance overload require conversion in parameter * Explain choice of type long for n parameter
113 lines
2.8 KiB
C++
113 lines
2.8 KiB
C++
// Copyright (C) 2017 Michel Morin.
|
|
//
|
|
// Distributed under the Boost Software License, Version 1.0.
|
|
// (See accompanying file LICENSE_1_0.txt or copy at
|
|
// http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
#include <vector>
|
|
#include <list>
|
|
#include <boost/container/slist.hpp>
|
|
#include <boost/core/lightweight_test.hpp>
|
|
#include <boost/iterator/advance.hpp>
|
|
#include <boost/iterator/transform_iterator.hpp>
|
|
|
|
int twice(int x) { return x + x; }
|
|
|
|
template <typename Iterator>
|
|
void test_advance(Iterator it_from, Iterator it_to, int n)
|
|
{
|
|
boost::advance(it_from, n);
|
|
BOOST_TEST(it_from == it_to);
|
|
}
|
|
|
|
// Definitely not an iterator
|
|
struct Foo
|
|
{
|
|
int x = 0;
|
|
|
|
// Don't use type "int" for "n", otherwise it matches literal int exactly
|
|
// and doesn't demonstrate the effect of enable_if.
|
|
friend
|
|
void advance(Foo &value, long n)
|
|
{
|
|
value.x += 10 * n;
|
|
}
|
|
};
|
|
|
|
int main()
|
|
{
|
|
int array[3] = {1, 2, 3};
|
|
int* ptr1 = array;
|
|
int* ptr2 = array + 3;
|
|
|
|
{
|
|
test_advance(ptr1, ptr2, 3);
|
|
test_advance(ptr2, ptr1, -3);
|
|
|
|
test_advance(
|
|
boost::make_transform_iterator(ptr1, twice)
|
|
, boost::make_transform_iterator(ptr2, twice)
|
|
, 3
|
|
);
|
|
test_advance(
|
|
boost::make_transform_iterator(ptr2, twice)
|
|
, boost::make_transform_iterator(ptr1, twice)
|
|
, -3
|
|
);
|
|
}
|
|
|
|
{
|
|
std::vector<int> ints(ptr1, ptr2);
|
|
test_advance(ints.begin(), ints.end(), 3);
|
|
test_advance(ints.end(), ints.begin(), -3);
|
|
|
|
test_advance(
|
|
boost::make_transform_iterator(ints.begin(), twice)
|
|
, boost::make_transform_iterator(ints.end(), twice)
|
|
, 3
|
|
);
|
|
test_advance(
|
|
boost::make_transform_iterator(ints.end(), twice)
|
|
, boost::make_transform_iterator(ints.begin(), twice)
|
|
, -3
|
|
);
|
|
}
|
|
|
|
{
|
|
std::list<int> ints(ptr1, ptr2);
|
|
test_advance(ints.begin(), ints.end(), 3);
|
|
test_advance(ints.end(), ints.begin(), -3);
|
|
|
|
test_advance(
|
|
boost::make_transform_iterator(ints.begin(), twice)
|
|
, boost::make_transform_iterator(ints.end(), twice)
|
|
, 3
|
|
);
|
|
test_advance(
|
|
boost::make_transform_iterator(ints.end(), twice)
|
|
, boost::make_transform_iterator(ints.begin(), twice)
|
|
, -3
|
|
);
|
|
}
|
|
|
|
{
|
|
boost::container::slist<int> ints(ptr1, ptr2);
|
|
test_advance(ints.begin(), ints.end(), 3);
|
|
|
|
test_advance(
|
|
boost::make_transform_iterator(ints.begin(), twice)
|
|
, boost::make_transform_iterator(ints.end(), twice)
|
|
, 3
|
|
);
|
|
}
|
|
|
|
{
|
|
using boost::advance;
|
|
Foo bar;
|
|
advance(bar, 3);
|
|
BOOST_TEST(bar.x == 30);
|
|
}
|
|
|
|
return boost::report_errors();
|
|
}
|