I have memory allocator which allocates memory for an object and calls its constructor with any given arguments, see below.
// 0 args to constructor
template <class T>
inline T* AllocateObject() { return new (InternalAllocate(sizeof(T), alignof(T))) T(); }
// 1 args to constructor
template <class T, typename arg0>
inline T* AllocateObject(const arg0& a0) { return new (InternalAllocate(sizeof(T), alignof(T))) T(a0); }
template <class T, typename arg0>
inline T* AllocateObject(arg0& a0) { return new (InternalAllocate(sizeof(T), alignof(T))) T(a0); }
// 2 args to constructor
template <class T, typename arg0, typename arg1>
inline T* AllocateObject(arg0& a0, arg1& a1) { return new (InternalAllocate(sizeof(T), alignof(T))) T(a0,a1); }
template <class T, typename arg0, typename arg1>
inline T* AllocateObject(const arg0& a0, arg1& a1) { return new (InternalAllocate(sizeof(T), alignof(T))) T(a0,a1); }
template <class T, typename arg0, typename arg1>
inline T* AllocateObject(arg0& a0, const arg1& a1) { return new (InternalAllocate(sizeof(T), alignof(T))) T(a0,a1); }
template <class T, typename arg0, typename arg1>
inline T* AllocateObject(const arg0& a0, const arg1& a1) { return new (InternalAllocate(sizeof(T), alignof(T))) T(a0,a1); }
//.........
As you can see the number of calls grows with quite rapidly with the number of arguments. I have to alternate with ‘const’ and ‘non-const’ for each argument to make sure it plays fine with any argument I pass. (specifically, to be able to pass by references aswell as pass by value)
Is there any better way to perform the same task than to repeat this scheme? Basically I am looking at something like 8-10 arguments max and its just not very feasible I feel.
Thanks
Not a template solution, but a variable arguments
#definecould help you out of this problem.The exact format depends on your compiler, but in MSVC it would look like this: