Since you want a single underlying pool, and allocators can be copied and re-bound, you can't store your state directly in the allocator.
What you can do is store a pointer (or a shared_ptr
) to your state, such that copies of your allocator shallow-copy the pointer, referring to the same underlying pool.
Note that you either need to write a default constructor for your allocator, and have it create a new backing pool, or you need to create an allocator instance with a specific backing pool and pass it to the container constructor.
So this:
list<TKey, MemPool<TKey>> Keys;
will default construct an allocator (which will be something like MemPool<list<TKey>::node>
), and that allocator instance will have to create its own backing state; while this:
list<TKey, MemPool<TKey>> MoreKeys(Keys);
will copy that original allocator instance via a select_on_container_copy_construction() const
method you must provide (so you can make both containers, with their separate allocator instances, share the same pool); and finally this:
map<TKey, MapType, less<TKey>, MemPool<MapType>> Map(MemPool<MapType>(my_pool));
will use the specified backing pool.