Topic: template keyword: to add or not to add.


Author: Richard Corden <richards_corden@hotmail.com>
Date: Mon, 8 Apr 2002 15:20:40 GMT
Raw View
I'm having trouble with the following example:

namespace NS
{
  template <typename T> struct D
  {
    void foo ();
  };
}

template <typename T> struct C
{
  void foo ();
};

struct B : NS::D <int>, C<int> {};

template <typename T> struct A
{
  void foo (T * t)
  {
    t->C<int>::foo ();           // 1
    t->D<int>::foo ();           // 2
    t->template D<int>::foo ();  // 3
  }
};

template struct A<B>;

Am I correct to use the 'template' keword for D?  Without 'template'
how does the compiler know to treat D as a template name?

When I use comeau online, I get errors for 2 and 3.  2 complains that
type name int is not allowed, and 3 fails because the name following
the 'template' keyword must be a member template.

Thanks in advance...

Richard

--
Richard Corden
To reply remove 's' from address

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "Giovanni Bajo" <giovannibajo@REMOVEliberoTHIS.it>
Date: Mon, 8 Apr 2002 17:58:42 GMT
Raw View
"Richard Corden" <richards_corden@hotmail.com> ha scritto nel messaggio
news:83bscuwdky.fsf@leinster.programmingresearch.ie...

> namespace NS
> {
>   template <typename T> struct D
>   {
>     void foo ();
>   };
> }
>
> template <typename T> struct C
> {
>   void foo ();
> };
>
> struct B : NS::D <int>, C<int> {};
>
> template <typename T> struct A
> {
>   void foo (T * t)
>   {
>     t->C<int>::foo ();           // 1
>     t->D<int>::foo ();           // 2
>     t->template D<int>::foo ();  // 3
>   }
> };
>
> template struct A<B>;

> Am I correct to use the 'template' keword for D?  Without 'template'
> how does the compiler know to treat D as a template name?
> When I use comeau online, I get errors for 2 and 3.  2 complains that
> type name int is not allowed, and 3 fails because the name following
> the 'template' keyword must be a member template.

To my knowledge, 'template' keyword must be used only for member template
functions:

a->template Func<int,int>(b,c);

In your example, you're simply forgetting to specify the namespace:

     t->C<int>::foo ();           // correct
     t->NS::D<int>::foo ();           // correct
     t->template NS::D<int>::foo ();  // ill-formed (illegal use of template
keyword)

Giovanni Bajo

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
Date: Mon, 8 Apr 2002 20:22:06 GMT
Raw View
"Giovanni Bajo" <giovannibajo@REMOVEliberoTHIS.it> writes:

[...]

| To my knowledge, 'template' keyword must be used only for member template
| functions:

And only if the expression designating the object for which the member
function is being called is dependent.

--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
Date: Mon, 8 Apr 2002 20:22:16 GMT
Raw View
Richard Corden <richards_corden@hotmail.com> writes:

| I'm having trouble with the following example:
|
| namespace NS
| {
|   template <typename T> struct D
|   {
|     void foo ();
|   };
| }
|
| template <typename T> struct C
| {
|   void foo ();
| };
|
| struct B : NS::D <int>, C<int> {};
|
| template <typename T> struct A
| {
|   void foo (T * t)
|   {
|     t->C<int>::foo ();           // 1
|     t->D<int>::foo ();           // 2

#2 should read

     t->NS::D<int>::foo ();

|     t->template D<int>::foo ();  // 3

This is not OK.  See above.


[...]

| When I use comeau online, I get errors for 2 and 3.  2 complains that
| type name int is not allowed, and 3 fails because the name following
| the 'template' keyword must be a member template.

Right.

--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Richard Corden <richards_corden@hotmail.com>
Date: Tue, 9 Apr 2002 14:39:25 GMT
Raw View
Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr> writes:

> #2 should read
>
>      t->NS::D<int>::foo ();
>
> |     t->template D<int>::foo ();  // 3
>
> This is not OK.  See above.
>
> [...]

The problem here then, is that I must have the definition of NS::D
above A for this example to work:

<CODE>
template <typename T> struct A
{
  void foo (T * t)
  {
    t->NS::D<int>::foo ();  // 4
  }
};

namespace NS
{
  template <typename T> struct D
  {
    void foo ();
  };
}

template <typename T> struct C
{
  void foo ();
};

struct B : NS::D <int>, C<int> {};

template struct A<B>;
</CODE>

This code now fails, but it only fails as D is a template name:

<CODE>
template <typename T> struct A
{
  void foo (T * t)
  {
    t->C::foo (); // 5
  }
};

namespace NS
{
  struct C
  {
    void foo ();
  };
}

struct B : NS::C
{};

template class A<B>;
</CODE>

Is there a reason that 14.2 is worded so using the template keyword
is not allowed as in my example 3 above?

It seems to me that the wording could be relaxed, so that these
examples would be legal.

Thanks again,

Richard

--
Richard Corden

To reply remove 's' from address

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]