Re: template keyword needed
"George" <George@discussions.microsoft.com> wrote in message
news:CB30480C-5EB1-4B63-A20E-1CD8811EA1E7@microsoft.com
[Code]
typedef typename A::template rebind <Link>::other Link_alloc;
[/Code]
It means if A is allocator, we can use use to allocate arbitrary type
of object instance, said by Bjarne.
I understand this statement, my question is about the grammar. Do we
need the template keyword? Why?
Presumably, A is a template parameter. So rebind is a dependent name,
the compiler doesn't know how it's declared (it will only be known once
the template is instantiated and A is replaced with some concrete type).
Under these circumstances, there's an ambiguity of whether '<' should be
parsed as a less-than operator or as an opening angle bracket in a
template identifier. To resolve it, the rule is that a dependent name is
not a template (and thus '<' is less-than) unless preceded with a
keyword 'template'.
I have tested without temlpate keyword, the code still works.
[Code]
template <class T1> class Foo {
public:
template <class T2> class Goo {
public:
typedef T2 GooValueType;
};
};
int main()
{
Foo <int>::Goo<char>::GooValueType a1;
Foo <int>::template Goo<char>::GooValueType a2;
return 0;
}
[/Code]
In your example, Goo is not a dependent name. Try this:
template <typename T> void f() {
T::/*template*/ Goo<char> goo;
}
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925