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?
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.
In the former, the compiler thinks that you mean...
a.f < 0 ...gibberish....
Andrey's answer fixes that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With