Does std::map assign its comparator?
Asked Answered
M

2

8

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.

Motet answered 20/7, 2017 at 14:58 Comment(0)
T
11

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.

Tetradymite answered 20/7, 2017 at 15:6 Comment(0)
T
-2

From cplusplus.com you can see that under copy-constructor (3) says

(3) map (const map& x);

The container keeps an internal copy of alloc and comp, which are used to allocate storage and to sort the elements throughout its lifetime. The copy constructor (3) creates a container that keeps and uses copies of x's allocator and comparison object.

Tabbie answered 20/7, 2017 at 15:6 Comment(3)
Isn't the OP asking for a Standards reference? Also, cplusplus.com is hardly a reliable reference for anything.Instance
Also the OP is asking about the copy assignment operator, not the copy constructor.Tetradymite
I was asking about operator=. Part of the reason asking it was that sites like cplusplus.com do tell nothing about that important detail. See: cplusplus.com/reference/map/map/operator=Walton

© 2022 - 2024 — McMap. All rights reserved.