开发者

Connecting std::set<T> to a memory pool

开发者 https://www.devze.com 2023-03-14 02:18 出处:网络
I am currently experimenting with custom allocators. I have a sm开发者_如何学运维all application that implements a custom allocator with a memory pool.

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 type IntAllocator for objects of type int, a related allocator type for objects of type long could be obtained using IntAllocator::rebind<long>::other.

0

精彩评论

暂无评论...
验证码 换一张
取 消