Andrew Tomazos
Andrew Tomazos

Reputation: 68618

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?

Upvotes: 3

Views: 158

Answers (1)

NathanOliver
NathanOliver

Reputation: 180500

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

Upvotes: 5

Related Questions