lundi 5 juin 2017

Why get_allocator returns a copy and not a reference

I am using a custom allocator, which has a small simple state internally. When I use it with std::vector, I was surprised to see that get_allocator() actually returns copy to the allocator.

This is documented here: http://ift.tt/1pPlvVU

What is the reason for this?

One reason I can imagine is that get_allocator is a const member, I wouldn't mind getting a const reference to the allocator. Besided the internal allocator could have been declared mutable.

Am I missing something? Is there a workaround to get the internal allocator of std::vector<T, special_allocator<T>> to inspect the state of the allocator?

Note: There is also the possibility that if the allocator is stateful then, perhaps one is supposed to use references all the way? Like std::vector<T, special_allocator<T>&>

Aucun commentaire:

Enregistrer un commentaire