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
)
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.
The namespace alias
is provided as a shorthand for
.
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:
in
such that
is
,
is
,
direct-non-list-initialized with
, for every argument
in
(in other words, range adaptor objects bind arguments by value), and
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 are objects whose type is the same as one of the following objects (ignoring cv-qualification):
}}.
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:
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.
Following exposition-only concepts are used for several types, but they are not parts of the interface of standard library.
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 << ' '; }}
0 4 16 0 4 16 }}