title: Inheriting template methods
body: <p>I have a class similar to the following:</p>
  
  <pre><code>class SomeClass
  {
      public:
          template&lt;typename... Args&gt;
          void doSomething(Args &amp;&amp; ... args);
  
          //... other methods etc.
  };
  </code></pre>
  
  <p>However, what I really want is to have two different kinds of <code>SomeClass</code>.  Ideally, I would be able to derive from a common interface to make <code>SomeOtherClass</code>, but I need to have a different implementation of <code>doSomething</code> and templated methods cannot be virtual.  I could make a templated class, but then every method that takes one of these (and there are many) would themselves have to be templates etc.</p>
  
  <p>The best I've been able to come up with is to implement both types of <code>doSomething</code> in the base class and have that method call a virtual method to determine which to use at runtime.</p>
  
  <p>Is there a better solution?</p>
  
  <h2>Further explanation</h2>
  
  <p>I have many methods that look similar to this:</p>
  
  <pre><code>void foo(SomeClass * obj);
  </code></pre>
  
  <p><code>foo</code> calls <code>obj-&gt;doSomething</code> and this all works fine, however I've since realized that I need a different kind of <code>SomeClass</code> but want it to work with these same methods, for example:</p>
  
  <pre><code>class SomeClass
  {
      public:
          // This won't work
          template&lt;typename... Args&gt;
          virtual void doSomething(Args &amp;&amp; ... args) = 0;
  
          // ... other common methods
  };
  
  class TheFirstType
  {
      public:
          template&lt;typename... Args&gt;
          void doSomething(Args &amp;&amp; ... args);
  
          // ... other specific methods
  };
  
  class TheSecondType
  {
      public:
          template&lt;typename... Args&gt;
          void doSomething(Args &amp;&amp; ... args);
  
          // ... other specific methods
  };
  </code></pre>
  
  <p>The above would be ideal, if it were legal, but virtual methods cannot be templated.  I've thus far gotten around this limitation by only having <code>doSomething</code> defined in the base class, but with both the implementation for <code>TheFirstType</code> and <code>TheSecondType</code> separated by an if statement that checks on what type the instance actually is:</p>
  
  <pre><code>template&lt;typename... Args&gt;
  void SomeClass::doSomething(Args &amp;&amp; ... args)
  {
      if (this-&gt;type() == FIRST_TYPE) {
          // ... the implementation that should rightfully be part of TheFirstType
      } else if (this-&gt;type() == SECOND_TYPE) {
          // ... the implementation that should be part of TheSecondType
      }
  }
  </code></pre>
  
  <p>This seems messy, however, so I was wondering if there is a better way.</p>