Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange GCC 'expected primary expression...' error [duplicate]

Tags:

c++

gcc

templates

Possible Duplicate:
Templates: template function not playing well with class’s template member function

template <typename T>
struct A
{
    template <int I>
    void f();
};

template <typename T>
void F(A<T> &a)
{
    a.f<0>(); // expected primary-expression before ‘)’ token
}

int main()
{
    A<int> a;

    a.f<0>(); // This one is ok.
}

What it's all about?

like image 791
uj2 Avatar asked Jun 16 '10 17:06

uj2


2 Answers

When a dependent name is used to refer to a nested template, the nested name has to be prepended with the keyword template to help the compiler understand that you are referring to a nested template and parse the code correctly

template <typename T>
void F(A<T> &a)
{
    a.template f<0>();
}

Inside main the name a is not dependent, which is why you don't need the extra template keyword. Inside F name a is dependent, which is why the keyword is needed.

This is similar to the extra typename keyword when referring to nested type names through a dependent name. Just the syntax is slightly different.

like image 140
AnT Avatar answered Nov 02 '22 18:11

AnT


In the former, the compiler thinks that you mean...

a.f < 0 ...gibberish....

Andrey's answer fixes that.

like image 1
Edward Strange Avatar answered Nov 02 '22 18:11

Edward Strange