Cannot deduce template arguments

59 views Asked by At

I've the following code.

template <size_t StateSize, typename VariableType>
class DifferentialEquation {
public:

  using StateType = std::array<VariableType, StateSize>;
  using Var = VariableType;

  // ...
};

template <size_t StateSize, typename VariableType>
class RungeKutta4 {
  // ...
};

template<
  template<size_t, class> class IntegratorStrategy,
  class VariableType,
  size_t StateSize
>
class Integrator {
public:

  using DE = DifferentialEquation<StateSize, VariableType>;
  using Strategy = IntegratorStrategy<StateSize, VariableType>;

public:

  Integrator(DE de) : m_de(de) {}
  
  // ...

private:

  DE m_de;
  Strategy m_strategy;
};

Basically, the DifferentialEquation represents a differential equation (you don't say?) and DifferentialEquation::StateType represent a state vector, that's simple a std::array.

RungeKutta4 represents a method for integration that I want to use as integration strategy.

Integrator is the class that performs the integration. It must be aware of the DifferentialEquation::StateType since it uses it in order to perform calculus.

I can instantiate the classes with this code, that works:

DifferentialEquation<2, double> deq;
Integrator<RungeKutta4, double, 2> integrator(deq);

The problem is that I'd like to remove redundant template arguments in Integrator template class instantiation, because double and 2 must be the same arguments used as template arguments of DifferentialEquation. So I'd like to write:

DifferentialEquation<2, double> deq;
Integrator<RungeKutta4> integrator(deq);

The problem is that this code does not compile, and in Visual Studio I obtain following errors:

IntegrationTest.cpp(44): error C2955: 'Integrator': use of class template requires template argument list
  IntegrationTest.cpp(23): note: see declaration of 'Integrator'
IntegrationTest.cpp(44): error C3200: 'IntegratorStrategy<StateSize,VariableType>': invalid template argument for template parameter 'IntegratorStrategy', expected a class template
IntegrationTest.cpp(44): error C2955: 'Integrator': use of class template requires template argument list
  IntegrationTest.cpp(23): note: see declaration of 'Integrator'
IntegrationTest.cpp(44): error C2955: 'Integrator': use of class template requires template argument list
  IntegrationTest.cpp(23): note: see declaration of 'Integrator'
IntegrationTest.cpp(44): error C2639: trailing return type 'Integrator' of deduction guide should be a specialization of 'Integrator'
  ninja: build stopped: subcommand failed.

Row 44 is where I write Integrator<RungeKutta4> integrator(deq);.

I've tried to use some template deduction guide but I obtained no results, since I never used them, for example with:

template<
  template<size_t, class> class IntegratorStrategy,
  class VariableType,
  size_t StateSize
>
Integrator(DifferentialEquation<StateSize, VariableType>) -> Integrator<
  IntegratorStrategy<StateSize, VariableType>,
  VariableType, StateSize
>;

But obviously is wrong.

How can I fix my code? What should I do in order to instantiate the Integrator class without repeating the template arguments needed by DifferentialEquation?

0

There are 0 answers