map<const T, const U> is assignable even though pair<const T, const U> is not

Jonathan Wakely jwakely@redhat.com
Thu Apr 23 10:48:00 GMT 2015


Marshall Clow sent me this problem:

On 22 April 2015 at 21:49, Marshall Clow wrote:
> The following code fails to compile w/libc++, but succeeds with
> libstdc++.
>
> #include <map>
>
> int main () {
> std::map<const int, const int> foo1, foo2;
> foo2 = foo1;
> }
>
> assigning a allocator-aware container requires that the value type
> be
> CopyAssignable, which pair<const int, const int> is definitely not.
>

I think this is a consequence of our optimization to recycle nodes on
assignment, which runs a destructor and then constructs a new element
with placement new, so it doesn't actually do any assignments.

The example violates the preconditions on the assignment, so it's
undefined behaviour, but it's a bit surprising that it Just Works.

Do we want to make this type non-CopyAssignable?



More information about the Libstdc++ mailing list