CKTIS / C++
Operator |: composing range adaptors
Operator | either applies an adaptor closure to a range or composes two closures into a pipeline. Distinguish R | C, which invokes the adaptor, from C | D, which creates an operation for later use.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
R | C
C | D
R | (C | D)The symbol Range adaptor composition operator | here means composing adaptor closures: C | D creates a new closure that invokes C first, then D. By contrast, R | C is equivalent to C(R) for a suitable range R.
std::views::filter(pred) binds the predicate and produces a closure; std::views::filter itself needs another argument. std::views::transform(fn) works similarly. Piping is not for arbitrary functions: operands must satisfy the adaptor requirements.
In the example, pipeline first selects even numbers and then multiplies them by 10. Only values | pipeline supplies the range. The loop prints 20, 40, and 60, each on its own line.
Examples
#include <iostream>
#include <ranges>
int main() {
int values[]{1, 2, 3, 4, 5, 6};
auto pipeline = std::views::filter([](int x) { return x % 2 == 0; })
| std::views::transform([](int x) { return x * 10; });
for (int x : values | pipeline) {
std::cout << x << '\n';
}
}
Result
20 40 60
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.