How to save template parameters in something like structure?

I have a simulation code where I want to perform some configuration at compile time: for example, I need to determine the dimension, data type and class containing low-level operations (compilation time for inline).

Sort of:

template <int DIMENSION, class DATATYPE, class OPERATIONS> class Simulation{ ... } template <int DIMENSION, class DATATYPE, class OPERATIONS> class SimulationNode{ ... } template <int DIMENSION, class DATATYPE, class OPERATIONS> class SimulationDataBuffer{ ... } 

Firstly, it is extremely unpleasant to write the entire set of parameters for each class. Secondly, even worse, it may be necessary to introduce an additional parameter, and I will have to change all the classes.

Is there something like a structure for template parameters?

Sort of

 struct { DIMENSION = 3; DATATYPE = int; OPERATIONS = SimpleOps; } CONFIG; template <class CONFIG> class Simulation{ ... } template <class CONFIG> class SimulationNode{ ... } template <class CONFIG> class SimulationDataBuffer{ ... } 
+6
source share
1 answer

Of course, create a class template that provides aliases for your types and a static member for your int .

 template <int DIMENSION, class DATATYPE, class OPERATIONS> struct Config { static constexpr int dimension = DIMENSION; using datatype = DATATYPE; using operations = OPERATIONS; }; 

Then you can use it as follows:

 template <class CONFIG> class Simulation{ void foo() { int a = CONFIG::dimension; } typename CONFIG::operations my_operations; } using my_config = Config<3, int, SimpleOps>; Simulation<my_config> my_simulation; 
+9
source

Source: https://habr.com/ru/post/987191/


All Articles