以下是个简单的模板类测试代码,模板类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++》

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐