title: How to create a multiple typed object pool in C++
body: <p>Im trying to create a system capable of allocating any type, and grouping same types together in arrays.</p>
  
  <p>I want to be able to retrieve each array later using so I can iterate over each type.</p>
  
  <p>Something like this:</p>
  
  <pre><code>ObjectDatabase
  {
     template&lt;typename T&gt;
     T* Allocate();
  
     template&lt;typename T&gt;
     Array&lt;T&gt;&amp; GetObjects();
  }
  </code></pre>
  
  <p>My Array type is actually a pool so allocation/deletion is fast.</p>
  
  <p>I thought about mapping each Array in a std::map using an int representing the type id for each T, but then each type T would need to inherit from a base class, so it can be stored in the map, and thus leading to casting when I iterate over the array.</p>
  
  <p>I think this pattern has been done before but I'm not sure how.</p>
  
  <p>Can someone help?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>So I'm trying to basically create a structure like this:</p>
  
  <pre><code>struct ObjectDatabase
  {
      Array&lt;Entities&gt; mEntities;
      Array&lt;Transforms&gt; mTransforms; 
      Array&lt;Physics&gt; mPhysics; 
      Array&lt;Graphics&gt; mGraphics; 
  }
  </code></pre>
  
  <p>But I wanted to somehow create the set of arrays at compile time.. using templates?</p>
  
  <p>Then provide template functions to get access to each array, and to allocate from each array</p>