title: Can template polymorphism be used in place of OO polymorphism?
body: <p>I am trying to get my head around applying template programming (and at some future point, template metaprogramming) to real-world scenarios. One problem I am finding is that C++ Templates and Polymorphism don't always play together the way I want.</p>
  
  <p>My question is if the way I'm trying to apply template programming is improper (and I should use plain old OOP) or if I'm still stuck in the OOP mindset.</p>
  
  <p>In this particular case, I am trying to solve a problem using the strategy-pattern. I keep running into the problem where I end up wanting something to behave polymorphically which templates don't seem to support.</p>
  
  <p>OOP Code using composition:</p>
  
  <pre><code>class Interpolator {
     public:
       Interpolator(ICacheStrategy* const c, IDataSource* const d);
       Value GetValue(const double);
  }
  
  void main(...) {
      Interpolator* i;
      if(param==1)
         i = new Interpolator(new InMemoryStrategy(...), new TextFileDataSource(...));
      else if(param==2)
         i = new Interpolator(new InMemoryStrategy(...), new OdbcDataSource(...));
      else if(param==3)
         i = new Interpolator(new NoCachingStrategy(...), new RestDataSource(...));
  
      while(run) {
         double input = WaitForRequest();
         SendRequest( i-&gt;GetValue(input));
      }
  }
  </code></pre>
  
  <p>Potential Template Version:</p>
  
  <pre><code>class Interpolator&lt;class TCacheStrategy, class TDataSource&gt; {
     public:
       Interpolator();
       Value GetValue(const double);               //may not be the best way but
       void ConfigCache(const&amp; ConfigObject);      //just to illustrate Cache/DS         
       void ConfigDataSource(const&amp; ConfigObject); //need to configured
  
  }
  
  //Possible way of doing main?
  void main(...) {
      if(param==1)
         DoIt(Interpolator&lt;InMemoryStrategy,TextFileDataSource&gt;(),c,d);
      else if(param==2)
         DoIt(Interpolator&lt;InMemoryStrategy,OdbcDataSource&gt;(),c,d)
      else if(param==3)
         DoIt(Interpolator&lt;NoCachingStrategy,RestDataSource&gt;(),c,d)
  
  }
  
  template&lt;class T&gt;
  void DoIt(const T&amp;  t, ConfigObject c, ConfigObject d) {
     t.ConfigCache(c);
     t.ConfigDataSource(c);
     while(run) {
        double input = WaitForRequest();
        SendRequest( t.GetValue(input));
     }
  }
  </code></pre>
  
  <p>When I try to convert the OOP implementation to a template-based implementation, the Interpolator code can be translated without a lot of pain. Basically, replace the "interfaces" with Template type parameters, and add a mechanism to either pass in an instance of Strategy/DataSource or configuration parameters.</p>
  
  <p>But when I get down to the "main", it's not clear to me how that should be written to take advantage of templates in the style of template meta programming. I often want to use polymorphism, but it doesn't seem to play well with templates (at times, it feels like I need Java's type-erasure generics... ugh).</p>
  
  <p>When I often find I want to do is have something like <code>TemplateType&lt;?,?&gt; x = new TemplateType&lt;X,Y&gt;()</code> where x doesn't care what X,Y is.</p>
  
  <p>In fact, this is often my problem when using templates.</p>
  
  <ol>
  <li>Do I need to apply one more level of
  templates?  </li>
  <li>Am I trying to use my shiny new power template wrench to
  install a OOP nail into a PCI slot?  </li>
  <li>Or am I just thinking of this all
  wrong when it comes to template
  programming?</li>
  </ol>
  
  <p>[Edit] A few folks have pointed out this is not actually template metaprogramming so I've reworded the question slightly. Perhaps that's part of the problem--I have yet grok what TMP really is.</p>