本文介绍了从模板函数调用的模板类的模板成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 这无法编译:template<class X> struct A { template<int I> void f() {}};template<class T> void g(){ A<T> a; a.f<3>(); // Compilation fails here (Line 18)}int main(int argc, char *argv[]){ g<int>(); // Line 23}编译器(gcc)说: hhh.cpp:在函数'void g()': hhh.cpp:18:错误:预期在')'标记之前的主表达式hhh.cpp:18: error: expected primary-expression before ')' token hhh.cpp:在函数'void g()[with T = int]'中:hhh.cpp: In function 'void g() [with T = int]': hhh.cpp:23:从此处实例化hhh.cpp:23: instantiated from here hhh.cpp:18:错误:成员的无效使用(您忘了'&'吗?)hhh.cpp:18: error: invalid use of member (did you forget the '&' ?)有人可以解释为什么会这样吗?Can anyone explain why this is? Is there a way to get it to work?推荐答案请尝试以下代码:template<class T> void g(){ A<T> a; a.template f<3>(); // add `template` keyword here}根据C ++'03 Standard 14.2 / 4:According to C++'03 Standard 14.2/4:根据n2857 14.3草案,未来的C ++标准似乎仍然需要此关键字。 / 4。一些编译器具有特殊的模式,该模式允许无错误地编译原始代码(Comeau以所谓的放松模式进行编译)。Future C++ Standard seems to be still require this keyword according to draft n2857 14.3/4. Some compilers has special mode that allows to compile original code without errors (Comeau compiles it in so called relaxed mode). 这篇关于从模板函数调用的模板类的模板成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 10:57
查看更多