Considering:
#include <algorithm>
#include <vector>
struct A {
double dummy;
bool operator<(const A& a) { ///Here I am missing a `const`
return dummy < a.dummy;
}
};
int main()
{
std::vector<A> a;
a.push_back({0.9});
a.push_back({0.4});
std::sort(a.begin(),a.end());
return 0;
}
This compiles fine on gcc, but on clang it gives
/usr/include/c++/v1/algorithm:719:71: error: invalid operands to binary expression ('const A' and 'const A')
bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
and a long list of failed instantiation. Here is my minimal example in action: https://rextester.com/VEO17629
I could finally solve it when I found Invalid operand to binary expression on std::max_element (the operator < must have a const specifier).
The curious thing is that the error goes away also if I call std::sort specifying the operator std::less<>():
std::sort(a.begin(),a.end(),std::less<>());
Why does specifying the compare operator std::less<>() solve the error?