我想寫一個使用STL分配器的容器類。我現在做的是有一個私有成員如何在我自己的容器類中使用std :: allocator
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'。 – AraK