c++ - Is there a better way to do this than writing a wrapper allocator that stores a reference to a stateful allocator object? -
for example:
struct foo { mypoolalloc<char> pool; std::vector<int , mypoolalloc<char>> vec_int; // wrapper allocator replace mypoolalloc<> here. std::vector<std::function<void()> , mypoolalloc<char>> vec_funcs; // wrapper allocator replace mypoolalloc<> here. foo() : vec_int(pool) , vec_funcs(pool) {} // want store lambdas captured variables using custom allocator well: template<typename func> void emplace_back(const func& func) { vec_funcs.emplace_back(std::allocator_arg , pool , func); } };
in above code, want allocations (besides pool itself) pull same pool
object. best way writing wrapper allocator stores reference actual stateful allocator object? , pass following constructors (example):
: vec_int ((mywrapperalloc<char>(pool)));
is there cleaner way writing whole wrapper class mypoolalloc<>
?
the standard "allocator" concept have been better named "allocatorreference." each object either refers global instance (stateless) or external object (stateful).
either way, allocator instance within allocator-aware container not own memory pool itself. it's proxy. note allocator objects copied rebound , returned value. don't want vector::get_allocator
copy whole memory pool.
so, yes, need 2 classes.
the "wrapper," "proxy," or "reference" satisfies standard allocator requirements , takes template parameter allocated type.
the memory pool has nothing allocator interface know how perform allocations.
Comments
Post a Comment