Use of std::sort(vec.begin(), vec.end())
depends only on the operator<
function. It does not require that the function be able to work with const
objects.
std::greater
, on the other hand, requires the function be able to work with const
objects.
You will see a similar problem if you use std::less
, such as std::sort(vec.begin(), vec.end(), std::less<MyStruct>())
.
Having said that, there is no reason for the operator<
function and the operator>
function to be non-const
member functions. Any member function that does not modify member data should be made a const
member function.