title: Building a variadic template class with a set of function overloads based on the template arguments?
body: <p><strong>Motivation</strong>: I need to build a base class for a C++17 library that will have overloads for a virtual function based on types that the user identifies at compile time.  Basically, when a particular overload of the function is called on the base class, I want to make sure the correct version is called in the derived class.  My initial instinct was to build a virtual template function, but of course C++ cannot allow this because the compiler will not know what versions of it to put in the virtual function table.  Since I will know all of the types at compile time, however, is it possible to make the base class itself a variadic template and use the template arguments to build the set of overloaded version of the virtual function that would be needed?</p>
  
  <p>Fold expressions are right out because they can't be used to declare functions.  Recursive templates seem promising, but I worry that having a long chain of inheritance for the base class will create a performance hit.  Here's the working code that I have:</p>
  
  <pre><code>template &lt;typename... Ts&gt; class MyClass;
  
  template &lt;typename T, typename... Ts&gt;
  struct MyClass&lt;T, Ts...&gt; : public MyClass&lt;Ts...&gt; {
    using MyClass&lt;Ts...&gt;::MyFunction;
    virtual bool MyFunction(T in) { return true; }
  };
  
  template &lt;&gt;
  struct MyClass&lt;&gt; {
    virtual bool MyFunction(...) { return false; }
  };
  </code></pre>
  
  <p>Should this technique be sufficient?  Or does anyone have other ideas on how I might accomplish this goal?</p>
  
  <p>My other ideas include:</p>
  
  <ul>
  <li><p>Cap the number of template arguments that can be handled and enable-if each overload based on whether the argument list is long enough to include it.  Downside: this technique would be an arbitrary limit on the number of types.</p></li>
  <li><p>Use a variadic macro to build the class.  Downside: This technique would be confusing and inelegant.</p></li>
  <li><p>Create a function to assign a type to an ID number in the base class, pass it to the derived class as a <code>void *</code> with its id, and translate it back at that point to make the appropriate overloaded call.  Downside: this technique would be tricky to keep type safe and to minimize the amount of work the end-user needs to do.</p></li>
  </ul>
  
  <p>Right now I'm leaning toward implementing the first of these alternatives and doing some performance testing to compare it to my working version, but would love if there is something cleaner that I'm missing.</p>