template<typename X> struct A { template<int N> int foo() const { return N; } }; template<typename X> struct B { int bar(const A<X>& v) { return v.foo<13>(); } }; #include <iostream> using std::cout; using std::endl; int main() { A<double> a; B<double> b; cout << b.bar(a) << endl; return 0; }
函数内部 B::bar 编译器抱怨:
B::bar
和int到二进制运算符<
如果A不是模板,那么一切都可以编译。
改变 return v.foo<13>(); return v.template foo<13>(); foo 是一个依赖名称,您需要使用 .template 构造。
return v.foo<13>();
return v.template foo<13>();
foo
.template