I've got a model designed in C++ which is dependant on run-time polymorphism and virtual functions. The model works completely fine out of the box. Let me investigate transforming this to templates and compile-time polymorphism. The reason why are that:
- Just one class within the model is really dynamic at run-time, the relaxation are compile-time choices. The polymorphism is purely to create alterations in the model simpler and much more flexible.
- I must inline several small functions within the model, that is impossible with virtual functions.
This is a simplified hypothetical illustration of what my model appears like now:
class Particle class Electron : public Particle // Physics Models ///////////////////////////////// class PhysicsModel class NewtonPhysics : public PhysicsModel class QuantumPhysics : public PhysicsModel // SimulationModels //////////////////////////// class SimulationModel class HadronCollider : SimulationModel
But say if I wish to inline the Particle functions, like
move(), since it is the interior loop of a number of for-loops and may greatly enjoy the speed bonus. And also at compile-time, I understand what physics model and simulation model I am running, however the Particle is really a run-time decision.
So, what about:
template <typename TParticleType> class PhysicsModel template <typename TParticleType, typename TPhysicsModelType> class SimulationModel
Okay, to date so great. However allows say at compile time I have made the decision we are using running the Hadron Simulation, with Quantum Physics model, and we are reading through within an input file of contaminants. I wish to avoid something of the flavor:
int primary() situation ELECTRON: HadronSimulation<Electron, QuantumPhysics>.run() situation PROTON: HadronSimulation<Proton, QuantumPhysics>.run() // ...
... and prefer to place the "QuantumPhysics" type elsewhere therefore it is just in the same location. It is really an oversimplified example, and really I'd have about 4-5 parameters that are compile-time choices.
Essentially I am searching for a design that enables me to suit inside a run-time parameter into this kind of template framework. Can there be a great way to do that? Apologize if the appears as an idiotic question, but I am brand-new to templates in C++. Thanks ahead of time.
Static and dynamic polymorphism don't mix well, and where they connect to each other, some type of branching is needed to show run-time information right into a choice among statically set up variants. You could utilize an easy type generator to a minimum of lessen the repeating your physics model:
template< class Particle > class HadronSimulationGenerator int primary() situation ELECTRON: HadronSimulationGenerator<Electron>::type.run() situation PROTON: HadronSimulationGenerator<Proton>::type.run() // ...
Very little more attractive.
How have you fix your
ParticleType within the first example (without templates)? Was the constructor accountable for instantiating both? If so, then use a similar method to hide this in the user by doing all of your
switch within the constructor of
class SimulationModel situation QUANTUM: switch(currentParticleType) situation ELECTRON: SimulationModelImpl<Electron, QuantumPhysics>::run() break situation PROTON: SimulationModelImpl<Proton, QuantumPhysics>::run() break ... template <typename TParticleType, typename TPhysicsModelType> class SimulationModelImpl