I am trying to implement an STL style container class, and I have a question with respect to the use of allocators within my class:
What is the purpose of the static member functions in STL's allocator_traits?
Until now, I thought that I am supposed to instantiate the allocator_type (probably via some kind of empty base optimization to improve the memory footprint). Thus, I would end up with something like this:
struct EmptyBaseOpt : allocator_type
{
EmptyBaseOpt(const allocator_type & a) : allocator_type(a) { }
pointer prefix_ptr;
}
EmptyBaseOpt ebo;
And then, I could use the allocator in the following way:
allocator_type & alloc = ebo;
alloc.allocate(100, ebo.prefix_ptr);
On the other hand, the allocator_traits in C++11 seem to imply the following usage:
std::allocator_traits<allocator_type>::allocate(100, ebo.prefix_ptr);
I guess this static allocate member function will probably create a temporary ad-hoc instance of allocator_type via its default constructor. But this leads to the following questions:
-
What will happen if
allocator_typeis a stateful allocator? Will such allocators be able to keep their state if I use the static member functions inallocator_traitsinstead of calling the non-static methods from an instance ofallocator_type? -
Why should I instantiate
allocator_typeat all and bother with stuff like EBO if I can directly use the static member functions inallocator_traitsinstead? -
As stated before, my understanding is that any class-like template parameters should be instantiated inside my container class in order to allow for stateful versions of these parameters. Is this understanding correct, and how does it fit with the static member functions in
allocator_traits?
Best regards, Alexander
Aucun commentaire:
Enregistrer un commentaire