简体   繁体   English

std :: map中Key类型的赋值运算符要求

[英]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 a value_type of T using allocator of type A , u denotes a variable, a and b denote non-const lvalues of type X , t denotes an lvalue or a const rvalue of type X , rv denotes a non-const rvalue of type X , and m is a value of type A . 在表99中, X表示使用类型A分配器的value_typeT识别分配器的容器类, u表示变量, ab表示类型X非常量左值, t表示类型的lvalue或const rvalue Xrv表示类型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 and multimap , the requirements placed on value_type in Table 96 apply instead to key_type and mapped_type . 关联容器满足分配器感知容器的所有要求(23.2.1),除了对于mapmultimap ,表96中对value_type的要求适用于key_typemapped_type [ Note : For example, in some cases key_type and mapped_type are required to be CopyAssignable even though the associated value_type , pair<const key_type, mapped_type> , is not CopyAssignable . [ :例如,在一些情况下key_typemapped_type需要是CopyAssignable即使相关value_typepair<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_typepair<const key_type, mapped_type>永远不是CopyAssignable ,因此阅读表99要求来引用它是相当荒谬的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM