user1072688
user1072688

Reputation: 61

c++ compiler treats template syntax as '<' operator

I'm trying to compile the following code:

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

and compiler interprets < in T::a<5> as an operator <, resulting in error:

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

Is there any way to explicitly instantiate T::a<5> without compiler errors? Thank you.

gcc version 4.5.1 20100924 (Red Hat 4.5.1-4) (GCC)

Upvotes: 3

Views: 220

Answers (1)

iammilind
iammilind

Reputation: 69988

Yes, change that line to:

T::template a<5>();

Compiler doesn't know if T::a is a function (because of its template nature). By mentioning template, you inform compiler explicitly. This question is asked many times, here is one of them.

Upvotes: 6

Related Questions