Template Constructor

I'm working on a virtual machine that uses a list of memory pools to allocate the created objects on. The current implementation allocates a fixed size of memory up front, then tries to allocate objects in this fixed block. The problem is the constructors of a different set of types will have to be called, each taking another number of parameters. A code to illustrate what I'm trying to accomplish:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class MemoryPool
{
    public:
        MemoryPool(size_t size); //Constructor
        ~MemoryPool(); //Destructor
        void* Allocate(size_t size); //Allocates a fixed-size block from the pool
        
        template <typename T>
        T* Allocate(...) //Allocates one instance of T from the pool
        {
            void* block = this->Allocate(sizeof(T));
            new(block) T(/*Forwarding the parameters*/);
            return static_cast<T*>(block);
        }
};


The question arises in the Allocate function, how do I pass the parameters given to the constructor called by placement new?
Last edited on
I would guess that this is what you're asking:
1
2
3
4
5
6
7
        template <typename T, typename... args_type>
        T* Allocate(args_type... args) //Allocates one instance of T from the pool
        {
            void* block = this->Allocate(sizeof(T));
            new(block) T(args...);
            return static_cast<T*>(block);
        }
Thank you for your help, it works perfectly. I forgot all about the new variadric templates, I don't use them that often, but this is clearly the solution.
Topic archived. No new replies allowed.