【发布时间】:2013-08-13 19:42:50
【问题描述】:
我有两个模板 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 这样的大量代码清单,我想减少重复(以避免错误)。
【问题讨论】:
-
A
不是类型 A,因此未定义 A ::typeA。你应该做的是 A : B 如果你想要你想要的功能,但这更有可能是一个 XY 问题? -
或者 'template struct A
: B::typeA>...' 但是这个接缝没用
标签: c++ templates inheritance