Possible Duplicate:
Where and why do I have to put “template” and “typename” on dependent names?
I have following constellation:
template<typename T>
class A{
template<typename U>
A<U> f()const;
}
template<typename T, typename U>
A<U> F(const A<T> &I)
{
return I.f<U>();//this does not work
}
The compiler error on the marked line is:
error: expected initializer before ‘>’ token
So how do I write the line correctly?
That’s two-phase lookup for you. Do this:
This is necessary because, when the compiler compiles the function template
F(), it does not know whatTit might be instantiated with.Acould be specialized after the definition ofF(), which would only be known the momentF()is actually instantiated. Therefore, when the compiler encounters its definition,I.f<Ucould also be, say, a comparison between a memberfofA<T>with someU.In order to resolve this ambiguity, you need to tell the compiler that the opening
<is actually starting a template instantiation.