typedef typename alloc::template rebind<voidptr>::other
voidptr_alloc;
nodeptr new_node(const t& x, nodeptr next) {
alloc a = get_allocator();
nodeptr p = node_alloc(a).allocate(1);
try {
a.construct(p->val, x);
}
catch(...) {
node_alloc(a).deallocate(p, 1);
throw;
}
voidptr_alloc(a).construct(p->next, voidptr(next));
return p;
}
...
};
最后,以防你认为这么费劲才获得这么小的好处太不值了,提醒一下:仅仅因为你能写一个使用allocator的容器,并不意谓你必须,或你应该。有时你可能写一个依赖于特殊的内存分配策略的容器类,比如复杂到基于磁盘的b树容器或简单到我的书中所描述的block类。即使你确实想写一个使用allocator的容器类,你也不是必须支持可能的指针类型。你可以写一个容器,而要求所有用户自定义的allocator使用普通指针,并在文档中明确说明这个限制。不是每件事都要完全通用。