Super simple templated memory pool in C++
This currently will not work.

As every-time you create an object derived from MemoryPool you re-initialize the myUnusedMemory structure thus potentially messing up your management.

struct Game : public ag::util::MemoryPool&lt;Game, 2&gt;  {};


int main()
{
    Game* p1  = new Game;
    Game* p2  = new Game;
    delete p1;
    delete p2;

    // myUnusedMemory now set like this:
    // myUnusedMemory[0] = &amp;myMemory[1];
    // myUnusedMemory[1] = &amp;myMemory[0];

    // Now when we create the next one it resets the array:

    Game* p3  = new Game;
    // myUnusedMemory now set like this: (because the constructor reset the array)
    // myUnusedMemory[0] = &amp;myMemory[0];
    // myUnusedMemory[1] = &amp;myMemory[1];

    Game* p4  = new Game;  // Now p3 and p4 point at the same memory location
}


This line:

static T myMemory[capacity];


Is also not good as you are actually initializing the objects. Thus, when you are calling new your new operator is returning a pointer to an already initialized object . The constructor will then be re-called. Thus overriding the original content without the destructor being called (thus leaking any memory that should have been deleted).

I suppose you could call the destructor inside the new operator but that seems wasteful (and may be expensive) and sort of counter intuitive. So you want to use uninitialized memory for your pool.
You could do this:

static char myMemory[capacity * sizeof(T)];


But unfortunately this is not guaranteed to be correctly aligned.
If you dynamically allocate the memory then it will be fine. Use std::vector and it will be dynamically allocated and safe.

static std::vector&lt;char&gt;  myMemory(capacity * sizeof(T));