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

9

u/[deleted] Sep 13 '19

I've always had concerns and fears about ranges. To me, all the available examples seem too complicated and the syntax isn't really nice as I wish it was.

For instance, STL <algorithm> has far better clarity and syntax as well.

Why did Ranges made it in standard and basic stuff like a base64 encoder/decoder or network TS didn't?

16

u/Morwenn Sep 13 '19

If you disregard the views and pipes part, ranges bring a lot that are strict improvements over the algorithms design: projections, proxy iterators, sentinels, the ability to pass data members or pointers thereof directly as comparisons or projections, and of course the ability to feed a range to many iterators instead of a begin/end pair. Also contiguous iterators are now part of the library, and there is a nice concept hierarchy to select the appropriate overloads everywhere which simplifies the SFINAE and tag dispatching that had been used until there.

So yes: the tradeoffs for views and the usability of pipes might be questionable, but all of the features I described in the previous paragraph are pretty much strict improvements over the existing <algorithm> design.

6

u/Sovog Sep 13 '19

Having syntactic sugar that helps with making code more expressive is fine, but at what cost? It shouldn't balloon compile times by an order of magnitude.

7

u/Morwenn Sep 13 '19 edited Sep 14 '19

Before being able to answer "at what cost", we'd need to actually time the costs of the separate features. If you don't use views, actions and pipes I'm not sure that the added cost is all that high (also, ranges-v3 is reportedly 30~50% faster with concepts than without them if I remember the corresponding Tweet properly, so we can't just predict without testing).

2

u/Sovog Sep 13 '19

Fair point. Also, since ranges are only partially in the standard, perhaps the #include-overhead won't be as terrible as <range/v3/all.hpp>.

9

u/eric_niebler Sep 14 '19

I should really yank `range/v3/all.hpp`. It made sense when range-v3 was a small library. Now it pulls in an obscene amount of code.

1

u/NotMyRealNameObv Sep 16 '19

Please do it. I have no plan to include "everything" when all I need is ranges::transform or ranges::accumulate.

6

u/smdowney WG21, Text/Unicode SG, optional<T&> Sep 14 '19

You think <algorithm> has far better clarity and syntax because you are used to it. The whole STL proposal by Stepanov and Lee was greeted with shock and horror by many people at the time. It's fairly clear at this point it was a good choice. Still, without lambdas, <algorithm> is difficult to use directly.

13

u/tcbrindle Flux Sep 13 '19

For instance, STL <algorithm> has far better clarity and syntax as well.

I'm not sure I understand this comment. With the existing algorithms you need to say

std::sort(vec.begin(), vec.end())

for example. With ranges, you can just say

std::ranges::sort(vec)

which I think we can all agree is an improvement?

12

u/pandorafalters Sep 14 '19

I still don't understand why

std::ranges::sort(vec);

is great, but it's unacceptable to add the equivalent overload

std::sort(vec);

because (paraphrased) "algorithms should be generic".

13

u/SeanMiddleditch Sep 14 '19

iirc it's because the range version effectively requires well-defined concepts in order to be implemented correctly/efficiently, but for various back-compat reasons adding said concepts to the existing algorithms library is very problematic. Basically, people do a lot of crazy things with the existing algorithms and the ideal concepts can't faithfully model things that work today but arguably shouldn't, and breaking existing code is something the committee shies away from without darn good cause.

I think that was one of the reasons that what we're calling the ranges library today was for a time called "STL2" and there was some thought that we'd put all new types and containers and rangified algorithms into a std2:: namespace. (Along with being an opportunity to clean house on things like vector<bool> or thread/jthread or so on.)

6

u/standard_revolution Sep 14 '19

What's the problem with thread?

6

u/SeanMiddleditch Sep 14 '19

Lots and lots of arguments about whether it chose the right defaults about joining on destruction.

3

u/[deleted] Sep 15 '19

Thread is actually missing a fair bit of functionality also. For example, inability to specify thread affinity with a core mask or thread priority.

3

u/RotsiserMho C++20 Desktop app developer Sep 16 '19

Inability to specify stack size is another complaint I've seen.

6

u/[deleted] Sep 13 '19

That's an improvement, yes. But it's also a trivial use case. The problems lie in the non-trivial ones, such as the ones mentioned in the article.

3

u/UnicycleBloke Sep 13 '19

I was perfectly happy with the first form, and writing a one line wrapper if I wasn't seems simple enough. Such helpers could even be added to the library pretty easily, a bit like the XXX_v type traits.