简体   繁体   中英

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:

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? 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:

[...]All other constructors for these container types take a const allocator_type& argument. [ 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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