title: Template metaprogramming to avoid cast
body: <p>Consider the following code:</p>
  
  <pre><code>#include &lt;utility&gt;
  #include &lt;tuple&gt;
  #include &lt;map&gt;
  using namespace std;
  enum class Type { D1, D2 };
  
  struct Base { 
   virtual ~Base() = default;
   virtual void doSth() = 0;
   virtual Type getType() = 0; 
  };
  
  struct D1 : Base { 
   Type getType() override { return Type::D1; } 
   void doSth() override { }
  };
  
  struct BaseExt : Base {
      virtual void doSth2() = 0;
  };
  
  struct D2 : BaseExt { 
   Type getType() override { return Type::D2; }
   void doSth() override { }
   void doSth2() override {}  
  };
  
  template &lt;typename... Deriveds&gt;
  struct Manager
  {
      Manager(Deriveds... ds) {
          _t = tuple&lt;Deriveds...&gt;(ds...); 
         // MAGIC NEEDED HERE TO POPULATE _mapOfTypes
      } 
  
      void go(Type /* t */) {
          // MAGIC NEEDED HERE
          //std::get&lt;_mapOfTypes[t]&gt;(_t)-&gt;doSth();
      }
  
      void go2()
      {
         // std::get&lt;_mapOfTypes[Type::D2]&gt;(_t)-&gt;doSthD2(); // THIS IS WHAT I WANT TO ACHIEVE
      }
  
      tuple&lt;Deriveds...&gt; _t;
      map&lt;Type, int&gt; _mapOfTypes;
  };
  
  int main()
  {
     D1 d1;
     D2 d2;
     Manager&lt;D1,D2&gt; m(d1,d2);
     m.go(Type::D1);
     m.go2();
  }
  </code></pre>
  
  <p>Link to this code:
  <strong><a href="https://wandbox.org/permlink/Jr5HJWimrL1wvZbs" rel="nofollow noreferrer">https://wandbox.org/permlink/Jr5HJWimrL1wvZbs</a></strong></p>
  
  <p>I know that I can store pointers to Base in the Manager, that's simple, oldschool. But in the case when I need to call a method from BaseExt (in go2 method), I would have to make a static_cast from Base* to BaseExt*. </p>
  
  <p>I am thinking if it is possible to do it differently with template metaprogramming. I would like to call the method doSth2 without any cast. Since I have a tuple of concrete types implementing Base or Base2 and I know in the compile time which object is in which place in the tuple, I would like to make a call to this object without using the Base or Base2 interface.</p>
  
  <p>I tried implementing this but I got stuck. I know that I can store parameter_pack in a tuple and later operate on this tuple using index_sequence. However I have a problem with populating the _mapOfTypes. Is it possible to do this? </p>