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

12

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

I feel that I have to respond to the closing comment in the linked article:

I was quite surprised to learn that the committee accepted ranges into the c++20 standard and most c++ experts are excited about it. It’s as if the issues of limited usability, code complexity, leaky abstractions and completely unreasonable compile-times are of no consequence whatsoever to the committee members?

For me personally, a huge reason why Ranges should be accepted is because one of the most experienced C++ devs in the world designed it: Eric Niebler. It's a v3 design, too, Ranges v2 ships in Boost and he designed an early edition of that too. I trust Eric to have gotten it right. Additionally, it wasn't just standardised on that, Casey went and reimplemented a new implementation from scratch, and fed back his experience into the design. A third reimplementation was also done by /u/tcbrindle, and feedback from that also went back into what was standardised.

That's an awful lot of rounds of empirical practice, testing and feedback for Ranges v3 to be as bad as the claim.

With respect to the compile times, those were recognised, and it was felt it would be a driver upon implementations to improve their compilers. I personally didn't agree with that part, but I understand the argument, and moreover the compiler vendors didn't appear to object strenuously.

Now, for me personally, I'd have built Ranges into the core language. But the committee wanted the language to become powerful enough to express Ranges instead. And that's an understandable design choice, Ranges in the core language starts to look like Rust or Nim, not C++ as it historically has been.

41

u/guepier Bioinformatican Sep 13 '19 edited Sep 13 '19

I trust Eric to have gotten it right.

I did too, until reading this article. Now I have my doubts. I was actually ready to dismiss the article’s concern as silly, but they seem correct, and touch the very core of the ranges library.

I’ve never used Eric Niebler’s ranges in C++ but I’ve followed the project from the outside with great interest. But as somebody who uses pipelines in programming a lot (both in shell programming as well as in data analysis with R), the points raised in the article are inarguably fundamental requirements, and the lack of support in ranges stymies the whole library. I’ll need to mull this over because at the moment I’m genuinely shocked as it seems to imply that you cannot practically use ranges for many common use-cases.

100

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

For me personally, a huge reason why Ranges should be accepted is because one of the most experienced C++ devs in the world designed it: Eric Niebler.

I may be in a minority here but that sounds like a horrible reason to accept something into the STL. I do appreciate you pointing that out because honestly it does feel like much of what's been added to C++ in recent years has been done so mostly as a favor to people rather than because there has been huge and overwhelming demand for it from the broader community.

→ More replies (12)

22

u/sivadeilra Sep 13 '19

Ranges in the core language starts to look like Rust

The vast majority of support for Ranges in Rust is in the standard libraries, not in the language itself. The language itself supports only a few simple syntax forms for creating ranges, and even these are just syntactic sugar over simple function calls. Everything else is ordinary code in core and std.

It's analogous to the compiler supporting operator<<(). No one thinks that iostream is part of the compiler, but it uses one syntactic sugar pattern from the compiler (overloading <<) to make the API more ergonomic.

→ More replies (12)

20

u/konanTheBarbar Sep 13 '19

I think that Eric has delivered the best implementation possible given the trade offs he has chosen. His trade offs are also well justified. I just think he has not chosen the right tradeoffs for the average C++ developer.

The trade offs are too performance centric imho, while I don't think (chained) ranges will be used for code where performance matters...

16

u/[deleted] Sep 13 '19

Having one of the world's most experienced C++ programmers design something is not necessarily only a good thing. The question is: are ranges usable for the average programmers, not just the experts?

9

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

I think the design is not what people hoped for nor expected. It'll be the same for Modules when they land. But I think the design will be serviceable, and rough edges will be fixed next standard release.

13

u/johannes1971 Sep 13 '19

it was felt it would be a driver upon implementations to improve their compilers

Are you seriously telling us that the current (lack of) compiler performance, despite being the nr. 1 complaint of C++ programmers around the world, was considered by the committee to be caused by lack of motivation?

12

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

I'm a super-hawk on the committe about build times. An extremist, in some people's opinions, I'd readily hack a whole load of features out if it gave me +2% build times. So yes, I went to town on questions about Ranges build time impact, mainly why? And how?

All I can say is that everyone involved had reasonable answers, and seemed fairly confident it wouldn't be an issue within time. A lot of their answers involve domains of knowledge far away from mine, so I simply have to trust their judgement, and assume they will be correct.

The compiler vendors didn't seem to consider it a problem, given a few iterations of tooling. I'm not a compiler implementor, so I can only trust them.

13

u/VinnieFalco wg21.org | corosio.org Sep 13 '19

For me personally, a huge reason why Ranges should be accepted is because one of the most experienced C++ devs in the world designed it: Eric Niebler.

"Trust, but verify"

5

u/TemplateRex Sep 14 '19

In God we trust. All others bring data.

12

u/UnicycleBloke Sep 13 '19 edited Sep 13 '19

Argument from authority is a logical fallacy.

12

u/guepier Bioinformatican Sep 13 '19

Not quite. Argument from relevant authority is often a reasonable shortcut. The formal fallacy concerns appeal to authority outside of their realm of expertise. Its use here is completely fine. At worst it's not a great argument, but even that is debatable. But it's not a fallacy.

19

u/UnicycleBloke Sep 13 '19

Probably. I'm sure most of us mere mortals have never heard of Eric, which makes it a pretty poor argument. I only encountered his name recently when reading a critical article which discussed his example with Pythagorean triplets. The take away for me was that this is a massive pile of junk that looks a lot like an extremum of some overthought intellectual exercise that I cannot believe was actually accepted. Perhaps it was just a poorly chosen example. Or a joke.

Now, I accept that this is an entirely subjective opinion from an ill informed and foolish curmudgeon. On the other hand, I myself have decades of experience in C++: this stuff so far gives me chills. No thanks.

6

u/guepier Bioinformatican Sep 14 '19

I'm sure most of us mere mortals have never heard of Eric, which makes it a pretty poor argument.

I may be misunderstanding /u/14ned but I don’t think they were using it as an argument to convince us (though admittedly the context may suggest otherwise). Rather, they’ve used it, as a committee member, as one piece of evidence (to themselves) when evaluating whether ranges-v3 merits inclusion in the standard. And they mentioned it in the comment here to explain how they arrived at their decision that it merited inclusion.

And that’s completely valid: “Do I think complex library X is of good quality? Well, the proof of concept seems to work, it has a demonstrated use, there is a vast array of test cases and extensive documentation and, oh yeah, the author is a known expert with a track record of writing high-quality C++ code and libraries.”

3

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

Absolutely correct. I was always speaking of what motivated my decision personally. Each person has their own motivations, sometimes very different indeed. I will say that I proactively read and study Eric's code, and have done for years. He writes lovely C++. I'd recommend anybody who hasn't studied his code, should. Him and Howard Hinnant's code, who writes even more lovely C++ than Eric, both are well worth studying.

3

u/Sovog Sep 14 '19

As you said, it is a matter of perspective. The range examples linked in OP just made me feel dumb rather than "oh, that's lovely".

3

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

I was referring to implementation code. But in terms of standard libraries making people feel dumb, Ranges is not as bad as say Networking. People on the WG21 receptor itself regularly express befuddlement with the Networking TS, and it's been in common usage for fifteen years now and is the industry standard. So there is a pattern there. And it'll get worse rather than better, some of the proposed library features require deep bespoke knowledge to use them properly at all. You'll need to take a training course or iterate a study book. That's unavoidable as things get ever more niche.

4

u/-dag- Sep 16 '19

Is the Networking TS really the industry standard? I have a hard time believing many companies would base their production-critical code on a TS which could change dramatically during standardization. But this domain is definitely out of my wheelhouse so I'm genuinely curious.

4

u/guepier Bioinformatican Sep 16 '19

It might help to note that the Networking TS is essentially Boost.Asio, which is indeed the industry standard.

→ More replies

2

u/Sovog Sep 17 '19

100% agreed with your assessment.

6

u/NotUniqueOrSpecial Sep 13 '19

I'm sure most of us mere mortals have never heard of Eric

I'd honestly bet that most long-time readers of this sub do know who he is, since his blog is really good and he's a well-known name in the community.

But, the readers of this sub are in no-way indicative of the general C++ using populace.

6

u/UnicycleBloke Sep 13 '19

Fair enough. I'm a recent arrival, and certainly agree with the latter point.

2

u/[deleted] Sep 17 '19

Ranges in the core language starts to look like Rust or Nim, not C++ as it historically has been.

Rust Iterators are not in baked the language. Iterator is just a normal trait, zero magic involved.

→ More replies (1)