Some more details about the Ranges proposal that was well received by the ISO C++ committee at this month's meeting in Urbana-Champaign:
Container Algorithms
by Eric Niebler
From the article:
... So to sum up, in the world of N4128, we have this:
- Eager algorithms that can mutate but that don’t compose.
- Lazy algorithms that can’t mutate but do compose.
Whoops! Something is missing. If I want to read a bunch of ints, sort them, and make them unique, here’s what that would look like in N4128:
extern std::vector<int> read_ints(); std::vector<int> ints = read_ints(); std::sort(ints); auto i = std::unique(ints); ints.erase(i, ints.end());Blech! A few people noticed this shortcoming of my proposal. A week before the meeting, I was seriously worried that this issue would derail the whole effort. I needed a solution, and quick.
Container Algorithms
The solution I presented in Urbana is container algorithms. These are composable algorithms that operate eagerly on container-like things, mutating them in-place, then forwarding them on for further processing. For instance, the read+sort+unique example looks like this with container algorithms:
std::vector<int> ints = read_ints() | cont::sort | cont::unique;Much nicer. Since the container algorithm executes eagerly, it can take a vector and return a vector. The range views can’t do that...
Add a Comment
Comments are closed.