title: How else to achieve "templated function pointers"?
body: <p>Is it possible to establish a set of templated function pointers, without the hassle of doing so manually?  Here's an example to illustrate what the heck I'm talking about.</p>
  
  <p>Let's say I have a frequently-called function "write" of which I have two implementations (write0 and write1) that I'd like to be able to switch between dynamically.  These write functions are templated on the argument type.  One way to do this is to just have a templated front-end function write() which internally uses an if statement.</p>
  
  <p>This turns out to be fast enough for my needs, but now I was left wondering if I can do the same using function pointers (just for fun).  The problem with this approach is that setting up the function pointers is a hassle.   Are there any other ways to essentially achieve the ideal of write() but without the conditional (direct static dispatch)?</p>
  
  <p>(Other "rules": I can't change the Msg classes to have write() methods, and I can't change the use site code to replace Msgs with adaptors for Msgs.)</p>
  
  <p>FWIW, I found <a href="http://www.devarticles.com/c/a/Cplusplus/Function-Pointers-part-1/3/" rel="nofollow noreferrer">this article</a> basically saying the same thing I'm saying here.</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  template&lt;typename T&gt; void write0(T msg) { cout &lt;&lt; "write0: " &lt;&lt; msg.name() &lt;&lt; endl; }
  template&lt;typename T&gt; void write1(T msg) { cout &lt;&lt; "write1: " &lt;&lt; msg.name() &lt;&lt; endl; }
  
  // This isn't so bad, since it's just a conditional (which the processor will
  // likely predict correctly most of the time).
  bool use_write0;
  template&lt;typename T&gt; void write(T msg) { if (use_write0) write0(msg); else write1(msg); }
  
  struct MsgA { const char *name() { return "MsgA"; } };
  struct MsgB { const char *name() { return "MsgB"; } };
  struct MsgC { const char *name() { return "MsgC"; } };
  struct MsgD { const char *name() { return "MsgD"; } };
  
  // This doesn't work: templates may not be virtual.
  #if 0
  struct Writer { template&lt;typename T&gt; virtual void write(T msg) = 0; };
  struct Writer0 { template&lt;typename T&gt; virtual void write(T msg) { cout &lt;&lt; "write0: " &lt;&lt; msg.name() &lt;&lt; endl; } };
  struct Writer1 { template&lt;typename T&gt; virtual void write(T msg) { cout &lt;&lt; "write0: " &lt;&lt; msg.name() &lt;&lt; endl; } };
  #endif
  
  int main(int argc, char **argv) {
    use_write0 = argc == 1;
  
    // I can do this:
    write(MsgA());
  
    // Can I achieve the following without the verbosity (manual setup, named
    // template instantiations, etc.)?
    void (*pwriteA)(MsgA) = use_write0 ? (void(*)(MsgA)) write0&lt;MsgA&gt; : (void(*)(MsgA)) write1&lt;MsgA&gt;;
    void (*pwriteB)(MsgB) = use_write0 ? (void(*)(MsgB)) write0&lt;MsgB&gt; : (void(*)(MsgB)) write1&lt;MsgB&gt;;
    void (*pwriteC)(MsgC) = use_write0 ? (void(*)(MsgC)) write0&lt;MsgC&gt; : (void(*)(MsgC)) write1&lt;MsgC&gt;;
    void (*pwriteD)(MsgD) = use_write0 ? (void(*)(MsgD)) write0&lt;MsgD&gt; : (void(*)(MsgD)) write1&lt;MsgD&gt;;
    pwriteA(MsgA());
    pwriteB(MsgB());
    pwriteC(MsgC());
    pwriteD(MsgD());
  
    return 0;
  }
  </code></pre>