title: How to deal with virtual template functions where the number of template arguments is known in C++?
body: <p>I have a pure virtual model interface</p>
  
  <pre><code>class ModelInterface {
  public:
    virtual ~ModelInterface() {}
    virtual double get_value(double scaleFactor) = 0;
  };
  </code></pre>
  
  <p>that requires all derived models to implement the <code>get_value()</code> function. A model implementation might look like</p>
  
  <pre><code>class Model : public ModelInterface {
  public:
    Model(double value) : _value(value) {}
    double get_value(double scaleFactor) {
      return _value * scaleFactor;
    }
  private:
    double _value;
  };
  </code></pre>
  
  <p>In the beginning of the main routine, a model is created and handled with an interface pointer to it</p>
  
  <pre><code>int main ()
  {
    ModelInterface *model = new Model(5.3);
  </code></pre>
  
  <p>At some other point, the model is used to calculate the desired value</p>
  
  <pre><code>  double scale_factor = 2.2;
    double value = model-&gt;get_value(scale_factor);
    // do something with the value
  
    return 0
  }
  </code></pre>
  
  <p>Now I need to replace the type <code>double</code> in function <code>double get_value(double scaleFactor)</code> with some other number type, in this case one that can handle automatic differentiation, let's call it <code>adouble</code>. However, I still need the double version as well. Sometimes I need derivatives, then the function should be called with the <code>adouble</code> type. If I don't need derivative information, I want to call the function with the <code>double</code> type to save resources. I want to be able to do something like</p>
  
  <pre><code>double scale_factor1;
  double value1 = model-&gt;get_value(scale_factor1);
  adouble scale_factor2;
  adouble value2 = model-&gt;get_value(scale_factor2);
  </code></pre>
  
  <p>My first intention was to add a template parameter to the line <code>virtual double get_value(double scaleFactor) = 0;</code>, such that <code>template &lt;class T&gt; virtual T get_value(T scaleFactor) = 0;</code>, however GCC told me virtual templates are not allowed. The next solution I found is to make the whole virtual class templated, and not only the function, like</p>
  
  <pre><code>template &lt;class T&gt;
  class ModelInterface {
  public:
    virtual ~ModelInterface() {}
    virtual T get_value(T scaleFactor) = 0;
  };
  </code></pre>
  
  <p>But this does not solve my problem, as I do only have one instantiation of the model and this instantiation would be bound to either double or adouble (or any other type) but I cannot call the function <code>get_value()</code> of the same object with different argument types.</p>
  
  <p>I also came across quite complex constructs with helpers and visitors and whatever that should be able to solve the general problem. However I'm in doubt whether or not there is any easy solution for my case, as the number of different types I would like to use is fixed (2) and also the types are known (<code>double</code> and <code>adouble</code>). Of course I would like to avoid to implement the function <code>get_value()</code> twice for each type because the algorithms used are exactly the same for both types and it would be a huge duplicate of code. </p>