我注意到分配器只能分配类型为T
的对象并保留大小为n * sizeof(T)
的内存块。然而,在std::list<T>
类型中的链表节点不一定是T
类型的对象,也不一定与T
对象具有相同的大小。在这种情况下,std::list
如何使用std::allocator
来分配内存?
node<T>
)。基本上像这样:std::allocator<int> int_alloc;
std::allocator<int>::rebind<node<int>> node_alloc;
//Perhaps more useful:
decltype(int_alloc)::rebind<node<int>> node_alloc;
std::allocator<T>::rebind
并将n
设置为 1。 :D - yzt