c++boostiteratorstdboost-range

How to use `boost::range` iterators with standard iterators


I have functions that take in std::vector iterators, as in

typedef std::vector<Point> Points;

Points ConvexHull(Points::const_iterator first, Points::const_iterator last);

I usually pass the std iterators to them, but occasionally I need to work with boost iterators, such as boost::join's range iterator. How should I change the parametrizations of my functions, ideally without templates, so that they accept both iterators? Moreover, how do I indicate in each type which iterator concepts I need?

I tried looking at the boost::range documentation but it's overwhelmingly confusing for me and I don't know where to start.

For example, I couldn't find the difference between boost::range_details::any_forward_iterator_interface and boost::range_details::any_forward_iterator_wrapper, and whether I should use either of those to specify that I need a forward iterator.


Edit:

If I use boost::any_range, how can I pass non-const lvalue references?

For example:

template<typename T>
using Range = boost::any_range<T, boost::random_access_traversal_tag, 
                               T, std::ptrdiff_t>;


f(Range<Point> &points);  // defined elsewhere

// -------------

vector<Point> vec;
f(vec);  // error; cannot bind non-const lvalue reference to unrelated type

Solution

  • You should strongly consider using a template. Doing so let's the compiler keep useful information about what operations are actually occurring, which greatly helps it generate optimised output. The std:: convention is to name the type parameter for the concept required. E.g.

    template< class BidirIt, class UnaryPredicate > // anything bidirectional (which includes random access)
    BidirIt std::partition( BidirIt first, BidirIt last, UnaryPredicate p );
    

    If you really don't want a template, you still shouldn't name anything in a detail namespace. Something like

    #include <boost/range/any_range.hpp>
    
    using PointRange = boost::any_range<Point, boost::random_access_traversal_tag>; // or another traversal tag.
    using PointIterator = PointRange::iterator;
    

    You will likely need to pass PointRange & less frequently than, say, int *&. Almost always passing by value is the correct behaviour. It is cheap to copy, as it holds a begin and end iterator from the Range that it was constructed from, nothing more.