title: C++ Template Design Question
body: <p>I have a model written in C++ which is based on run-time polymorphism and virtual functions.  The model works completely fine as is.  I'd like to investigate converting this to templates and compile-time polymorphism.  The reasons are that:</p>
  
  <ol>
  <li>Only one class in the model is truly dynamic at run-time, the rest are compile-time decisions.  The polymorphism is purely to make changes in the model easier and more flexible.</li>
  <li>I would like to inline several small functions in the model, which is impossible with virtual functions.</li>
  </ol>
  
  <p>Here is a simplified hypothetical example of what my model looks like now:</p>
  
  <pre><code>class Particle
  {
  public:
    // ...
    virtual void move();
  };
  
  class Electron : public Particle  { /*...*/ };
  
  
  // Physics Models /////////////////////////////////
  
  class PhysicsModel
  {
  public:
    // ...
    virtual void doStuffWithParticles();
  private:
    Particle* theParticle;
  };
  
  class NewtonPhysics : public PhysicsModel  { /*...*/ };
  class QuantumPhysics : public PhysicsModel  { /*...*/ };
  
  
  // SimulationModels ////////////////////////////
  
  class SimulationModel
  {
  public:
    virtual void runSimulation();
    // ...
  private:
    PhysicsModel* thePhysics;
    Particle* theParticle;
  };
  
  class HadronCollider : SimulationModel { /*...*/ };
  </code></pre>
  
  <p>But say if I want to inline the Particle functions, like <code>move()</code>, because it's the inner loop of a series of for-loops and could greatly benefit from the speed bonus.  And at compile-time, I know what physics model and simulation model I'm running, but the Particle is a run-time decision.  </p>
  
  <p>So, how about:</p>
  
  <pre><code>template &lt;typename TParticleType&gt;
  class PhysicsModel
  {
    // ...
    TParticleType theParticle;
  };
  
  template &lt;typename TParticleType,
            typename TPhysicsModelType&gt;
  class SimulationModel
  {
    TParticleType theParticle;
    TPhysicsModelType theModel;
  };
  </code></pre>
  
  <p>Okay, so far so good.  But now lets say at compile time I've decided we're using running the Hadron Simulation, with Quantum Physics model, and we're reading in an input file of particles.  I want to avoid something of this flavor:</p>
  
  <pre><code>int main()
  { 
    // ...
    switch( getUserInput()-&gt;currentParticleType )
    {
       case ELECTRON:  HadronSimulation&lt;Electron, QuantumPhysics&gt;.run();
       case PROTON:    HadronSimulation&lt;Proton, QuantumPhysics&gt;.run();
       // ...
    }
  }
  </code></pre>
  
  <p>... and would rather put the "QuantumPhysics" type somewhere else so it's just in the same place.  This is an oversimplified example, and actually I would have about 4-5 parameters which are compile-time decisions.  </p>
  
  <p>Basically I'm looking for a design that allows me to fit in a run-time parameter into this sort of template framework.  Is there a good way to do this?  Apologize if this seems like an idiotic question, but I'm completely new to templates in C++.  Thanks in advance.</p>