I am currently experimenting with custom allocators. I have a sm开发者_如何学运维all application that implements a custom allocator with a memory pool.
It works with std::vector
but when I try it with std::set
I get a runtime error:
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>
The problem is that std::set uses my allocator for both the data and the data-node. Since there is no pool registered for the data-node the code will fail.
The details can be found in the code.
Does anyone know what I do to get around this?
This is the rebind
mentioned by @BenVoigt.
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 typeIntAllocator
for objects of typeint
, a related allocator type for objects of typelong
could be obtained usingIntAllocator::rebind<long>::other
.
精彩评论