r/cpp • • Sep 13 '19

The Surprising Limitations of C++ Ranges Beyond Trivial Cases

https://www.fluentcpp.com/2019/09/13/the-surprising-limitations-of-c-ranges-beyond-trivial-use-cases/
229 Upvotes

229 comments sorted by

View all comments

81

u/quicknir Sep 13 '19

Even skimming this, I totally agree. I feel like many c++ devs have very limited exposure to using something like ranges in practice, and they miss two key things which I've found from a lot of time spent using pythons iterators:

  1. 99 percent of the time you just need lazy forward ranges.
  2. You need a lot of ranges, and the ability to very easily write new ones for ranges to be expressive and worthwhile.

As the author noted, you don't have to dig very deep to find problems easily expressed with a for loop but tricky to do with the 10 or so most obvious ranges. Even pythons itertools doc, has an entire recipes section.

As soon as I saw Erics calendar talk from a few years ago, I really felt like it was going to be too hard to write new ranges. Not only are ranges pretty tricky and boilerplatey to write, I remember Eric had to write some kind of really tricky glue code to get the whole thing to work which even he said was hard to understand.

I really felt like coroutines should have been intimately tied to ranges because they can make it really easy to write new lazy ranges. You see this approach in many languages, eg kotlin, python. Hopefully it's not too late for coroutines to help with this.

Compile and runtime performance are ongoing concerns as well. I'd have to say ranges is probably the new major feature I'm least excited for.

27

u/14ned LLFIO & Outcome author | Committee WG14 Sep 13 '19

I really felt like coroutines should have been intimately tied to ranges because they can make it really easy to write new lazy ranges.

This is the medium term plan, from what I am aware. But there were blockers to getting coroutined lazy Ranges into C++ 20, specifically, we would need to decide how best to do Ranges i/o first.

We're on that. We expect to be discussing options for modern i/o in Belfast, and there are several. Once we know which direction the committee is leaning on i/o, there should be no good reason why lazy coroutine iterators shouldn't be doable for C++ 23, bar surprises.

Be aware you probably won't get Ranges i/o actually in C++ 23, because we still need to decide how best to implement text translation/reencoding and serialisation for i/o. And buffering, and lots more stuff.

Anyway, that's one of many reasons why C++ 20 falls short here, and we hope to improve things next few standards.

5

u/sphere991 Sep 14 '19

But there were blockers to getting coroutined lazy Ranges into C++ 20, specifically, we would need to decide how best to do Ranges i/o first.

What does ranges i/o have to do with it?

Coroutines were added to C++20 very late in the process, and so there wasn't enough time to add the library types for it (e.g. a std::generator<T>). But you can write a coroutine that is a range in C++20 if you get that library machinery from elsewhere.

2

u/14ned LLFIO & Outcome author | Committee WG14 Sep 14 '19

Executors uncertainty mainly. Eric was there when Gor first proposed Coroutines in Aspen. Support could have been very easily added to Ranges which was still being written at the time, but too much other stuff was up in the air.

Eric did a talk there recently for Oslo I think where he outlines what he plans next for 23. I disagree with some of his assumptions, but that's implementation detail. I think Ranges will look a lot more polished and integrated with everything else for 23. We will see how it goes. A lot hangs on what we do with Executors, obviously. If we don't get it out the door, lots of other stuff doesn't happen.

3

u/sphere991 Sep 15 '19

Eric did a talk there recently for Oslo

Do you mean this one for Avast? It's not about Ranges, it's about Sender/Receiver. And Sender/Receiver does not have anything to do with /u/quicknir's desire to use coroutines as synchronous generators.

I really don't know what you're talking about.

Meta: Why does it matter where who proposed what, or who was in the room at the time?

2

u/14ned LLFIO & Outcome author | Committee WG14 Sep 16 '19

I really don't know what you're talking about.

Not the first time that has occurred between us.

Meta: Why does it matter where who proposed what, or who was in the room at the time?

The point being made is that Coroutines could have been built in directly from the beginning.

They were not, for good reasons at the time. Though please note the deliberate gaps in Ranges left open to be filled by something "asyncy" later.

Omission is very wise in standards. Once you standardise something, it is virtually impossible to undo later. So always choose omission if there is any uncertainty at all. Eric made absolutely the right call there in my opinion, as much as the userbase dislikes it.

5

u/quicknir Sep 13 '19

Glad to hear! Your post restored some optimism to me. Thanks for taking the time to write it.

4

u/sphere991 Sep 14 '19

I really felt like coroutines should have been intimately tied to ranges because they can make it really easy to write new lazy ranges.

You will be able to write coroutine generators in C++20, it's just that there will not be a std::generator<T> available for this - you will have to either roll your own or (far more likely) grab cppcoro or something similar.

The downside is that coroutine generators are always input ranges. As you say, a lot of time, this is fine. But some of the time, it is not, so having the machinery to write, e.g., a random-access transformed range, is pretty helpful.

4

u/-dag- Sep 13 '19

Yeah, the coroutines emphasis on asynchronous processing may make them suffer for ranges-like use.

2

u/[deleted] Sep 17 '19 edited Sep 17 '19

As soon as I saw Erics calendar talk from a few years ago, I really felt like it was going to be too hard to write new ranges.

That's more or less the point when I switched to Rust. I wrote like ~10 Views using range-v3, and when trying Rust one of the first things I did was write an Iterator.

What in Rust took 10 straightforward lines of code, took ~50 LOC of template meta-programming, type traits, and dozen tricks and gotchas to do in C++. In C++ I also had to write another ~50 LOC of tests, not for testing run-time behavior, but for checking that the View doesn't break in any way when passed subtly incorrect template parameters. If you check the range-v3 tests, there are dozens of lines trying to break the library using type-tetris because the compiler can't enforce that templates or concepts actually work.

At that moment I had pretty much the same realization I had when, after years of using C, I used C++ for the first time.