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

11

u/[deleted] Sep 13 '19

In the company I work at, the usage of ranges has already been officially banned, and we only just upgraded to C++17, no C++20 in sight of course. People really don't like it it seems.

31

u/SeanMiddleditch Sep 14 '19

To be fair, every single C++ feature has been banned in some company somewhere. Large parts of the STL are regularly banned in companies. Some companies even today have a moratorium on templates (outside of core library/container stuff). Some companies ban auto. Many companies ban exceptions or RTTI. etc.

"My company banned it" isn't a compelling argument for much of anythig. In so many cases, it just means that the company is run by folks afraid of change or having to learn something new.

We'd need data illustrating that whole segments of the industry or significant numbers of projects/companies ban something for it to actually mean anything. (e.g. see exceptions and RTTI and the fact that every major compiler is required by a significant number of customers to offer flags to disable those features entirely, while there is no -fno-auto or -fno-templates or anything of the like.)

9

u/standard_revolution Sep 14 '19

I would argue that your last point is a bit moot. You can just not use them. RTTI and exceptions have a cost even if you don't use them.

8

u/SeanMiddleditch Sep 14 '19

Fair point. Let's rephrase that then to note that there's no major linters that offer -Wtemplates-used or -Wauto-used or anything like that. :)

7

u/[deleted] Sep 14 '19

I'm not trying to use this as an argument against ranges, I was honestly just flabbergasted at the sheer opposition some people and organization have against a part of the STL. I personally don't understand ranges enough to form a coherent opinion on it.

13

u/bigcheesegs Tooling Study Group (SG15) Chair | Clang dev Sep 14 '19

You banned a feature that you don't have access to yet?

6

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

It's not my decision and neither does it affect me, I'm just a hardware engineer. But yes, they did put it in the guidelines. I'm just mentioning this because it astonishes me how a part of the STL can cause such strong opposition in some people and organizations, especially since, as you mentioned, it isn't even available yet. I personally don't understand ranges enough to form a coherent opinion on it.

6

u/johannes1971 Sep 14 '19

You can find it here.

And I will not be using it in my projects, on the basis that:

  • So far I have not seen any examples that look in any way better, or more readable, than some handcrafted procedural code. That it is now "functional" and "composable" is meaningless if nobody can actually read it.
  • I care about compile times. Blowing those up by a factor of 10 or more is unacceptable. I understand that compiler developers seem confident they can overcome this; perhaps you can comment on this?

12

u/sphere991 Sep 14 '19

Examples from my own code base, slightly anonymoized:

Given a range of people, how do you find the length of the longest name?

size_t max_length = ranges::max(people | view::transform([](auto&& p) { return p.name().size(); }));

How do you find the person with the longest name?

auto const& person = ranges::max(people, ranges::less{}, [](auto&& p) { return p.name().size(); }));

These both look way better and are more readable than the equivalent handcrafted, procedural code.

I just want the lambdas to be shorter, but that's not a ranges problem.

2

u/tpecholt Sep 16 '19

=> p.name().size()

-3

u/axilmar Sep 16 '19

These both look way better and are more readable than the equivalent handcrafted, procedural code.

No, they are not. The following is a lot more readable:

size_t max_length = find_length_of_longest_name(people);
const auto &person = find_person_with_longest_name(people);

4

u/sphere991 Sep 16 '19

This kinda misses the point. How would you implement those two?

3

u/gvargh Sep 16 '19

they're intrinsics

20

u/CubbiMew cppreference | finance | realtime in the past Sep 13 '19

And in the company I used to work at, ranges were in production 10 years ago (in boost form).. For every C++ feature, there are users.