【发布时间】:2011-01-31 08:34:13
【问题描述】:
我正在尝试编写一个使用 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++对吗?
【问题讨论】:
-
标准库中没有名为
allocator_interface的东西。
标签: c++ g++ memory-management stlport