Generic allocator class without variadic templates?
- by rainer
I am trying to write a generic allocator class that does not really release an object's memory when it is free()'d but holds it in a queue and returns a previously allocated object if a new one is requested. Now, what I can't wrap my head around is how to pass arguments to the object's constructor when using my allocator (at least without resorting to variadic templates, that is). The alloc() function i came up with looks like this:
template <typename... T>
inline T *alloc(const &T... args) {
T *p;
if (_free.empty()) {
p = new T(args...);
} else {
p = _free.front();
_free.pop();
// to call the ctor of T, we need to first call its DTor
p->~T();
p = new( p ) T(args...);
}
return p;
}
Still, I need the code to be compatible with today's C++ (and older versions of GCC that do not support variadic templates). Is there any other way to go about passing an arbitrary amount of arguments to the objects constructor?