HNHacker News
TopNewBestAskShowJobs

eric_niebler

39 karma · joined November 24, 2013

submissionscomments
eric_niebler··on C++20 Ranges
Why it took so long is that:

(a) C++ is an international standard, has a huge installed base of business- and mission-critical software, probably billions of lines of code, tens of millions of programmers, and we can't afford to get things wrong (though we still do sometimes).

(b) Nobody has built a large, _very_ generic, general purpose, high-performance library, on top of the (also new) C++ concepts language feature before. We were figuring it out as we went.

(c) While standardizing this piece, we were also thinking 5 to 10 years down the road and trying to make sure what we were standardizing now would evolve well. For instance, we already know pretty well what using ranges with coroutines will look like (i.e., reactive streams). We already know what other lazy range adaptors we want, and we know they work with the bits we're getting for C++20. We know how to integrate eager adaptors that transform containers and compose. This all needed to be built, tested, and well-understood.

As for why it is `std::ranges::sort` instead of `std::sort`, this exercise wasn't just about adding overloads that take ranges instead of iterators. It was also about rigorously specifying in code what the requirements of each algorithm are. For Reasons, we can't just slap new requirements on old algorithms that have been shipping for 20 years. It will break mountains of code. Nobody likes having two `sort` algorithms in two different namespaces. It is preferable to the alternative.

eric_niebler··on C++20 Ranges
I just upgraded my GCE instance. Let's see if it can withstand this withering assault. <makes popcorn>
eric_niebler··on Container Algorithms
Actually, the authors of Boost.Range got the idea from an earlier range library of mine, which is noted in the Acknowledgements section of the Boost.Range documentation [here](http://www.boost.org/libs/range/doc/html/range/history_ack.h...):

> Eric Niebler contributed the Range Adaptor idea which is arguably the single biggest innovation in this library.

eric_niebler··on Container Algorithms
In my experience, it's really important to be able to look at some code and know its performance characteristics: does it allocate memory? Who is managing the lifetime of the objects and how many times are the elements copied/moved? How many passes over the data are made? The separation of eager from lazy adaptors makes it possible to know those performance characteristics at a glance.

Looking at your code:

    std::vector<int> ints;
    read_ints() | cont::sort | cont::unique > ints;
If `cont::unique` is lazy (non-owning), what is the lifetime of the elements allocated by `cont::sort` (or moved from `read_ints()`)? If the code had been written instead like below, who is responsible for keeping the elements alive?

    std::vector<int> ints;
    auto tmp = read_ints() | cont::sort | cont::unique;
    tmp > ints;
There's a whole host of issues here that you're not dealing with.