Made an Object pool in C++ I'm making a Minecraft clone and have made an Object Pool. Looking for some advice on how I have implemented it. 

Uses m_availableObjects as a O(1) way of searching for the next available object. 

Thank you in advance. 

constexpr int INVALID_OBJECT_ID = -1;

//Internal Use - Object Pool
template &lt;class Object&gt;
struct ObjectInPool : private NonCopyable
{
    ObjectInPool(int ID)
        : object(),
        inUse(false),
        ID(ID)
    {}
    ObjectInPool(ObjectInPool&amp;&amp; orig) noexcept
        : object(std::move(orig.object)),
        inUse(orig.inUse),
        ID(orig.ID)
    {
        orig.ID = INVALID_OBJECT_ID;
        orig.inUse = false;
    }
    ObjectInPool&amp; operator=(ObjectInPool&amp;&amp; orig) noexcept
    {
        object = std::move(orig.object);
        inUse = orig.inUse;
        ID = orig.ID;

        orig.ID = INVALID_OBJECT_ID;
        orig.inUse = false;
        return *this;
    }
    ~ObjectInPool() {}

    Object object;
    bool inUse;
    int ID;
};

//External Use - Object Pool
template &lt;class Object&gt;
class ObjectPool;
template &lt;class Object&gt;
struct ObjectFromPool : private NonCopyable
{
    ObjectFromPool(ObjectInPool&lt;Object&gt;* objectInPool, std::function&lt;void(int)&gt; func)
        : objectInPool(objectInPool),
        m_func(func)
    {
        if (objectInPool)
        {
            objectInPool-&gt;inUse = true;
        }
    }
    ~ObjectFromPool()
    {
        if (objectInPool)
        {
            objectInPool-&gt;object.reset();
            objectInPool-&gt;inUse = false;

            m_func(objectInPool-&gt;ID);
        }
    }
    ObjectFromPool(ObjectFromPool&amp;&amp; orig) noexcept
        : objectInPool(orig.objectInPool),
        m_func(orig.m_func)
    {
        orig.objectInPool = nullptr;
    }
    ObjectFromPool&amp; operator=(ObjectFromPool&amp;&amp; orig) noexcept
    {
        objectInPool = orig.objectInPool;
        m_func = orig.m_func;

        orig.objectInPool = nullptr;

        return *this;
    }

    Object* getObject() const 
    {
        return (objectInPool ? &amp;objectInPool-&gt;object : nullptr);
    }

private:
    ObjectInPool&lt;Object&gt;* objectInPool;
    std::function&lt;void(int)&gt; m_func;
};

using std::placeholders::_1;

//Object Pool
template &lt;class Object&gt;
class ObjectPool : private NonCopyable, private NonMovable
{
public:
    ObjectPool(int visibilityDistance, int chunkWidth, int chunkDepth)
        : m_objectPool()
    {
        //Added a little bit more than neccessary due to how the inifinite map generates
        int x = visibilityDistance / chunkWidth;
        x += x += 1;
        int z = visibilityDistance / chunkDepth;
        z += z += 1;

        m_objectPool.reserve(x * z);
        for (int i = 0; i &lt; x * z; ++i)
        {
            m_objectPool.emplace_back(i);
            m_availableObjects.push(i);
        }
    }

    ObjectFromPool&lt;Object&gt; getNextAvailableObject()
    {   
        if (!m_availableObjects.empty())
        {
            int ID = m_availableObjects.top();
            m_availableObjects.pop();
            assert(ID &lt; m_objectPool.size());
            return ObjectFromPool&lt;Object&gt;(&amp;m_objectPool[ID], std::bind(&amp;ObjectPool&lt;Object&gt;::releaseID, this, _1));
        }
        else
        {
            return ObjectFromPool&lt;Object&gt;(nullptr, std::bind(&amp;ObjectPool&lt;Object&gt;::releaseID, this, _1));
        }
    }

private:
    std::stack&lt;int&gt; m_availableObjects;
    std::vector&lt;ObjectInPool&lt;Object&gt;&gt; m_objectPool;

    void releaseID(int ID)
    {
        assert(ID != INVALID_OBJECT_ID);
        m_availableObjects.push(ID);
    }
};
This looks way too overengineered. Is there really a need for all the complexity? Use the KISS principle!

I'm assuming you have this pool because you want to avoid the cost of constructing and destructing Objects, and possibly to allocate a lot of instances of Object up front to avoid latency later on. If that's the case, I would instead write the pool like so:

template&lt;typename Object&gt;
class ObjectPool: private NonCopyable
{
    std::deque&lt;std::unique_ptr&lt;Object&gt;&gt; m_objects;

public:
    ObjectPool(size_t count = 0) {
        for(size_t i = 0; i &lt; count; ++i) {
            auto object = std::make_unique&lt;Object&gt;();
            enqueue(object);
        }
    }

    std::unique_ptr&lt;Object&gt; dequeue() {
        if (m_objects.empty()) {
            return std::make_unique&lt;Object&gt;();
        } else {
            auto object = std::move(m_objects.back());
            m_objects.pop_back();
            return object;
        }
    }

    void enqueue(std::unique_ptr&lt;Object&gt; &amp;object) {
        m_objects.emplace_back(std::move(object));
    }
};


First, the constructor of this object pool just takes the initial size of the pool as a single value, so it doesn't have to know the specifics of chunk sizes.

Second, the dequeue() function creates a new object for you automatically if the pool is empty. Maybe that is not desired, in which you could return an empty std::unique_pointer instead.

Last but not least, instead of leaving it up to the destructor of an ObjectFromPool to return the object back to the pool it came from, there is now a counterpart to dequeue(): enqueue(). It simply returns an object back to its pool. This avoids having to wrap objects in custom classes.

The only issue with this solution might be that you forget to return an object to its pool, but on the other hand it will still safely destruct itself once its lifetime ends.