简体   繁体   English

std :: map是按引用,按值分配分配器还是将其纯粹用作类型?

[英]Does a std::map take an allocator by reference, by value or use it purely as a type?

When a std::map takes an Allocator as a parameter in a constructor, it takes it by reference with the type taken from its class template parameter: std::map在构造函数中将Allocator用作参数时,它将通过引用从其类模板参数获取的类型进行引用:

explicit map(const Allocator& alloc);

Does it store this reference in the object, or does it take a copy (store it by value), or does it do neither and only use it through the template parameter as a type? 它是否将此引用存储在对象中,还是要获取副本(按值存储),还是不执行任何操作,仅通过template参数将其用作类型? How did you determine this? 您如何确定的?

The allocator is copied into the map. 分配器被复制到映射中。 std::map doesn't specify what it does so we fall back to [container.requirements.general]/8 which states: std::map没有指定要执行的操作,因此我们回溯到[container.requirements.general] / 8,其中指出:

[...]All other constructors for these container types take a const allocator_type& argument. [...]这些容器类型的所有其他构造函数都使用const allocator_type&参数。 [ Note: If an invocation of a constructor uses the default value of an optional allocator argument, then the Allocator type must support value-initialization. [注意:如果构造函数的调用使用可选分配器参数的默认值,则分配器类型必须支持值初始化。 — end note ] A copy of this allocator is used for any memory allocation and element construction performed, by these constructors and by all member functions, during the lifetime of each container object or until the allocator is replaced.[...] —结束说明]在每个容器对象的生命周期内或直到替换分配器之前,此分配器的副本用于由这些构造函数和所有成员函数执行的任何内存分配和元素构造。[...]

Emphasis mine 重点矿

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

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