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.
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.
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.
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.
© 2022 - 2024 — McMap. All rights reserved.
cplusplus.com
is hardly a reliable reference for anything. – Instance