[英]c++: Calling a template method of a base class from a template child class though multilevel inheritance
I'm looking to call a method of a base class A from a child class D that inherits it through C::A and B::A. 我想从子类D调用基类A的方法,通过C :: A和B :: 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>();
}
};
This works as expected, D calls both B::A::Func and C::A::Func with a template argument of the child class when initialized. 这按预期工作,D在初始化时使用子类的模板参数调用B :: A :: Func和C :: A :: Func。 This doesn't seem to work when D is a template class, however. 但是,当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> >();
}
};
The problem seems to be the template argument D2 to Func, but can't figure it out beyond that. 问题似乎是模板参数D2到Func,但无法解决这个问题。
When you are working with a name whose value/type/ template
status is dependent on a template
type parameter, you must disambiguate which one it is for the compiler manually. 当您使用其值/类型/ template
状态取决于template
类型参数的名称时,必须手动消除编译器的名称。
This is to make the parsing far easier, and able to be done long before you pass a type into the template
. 这是为了使解析更容易,并且可以在将类型传递到template
之前很久就完成。
You may think that is obviously a template
function call, but the <
and >
could be comparisons, and the template functions could be values or types or whatever. 您可能认为这显然是template
函数调用,但<
和>
可以进行比较,模板函数可以是值或类型或其他。
By default, such dependent names are assumed to be values. 默认情况下,假定此类从属名称为值。 If you want them to treat one as a type, use typename
. 如果您希望将它们视为一种类型,请使用typename
。 If you want to treat one as a template, use template
as a keyword. 如果要将其中一个视为模板,请使用template
作为关键字。
So something like this: 所以像这样:
static_cast<B<PType>*>( this )->template A<PType>::template Func< D2<PType> >();
now, when you are interacting with a fully instantiated template
this is not required. 现在,当您与完全实例化的template
交互时,这不是必需的。 So: 所以:
static_cast<B<int>*>( this )->A<int>::Func< D2<PType> >();
consists of a fully instantiated template
type B<int>
, so A
's category is no longer dependent on a (locally undetermined) template
argument. 由完全实例化的template
类型B<int>
,因此A
的类别不再依赖于(本地未确定的) template
参数。 Similarly, ->A<int>
is a fully instantiated template
type, so ::Func
is no longer dependent on a (locally undetermined) template
argument. 类似地, ->A<int>
是完全实例化的template
类型,因此::Func
不再依赖于(本地未确定的) template
参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.