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) +
[
â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::iotaThe 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.
namespace std { namespace views = ranges::views;
The namespace alias std::views
is provided as a shorthand for std::ranges::views
.
See RangeAdaptorObject (RAO).
[edit] Range adaptor closure objectsSee RangeAdaptorClosureObject (RACO).
[edit] Customization point objectsSee Customization point object (CPO).
[edit] Assignable wrapperSome 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.
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).
const
type template< bool Const, class T >The alias template /*maybe-const*/ is a shorthand used to conditionally apply a const qualifier to the type T
.
template< /*is-integer-like*/ T >
using /*make-signed-like-t*/<T> = /* see description */;
template< /*is-integer-like*/ T >
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);
For an
integer-like typeT
:
T
is an integer type, /*make-signed-like-t*/<T> is std::make_signed_t<T>.T
.For an integer-like type
T
:
T
is an integer type, /*make-unsigned-like-t*/<T> is std::make_unsigned_t<T>.T
.3) Explicitly converts t to /*make-unsigned-like-t*/<T>.
[edit] Customization point object helpers (1) (exposition only*) template< class T >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.
1) /*possibly-const-range*/returns the const-qualified version of
rif
const Rmodels
input_range
; otherwise, returns
rwithout any casting.
2) /*as-const-pointer*/ returns a pointer to object of constant type.
[edit] Range adaptor helpers (1) (exposition only*) template< class F, class 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.
1) /*tuple-transform*/ returns a new tuple constructed by applying f to each element of tuple.
2) /*tuple-for-each*/ applies f to each element of tuple and returns nothing.
3) /*as-lvalue*/ forwards rvalue t as lvalue.
[edit] Helper conceptsFollowing exposition-only concepts are used for several types, but they are not parts of the interface of standard library.
(1) (exposition only*) template< class I >concept /*has-arrow*/ =
ranges::input_iterator<I> &&
#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:
[edit] Defect reportsThe following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior LWG 3509possibly-const-range
and as-const-pointer
possibly-const-range
would not add const-qualification
constant_range
adds const-qualification
has-arrow
did not require i to be const-qualified requires [edit] See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4