Quantcast
Viewing latest article 1
Browse Latest Browse All 3

Answer by R Sahu for Why is const required for 'operator>' but not for 'operator

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.


Viewing latest article 1
Browse Latest Browse All 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>