我有以下模板,其范围仅用于创建我在其他地方需要的一些
using
声明:
template<
typename VariableType,
size_t StateSize,
template<size_t, typename> typename IntegrationStrategy
>
class Configuration {
public:
using State = std::array<VariableType, StateSize>;
using Strategy = IntegrationStrategy<StateSize, VariableType>;
using Var = VariableType;
};
它似乎有效,因为这段代码可以编译:
template <size_t StateSize, typename VariableType>
class RungeKutta4 {
};
// ...
using Conf = Configuration<double, 5, RungeKutta4>;
static_assert(std::is_same_v<double, Conf::Var>);
static_assert(std::is_same_v<std::array<double, 5>, Conf::State>);
static_assert(std::is_same_v<RungeKutta4<5, double>, Conf::Strategy>);
现在我想在另一个类中使用
Configuration
作为模板参数,因为我需要使用它定义的 Var
、State
和 Strategy
:
template <Configuration Conf>
class DifferentialEquation {
public:
void dummy(const Conf::State& state) {}
};
不幸的是这段代码无法编译。我也尝试过类似模板模板参数的东西:
template <template<
typename,
size_t,
template<size_t, typename> typename> typename Conf>
class DifferentialEquation {
public:
void dummy(const Conf::State& state) {}
};
但是不起作用。
我应该在
DifferentialEquation
类中使用什么作为模板参数,以便在我的 using
专业化中使用 Configuration
声明,并在该类内的方法和参数中使用 Conf::State
?
编辑:
这是我尝试编译最后一个
DifferentialEquation
时的错误:
\DifferentialEquation.hpp(22): error C3205: argument list for template template parameter 'Conf' is missing
/DifferentialEquation.hpp(40): note: see reference to class template instantiation 'DifferentialEquation<Conf>' being compiled
\DifferentialEquation.hpp(22): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
\DifferentialEquation.hpp(22): error C2144: syntax error: 'unknown-type' should be preceded by ')'
\DifferentialEquation.hpp(22): error C2144: syntax error: 'unknown-type' should be preceded by ';'
\DifferentialEquation.hpp(22): error C2039: 'State': is not a member of '`global namespace''
\DifferentialEquation.hpp(22): error C2143: syntax error: missing ';' before '&'
\DifferentialEquation.hpp(22): error C2059: syntax error: ')'
\DifferentialEquation.hpp(22): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
ninja: build stopped: subcommand failed.
编辑:
我正在尝试按照建议将模板参数添加到
Conf
,但我无法添加第三个参数,即模板参数。
如果我将名称添加到模板参数中,它会编译:
template <template<
typename X,
size_t Y,
template<size_t, typename> typename Z> typename Conf>
class DifferentialEquation {
public:
// void dummy(const Conf::State& state) {}
};
但是当我尝试使用它时无法编译:
template <template<
typename X,
size_t Y,
template<size_t, typename> typename Z> typename Conf>
class DifferentialEquation {
public:
void dummy(const Conf::State<X, Y, Z>& state) {} // error
void dummy(const Conf<X, Y, Z<Y, X>>::State& state) {} // error
};
我应该使用什么正确的组合?
我应该在 DifferentialEquation 类中使用什么作为模板参数,以便在我的 Configuration 专业化中使用 using 声明,并在该类中的方法和参数中使用 Conf::State ?
执行此操作的常用方法(语法方面)是将
Conf
所需的模板参数添加到最顶部的模板参数列表,如下所示:
template <template<
typename,
size_t,
template<size_t, typename> typename> typename Conf,
//note these next three parameter are added so that we can used them as arguments of Conf
typename T,
std::size_t t,
template<size_t, typename>typename temp>
class DifferentialEquation {
public:
void dummy(const typename Conf<T, t, temp>::State& state) {} //works now
};
此外,在需要的地方(如果有的话)请在上面的代码中添加
typename
或 template
关键字。