我正在寻找从通过 C::A 和 B::A 继承它的子类 D 调用基类 A 的方法。
template <class PType>
class A
{
public:
template <class ChildClass>
void Func( void )
{
std::cout << "Called A<PType>::Func<ChildClass>" << std::endl;
}
};
template <class PType>
class B : public A<PType>
{
};
template <class PType>
class C : public A<PType>
{
};
class D : public B<int>, public C<int>
{
public:
D()
{
static_cast<B<int>*>( this )->A<int>::Func<D>();
static_cast<C<int>*>( this )->A<int>::Func<D>();
}
};
这按预期工作,D 在初始化时使用子类的模板参数调用 B::A::Func 和 C::A::Func。但是,当 D 是模板类时,这似乎不起作用。
template <class PType>
class D2 : public B<PType>, public C<PType>
{
public:
D2()
{
//expected primary-expression before ‘>’ token
//expected primary-expression before ‘)’ token
static_cast<B<PType>*>( this )->A<PType>::Func< D2<PType> >();
static_cast<C<PType>*>( this )->A<PType>::Func< D2<PType> >();
}
};
问题似乎是 Func 的模板参数 D2,但除此之外无法解决。
最佳答案
当您使用的名称的值/类型/template
状态取决于 template
类型参数,您必须手动为编译器消除歧义。
这是为了使解析更容易,并且能够在将类型传递到 template
之前很久就完成.
你可能认为这显然是一个 template
函数调用,但是 <
和 >
可以是比较,模板函数可以是值或类型或其他任何东西。
默认情况下,此类依赖名称被假定为值。如果您希望他们将一个视为一种类型,请使用 typename
.如果您想将其中一个视为模板,请使用 template
作为关键字。
所以像这样:
static_cast<B<PType>*>( this )->template A<PType>::template Func< D2<PType> >();
现在,当您与完全实例化的 template
交互时这不是必需的。所以:
static_cast<B<int>*>( this )->A<int>::Func< D2<PType> >();
由一个完全实例化的 template
组成输入 B<int>
, 所以 A
的类别不再依赖于(本地未确定)template
争论。同样,->A<int>
是一个完全实例化的 template
类型,所以 ::Func
不再依赖于(本地未确定)template
争论。
关于c++:通过多级继承从模板子类调用基类的模板方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23439017/