Ranges (C++)

Ranges in the C++ language are an abstraction of any iterable sequence of elements as a single unified object, i.e. the concept of a range in computer science. It is represented by an iterator that marks the beginning of the range, and a sentinel that marks the end, and provides in particular a functional-style API for algorithms over ranges. It was introduced in C++20, but had been under development as the Ranges Technical Specification up to then.

Its contents in the C++ Standard Library are provided primarily in the <ranges> header, under the namespace std::ranges, however other headers (such as <algorithm>) contain std::ranges symbols.

It is lazily evaluated, and uses operator| as a pipe operator to chain operations, similar to a fluent API.

Its closest equivalents elsewhere, include:

History

The algorithms library in <algorithm>, first introduced in C++98, required verbose iterator pairs begin(), end() on calls, and lacked easy ways to chain operations without having to create temporary instances. The Boost.Range library was developed in 2003, as an alternative paired iterators into a single abstraction.

C++11 introduced the foreach loop (also known as the "range-based for-loop" or the "enhanced for-loop"), following Java's syntax (which added foreach loops for java.lang.Iterable types). This simplified C++ iterators in for loops significantly:

using std::vector;

vector<int> v = { /* Populate the vector here... */ };

// Before C++11:
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
    // ...
}

// After C++11:
for (int i : v) {
    // ...
}

The modern form of C++ ranges was created around 2013 with the creation of the range-v3 library, by Eric Niebler, which used the same composable range views and pipe syntax. It was finally submitted as a paper in 2014, but as it relied heavily on the concepts feature (which was not added until C++20), it remained in the Ranges Technical Specification (TS), until it was proposed once again as "The One Ranges Proposal", finally being adopted in November 2018.

The library would continue to undergo additions, in particular the addition of a conversion function std::ranges::to<T>() in C++23 to create objects out of ranges more easily, as well as more algorithms and views in C++23 and C++26.

The API and style of the ranges library heavily influenced the development of the std::execution library, added in C++26, which also uses lazy evaluation and the piping operator.

The "actions" feature in the ranges-v3 library currently remains outside of the standard ranges library; "actions" are eager, mutating operations that like views, chain using the pipe operator

Library architecture

The library contains the std::ranges namespace, which itself contains the types, concepts, and eager algorithms (operations that execute immediately on data). The most fundamental two concepts in std::ranges, are:

The std::ranges::views namespace (also shortened inside the standard library as just std::views) provide function objects (customisation points and range adaptor objects) that are specifically designed for lazy evaluation and pipe composition. These do not immediately perform work; they are lazily evaluated, and essentially create a description of work that only compute elements on demand upon iteration.

import std;

using std::vector;
using std::ranges::to;
using std::views::filter;
using std::views::transform;

vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

// Pipeline: filter even numbers, double them, and then sum the result
vector<int> result = numbers
    | filter([](int n) -> bool { return n % 2 == 0; })
    | transform([](int n) -> int { return n * 2; })
    | to<vector>();

The C++ generator class, std::generator<Ref, V, Alloc>, is itself a range, allowing lazy sequences with coroutines (co_yield) to be used.

See also