[英]Assignment operator requirement for Key type in std::map
It seems to suggest here What requirements must std::map key classes meet to be valid keys? 似乎在这里建议std :: map密钥类必须满足哪些要求才能成为有效密钥? , and in a few other posts, that the Key type of an srd::map must have an assignment operator.
,以及其他一些文章中,srd :: map的Key类型必须具有赋值运算符。 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". 上面的代码使用GCC 4.9和Visual Studio 2013编译,但是失败了,抱怨缺少赋值运算符,在运行以下命令的Ubuntu 14.10机器上使用clang 3.5,运行以下命令“ clang ++ -std = c ++ 11 -stdlib = libc ++ code.cpp ”。 Clang does succeed when using the GCC standard library.
使用GCC标准库时,Clang确实成功。 I suspect the clang standard library is broken here.
我怀疑clang标准库在这里坏了。
§23.1 [container.requirements.general]/p15 & Table 99: §23.1[container.requirements.general] / p15和表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
.在表99中,
X
表示使用类型A
分配器的value_type
为T
识别分配器的容器类,u
表示变量,a
和b
表示类型X
非常量左值,t
表示类型的lvalue或const rvalueX
,rv
表示类型X
非常数值,m
是类型A
的值。
The relevant part of Table 99 (Allocator-aware container requirements) is: 表99(可识别分配器的容器要求)的相关部分是:
+-----------+-------------+--------------------------------+------------+
|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 然后§23.2.4[associative.reqmts] / p7说
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
.关联容器满足分配器感知容器的所有要求(23.2.1),除了对于
map
和multimap
,表96中对value_type
的要求适用于key_type
和mapped_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
.[ 注 :例如,在一些情况下
key_type
和mapped_type
需要是CopyAssignable
即使相关value_type
,pair<const key_type, mapped_type>
是不是CopyAssignable
。 — 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
. 请注意,此表引用了表96,但要注意的是,该表显然也要涵盖表99,因为表96中的任何内容实际上都不需要
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. 由于
value_type
, pair<const key_type, mapped_type>
永远不是CopyAssignable
,因此阅读表99要求来引用它是相当荒谬的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.