C++20 ranges::sort
supports projections, and that is great, but I want to do stuff that is more complex, in particular sort on result of function that operates on projected member.
For function call I tried the transform and then sort that view, but C++20 sort seems to not work on views, this answer explains why(for range-v3).
In other words can I write this without using a lambda/functor?
struct S{
int val;
};
int origin = 47;
// assume we can not change Distance to take S
int Distance(int val){
return std::abs(val - origin);
}
void my_sort(std::vector<S> ss) {
std::ranges::sort(ss, std::greater<>{}, [](const S& s){
return Distance(s.val);
});
}