How std :: list highlights nodes against elements

How does std::list highlight the nodes where it stores next / prev pointers and the T element it contains?

I think that standard allocators can only be used to allocate memory for one type (since std::allocator::allocate allocates memory in increments of sizeof(T) ). Thus, it is not possible to select the node list and the contained object in one distribution, which means that the nodes must be distributed with what the implementation decides, and the nodes store pointers to objects instead of the objects themselves and this implies two levels of indirection to get from a pointer to list node to the object it contains, which seems inefficient. Is that the case?

+6
source share
1 answer

The dispenser has a member class, rebind, which is responsible for distributing other types. The std::allocator page here is an example of what you are asking. I will bring it here:

up to C ++ 11

std::list<T, A> selects nodes of some internal type Node<T> using the allocator A::rebind<Node<T>>::other

since c ++ 11

std::list<T, A> allocates nodes of some internal type Node<T> using the distributor std::allocator_traits<A>::rebind_alloc<Node<T>> , which is implemented in terms of A::rebind<Node<T>>::other if A is std::allocator

+6
source

Source: https://habr.com/ru/post/972893/


All Articles