我有两个模板 A 和 B 都具有相同的模板类型。A 总是从 B 继承,但它总是为 B 选择与 A 本身使用的相同的模板。
这本身就很好,但这需要我编写两次模板类型。从子类继承时是否可以以某种方式对 A 中的类型进行 typedef 并引用泛型 typedef 名称?
下面是一些无法编译但应该清楚地了解我想要做什么的示例代码:
// #1
template <typename T>
struct A
{
typename T type;
// class details here
};
// #2
template <typename T>
struct B
{
// class details here
};
// #3
template <>
struct A<int>
: B<type> // Compiler complains here (type not defined)
//: B<A::type> // Compiler complains here (type not defined)
// I could write ": B<int>" instead, but this is repitition I want to avoid
{
// class specialization details here
};
我对替代解决方案持开放态度。这对我来说很重要的原因是我有一个像#3 这样的大量代码清单,我想减少重复(以避免错误)。