I still can't find anything which says this explicitly, but if you modify a key[*], then for example your comparator doesn't satisfy the requirements of 25.3 (strict weak order):
comp induces a well-defined relation
on the equivalence classes determined
by equiv
comp
isn't a well-defined relation if it returns different values for the same inputs at different times.
In your case I believe that it's fine to modify fields of MyClass
which aren't involved in the comparison.
Interestingly, 23.1.2/2 says, "Each associative container is parameterized on Key and an ordering relation Compare that induces a strict weak ordering (25.3) on elements of Key". I think we can take this to mean that the comparator induces a strict weak ordering on Key
objects which are elements of the container, not necessarily on all objects of type Key
. For instance if the Key is a pointer then I'm pretty sure it's fine to write a comparator that dereferences it, provided you don't use a null pointer as a key. By the same reasoning, I hope we can modify a key which isn't in the container.
[*] by "modify", I mean do anything which changes the results of the comparator with that key and some other key. In this case of course you aren't really modifying the key itself (which is just a pointer value), but that's what I'm calling it.