It seems to suggest here What requirements must std::map key classes meet to be valid keys?, and in a few other posts, that the Key type of an srd::map must have an assignment operator. However I have not been able to find that requirement in the standard.
#include <map>
struct Foo
{
Foo& operator=( const Foo& ) = delete;
int id;
};
bool operator<( const Foo&, const Foo& ) { return( false ); }
int main( int, char** )
{
std::map<Foo,int> a;
std::map<Foo,int> b;
a = b; // Should this work if Foo does not have an assignment operator?
return( false );
}
The above compiles with GCC 4.9 and Visual Studio 2013 but fails, complaining about the lack of an assignment operator, with clang 3.5 on an Ubuntu 14.10 box running the following command "clang++ -std=c++11 -stdlib=libc++ code.cpp". Clang does succeed when using the GCC standard library. I suspect the clang standard library is broken here.
§23.1 [container.requirements.general]/p15 & Table 99:
In Table 99,
X
denotes an allocator-aware container class with avalue_type
ofT
using allocator of typeA
,u
denotes a variable,a
andb
denote non-const lvalues of typeX
,t
denotes an lvalue or a const rvalue of typeX
,rv
denotes a non-const rvalue of typeX
, andm
is a value of typeA
.
The relevant part of Table 99 (Allocator-aware container requirements) is:
+-----------+-------------+--------------------------------+------------+
|Expression | Return type | Assertion/note | Complexity |
| | | pre-/post-condition | |
|-----------+-------------+--------------------------------+------------+
| a = t | X& | Requires: T is CopyInsertable | linear |
| | | into X and CopyAssignable. | |
| | | post: a == t | |
+-----------+-------------+--------------------------------+------------+
And then §23.2.4 [associative.reqmts]/p7 says
The associative containers meet all the requirements of Allocator-aware containers (23.2.1), except that for
map
andmultimap
, the requirements placed onvalue_type
in Table 96 apply instead tokey_type
andmapped_type
. [ Note: For example, in some caseskey_type
andmapped_type
are required to beCopyAssignable
even though the associatedvalue_type
,pair<const key_type, mapped_type>
, is notCopyAssignable
. —end note ]
Note that this references Table 96, but given the note the intent is clearly to cover Table 99 as well, since nothing in Table 96 actually requires CopyAssignable
. Since the value_type
, pair<const key_type, mapped_type>
, is never CopyAssignable
, reading the Table 99 requirements to refer to it would be rather absurd.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With