Range v3 is a generic library that augments the existing standard library with facilities for working with ranges. A range can be loosely thought of a pair of iterators, although they need not be implemented that way. Bundling begin/end iterators into a single object brings several benefits.
It's more convenient to pass a single range object to an algorithm than separate begin/end iterators. Compare:
with
Range v3 contains a full implementation of all the standard algorithms with range-based overloads for convenience.
Having a single range object permits pipelines of operations. In a pipeline, a range is lazily adapted or eagerly mutated in some way, with the result immediately available for further adaptation or mutation. Lazy adaption is handled by views, and eager mutation is handled by actions.
A view is a lightweight wrapper that presents a view of an underlying sequence of elements in some custom way without mutating or copying it. Views are cheap to create and copy, and have non-owning reference semantics. Below are some examples:
When you want to mutate a container in-place, or forward it through a chain of mutating operations, you can use actions. The following examples should make it clear.
vector
that already contains some data:Range v3 provides a utility for easily creating your own range types, called range_facade
. The code below uses range_facade
to create a range that traverses a null-terminated string:
The range_facade
class generates an iterator and begin/end member functions from the minimal interface provided by c_string_range
. This is an example of a very simple range for which it is not necessary to separate the range itself from the thing that iterates the range. Future examples will show examples of more sophisticated ranges.
With c_string_range
, you can now use algorithms to operate on null-terminated strings, as below:
Often, a new range type is most easily expressed by adapting an existing range type. That's the case for many of the range views provided by the Range v3 library; for example, the view::remove_if
and view::transform
views. These are rich types with many moving parts, but thanks to a helper class called range_adaptor
, they aren't hard to write.
Below in roughly 2 dozen lines of code is the transform
view, which takes one range and transforms all the elements with a unary function.
Range transformation is achieved by defining a nested adaptor
class that handles the transformation, and then defining begin_adaptor
and end_adaptor
members that return adaptors for the begin iterator and the end sentinel, respectively. The adaptor
class has a current
member that performs the transformation. It is passed an iterator to the current element. Other members are available for customization: equal
, next
, prev
, advance
, and distance_to
; but the transform adaptor accepts the defaults defined in adaptor_base
.
With transform_view
, we can print out the first 20 squares:
The transform_view
defined above is an InputRange when its wrapping an InputRange, a ForwardRange when its wrapping a ForwardRange, etc. That happens because of smart defaults defined in the adaptor_base
class. That frees you from having to deal with a host of niggly detail when implementing iterators.
*(Note: the above transform_view
always stores a copy of the function in the sentinel. That is only necessary if the underlying range's sentinel type models BidirectionalIterator. That's a finer point that you shouldn't worry about right now.)*
The Range v3 library makes heavy use of concepts to constrain functions, control overloading, and check type constraints at compile-time. It achieves this with the help of a Concepts Lite emulation layer that works on any standard-conforming C++11 compiler. The library provides many useful concepts, both for the core language and for iterators and ranges. You can use the concepts framework to constrain your own code.
For instance, if you would like to write a function that takes an iterator/sentinel pair, you can write it like this:
You can then add an overload that take an Iterable:
With the type constraits expressed with the CONCEPTS_REQUIRES_
macro, these two overloads are guaranteed to not be ambiguous.
Range v3 forms the basis for a proposal to add ranges to the standard library (N4128), and will also be the basis for a Technical Specification on Ranges. Its design direction has already passed an initial review by the standardization committee. What that means is that you may see your compiler vendor shipping a library like Range v3 at some point in the future. That's the hope, anyway.
Enjoy!