The C++ standard states that it is assumed that no non-constant function will be applied via the dereferenced iterators, first by stating it in terms of the Compare
functor that can be passed to sort
:
Compare
is a function object type (20.8). The return value of the function call operation applied to an object of typeCompare
, when contextually converted to bool (4), yields true if the first argument of the call is less than the second, and false otherwise.Compare
comp
is used throughout for algorithms assuming an ordering relation. It is assumed thatcomp
will not apply any non-constant function through the dereferenced iterator.
(emphasis mine)
and then, by stating the relation between Compare
and operator<
:
For all algorithms that take
Compare
, there is a version that usesoperator<
instead. That is,comp(*i, *j) != false
defaults to*i < *j != false
. For algorithms other than those described in 25.4.3 to work correctly,comp
has to induce a strict weak ordering on the values.
Both quotes from . From 25.4 Sorting and related operations.
So, although it is not explicitly stated that a member operator<
has to be const
, the assumption that it is means it must be. It would not make sense to place restrictions on comp
and not on operator<
.
I would say Visual C++ is at fault here since it allows for non-const functions to be called on the dereferenced iterators.