我正在尝试编写一个使用 STL 分配器的容器类。我目前所做的是拥有一个私人会员
std::allocator<T> alloc_;
(这稍后将被模板化,以便用户可以选择不同的分配器)然后调用
T* ptr = alloc_.allocate(1,0);
获取指向新分配的“T”对象的指针(并使用 alloc_.construct 调用构造函数;请参见下面的答案)。这适用于 GNU C++ 库。
但是,使用 Solaris 上的 STLPort,这无法正确执行并导致各种奇怪的内存损坏错误。如果我改为
std::allocator_interface<std::allocator<T> > alloc_;
然后一切正常。
使用 stl::allocator 的正确方法是什么?STLPort/Solaris 版本无法用 g++ 编译,但 g++ 对吗?