【发布时间】:2021-08-24 14:31:45
【问题描述】:
背景
我有一个类,我已经模板化了通过继承获得编译时多态性作为运行时多态性的替代方案。
我对这个类中的一些方法有两种不同的特化。我们称他们为T_a 和T_b。
问题
我想根据模板参数用不同的值初始化这个类的一个常量但非静态的对象数组。
template<class T>
class A {
const anotherObject& aO;
const ConfigPar configPars[];
}
无效尝试
专业化
首先,我认为我可以专门化成员:
template<class T>
class A {
const ConfigPar configPars[];
A(T t);
}
template<>
const ConfigPar A<T_a>::configPars[] {
{anotherObject.foo1, "bar1"},
{anotherObject.foo2, "bar2"},
}
template<>
const ConfigPar A<T_b>::configPars[] {
{anotherObject.foo1, "bar1"},
{anotherObject.foo2, "bar2"},
{anotherObject.foo3, "bar3"},
}
但是,这不起作用,因为显然您只能专门化静态类成员。但是,configPars 数组中对象的值依赖于另一个实例成员,所以我不能将configPars 设为静态。
构造函数重载
接下来,我想重载A的构造函数,只是在构造函数中以不同的方式初始化configPars:
template<class T>
class A {
const ConfigPar configPars[];
A(T_a t) : configPars{
{anotherObject.foo1, "bar1"},
{anotherObject.foo2, "bar2"},
} {}
A(T_b t) : configPars{
{anotherObject.foo1, "bar1"},
{anotherObject.foo2, "bar2"},
{anotherObject.foo3, "bar3"},
} {}
}
但是,在此我的编译器抱怨error: too many initializers for 'const ConfigPar [0]'。显然我不能在声明中忽略数组的大小?
问题在于两个特化(或重载的构造函数,在这种情况下)之间的数组大小不同。
我该如何处理?
【问题讨论】:
-
const ConfigPar configPars[];不合法。所有类成员都必须具有定义的大小,这意味着数组大小必须在类定义中已知。您可以使用const std::vector<ConfigPar>代替它。 -
@NathanOliver 我明白了。是否无法根据模板参数定义大小,例如在专业?这是嵌入式的(在微控制器上运行),所以很遗憾我无法访问标准库的
std::容器。