Ranges library (since C++20)
The ranges library is an extension and generalization of the algorithms and iterator libraries that makes them more powerful by making them composable and less error-prone.
The library creates and manipulates range views, lightweight objects that indirectly represent iterable sequences (ranges). Ranges are an abstraction on top of
-
[
begin,
end)
– iterator pairs, e.g. ranges made by implicit conversion from containers. All algorithms that take iterator pairs now have overloads that accept ranges (e.g. ranges::sort) - begin
+
[
0,
size)
– counted sequences, e.g. range returned by views::counted -
[
begin,
predicate)
– conditionally-terminated sequences, e.g. range returned by views::take_while -
[
begin,
..)
– unbounded sequences, e.g. range returned by views::iota
The ranges library includes range algorithms, which are applied to ranges eagerly, and range adaptors, which are applied to views lazily. Adaptors can be composed into pipelines, so that their actions take place as the view is iterated.
<ranges>
namespace views = ranges::views;
The namespace alias std::views
is provided as a shorthand for std::ranges::views
.
std::ranges
Contents
- 1 Range access
- 2 Range primitives
- 3 Dangling iterator handling
- 4 Other utilities
- 5 Range concepts
- 6 Range conversions
- 7 Views
- 8 Range factories
- 9 Range adaptors
- 10 Range generators (since C++23)
- 11 Helper items
- 11.1 Range adaptor objects
- 11.2 Range adaptor closure objects
- 11.3 Customization point objects
- 11.4 Assignable wrapper
- 11.5 Non-propagating cache
- 11.6 Conditionally-const type
- 11.7 Integer-like type helper templates
- 11.8 Customization point object helpers
- 11.9 Range adaptor helpers
- 11.10 Helper concepts
- 12 Notes
- 13 Example
- 14 Defect reports
- 15 See also
Range access
<ranges>
<iterator>
(customization point object)[edit]
(customization point object)[edit]
(customization point object)[edit]
(customization point object)[edit]
(customization point object)[edit]
(customization point object)[edit]
(customization point object)[edit]
(customization point object)[edit]
(customization point object)[edit]
(customization point object)[edit]
(customization point object)[edit]
(customization point object)[edit]
(customization point object)[edit]
(customization point object)[edit]
Range primitives
<ranges>
(alias template)[edit]
(alias template)[edit]
(alias template)[edit]
Dangling iterator handling
<ranges>
subrange
should not be returned since it would be dangling (class) [edit]
subrange
type of a borrowed_range
(alias template)[edit]
Other utilities
<ranges>
(class template) [edit]
Range concepts
<ranges>
begin
iterator and an end
sentinel (concept) [edit]
range
and iterators obtained from an expression of it can be safely returned without danger of dangling (concept) [edit]
(concept) [edit]
(concept) [edit]
(concept) [edit]
input_iterator
(concept) [edit]
output_iterator
(concept) [edit]
forward_iterator
(concept) [edit]
bidirectional_iterator
(concept) [edit]
random_access_iterator
(concept) [edit]
contiguous_iterator
(concept) [edit]
(concept) [edit]
range
to be safely convertible to a view
(concept) [edit]
(concept) [edit]
Range conversions
<ranges>
(function template) [edit]
Views
<ranges>
view
, using the curiously recurring template pattern (class template) [edit]
view
(class template) [edit]
[edit] Range factories
<ranges>
std::ranges
view
that contains a single element of a specified value(class template) (customization point object)[edit]
view
consisting of a sequence generated by repeatedly incrementing an initial value(class template) (customization point object)[edit]
view
consisting of a generated sequence by repeatedly producing the same value(class template) (customization point object)[edit]
view
consisting of the elements obtained by successive application of operator>>
on the associated input stream(class template) (customization point object)[edit]
[edit] Range adaptors
<ranges>
std::ranges
(class template) [edit]
view
of a sequence that casts each element to an rvalue(class template) (range adaptor object)[edit]
view
that consists of the elements of a range
that satisfies a predicate(class template) (range adaptor object)[edit]
view
of a sequence that applies a transformation function to each element(class template) (range adaptor object)[edit]
view
consisting of the first N elements of another view
(class template) (range adaptor object)[edit]
view
consisting of the initial elements of another view
, until the first element on which a predicate returns false(class template) (range adaptor object)[edit]
view
consisting of elements of another view
, skipping the first N elements(class template) (range adaptor object)[edit]
view
consisting of the elements of another view
, skipping the initial subsequence of elements until the first element where the predicate returns false(class template) (range adaptor object)[edit]
view
consisting of the sequence obtained from flattening a view
of range
s (class template) (range adaptor object)[edit]
view
consisting of the sequence obtained from flattening a view of ranges, with the delimiter in between elements(class template) (range adaptor object)[edit]
view
over the subranges obtained from splitting another view
using a delimiter(class template) (range adaptor object)[edit]
view
over the subranges obtained from splitting another view
using a delimiter(class template) (range adaptor object)[edit]
view
consisting of concatenation of the adapted views(class template) (customization point object)[edit]
view
that iterates over the elements of another bidirectional view in reverse order(class template) (range adaptor object)[edit]
view
consisting of tuple-like values and a number N and produces a view
of Nth element of each tuple(class template) (range adaptor object)[edit]
view
consisting of pair-like values and produces a view
of the first elements of each pair(class template) (range adaptor object)[edit]
view
consisting of pair-like values and produces a view
of the second elements of each pair(class template) (range adaptor object)[edit]
view
that maps each element of adapted sequence to a tuple of both the element's position and its value(class template) (range adaptor object)[edit]
view
consisting of tuples of references to corresponding elements of the adapted views(class template) (customization point object)[edit]
view
consisting of results of application of a transformation function to corresponding elements of the adapted views(class template) (customization point object)[edit]
view
consisting of tuples of references to adjacent elements of the adapted view(class template) (range adaptor object)[edit]
view
consisting of results of application of a transformation function to adjacent elements of the adapted view(class template) (range adaptor object)[edit]
view
s that are N
-sized non-overlapping successive chunks of the elements of another view
(class template) (range adaptor object)[edit]
view
whose Mth element is a view
over the Mth through (M + N - 1)th elements of another view
(class template) (range adaptor object)[edit]
view
into subranges between each pair of adjacent elements for which the given predicate returns false(class template) (range adaptor object)[edit]
view
consisting of elements of another view
, advancing over N elements at a time(class template) (range adaptor object)[edit]
view
consisting of tuples of results calculated by the n-ary cartesian product of the adapted views(class template) (customization point object)[edit]
view
that caches the last-accessed element of its underlying sequence(class template) (range adaptor object)[edit]
view
into a range that is input_range
-only and non-common_range
(class template) (range adaptor object)[edit]
[edit] Range generators (since C++23)
<generator>
std
[edit] Helper items
[edit] Range adaptor objects
See RangeAdaptorObject (RAO).
[edit] Range adaptor closure objects
See RangeAdaptorClosureObject (RACO).
[edit] Customization point objects
See Customization point object (CPO).
[edit] Assignable wrapper
Some range adaptors wrap their elements or function objects with the copyable-box
(until C++23)movable-box
(since C++23). The wrapper augments the wrapped object with assignability when needed.
[edit] Non-propagating cache
Some range adaptors are specified in terms of an exposition-only class template non-propagating-cache
, which behaves almost like std::optional <T> (see description for differences).
[edit] Conditionally-const
type
using /*maybe-const*/ = std::conditional_t <Const, const T, T>;
The alias template /*maybe-const*/ is a shorthand used to conditionally apply a const qualifier to the type T
.
[edit] Integer-like type helper templates
using /*make-signed-like-t*/<T> = /* see description */;
using /*make-unsigned-like-t*/<T> = /* see description */;
/*make-unsigned-like-t*/<T> /*to-unsigned-like*/( T t )
{
return static_cast</*make-unsigned-like-t*/<T>>(t);
T
:
- If
T
is an integer type, /*make-signed-like-t*/<T> is std::make_signed_t <T>. - Otherwise, /*make-signed-like-t*/<T> is a corresponding unspecified signed-integer-like type of the same width as
T
.
T
:
- If
T
is an integer type, /*make-unsigned-like-t*/<T> is std::make_unsigned_t <T>. - Otherwise, /*make-signed-like-t*/<T> is a corresponding unspecified unsigned-integer-like type of the same width as
T
.
[edit] Customization point object helpers
constexpr auto& /*possibly-const-range*/(R& r) noexcept
{
if constexpr (ranges::input_range <const R>)
return const_cast<const R&>(r);
else
return r;
constexpr auto /*as-const-pointer*/( const T* p ) noexcept
{
return p;
Some range access customization point objects are specified in terms of these exposition-only function templates.
input_range
; otherwise, returns r without any casting.[edit] Range adaptor helpers
constexpr auto /*tuple-transform*/( F&& f, Tuple&& tuple )
{
return std::apply ([&]<class... Ts>(Ts&&... args)
{
return std::tuple <std::invoke_result_t <F&, Ts>...>
(std::invoke (f, std::forward <Ts>(args))...);
}, std::forward <Tuple>(tuple));
constexpr void /*tuple-for-each*/( F&& f, Tuple&& tuple )
{
std::apply ([&]<class... Ts>(Ts&&... args)
{
(static_cast<void>(std::invoke (f, std::forward <Ts>(args))), ...);
}, std::forward <Tuple>(tuple));
constexpr T& /*as-lvalue*/( T&& t )
{
return static_cast<T&>(t);
Some range adaptors are specified in terms of these exposition-only function templates.
[edit] Helper concepts
Following exposition-only concepts are used for several types, but they are not parts of the interface of standard library.
concept /*simple-view*/ =
ranges::view <R> && ranges::range <const R> &&
std::same_as <ranges::iterator_t <R>, ranges::iterator_t <const R>> &&
concept /*has-arrow*/ =
ranges::input_iterator<I> &&
concept /*different-from*/ =
concept /*range-with-movable-references*/ =
ranges::input_range <R> &&
std::move_constructible <ranges::range_reference_t <R>> &&
concept /*all-random-access*/ =
(ranges::random_access_range
concept /*all-bidirectional*/ =
(ranges::bidirectional_range
concept /*all-forward*/ =
(ranges::forward_range
[edit] Notes
[edit] Example
#include <iostream> #include <ranges> int main() { auto const ints = {0, 1, 2, 3, 4, 5}; auto even = [](int i) { return 0 == i % 2; }; auto square = [](int i) { return i * i; }; // the "pipe" syntax of composing the views: for (int i : ints | std::views::filter (even) | std::views::transform (square)) std::cout << i << ' '; std::cout << '\n'; // a traditional "functional" composing syntax: for (int i : std::views::transform (std::views::filter (ints, even), square)) std::cout << i << ' '; }
Output:
0 4 16 0 4 16
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 3509 (P2281R1) |
C++20 | it was unclear how range adaptor objects bound trailing arguments | they are bound by value |
LWG 3948 | C++23 | possibly-const-range and as-const-pointer were not declared noexcept |
declared noexcept |
LWG 4027 | C++23 | possibly-const-range would not add const-qualificationfor ranges that has already modeled constant_range
|
adds const-qualification for such ranges |
LWG 4112 | C++20 | has-arrow did not require i to be const-qualified
|
requires |