Advertisement
Intermediate C++, Ranges & Views

What does the pipe operator | do with ranges?

Short answer

The | operator composes range adaptors into a pipeline, feeding the output of one view into the next, read left to right. It makes transformations readable, like Unix pipes: range | filter | transform | take.

Save C++ mock interview
Advertisement
AModel answer
The | operator composes range adaptors into a pipeline, feeding the output of one view into the next, read left to right. It makes transformations readable, like Unix pipes: range | filter | transform | take. Each stage is a lazy view, so the whole pipeline evaluates on demand during iteration.
auto r = v | std::views::filter(isEven)
             | std::views::transform(square);
Real-world example
A data pipeline reads as filter-then-transform via |, far clearer than nested algorithm calls.
Likely follow-ups
  • How is the pipeline evaluated? | Why is | readable?
Related topics