C++ Reference ยท Algorithms
Composing C++ range views with the pipe operator
How to chain C++20 views with the pipe operator: evaluation order, reusable pipeline objects, lifetimes, mutation through views and common compile errors.
The real strength of views is that they compose. The pipe operator | feeds a range into a range adaptor and gives you a new view, which you can pipe into the next adaptor, and so on. A chain such as numbers | filter(...) | transform(...) | take(5) reads left to right like a shell pipeline and is evaluated lazily, one element at a time, with no temporary containers between the steps.
r | adaptor is the same as adaptor(r). The pipe form is easier to read when there are several steps. You can also build a pipeline without data by piping adaptors together, and apply it to many ranges later.
auto pipeline = std::views::filter(is_even) | std::views::transform(square) | std::views::take(3); for (int x : numbers | pipeline) { /* ... */ }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
range | adaptor // same as adaptor(range) adaptor1 | adaptor2 // a new adaptor: apply adaptor1, then adaptor2 adaptor(args...) // an adaptor "closure" waiting for a range std::views::transform(f) // example of an adaptor closure
| Name | Description |
|---|---|
range |
A viewable range: an lvalue container, a view, or a borrowed range. |
adaptor |
A function-object returned by std::views::filter(...), take(...), or a plain one such as std::views::reverse. |
| |
Left to right: a | f | g means g(f(a)). |
Returns: a view whose type records every step (take_view<transform_view<filter_view<...>>>). Use auto to name it.
Errors / undefined behavior: a pipeline holding a view of a destroyed container is undefined behavior when iterated. Data races follow normal rules (iterating a view is not thread-safe if the container is being modified).
Usage notes
- Evaluation order. Elements flow one at a time through all stages. For the first example above,
8passesfilter, is squared, counted bytake, printed, and only then is6examined. - Order matters for cost and result.
take(3) | filter(...)takes three elements and then filters them;filter(...) | take(3)finds three matching elements. Filter early to reduce work for later stages. - Use
auto, not a named type. The type of a pipeline is long and changes whenever you add a step. If you must store the result in a class, materialize it withstd::ranges::tointo a container. - Rvalue containers.
std::vector<int>{1,2,3} | rv::filter(...)does not compile, because the view would dangle. Name the vector first. (Views passed as rvalues are fine, since they are cheap to copy.) - Passing views to functions. Take
std::ranges::input_range auto&&(or a constrained template). Afilterview is not const-iterable, so do not take it byconst&. - Mutating the base while a view exists can invalidate the view. After
v.push_back(...)a vector may reallocate; the views of it must be recreated. - Custom adaptors. In C++23 you can build your own with
std::ranges::range_adaptor_closure<T>; compose small lambdas as the simpler option. - Error messages. A mistake in a pipeline usually produces a long template error. Build pipelines step by step, assigning each to
auto, until the error points at one stage. - Materialize when needed. Pipelines end in a
forloop, astd::rangesalgorithm, orstd::ranges::to<std::vector>(), seestd::ranges::to.