Search code examples
c++dictionarylanguage-lawyerassignment-operator

Does std::map assign its comparator?


Is std::map copy assignment (in style map1 = map2;) required to copy comparator of map2 to map1?

I have tested that actual implementations do that. I am more interested about where in C++ standard it is specified.


Solution

  • If we look at [associative.reqmts]/12 we have

    When an associative container is constructed by passing a comparison object the container shall not store a pointer or reference to the passed object, even if that object is passed by reference. When an associative container is copied, either through a copy constructor or an assignment operator, the target container shall then use the comparison object from the container being copied, as if that comparison object had been passed to the target container in its constructor.

    emphasis mine

    So, in your example, map1 will get a copy of map2's comparator.