2011-06-19 33 views
2

我目前正在試驗自定義分配器。我有一個small application實現一個自定義分配器與內存池。將std :: set <T>連接到內存池

它與std::vector但是當我std::set嘗試,我得到一個運行時錯誤:

typedef std::set<Data, std::less<Data>, my_allocator<Data> > PoolSet; 

Pool<Data> pool(1024);  
PoolSet set; 
set.insert(Data()); // error: no pool found for type: std::_Rb_tree_node<Data> 

的問題是,性病::集使用我分配的數據和數據節點兩者。由於沒有爲數據節點註冊池,因此代碼將失敗。

詳情請見code

有沒有人知道我在做什麼來解決這個問題?

+0

你想在地圖內部數據結構進行合併?如果沒有,我想你可以用返回一個'std :: allocator'的方式來定義'my_allocator :: rebind'。 –

回答

1

這是@BenVoigt提到的rebind

http://en.wikipedia.org/wiki/Allocator_(C++)

Allocators are required to supply a template class member template <typename U> struct A::rebind { typedef A<U> other; }; , which enables the possibility of obtaining a related allocator, parametrized in terms of a different type. For example, given an allocator type IntAllocator for objects of type int , a related allocator type for objects of type long could be obtained using IntAllocator::rebind<long>::other .