\tableofcontents
\section tutorial-preface Preface
Range library for C++14/17/20. This code is the basis of the range support in C++20.
Development Status:
This code is fairly stable, well-tested, and suitable for casual use, although currently lacking documentation. No promise is made about support or long-term stability. This code will evolve without regard to backwards compatibility.
A notable exception is anything found within the ranges::cpp20 namespace.
Those components will change rarely or (preferably) never at all.
\subsection tutorial-installation Installation
This library is header-only. You can get the source code from the
range-v3 repository on github. To
compile with Range-v3, just #include the individual headers you want.
This distribution actually contains three separate header-only libraries:
The Range-v3 library is physically structured in directories by feature group:
<numeric> header.begin, end, and size, range traits and concepts, and conversions to
containers.\subsection tutorial-license License
Most of the source code in this project are mine, and those are under the Boost Software License. Parts are taken from Alex Stepanov's Elements of Programming, Howard Hinnant's libc++, and from the SGI STL. Please see the attached LICENSE file and the CREDITS file for the licensing and acknowledgements.
\subsection tutorial-compilers Supported Compilers
The code is known to work on the following compilers:
/permissive- and either /std:c++latest, /std:c++20, or /std:c++17\section tutorial-quick-start Quick Start
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: convenience, composability, and correctness.
Convenience
It's more convenient to pass a single range object to an algorithm than separate begin/end iterators. Compare:
std::vector<int> v{/*...*/};
std::sort( v.begin(), v.end() );
with
std::vector<int> v{/*...*/};
ranges::sort( v );
Range-v3 contains full implementations of all the standard algorithms with range-based overloads for convenience.
Composability
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.
For instance, the below uses views to filter a container using a predicate
and transform the resulting range with a function. Note that the underlying
data is const and is not mutated by the views.
std::vector<int> const vi{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
using namespace ranges;
auto rng = vi | views::remove_if([](int i){ return i % 2 == 1; })
| views::transform([](int i){ return std::to_string(i); });
// rng == {"2","4","6","8","10"};
In the code above, rng simply stores a reference to the underlying data and
the filter and transformation functions. No work is done until rng is
iterated.
In contrast, actions do their work eagerly, but they also compose. Consider the code below, which reads some data into a vector, sorts it, and makes it unique.
extern std::vector<int> read_data();
using namespace ranges;
std::vector<int> vi = read_data() | actions::sort | actions::unique;
Unlike views, with actions each step in the pipeline (actions::sort and
actions::unique) accepts a container by value, mutates it in place, and
returns it.
Correctness
Whether you are using views or actions, you are operating on data in a pure functional, declarative style. You rarely need to trouble yourself with iterators, although they are there under the covers should you need them.
By operating declaratively and functionally instead of imperatively, we reduce the need for overt state manipulation and branches and loops. This brings down the number of states your program can be in, which brings down your bug counts.
In short, if you can find a way to express your solution as a composition of functional transformations on your data, you can make your code correct by construction.
\subsection tutorial-views Views
As described above, the big advantage of ranges over iterators is their composability. They permit a functional style of programming where data is manipulated by passing it through a series of combinators. In addition, the combinators can be lazy, only doing work when the answer is requested, and purely functional, without mutating the original data. This makes it easier to reason about your code.
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 that use views:
Filter a container using a predicate and transform it.
std::vector<int> const vi{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
using namespace ranges;
auto rng = vi | views::remove_if([](int i){return i % 2 == 1;})
| views::transform([](int i){return std::to_string(i);});
// rng == {"2","4","6","8","10"};
Generate an infinite list of integers starting at 1, square them, take the first 10, and sum them:
using namespace ranges;
int sum = accumulate(views::ints(1)
| views::transform([](int i){return i*i;})
| views::take(10), 0);
Generate a sequence on the fly with a range comprehension and initialize a vector with it:
using namespace ranges;
auto vi =
views::for_each(views::ints(1, 10), [](int i) {
return yield_from(views::repeat_n(i, i));
})
| to<std::vector>();
// vi == {1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,...}
Logically, a view is a factory for iterators, but in practice a view is often
implemented as a state machine, with the state stored within the view object
itself (to keep iterators small) and mutated as the view is iterated. Because
the view contains mutable state, many views lack a const-qualified
begin()/end(). When const versions of begin()/end() are provided, they
are truly const; that is, thread-safe.
Since views present the same interface as containers, the temptation is to think
they behave like containers with regard to const-ness. This is not the case.
Their behavior with regards to const-ness is similar to iterators and
pointers.
The const-ness of a view is not related to the const-ness of the underlying
data. A non-const view may refer to elements that are themselves const, and
vice versa. This is analogous to pointers; an int* const is a const
pointer to a mutable int, and a int const* is a non-const pointer to a
const int.
Use non-const views whenever possible. If you need thread-safety, work with
view copies in threads; don't share.
Any operation on the underlying range that invalidates its iterators or
sentinels will also invalidate any view that refers to any part of that range.
Additionally, some views (e.g., views::filter), are invalidated when the
underlying elements of the range are mutated. It is best to recreate a view
after any operation that may have mutated the underlying range.
Below is a list of the lazy range combinators, or views, that Range-v3 provides, and a blurb about how each is intended to be used.
views::addressof\endlinkstd::addressof of each.views::adjacent_filter\endlinkadjacent_filter with std::not_equal_to filters out all the non-unique elements.)views::adjacent_remove_if\endlinkviews::all\endlinkany_view<T>(rng)\endlinkT; can store any range with this value type.views::c_str\endlink\0-terminated C string (e.g. from a const char*) as a range.views::cache1\endlinkview::filter and view::transform, for instance. views::cache1 is always single-pass.views::cartesian_product\endlinkn ranges, i.e., generates all n-tuples (e1, e2, ... , en) where e1 is an element of the first range, e2 is an element of the second range, etc.views::chunk\endlinkviews::common\endlinkend is the same as the begin. Useful for calling algorithms in the std:: namespace.views::concat\endlinkviews::const_\endlinkconst view of a source range.views::counted\endlinkit and a count n, create a range that starts at it and includes the next n elements.views::cycle\endlinkviews::delimit\endlinkviews::delimit can be called with an iterator and a value, in which case it returns a range that starts at the specified position and ends at the first occurrence of the value.views::drop\endlinkviews::drop_last\endlinkviews::drop_exactly\endlinkviews::drop_while\endlinkviews::empty\endlinkviews::enumerate\endlinkviews::filter\endlinkfilter adaptor.)views::for_each\endlinkviews::generate\endlinkviews::generate_n\endlinkviews::chunk_by\endlinkviews::chunk_by groups contiguous elements together with a binary predicate.views::indirect\endlinkviews::intersperse\endlinkviews::ints\endlinkints. When used without arguments, it generates the quasi-infinite range [0,1,2,3...]. It can also be called with a lower bound, or with a lower and upper bound (exclusive). An inclusive version is provided by closed_ints.views::iota\endlinkviews::ints that generates a sequence of monotonically increasing values of any incrementable type. When specified with a single argument, the result is an infinite range beginning at the specified value. With two arguments, the values are assumed to denote a half-open range.views::join\endlinkviews::keys\endlinkpairs (like a std::map), return a new range consisting of just the first element of the pair.views::linear_distribute\endlinkn values linearly in the closed interval [from, to] (the end points are always included). If from == to, returns n-times to, and if n == 1 it returns to.views::move\endlinkviews::partial_sum\endlinkviews::remove\endlinkviews::remove_if\endlinkfilter adaptor with the predicate negated.)views::repeat\endlinkviews::repeat_n\endlinkviews::replace\endlinkviews::replace_if\endlinkviews::reverse\endlinkviews::sample\endlinksize(range).views::single\endlinkviews::slice\endlinkend-2" syntax.views::sliding\endlinkn, place a window over the first n elements of the underlying range. Return the contents of that window as the first element of the adapted range, then slide the window forward one element at a time until hitting the end of the underlying range.views::split\endlinkviews::split_when\endlinktrue if and only if the element is part of a delimiter. The function should accept an iterator and sentinel indicating the current position and end of the source range and return std::make_pair(true, iterator_past_the_delimiter) if the current position is a boundary; otherwise std::make_pair(false, ignored_iterator_value). The elements matching the delimiter are excluded from the resulting range of ranges.views::stride\endlinkviews::tail\endlinkviews::take\endlinkviews::take is not a sized_range.)views::take_exactly\endlinkviews::take_exactly is a sized_range.)views::take_last\endlinksized_range. If the source range does not have at least count elements, the full range is returned.views::take_while\endlinkviews::tokenize\endlinkstd::regex_constants::match_flag_type, return a std::regex_token_iterator to step through the regex submatches of the source range. The submatch specifier may be either a plain int, a std::vector<int>, or a std::initializer_list<int>.views::transform\endlinkviews::trim\endlinkviews::unbounded\endlinkviews::unique\endlinkviews::values\endlinkpairs (like a std::map), return a new range consisting of just the second element of the pair.views::zip\endlinkmake_tuple on the Mth elements of all N ranges.views::zip_with\endlink\subsection tutorial-actions Actions
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.
Read data into a vector, sort it, and make it unique.
extern std::vector<int> read_data();
using namespace ranges;
std::vector<int> vi = read_data() | actions::sort | actions::unique;
Do the same to a vector that already contains some data:
vi = std::move(vi) | actions::sort | actions::unique;
Mutate the container in-place:
vi |= actions::sort | actions::unique;
Same as above, but with function-call syntax instead of pipe syntax:
actions::unique(actions::sort(vi));
Below is a list of the eager range combinators, or actions, that Range-v3 provides, and a blurb about how each is intended to be used.
actions::drop\endlinkN elements of the source range.actions::drop_while\endlinkactions::eraseactions::insertactions::join\endlinkactions::push_backactions::push_frontactions::remove_if\endlinkactions::remove\endlinkactions::reverse\endlinkactions::shuffle\endlinkactions::slice\endlinkactions::sort\endlinkactions::split\endlinkpair<bool, N>).actions::stable_sort\endlinkactions::stride\endlinkactions::take\endlinkN-th elements of the range, removes the rest.actions::take_while\endlinkactions::transform\endlinkactions::unique\endlinkactions::unstable_remove_if\endlinkremove_if. Requires bidirectional container.\subsection tutorial-utilities Utilities
Below we cover some utilities that range-v3 provides for creating your own view adaptors and iterators.
Range-v3 provides a utility for easily creating your own range types, called
\link ranges::view_facade ranges::view_facade\endlink. The code below uses
view_facade to create a range that traverses a null-terminated string:
#include <range/v3/view/facade.hpp>
// A range that iterates over all the characters in a
// null-terminated string.
class c_string_range
: public ranges::view_facade<c_string_range>
{
friend ranges::range_access;
char const * sz_ = "";
char const & read() const { return *sz_; }
bool equal(ranges::default_sentinel_t) const { return *sz_ == '\0'; }
void next() { ++sz_; }
public:
c_string_range() = default;
explicit c_string_range(char const *sz) : sz_(sz)
{
assert(sz != nullptr);
}
};
The view_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:
#include <iostream>
#include <range/v3/algorithm/for_each.hpp>
int main()
{
c_string_range r("hello world");
// Iterate over all the characters and print them out
ranges::for_each(r, [](char ch){
std::cout << ch << ' ';
});
// prints: h e l l o w o r l d
}
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 views::remove_if and views::transform views. These
are rich types with many moving parts, but thanks to a helper class called
\link ranges::view_adaptor ranges::view_adaptor\endlink, 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.
#include <range/v3/view/adaptor.hpp>
#include <range/v3/utility/semiregular_box.hpp>
// A class that adapts an existing range with a function
template<class Rng, class Fun>
class transform_view
: public ranges::view_adaptor<transform_view<Rng, Fun>, Rng>
{
friend ranges::range_access;
ranges::semiregular_box_t<Fun> fun_; // Make Fun model semiregular if it doesn't
class adaptor : public ranges::adaptor_base
{
ranges::semiregular_box_t<Fun> fun_;
public:
adaptor() = default;
adaptor(ranges::semiregular_box_t<Fun> const &fun) : fun_(fun) {}
// Here is where we apply Fun to the elements:
auto read(ranges::iterator_t<Rng> it) const -> decltype(fun_(*it))
{
return fun_(*it);
}
};
adaptor begin_adaptor() const { return {fun_}; }
adaptor end_adaptor() const { return {fun_}; }
public:
transform_view() = default;
transform_view(Rng && rng, Fun fun)
: transform_view::view_adaptor{std::forward<Rng>(rng)}
, fun_(std::move(fun))
{}
};
template<class Rng, class Fun>
transform_view<Rng, Fun> transform(Rng && rng, Fun fun)
{
return {std::forward<Rng>(rng), std::move(fun)};
}
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 read 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
\link ranges::adaptor_base ranges::adaptor_base\endlink.
With transform_view, we can print out the first 20 squares:
int main()
{
auto squares = ::transform(views::ints(1), [](int i){return i*i;});
for(int i : squares | views::take(20))
std::cout << i << ' ';
std::cout << '\n';
// prints 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400
}
The transform_view defined above is an input range when it is wrapping an
input range, a forward range when it's wrapping a forward range, 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
bidirectional_iterator. That's a finer point that you shouldn't worry about right
now.)
Each view_adaptor contains base() member in view and iterator.
base() - allow to access "adapted" range/iterator:
std::vector<int> vec;
auto list = vec | views::transfom([](int i){ return i+1; });
assert( vec.begin() == list.begin().base() );
assert( vec.begin() == list.base().begin() );
Like basic_iterator's cursor, view_adaptor's adaptor can contain mixin class too,
to inject things into the public interface of the iterator:
class adaptor : public ranges::adaptor_base
{
template<class BaseMixin>
struct mixin : BaseMixin
{
// everything inside this class will be accessible from iterator
using BaseMixin::BaseMixin;
auto& base_value() const
{
return *this->base();
}
int get_i() const
{
return this->get().i;
}
};
int i = 100;
};
From within mixin you can call:
get() - to access adaptor internalsbase() - to access adaptable iteratorIterator/sentinel adaptor may "override" the following members:
class adaptor : public ranges::adaptor_base
{
// !For begin_adaptor only!
template<typename Rng>
constexpr auto begin(Rng &rng)
{
return ranges::begin(rng.base());
}
// !For end_adaptor only!
template<typename Rng>
constexpr auto end(Rng &rng)
{
return ranges::end(rng.base());
}
template<typename I>
bool equal(I const &this_iter, I const &that_iter) const
{
return this_iter == that_iter;
}
// or
template<typename I>
bool equal(I const &this_iter, I const &that_iter, adaptor const &that_adapt) const
{
return
*this.some_value == that_adapt.some_value
&& this_iter == that_iter;
}
// !For end_adaptor only!
// Same as equal, but compare iterator with sentinel.
// Not used, if iterator same as sentinel, and both have the same adaptor.
template<typename I, typename S>
constexpr bool empty(I const &it, S const &end) const
{
return it == end;
}
// or
template<typename I, typename S, typename SA>
constexpr bool empty(I const &it, S const &end, SA const &end_adapt) const
{
return
*this.some_value == end_adapt.some_value
&& it == end;
}
template<typename I>
reference_t<I> read(I const &it)
{
return *it;
}
template<typename I>
void next(I &it)
{
++it;
}
// !For bidirectional iterator only!
template<typename I>
void prev(I &it)
{
--it;
}
// !For random access iterator only!
template<typename I>
void advance(I &it, difference_type_t<I> n)
{
it += n;
}
// !For "sized" iterators only!
template<typename I>
difference_type_t<I> distance_to(I const &this_iter, I const &that_iter)
{
return that_iter - this_iter;
}
// or
template<typename I>
difference_type_t<I> distance_to
(I const &this_iter, I const &that_iter, adaptor const &that_adapt)
{
return that_iter - this_iter;
}
}
As you can see, some "overrides" have effect only for begin_adaptor or
end_adaptor. In order to use full potential of adaptor, you need to have
separate adaptors for begin and end:
struct adaptor : adaptor_base
{
int n = 0;
void next(iterator_t<Rng>& it)
{
++n;
++it;
}
};
struct sentinel_adaptor : adaptor_base
{
int stop_at;
bool empty(const iterator_t<Rng>&, const adaptor& ia, const sentinel_t<Rng>& s) const
{
return ia.n == stop_at;
}
};
adaptor begin_adaptor() const { return {}; }
sentinel_adaptor end_adaptor() const { return {100}; }
Sometimes, you can use the same adaptor for both begin_adaptor and end_adaptor:
struct adaptor : adaptor_base
{
int n = 0;
void next(iterator_t<Rng>& it)
{
++n;
++it;
}
// pay attention, we use equal, not empty. empty() will never trigger.
template<typename I>
bool equal(I const &this_iter, I const &that_iter, adaptor const &that_adapt) const
{
return *this.n == that_adapt.n;
}
};
adaptor begin_adaptor() const { return {}; }
adaptor end_adaptor() const { return {100}; }
Note that all the data you store in the adaptor will become part of the iterator.
If you will not "override" begin_adaptor() or/and end_adaptor() in your view_adaptor, default ones will be used.
Here is an example of Range-v3 compatible random access proxy iterator.
The iterator returns a key/value pair, like the zip view.
#include <range/v3/iterator/basic_iterator.hpp>
#include <range/v3/utility/common_tuple.hpp>
using KeyIter = typename std::vector<Key>::iterator;
using ValueIter = typename std::vector<Value>::iterator;
struct cursor
{
// basic_iterator derives from "mixin", if present, so it can be used
// to inject things into the public interface of the iterator
struct mixin;
// This is for dereference operator.
using value_type = std::pair<Key, Value>;
ranges::common_pair<Key&, Value&> read() const
{
return { *key_iterator, *value_iterator };
}
bool equal(const cursor& other) const
{
return key_iterator == other.key_iterator;
}
void next()
{
++key_iterator;
++value_iterator;
}
// prev optional. Required for Bidirectional iterator
void prev()
{
--key_iterator;
--value_iterator;
}
// advance and distance_to are optional. Required for random access iterator
void advance(std::ptrdiff_t n)
{
key_iterator += n;
value_iterator += n;
}
std::ptrdiff_t distance_to(const cursor& other) const
{
return other.key_iterator - this->key_iterator;
}
cursor() = default;
cursor(KeyIter key_iterator, ValueIter value_iterator)
: key_iterator(key_iterator)
, value_iterator(value_iterator)
{}
KeyIter key_iterator;
ValueIter value_iterator;
};
struct cursor::mixin : ranges::basic_mixin<cursor>
{
using ranges::basic_mixin<cursor>::basic_mixin;
// It is necessary to expose constructor in this way
mixin(KeyIter key_iterator, ValueIter value_iterator)
: mixin{ cursor(key_iterator, value_iterator) }
{}
KeyIter key_iterator()
{
return this->get().key_iterator;
}
ValueIter value_iterator()
{
return this->get().value_iterator;
}
};
using iterator = ranges::basic_iterator<cursor>;
void test()
{
std::vector<Key> keys = {1};
std::vector<Value> values = {10};
iterator iter(keys.begin(), values.begin());
ranges::common_pair<Key&, Value&> pair = *iter;
Key& key = pair.first;
Value& value = pair.second;
assert(&key == &keys[0]);
assert(&value == &values[0]);
auto key_iter = iter.key_iterator();
assert(key_iter == keys.begin());
}
read() returns references. But the default for value_type, which is
decay_t<decltype(read())>, is common_pair<Key&, Value&>. That is not correct
in our case. It should be pair<Key, Value>, so we explicitly specify
value_type.
ranges::common_pair has conversions:
ranges::common_pair<Key&, Value&>↔ranges::common_pair<Key, Value>.
All ranges::common_pairs converts to their std::pair equivalents, also.
For more information, see http://wg21.link/P0186#basic-iterators-iterators.basic
\subsection tutorial-concepts Concept Checking
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 emulation layer that works on any standard-conforming C++14 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:
CPP_template(class Iter, class Sent, class Comp = /*...some_default..*/)
(requires sentinel_for<Sent, Iter>)
void my_algorithm(Iter first, Sent last, Comp comp = Comp{})
{
// ...
}
You can then add an overload that take a Range:
CPP_template(class Rng, class Comp = /*...some_default..*/)
(requires range<Rng>)
void my_algorithm(Rng && rng, Comp comp = Comp{})
{
return my_algorithm(ranges::begin(rng), ranges::end(rng));
}
With the type constraints expressed with the CPP_template macro, these
two overloads are guaranteed to not be ambiguous. When compiling with C++20
concepts support, this uses real concept checks. On legacy compilers, it falls
back to using std::enable_if.
\subsection tutorial-future Range-v3 and the Future
Range-v3 formed the basis for the
Technical Specification on Ranges,
which has since been merged into the working draft and shipped with C++20 in
the std::ranges namespace.
More range adaptors are slated for inclusion in C++23 and beyond.
The actions, as well as various utilities, have not yet been reviewed by the Committee, although the basic direction has already passed an initial review.