如何调用模板成员函数? [英] How to call a template member function?
问题描述
可能重复:
从模板函数调用的模板类的C ++模板成员函数
template<class T1>
class A {
template<class T0>
void foo() {}
};
template<class T0,class T1>
void bar( const A<T1>& b )
{
b.foo<T0>(); // This throws " expected primary-expression before ‘>’ token"
}
我可以将其更改为
b->A<T1>::template foo<T0>();
但我也可以将其更改为
b.A<T1>::template foo<T0>();
eh?
如何正确地调用原始代码意义上的模板成员函数?
How does one correctly call the template member function in the sense of the original code?
推荐答案
刚刚找到:
根据C ++'03 Standard 14.2 / 4:
According to C++'03 Standard 14.2/4:
当
。
或
- >
在后缀表达式中,或在限定id中的嵌套名称说明符之后,并且postfix-expression或qualified-id显式地取决于模板参数(14.6.2),成员模板名称必须以关键字template
为前缀。否则,假定名称命名为非模板。
When the name of a member template specialization appears after
.
or->
in a postfix-expression, or after nested-name-specifier in a qualified-id, and the postfix-expression or qualified-id explicitly depends on a template-parameter (14.6.2), the member template name must be prefixed by the keywordtemplate
. Otherwise the name is assumed to name a non-template.
正确的代码是:
b.template foo<T0>();
这篇关于如何调用模板成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!