【发布时间】:2018-03-14 11:28:09
【问题描述】:
我有很多 C++ 类使用相同的模板参数列表
template<typename T, typename Index, typename Bool, typename Data, Index n_x, Index n_u, Index n_c, Index n_w>
class A {
...
};
template<typename T, typename Index, typename Bool, typename Data, Index n_x, Index n_u, Index n_c, Index n_w>
class B {
...
};
template<typename T, typename Index, typename Bool, typename Data, Index n_x, Index n_u, Index n_c, Index n_w>
class C {
...
};
你明白了。然后我像
一样实例化它们A<T, Index, Bool, Data, n_x, n_u, n_c, n_w> a;
B<T, Index, Bool, Data, n_x, n_u, n_c, n_w> b;
C<T, Index, Bool, Data, n_x, n_u, n_c, n_w> c;
有没有办法以某种方式为这组模板参数创建一个别名,这样我就不必不断重新输入参数列表?
我有这样的想法......
using Params = T, Index, Bool, Data, n_x, n_u, n_c, n_w;
A<Params> a;
B<Params> b;
C<Params> c;
我意识到我可以创建一个单独的类来定义类型并使用它。但我想知道是否有一种方法可以在不定义新类的情况下做到这一点。
编辑
我不想使用宏。
我也不想使用默认值,因为这需要确保默认值在一堆文件中是统一的。我意识到我可以定义一个新的默认标头并将其包含在所有文件中,但这似乎是糟糕的编程。
【问题讨论】:
-
使用默认参数
-
创建宏...
-
我不想使用默认值,因为那样我就必须在每个文件中定义默认值。
-
如果您需要类似的东西,那么我认为设计或该设计的实现存在缺陷。
-
@bremen_matt 你想要什么是不可能的。唯一的方法是将其专门用于类型列表。