从模板函数调用模板类的模板成员函数
这不会编译:
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:在函数“void g() [with T = int]”中:
hhh.cpp:23:从这里实例化
hhh.cpp:18: 错误:成员的无效使用(您是否忘记了“&”?)
任何人都可以解释这是为什么吗?有办法让它发挥作用吗?
This doesn't compile:
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
}
The compiler (gcc) says:
hhh.cpp: In function 'void g()':
hhh.cpp:18: error: expected primary-expression before ')' token
hhh.cpp: In function 'void g() [with T = int]':
hhh.cpp:23: instantiated from here
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以下代码:
根据 C++'03 标准 14.2/4:
根据草案 n2857 14.3/4,未来的 C++ 标准似乎仍然需要此关键字。一些编译器具有特殊模式,允许编译原始代码而不会出现错误(Comeau 以所谓的“宽松模式”进行编译)。
Try the following code:
According to C++'03 Standard 14.2/4:
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).