Requirement of an assignment operator for a key type in std :: map

It seems here What requirements should correspond to the classes of the std :: map classes that are valid? , and in several other messages - the key type srd :: map must have an assignment operator. However, I could not find this 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 compilation with GCC 4.9 and Visual Studio 2013, but will not work, complaining about the lack of an assignment operator, with clang 3.5 in the Ubuntu 14.10 box that executes the following command: "clang ++ -std = C ++ 11 -stdlib = lib ++ code.cpp " Klang succeeds using the standard GCC library. I suspect the standard clang library is broken here.

+5
source share
1 answer

Β§23.1 [container.requirements.general] / p15 and Table 99:

In table 99, X denotes a container class supporting a dispenser, with value_type of T using a dispenser of type A , u means a variable, A and b denote non-constant lvalues ​​of type X , T denotes an lvalue or const rvalue of type X , rv denotes a non-const rvalue of type X , and m is a value of type A

Relevant part of Table 99 (container requirements that take into account Allocator requirements):

 +-----------+-------------+--------------------------------+------------+ |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

Associative containers meet all requirements. Containers intended for use of the product (23.2.1), except that for map and multimap requirements for value_type in table 96 are key_type instead of key_type and mapped_type . [Note: for example, in some cases key_type and mapped_type must be CopyAssignable although the associated value_type , pair<const key_type, mapped_type> are not CopyAssignable . -end note]

Please note that this link is shown in table 96, but given the note, it is obvious that the goal should also cover table 99, since table 96 does not actually require CopyAssignable . Since value_type , pair<const key_type, mapped_type> never CopyAssignable , reading the requirements of table 99 to reference it would be rather absurd.

+4
source

Source: https://habr.com/ru/post/1208008/


All Articles