use*_*688 3 c++ templates compiler-errors
我正在尝试编译以下代码:
struct A {
template<int N> static void a() {}
};
template<> void A::a<5>() {}
template<class T>
struct B {
static void b() {
T::a<5>();
}
};
void test() {
A::a<5>();
B<A>::b();
}
Run Code Online (Sandbox Code Playgroud)
和编译器解释<在T::a<5>作为操作者<,从而导致错误:
invalid operands of types ‘<unresolved overloaded function type>’ and ‘int’ to binary ‘operator<’
Run Code Online (Sandbox Code Playgroud)
有没有办法在T::a<5>没有编译器错误的情况下显式实例化?谢谢.
gcc版本4.5.1 20100924(Red Hat 4.5.1-4)(GCC)
是的,将该行更改为:
T::template a<5>();
Run Code Online (Sandbox Code Playgroud)
编译器不知道是否T::a是一个函数(因为它的template性质).通过提及template,您可以明确地通知编译器.这个问题被多次询问,这是其中之一.