Boost.Range

Utilities

Having an abstraction that encapsulates a pair of iterators is very useful. The
standard library uses std::pair in some circumstances, but that
class is cumbersome to use because we need to specify two template arguments,
and for all range algorithm purposes we must enforce the two template arguments
to be the same. Moreover, std::pair<iterator,iterator> is hardly
self-documenting whereas more domain specific class names are. Therefore these
two classes are provided:

The iterator_range class is templated on a Forward
Traversal Iterator and should be used whenever fairly general code is needed.
The sub_range class is templated on a Forward Range and it is less general,
but a bit easier to use since its template
argument is easier to specify. The biggest difference is, however, that a
sub_range can propagate constness because it knows what a
corresponding const_iterator is.

Both classes can be used as ranges since they implement the minimal interface
required for this to work automatically.

Class iterator_range

The intention of the iterator_range class is to encapsulate two
iterators so they fulfill the Forward Range concept. A few other
functions are also provided for convenience.

If the template argument is not a model of Forward Traversal Iterator, one can
still use a subset of the interface. In particular, size() requires
Random Access Iterators whereas empty() only requires Single
Pass Iterators.

Recall that many default constructed iterators
are singular and hence can only be assigned, but not compared or
incremented or anything. Likewise, if one creates a default constructed
iterator_range, then one have to be careful about not doing
anything besides copying.

If an instance of
iterator_range is constructed by a client with two iterators, the
client must ensure that the two iterators delimit a valid closed-open range
[begin,end).

It is worth noticing that the templated constructors and assignment operators
allow conversion from iterator_range<iterator> to
iterator_range<const_iterator>. Similarly, since the comparison
operators have two template arguments, we can compare ranges whenever the
iterators are comparable; for example when we are dealing with const and
non-const iterators from the same container.

Class sub_range

The sub_range class inherits all its functionality
from the iterator_range class.
The sub_range class is often easier to use because
one must specify the Forward Range
template argument instead of an iterator. Moreover, the sub_range
class can propagate constness since it knows what a corresponding
const_iterator is.

The class should be trivial to use as seen below.
Imagine that we have an algorithm that searches for a sub-string in a string.
The
result is an iterator_range, that delimits the match. We need to
store the result
from this algorithm. Here is an example of how we can do it with and without
sub_range