Table of Contents
C++ Ranges library
https://en.cppreference.com/w/cpp/ranges
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
)
- [start, size) counted sequences, e.g. range returned by
- [start, predicate) conditionally-terminated sequences, e.g. range returned by
- [start..) unbounded sequences, e.g. range returned by
The ranges library includes cpp/algorithm/ranges | range algorithms, which are applied to ranges eagerly, and Range Adaptors | 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.
The namespace alias
is provided as a shorthand for
.
Range factories
Range adaptors
<!–
–>
Some range adaptors wrap their elements or function objects with the cpp/ranges/copyable_wrapper | ''copyable wrapper''.
Range adaptor objects
Range adaptor objects are customization point objects that accept
as their first arguments and return a
. Some range adaptor objects are unary, i.e. they take one
as their only argument. Other range adaptor objects take a
and other trailing arguments.
If a range adaptor object takes more than one argument, it also supports partial application: let
be such a range adaptor object, and
be arguments (generally suitable for trailing arguments),
expression
has following properties:
- it is valid if and only if for every argument
in
such that
is
,
is
,
- when the call is valid, its result object stores a subobject of type
direct-non-list-initialized with
, for every argument
in
(in other words, range adaptor objects bind arguments by value), and
- the result object is a Range adaptor closure objects | range adaptor closure object.
Like other customization point objects, let
be an object of the cv-unqualified version of the type of any range adaptor objects,
be any group of arguments that satisfies the constraints of the
of the type of
,
calls to
,
,
, and
are all equivalent.
The result object of each of these expressions is either a
object or a range adaptor closure object.
Notes:
is unsupported for volatile-qualified or const-volatile-qualified version of range adaptor object types. Arrays and functions are converted to pointers while binding.
Range adaptor closure objects
Range adaptor closure objects are objects whose type is the same as one of the following objects (ignoring cv-qualification):
- unary range adaptor objects,
- the results of binding trailing arguments by range adaptor objects, and
- the results of chaining two range adaptor closure objects by
}}.
Range adaptor closure objects take one
as its only argument and return a
. They are callable via the pipe operator: if
is a range adaptor closure object and
is a
, these two expressions are equivalent (both well-formed or ill-formed):
C }}
This call forwards the bound arguments (if any) to the associated range adaptor object. The bound arguments (if any) are identically treated as lvalue or rvalue and cv-qualified to
.
Two range adaptor closure objects can be chained by
}} to produce another range adaptor closure object: if
and
are range adaptor closure objects, then
D}} is also a range adaptor closure object if it is valid.
The bound arguments of
D}} is determined as follows:
- there is a subobject in the result object of the same type (cv-qualification discarded) for every subobject in both operands that is a bound argument,
- such a bound argument is direct-non-list-initialized with the source subobject in its containing operand, where the source is identically treated as lvalue or rvalue and cv-qualified to the operand,
- the result is valid if and only if the initialization of all bound arguments are valid.
The effect and validity of the
of the result is determined as follows: given a
, these two expressions are equivalent (both well-formed or ill-formed):
C
D // (R
C)
D R
(C
D) }}
Notes:
is unsupported for volatile-qualified or const-volatile-qualified version of range adaptor object closure types.
Helper concepts
Following exposition-only concepts are used for several types, but they are not parts of the interface of standard library.
Notes
Example
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 }} ===Defect reports=== {{dr list begin}} {{dr list item | wg=lwg | dr=3509<!-- P2281R1 ---> | std=C++20 | before=it was unclear how range adaptor objects bound trailing arguments | after=they are bound by value}} {{dr list end}} {{langlinks | es | ja | ru | [[zh}}