Converting std::pair of iterators to boost::iterator_range
Asked Answered
D

1

10

I have a std::multimap, and I want to create a boost::iterator_range from equal_range. I found no simple way of doing it in the documentation, so I tried the following:

typedef std::multimap<int, std::string> Map;
Map map;
...
boost::iterator_range<Map::iterator> r(map.equal_range(2));

Surprisingly, it works (using GCC 4.1.2). I am curious how it works. I found no overload for iterator_range constructor that would do it, and multimap::iterator_range obviously has no overload that would return Boost ranges.

Disjoint answered 16/4, 2012 at 9:41 Comment(1)
Note that there is also boost::make_iterator_range that deduces the underlying iterator type from the pair.Hydrazine
K
10

iterator_range_core.hpp:

//! Constructor from a Range
template< class Range >
iterator_range( const Range& r ) :
    m_Begin( impl::adl_begin( r ) ), m_End( impl::adl_end( r ) )
{}

impl::adl_begin takes you to boost::begin. Having a look at begin.hpp we see (among other voodoo):

template< typename Iterator >
inline Iterator range_begin( const std::pair<Iterator,Iterator>& p )
{
    return p.first;
}

And for an example how types can be “adapted” into ranges have a look here (they use pair as an example).

Kaolack answered 16/4, 2012 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.