以下代码在Visual C++ 2010下编译良好,但在Android NDK r8b下不在GCC 4.6下编译.
template<typename A>
struct foo
{
template<typename B>
B method()
{
return B();
}
};
template<typename A>
struct bar
{
bar()
{
f_.method<int>(); // error here
}
private:
foo<A> f_;
};
Run Code Online (Sandbox Code Playgroud)
GCC给出的错误是
error : expected primary-expression before 'int'
error : expected ';' before 'int'
Run Code Online (Sandbox Code Playgroud)
对于标记线.对于我的生活,我无法弄清楚什么是错的.
GCC是正确的,因为f_
类型foo<A>
取决于模板参数A
,您需要method
使用template
关键字限定调用:
f_.template method<int>(); // This will work
Run Code Online (Sandbox Code Playgroud)