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

Viewed 143

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.

2 Answers

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...

To quote n1905 (the closest I could find to the official 2003 publication, though it's later), under general container requirements:

[lib.container.requirements]/11:

Unless otherwise specified all container types defined in this clause meet the following additional requirements:

  • no swap() function throws an exception unless that exception is thrown by the copy constructor or assignment operator of the container’s Compare object

So if a Compare object needs copying, this pretty much specifies it should be copied.

Related