Like forward_list, set, list, etc. Call std :: allocator?

I notice that a allocator can only allocate objects of type T and reserve memory blocks of size n * sizeof(T) . However, nodes of the node list inside the type std::list<T> not necessarily objects of type T , and they do not necessarily have the same size as objects of T In this case, how does std::list use std::allocator to allocate memory?

+6
source share
1 answer

That is why there is a type of re-binding . It allows you to create a similar allocator that instead allocates something else (for example, node<T> ).

Mostly like this:

 std::allocator<int> int_alloc; std::allocator<int>::rebind<node<int>> node_alloc; //Perhaps more useful: decltype(int_alloc)::rebind<node<int>> node_alloc; 

Of course, in a real situation, all this will be a template, but I hope this shows the idea.

Read the notes and examples here for more information.

+4
source

All Articles