0

Ranges in C++20 are not very appetizing for many people who came from less-verbose languages like Python, because typing std::ranges:: is too verbose.

The proposals seem to have own rationales, but what I don't understand are constrained algorithms, consider std::sort and std::ranges::sort:

std::sort: link

std::ranges::sort: link

I don't really understand why we have to type like this

std::vector<int> v {3, 1, 4, 2, 6};
std::ranges::sort(v);

Oh, my... just why? Why we can't have std::sort(v);? I can't find any overload resolution ambiguity between the range version of std::ranges::sort() and all versions of std::sort(). (I understand a version that takes an iterator pair, I hate the range version)

Do you know the reason behind this decision?

康桓瑋
  • 33,481
  • 5
  • 40
  • 90
frozenca
  • 839
  • 4
  • 14
  • 3
    Although you can't re-use the name `std`, if you want a shorter name for `std::ranges::sort`, you have a couple of choices. `using namespace std::ranges; /* ... */ sort (v);` or `using s = std::ranges; /* ... */ s::sort(v);` As for "why?" Mostly just that `std` is already bloated, holding a gazillion possible names, so something like `using namespace std;` is dangerous, but `using namespace std:::ranges;` should be quite safe. – Jerry Coffin Aug 01 '22 at 06:45
  • @JerryCoffin I completely agree with you, but `using namespace whatever;` is often frowned upon (of course, in non-header files) to many engineers including some of my coworkers – frozenca Aug 01 '22 at 07:08
  • There's good reason for that, at least with the `std` namespace. But `std::ranges` is a whole different story--the clear intent is that you generally bring it into scope, so you can do it's `foo | sort | take(6) | ...` kind of stuff. – Jerry Coffin Aug 01 '22 at 07:12

0 Answers0