I'm interested in a C++17 answer.
Here's a short example
#include <algorithm>
#include <range/v3/algorithm/sort.hpp>
#include <vector>
struct A {
int x;
A(int x) : x{x} {}
};
bool operator<(A const& a1, A const& a2) { // if this is here,
return a1.x < a2.x;
}
int main()
{
std::vector<A> v{A{1},A{3},A{6},A{5},A{2},A{0},A{4}};
std::sort(v.begin(), v.end()); // then this compiles and works,
//ranges::sort(v); // but this doens't compile!
}
The error isn't particularly intimidating, but I don't really understand it
hello.cpp:17:17: error: no match for call to ‘(const ranges::sort_fn) (std::vector<A>&)’
17 | ranges::sort(v); // doens't compile
| ~~~~~~~~~~~~^~~
In file included from /usr/include/range/v3/range_fwd.hpp:25,
from /usr/include/range/v3/algorithm/sort.hpp:39,
from hello.cpp:2:
/usr/include/range/v3/algorithm/sort.hpp:201:11: note: candidate: ‘template<class I, class S, class C, class P, bool CPP_true, typename std::enable_if<(sortable<I, C, P> && CPP_true), int>::type <anonymous>, typename std::enable_if<(random_access_iterator
<I> && CPP_true), int>::type <anonymous>, typename std::enable_if<(sentinel_for<S, I> && CPP_true), int>::type <anonymous> > I ranges::sort_fn::operator()(I, S, C, P) const’
201 | I RANGES_FUNC(sort)(I first, S end_, C pred = C{}, P proj = P{})
| ^~~~~~~~~~~
/usr/include/range/v3/algorithm/sort.hpp:201:11: note: template argument deduction/substitution failed:
hello.cpp:17:17: note: candidate expects 4 arguments, 1 provided
17 | ranges::sort(v); // doens't compile
| ~~~~~~~~~~~~^~~
In file included from /usr/include/range/v3/range_fwd.hpp:25,
from /usr/include/range/v3/algorithm/sort.hpp:39,
from hello.cpp:2:
/usr/include/range/v3/algorithm/sort.hpp:218:9: note: candidate: ‘template<class Rng, class C, class P, bool CPP_true, typename std::enable_if<(sortable<decltype (ranges::_::begin(declval<Rng&>())), C, P> && CPP_true), int>::type <anonymous>, typename std
::enable_if<(random_access_range<Rng> && CPP_true), int>::type <anonymous> > ranges::borrowed_iterator_t<Rng> ranges::sort_fn::operator()(Rng&&, C, P) const’
218 | RANGES_FUNC(sort)(Rng && rng, C pred = C{}, P proj = P{}) //
| ^~~~~~~~~~~
/usr/include/range/v3/algorithm/sort.hpp:218:9: note: template argument deduction/substitution failed:
In file included from /usr/include/range/v3/range_fwd.hpp:22,
from /usr/include/range/v3/algorithm/sort.hpp:39,
from hello.cpp:2:
/usr/include/range/v3/algorithm/sort.hpp:216:54: error: no type named ‘type’ in ‘struct std::enable_if<false, int>’
216 | requires sortable<iterator_t<Rng>, C, P> AND random_access_range<Rng>)
|
(Related c++20std-ranges-specific question here.)