forward_list,set,list等如何调用std :: allocator?

cqw*_*eur 6 c++ stl allocator

我注意到allocator只能分配类型的对象T并保留大小的内存块n * sizeof(T).std::list<T>但是,类型内部的链接列表节点不一定是类型的对象T,也不一定是与T对象相同的大小.在那种情况下,如何std::list使用std::allocator分配内存?

Cor*_*bin 5

这就是重新绑定类型存在的原因。它允许您创建一个类似的分配器,而不是分配其他东西(例如 a 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;
Run Code Online (Sandbox Code Playgroud)

当然,在真实情况下,这一切都是模板化的,但希望这能说明这个想法。

有关更多信息,请阅读此处的注释和示例。