c++模板:调用模板成员函数需不需要加template关键字?

以下是个简单的模板类测试代码,模板类A中定义了模板函数hello,在模板函数test中调用A::hello
template_test.cpp

template <class T>
struct A{
	template<class I>
	void hello(){}
	template<class I>
	void hello2(I i){}
};
template <class T>
void test(A<T> & a)
{
	a.hello<int>();
	a.hello2(100);
}

int main()
{
	A<int> a;
	test(a);
}

在Visual Studio 2015下可以正常编译通过,但在gcc 5.2.0下就不行,报错如下:

$ g++ template_test.cpp
template_test.cpp: In function 'void test(A<T>&)':
template_test.cpp:11:10: error: expected primary-expression before 'int'
  a.hello<int>();
          ^
template_test.cpp:11:10: error: expected ';' before 'int'


解决这个问题的办法很简单修改A::hello函数的调用方式,增加template关键字申明hello为模板函数

template <class T>
void test(A<T> & a)
{
	a.template hello<int>();
}

为什么会这样?
添加 template 关键字的目的是消除语法歧义,告诉编译器hello是个模板成员。否则编译器会将后面的<视为比较运算符。

同样是模板成员函数,hello2因为调用时不需要指定显式模板参数,不加template关键字也可以被编译正确识别。

如下是C++标准中的说明(《14.2 Names of template specializations》):

当类的模板成员名称出现在 .-> 在后缀表达式中,或在限定标识符中的嵌套名称说明符之后,并且后缀表达式或限定标识符显式依赖于模板参数(14.6.2),成员模板名称必须是以template关键字为前缀。否则,该名称被假定为非模板名。

以下是从C++标准文档(《Working Draft, Standard for Programming Language C++》 )摘录的14.2原文
在这里插入图片描述

那么为什么MSVC就不需要tempate关键字也能正常编译呢?我只能说在这个部分微软编译器更聪明些。

参考资料

《When do we need a .template construct》
《Confusing Template error (3)》

《模板和大于/小于符号的歧义》

《Working Draft, Standard for Programming Language C++》

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值