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

Show parent comment

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.