# Ranges library (C++20)

< cpp

The ranges library provides components for dealing with ranges of elements, including a variety of view adapters.

 Defined in header  namespace std {     namespace views = ranges::views; }

The namespace alias std::views is provided as a shorthand for std::ranges::views.

Defined in namespace std::ranges

## Contents

##### Range access
Defined in header <ranges>
Defined in header <iterator>
returns an iterator to the beginning of a range
(customization point object) 
returns a sentinel indicating the end of a range
(customization point object) 
returns an iterator to the beginning of a read-only range
(customization point object) 
returns a sentinel indicating the end of a read-only range
(customization point object) 
returns a reverse iterator to a range
(customization point object) 
returns a reverse end iterator to a range
(customization point object) 
returns a reverse iterator to a read-only range
(customization point object) 
returns a reverse end iterator to a read-only range
(customization point object) 
returns an unsigned integer equal to the size of a range
(customization point object) 
returns a signed integer equal to the size of a range
(customization point object) 
checks whether a range is empty
(customization point object) 
obtains a pointer to the beginning of a contiguous range
(customization point object) 
obtains a pointer to the beginning of a read-only contiguous range
(customization point object) 
##### Range primitives
Defined in header <ranges>
obtains associated types of a range
(alias template) 
##### Dangling iterator handling
Defined in header <ranges>
a placeholder type indicating that an iterator or a subrange should not be returned since it would be dangling
(class) 
obtains iterator type or subrange type of a borrowed_range
(alias template) 
##### Range concepts
Defined in header <ranges>
specifies that a type is a range, that is, it provides a begin iterator and an end sentinel
(concept) 
specifies that a type is a range and iterators obtained from an expression of it can be safely returned without danger of dangling
(concept) 
specifies that a range knows its size in constant time
(concept) 
specifies that a range is a view, that is, it has constant time copy/move/assignment
(concept) 
specifies a range whose iterator type satisfies input_iterator
(concept) 
specifies a range whose iterator type satisfies output_iterator
(concept) 
specifies a range whose iterator type satisfies forward_iterator
(concept) 
specifies a range whose iterator type satisfies bidirectional_iterator
(concept) 
specifies a range whose iterator type satisfies random_access_iterator
(concept) 
specifies a range whose iterator type satisfies contiguous_iterator
(concept) 
specifies that a range has identical iterator and sentinel types
(concept) 
specifies the requirements for a range to be safely convertible to a view
(concept) 
##### Views
Defined in header <ranges>
helper class template for defining views, using the curiously recurring template pattern
(class template) 
combines an iterator-sentinel pair into a view
(class template) 

###  Range factories

 Defined in header  Defined in namespace std::ranges an empty view with no elements (class template) (variable template)  a view that contains a single element of a specified value (class template) (customization point object)  ranges::iota_viewviews::iota(C++20) a view consisting of a sequence generated by repeatedly incrementing an initial value (class template) (customization point object)  a view consisting of the elements obtained by successive application of operator>> on the associated input stream (class template) 

 Defined in header  Defined in namespace std::ranges views::all_tviews::all(C++20) a view that includes all elements of a range (alias template) (range adaptor object)  ranges::ref_view(C++20) a view of the elements of some other range (class template)  a view that consists of the elements of a range that satisfies a predicate (class template) (range adaptor object)  a view of a sequence that applies a transformation function to each element (class template) (range adaptor object)  ranges::take_viewviews::take(C++20) a view consisting of the first N elements of another view (class template) (range adaptor object)  a view consisting of the initial elements of another view, until the first element on which a predicate returns false (class template) (range adaptor object)  ranges::drop_viewviews::drop(C++20) a view consisting of elements of another view, skipping the first N elements (class template) (range adaptor object)  a 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)  ranges::join_viewviews::join(C++20) a view consisting of the sequence obtained from flattening a view of ranges (class template) (range adaptor object)  a view over the subranges obtained from splitting another view using a delimiter (class template) (range adaptor object)  a view over the subranges obtained from splitting another view using a delimiter (class template) (range adaptor object)  views::counted(C++20) creates a subrange from an iterator and a count (customization point object)  converts a view into a common_range (class template) (range adaptor object)  a view that iterates over the elements of another bidirectional view in reverse order (class template) (range adaptor object)  takes a view consisting of tuple-like values and a number N and produces a view of N'th element of each tuple (class template) (range adaptor object)  ranges::keys_viewviews::keys(C++20) takes a view consisting of pair-like values and produces a view of the first elements of each pair (class template) (range adaptor object)  takes a view consisting of pair-like values and produces a view of the second elements of each pair (class template) (range adaptor object) 

Some range adaptors wrap their elements or function objects with the copyable wrapper.

Range adaptor objects are customization point objects that accept viewable_range as their first arguments and return a view. Some range adaptor objects are unary, i.e. they takes one viewable_range as its only argument. Other range adaptor objects takes a viewable_range and other trailing arguments.

If a range adaptor object takes more than one argument, it also supports partial application: let

• a be such a range adaptor object, and
• args... be arguments (generally suitable for trailing arguments),

expression a(args...) has following properties:

• it is valid if and only if for every argument e in args... such that E is decltype((e)), is true,
• when the call is valid, its result object stores a subobject of type std::decay_t<E> direct-non-list-initialized with std::forward<E>(e), for every argument e in args... (in other words, range adaptor objects bind arguments by value), and
• the result object is a range adaptor closure object.

Like other customization point objects, let

• a be an object of the cv-unqualified version of the type of any range adaptor objects,
• args... be any group of arguments that satisfies the constraints of the operator() of the type of a,

calls to

are all equivalent.

The result object of each of these expressions is either a view object or a range adaptor closure object.

Notes: operator() 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):

• the results of binding trailing arguments by range adaptor objects, and
• the results of chaining two range adaptor closure objects by operator|.

Range adaptor closure objects take one viewable_range as its only argument and return a view. They are callable via the pipe operator: if C is a range adaptor closure object and R is a viewable_range, these two expressions are equivalent (both well-formed or ill-formed):

C(R)
R | 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 C.

Two range adaptor closure objects can be chained by operator| to produce another range adaptor closure object: if C and D are range adaptor closure objects, then C | D is also a range adaptor closure object if it is valid.

The bound arguments of C | 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 operator() of the result is determined as follows: given a viewable_range R, these two expressions are equivalent (both well-formed or ill-formed):

R | C | D // (R | C) | D
R | (C | D)

Notes: operator() 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.

 template   concept __SimpleView =                         // exposition only     ranges::view && ranges::range &&     std::same_as, std::ranges::iterator_t> &&     std::same_as, std::ranges::sentinel_t>;

### Example

#include <ranges>
#include <iostream>

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; };

// "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

### 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 C++20 it was unclear how range adaptor objects bound trailing arguments they are bound by value