提问人:Jepessen 提问时间:10/24/2023 更新时间:10/24/2023 访问量:50
无法推断模板参数 [重复]
Cannot deduce template arguments [duplicate]
问:
我有以下代码。
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;
};
基本上,表示一个微分方程(你不是说吗?)并表示一个状态向量,这很简单。DifferentialEquation
DifferentialEquation::StateType
std::array
RungeKutta4
表示我想用作集成策略的集成方法。
Integrator
是执行集成的类。它必须意识到这一点,因为它使用它来执行微积分。DifferentialEquation::StateType
我可以使用以下代码实例化类,该代码有效:
DifferentialEquation<2, double> deq;
Integrator<RungeKutta4, double, 2> integrator(deq);
问题是我想在模板类实例化中删除多余的模板参数,因为 和 必须与 的模板参数相同。所以我想写:Integrator
double
2
DifferentialEquation
DifferentialEquation<2, double> deq;
Integrator<RungeKutta4> integrator(deq);
问题是此代码无法编译,并且在 Visual Studio 中出现以下错误:
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.
第 44 行是我写的地方。Integrator<RungeKutta4> integrator(deq);
我尝试使用一些模板推导指南,但我没有得到任何结果,因为我从未使用过它们,例如:
template<
template<size_t, class> class IntegratorStrategy,
class VariableType,
size_t StateSize
>
Integrator(DifferentialEquation<StateSize, VariableType>) -> Integrator<
IntegratorStrategy<StateSize, VariableType>,
VariableType, StateSize
>;
但显然是错误的。
如何修复我的代码?我应该怎么做才能在不重复所需的模板参数的情况下实例化类?Integrator
DifferentialEquation
答: 暂无答案
下一个:如何防止函数模板参数推导
评论
Integrator integrator(RungeKutta4{}, deq);
make_integrator