Search code examples
c++setcomparatorswap

Do copy/move/swap also operate on the comparators of std::set/map?


Which of the move-assignment, copy-assignment, and swap operations (if any) also operate on the embedded comparators for sets and maps?

(In other words, if I e.g. swap two sets, will their comparators be swapped too, or no?)

If the answer is different for older versions of C++ (e.g. C++98 or C++03) then please mention that.


Solution

  • Standard contains following guarantee

    For associative containers, no swap function throws an exception unless that exception is thrown by the swap of the container’s Compare object (if any).

    So answer is yes - Compare objects are also swapped by the swap function.

    The quote is for C++11-17. I do not have earlier drafts.

    For copy and move - I can only deduce - that since any reasonable implementation of copy/move uses swap the same answer is true. But I have not found the specific quote...