title: Is it bad practice to mix templates and runtime polymorphism and are there any alternatives?
body: <p>I am trying to link an enum value to some class object so that I can manipulate the object by only using the enum value. I initially wanted to use a templated approach for learning purposes and the fact it would allow some compile-time computations.</p>
  <p>After the initial implementation I've found myself in a situation where it seems like I need some sort of runtime code as well and I'm backtracking on the entire approach thinking that templating it is useless as I could achieve exactly the same thing with less obscure code just using polymorphism alone albeit introducing some call overhead.</p>
  <pre><code>enum Fruit { 
      All, 
      Orange, 
      Banana, 
      Mango
  };
  static const int FRUIT_COUNT = 4;
  
  class BasketBase
  {
  public:
  
      int m_otherFruitsBitfield; 
  };
  
  template &lt;Fruit T = Fruit::All&gt;
  class Basket {};
  
  template&lt;&gt;
  class Basket&lt;Orange&gt; : public BasketBase
  {
  public:
      void Open() { printf(&quot;Basket (Oranges) - Open\n&quot;); }
  
      static Basket&lt;Orange&gt; m_basket;
  };
  
  template&lt;&gt;
  class Basket&lt;Banana&gt; : public BasketBase
  {
  public:
      void Open() { printf(&quot;Basket (Bananas) - Open\n&quot;); }
      void Throw() { printf(&quot;Basket (Bananas) - Throwing&quot;); }
      void Drop() { printf(&quot;Basket (Bananas) - Dropping&quot;); }
  
      int m_numBananas;
      static Basket&lt;Banana&gt; m_basket;
  };
  
  Basket&lt;Orange&gt; Basket&lt;Orange&gt;::m_basket;
  Basket&lt;Banana&gt; Basket&lt;Banana&gt;::m_basket;
  
  template&lt;Fruit T&gt;
  Basket&lt;T&gt;&amp; GetBasket()
  {
      return Basket&lt;T&gt;::m_basket;
  }
  
  template&lt;Fruit T&gt;
  void OpenBasket()
  {
      GetBasket&lt;T&gt;().Open();
  }
  
  
  int main() {
  
    OpenBasket&lt;Orange&gt;();
    OpenBasket&lt;Banana&gt;();
  
    GetBasket&lt;Orange&gt;().m_otherFruitsBitfield = (1 &lt;&lt; Banana) | (1 &lt;&lt; Mango);
    GetBasket&lt;Banana&gt;().m_otherFruitsBitfield = (1 &lt;&lt; Mango);
  
    //opening the basket should now open every other basket in the bitfield
    int orangeBitfield = GetBasket&lt;Orange&gt;().m_otherFruitsBitfield;
  
      for (int i = 0; i &lt; FRUIT_COUNT; ++i)
      {
          Fruit fruit = static_cast&lt;Fruit&gt;(i);
          if (orangeBitfield &amp; fruit)
          {
              //OpenBasket(fruit); //runtime
              //OpenBasket&lt;fruit&gt;() //nope
          }
      }
  }
  </code></pre>
  <p>The code above is a simplified version, but serves the same purpose. The aim of this was to have some sort of <em>shared</em> object (<code>Basket</code>) that would be tied to an individual enum value (<code>Fruit</code>), and client code could call into the object by passing the enum. Each <code>Basket</code> does share some common functionality, like <code>Open</code>, but outside of that is completely independent and may contain specific data to the fruit being held.</p>
  <p>Using the above approach I was able to compute all of the bitfields/object initializing at compile-time since I knew the data upfront, however <code>otherFruitsBitfield</code> can now change at runtime which means I can't use the templated versions of <code>GetBasket&lt;&gt;()</code> and would need a runtime one. Same applies to another variable which stores the currently viewed basket.</p>
  <p>To fix this, I can think to add some virtual calls to the base class and override them, but then I question what's the point in using the template? I can achieve the same thing by storing a list of <code>BasketBase*</code> and just doing everything at runtime but I would like to get out of my comfort zone regarding implementation.</p>
  <p>Am I missing something here or is this not a valid use of a template? Could anyone provide some more information on these issues and any potential solutions?</p>