如何调用模板成员函数? [英] How to call a template member function?

查看:102
本文介绍了如何调用模板成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

从模板函数调用的模板类的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 keyword template. Otherwise the name is assumed to name a non-template.

正确的代码是:

b.template foo<T0>();

这篇关于如何调用模板成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆