以下是个简单的模板类测试代码,模板类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)》
模板成员函数调用的GCC与MSVC编译差异解析
本文探讨了在C++中,模板成员函数调用时遇到的GCC和MSVC编译器行为差异。当使用模板类的模板成员函数并指定模板参数时,在GCC下需要显式添加`template`关键字来消除语法歧义,而MSVC则不需要。问题源于C++标准中关于模板成员函数调用的规定,但不同编译器的实现细节导致了这一差异。解决方案是添加`template`关键字,确保编译正确。
427

被折叠的 条评论
为什么被折叠?



