2011-11-30 49 views
3

我试图编译下面的代码:C++编译器将模板语法如“<”操作者

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(); 
} 

和编译器在T::a<5>解释<作为操作<,导致错误:

invalid operands of types ‘<unresolved overloaded function type>’ and ‘int’ to binary ‘operator<’ 

有没有什么办法显式实例化T::a<5>没有编译错误? 谢谢。

gcc版本4.5.1 20100924(红帽4.5.1-4)(GCC)

+0

[template disambiguator]的可能重复(http://stackoverflow.com/questions/4077110/template-disambiguator) – iammilind

回答

6

是,更改该行:

T::template a<5>(); 

编译器不知道是否T::a是一个函数(因为其性质为template)。通过提到template,您可以明确地通知编译器。这个问题被问了很多次,这里是one of them

+1

+1。正确.... – Nawaz

+0

明白了,谢谢! – user1072688

+0

@ user1072688,如果这篇文章回答你的问题,那么你可以通过在它下面打勾右边的刻度线来接受它。 – iammilind