title: Faking virtual variadic template function
body: <p>I am trying to modularise logging code from a large codebase because the live logging framework (Apache) makes our code tightly coupled to it and this makes it very hard to write unit tests. I am stuck by the fact I cannot have virtual templated functions. My current approach can be summarised by the following:</p>
  
  <pre><code>// Context.h
  struct Logger
  {
      template &lt;typename... Args&gt;
      void operator()(const char* aFormat, Args&amp;&amp;... aArgs)
      {
           // This function would ideally be virtual.
           // Is there a funky way to get this function to call
           // a derived class' implementation instead.
           std::cerr &lt;&lt; "I don't want to see this printed" &lt;&lt; std::endl;
      }
  };
  
  class Context
  {
  public:
      Context(const Logger&amp; aLogger)
      :   iLogger(aLogger)
      {
      }
  
      template &lt;typename... Args&gt;
      void DEBUG(const char* aFormat, Args&amp;&amp;... aArgs)
      {
          iLogger(aFormat, aArgs...);
      }
  
  private:
      const Logger&amp; iLogger;
  };
  
  
  // MyType.h
  #include "Context.h"
  
  class MyType
  {
  public:
      MyType(Context&amp; aCtx)
      :   iCtx(aCtx)
      {
          DEBUG("&gt; ctor");
          DEBUG("&lt; ctor. this=%p", this);
      }
  
  private:
      template &lt;typename... Args&gt;
      void DEBUG(const char* aFormat, Args&amp;&amp;... aArgs)
      {
          iCtx.DEBUG(aFormat, aArgs...);
      }
  
      Context&amp; iCtx;
  };
  
  
  // main.cpp
  #include "MyType.h"
  
  template &lt;typename... Args&gt;
  static void StdErrLog(const char* aFormat, Args&amp;&amp;... aArgs)
  {
      fprintf(stderr, aFormat, aArgs...);
  }
  
  struct StdErrLogger : public Logger
  {
  
      // This function never gets called because it's not virtual.
      template &lt;typename... Args&gt;
      void operator(const char* aFormat, Args&amp;&amp;... aArgs)
      {
          StdErrLog(aFormat, aArgs...);
      }
  }
  
  int main(...)
  {
      StdErrLogger logger; // For unit tests this could be 'EmptyLogger' for example.
      Context ctx(logger);
  
      MyType t(ctx);
  }
  </code></pre>
  
  <p>So close yet so far.
  Is there anything I can do to make this work without template-ising the <code>Context</code> class? The code base isn't templated at all and I'm very reluctant to go down this route as it'll be a lot of tedious work.</p>
  
  <p>If it can be done by keeping the templates down to function-level scope I'd be very happy to see solutions. Function pointers are acceptable too, but I'm not sure about the feasibility of getting the address of variadic templated functions.</p>
  
  <p>Thanks</p>