help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: template function in template class


From: Jeff Schwab
Subject: Re: template function in template class
Date: Sun, 28 Oct 2007 07:55:35 -0700
User-agent: G2/1.0

On Oct 27, 3:06 pm, mathieu <mathieu.malate...@gmail.com> wrote:
> Hi there,
>
>   Why do I need the template keyword in this case:
>
> template <typename T1>
> struct A
> {
>   template <typename T2>
>   void foo() {}
>
> };
>
> template <typename T1>
> struct B
> {
>   template <typename T2>
>   void bar() {
>     A<T1> a;
>     // a.foo<T2>(); // does not compile with gcc
>     a.template foo<T2>();
>   }
>
> };
>
> int main()
> {
>   B<int> b;
>   return 0;
>
> }

The ".template" tells the compiler that the following less-than symbol
(<) is the beginning of a sequence of template arguments, rather than
a binary operator.  As B::bar is being parsed, the compiler doesn't
know whether somebody has done this:

template <>
struct A<int>
{
    double foo;
};



reply via email to

[Prev in Thread] Current Thread [Next in Thread]