Standard algorithms require passing verbose .begin() and .end() iterators, which is repetitive and error prone. C++20 introduced the <ranges> library, allowing us to pass containers directly. It also added views for lazy, zero copy pipeline evaluations, and the spaceship operator <=> for automatic comparison generation.
Let's look at range pipelines, lazy evaluation views, and three way comparison mechanics.
Ranges: Passing Containers Directly
In C++20, you no longer need to write std::sort(vec.begin(), vec.end());. You can pass the vector container directly to the ranges library equivalent, reducing boilerplate:
Lazy Range Views, Pipe Composition, and Compile-Time Composability
Range Views are lazy, non owning wrappers. In other languages, if you filter an array and then map it, the language allocates a temporary array on the heap for the filtered results, then loops over it again to map it. This destroys performance.
C++ Views compose at compile time. When you chain views using the pipe | operator, the compiler fuses the logic into a single, highly optimized state machine. No intermediate heap arrays are allocated, and the data is only processed exactly when you request it.
The Spaceship Operator (<=>)
Writing comparison operators (<, <=, >, >=, ==, !=) for custom structs requires writing six separate functions. C++20 solved this by introducing the Spaceship Operator (or three way comparison operator: <=>).
If you declare the spaceship operator set to default, the compiler automatically generates all six comparison operators for you based on member declaration order:
Performance Benefits
Under the hood, range views avoid intermediate heap allocated vectors. By combining multiple filters and transforms into a single iterator sweep, the compiler generates assembly that performs calculations in place. This saves memory bandwidth and keeps data inside CPU L1 cache lines.
* The Tap Water Metaphor: Classic STL algorithms are like filtering all the water in a lake and bottling it (creating intermediate copies). C++20 range views are like connecting a filter attachment directly to the tap. The water is only filtered as it runs out when you turn it on (lazy evaluation), saving storage and work.