Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > typename missing

Reply
Thread Tools

typename missing

 
 
Conrad Weyns
Guest
Posts: n/a
 
      04-23-2004
vc7.1 has no problem with the following snippet:

template <typename T> class TTest
{
std::list<T*>::iterator m_It;
};

but metrowerks codewarrior 9.2 kicks my butt and online Comeau confirms:

1. MW:
Warning : 'typename' is missing in template argument dependent qualified
type
Tests.cxx line 52 std::list<typename T*>::iterator it;

2. Comeau:
"ComeauTest.c", line 5: error: nontype "std::list<_Tp, _Alloc>::iterator
[with
_Tp=T *, _Alloc=std::allocator<T *>]" is not a type name
std::list<T*>::iterator m_It;
^


There were not that many places to insert "typename", so after a while I
found that the following makes
CW and Comeau happy:

template <typename T> class TTest
{
typename std::list<T*>::iterator m_It;
};


Can anyone here help me understand this?
regards,
Conrad Weyns




 
Reply With Quote
 
 
 
 
Jeff Schwab
Guest
Posts: n/a
 
      04-23-2004
Conrad Weyns wrote:
> vc7.1 has no problem with the following snippet:
>
> template <typename T> class TTest
> {
> std::list<T*>::iterator m_It;
> };
>
> but metrowerks codewarrior 9.2 kicks my butt and online Comeau confirms:

....
> There were not that many places to insert "typename", so after a while I
> found that the following makes
> CW and Comeau happy:
>
> template <typename T> class TTest
> {
> typename std::list<T*>::iterator m_It;
> };
>
>
> Can anyone here help me understand this?


Comeau and MetroWerks are right. The compiler should be checking syntax
when the template definition is first encountered, but at that point it
doesn't yet know (unless you tell it) that dependent_type<T>::iterator
is a type name.
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      04-23-2004

"Conrad Weyns" <> wrote in message
news:c6b9ut$1ece$...
> vc7.1 has no problem with the following snippet:
>
> template <typename T> class TTest
> {
> std::list<T*>::iterator m_It;
> };
>
> but metrowerks codewarrior 9.2 kicks my butt and online Comeau confirms:
>
> 1. MW:
> Warning : 'typename' is missing in template argument dependent qualified
> type
> Tests.cxx line 52 std::list<typename T*>::iterator it;
>
> 2. Comeau:
> "ComeauTest.c", line 5: error: nontype "std::list<_Tp, _Alloc>::iterator
> [with
> _Tp=T *, _Alloc=std::allocator<T *>]" is not a type name
> std::list<T*>::iterator m_It;
> ^
>
>
> There were not that many places to insert "typename", so after a while I
> found that the following makes
> CW and Comeau happy:
>
> template <typename T> class TTest
> {
> typename std::list<T*>::iterator m_It;
> };
>
>
> Can anyone here help me understand this?
> regards,
> Conrad Weyns
>


When the compiler first looks at TTest cannot tell if iterator is a typedef
or a static member in the class std::list<T*>. This is because it does not
know the type of T at this point. It cannot even find this out by looking up
the definition of std::list because there maybe specialisation of std::list
which are not visible while it is first looking at TTest (actually that not
likely to be true for std::list, but it's true of templates in general).

The classic example of why the compiler needs to know if a 'dependant name'
is a typedef or a static member is this

template <typename T>
class Awkward
{
T::some_name * global;
};

Now is that a declaration of a variable called global of type T::some_name,
or is it a multiplication of the static member T::some_name and the global
variable called global?

In general the compiler cannot resolve these ambiguities so you have to do
yourself. Nevertheless in most cases the compiler can work out what you mean
(or at least take a reasonable guess) so many compilers let these things go.

Actually this is the simplest of the syntactic ambiguities introduced by
templates, but I'll not post any others (unless you're particularly
interested) since I'd have to look them up myself.

John


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      04-23-2004
>
> template <typename T>
> class Awkward
> {
> T::some_name * global;
> };
>
> Now is that a declaration of a variable called global of type

T::some_name,

I meant of type T::some_name*

> or is it a multiplication of the static member T::some_name and the global
> variable called global?
>


In essence is the * a multiplication or does it indicate a pointer?

john


 
Reply With Quote
 
Conrad Weyns
Guest
Posts: n/a
 
      04-23-2004

"John Harrison" <> wrote in message
news:c6bbir$a4ii7$...
>
> "Conrad Weyns" <> wrote in message
> news:c6b9ut$1ece$...
> > vc7.1 has no problem with the following snippet:
> >
> > template <typename T> class TTest
> > {
> > std::list<T*>::iterator m_It;
> > };
> >
> > but metrowerks codewarrior 9.2 kicks my butt and online Comeau confirms:
> >
> > 1. MW:
> > Warning : 'typename' is missing in template argument dependent qualified
> > type
> > Tests.cxx line 52 std::list<typename T*>::iterator it;
> >
> > 2. Comeau:
> > "ComeauTest.c", line 5: error: nontype "std::list<_Tp, _Alloc>::iterator
> > [with
> > _Tp=T *, _Alloc=std::allocator<T *>]" is not a type name
> > std::list<T*>::iterator m_It;
> > ^
> >
> >
> > There were not that many places to insert "typename", so after a while I
> > found that the following makes
> > CW and Comeau happy:
> >
> > template <typename T> class TTest
> > {
> > typename std::list<T*>::iterator m_It;
> > };
> >
> >
> > Can anyone here help me understand this?
> > regards,
> > Conrad Weyns
> >

>
> When the compiler first looks at TTest cannot tell if iterator is a

typedef
> or a static member in the class std::list<T*>. This is because it does not
> know the type of T at this point. It cannot even find this out by looking

up
> the definition of std::list because there maybe specialisation of

std::list
> which are not visible while it is first looking at TTest (actually that

not
> likely to be true for std::list, but it's true of templates in general).
>
> The classic example of why the compiler needs to know if a 'dependant

name'
> is a typedef or a static member is this
>
> template <typename T>
> class Awkward
> {
> T::some_name * global;
> };
>
> Now is that a declaration of a variable called global of type

T::some_name,
> or is it a multiplication of the static member T::some_name and the global


> variable called global?
>
> In general the compiler cannot resolve these ambiguities so you have to do
> yourself. Nevertheless in most cases the compiler can work out what you

mean
> (or at least take a reasonable guess) so many compilers let these things

go.
>
> Actually this is the simplest of the syntactic ambiguities introduced by
> templates, but I'll not post any others (unless you're particularly
> interested) since I'd have to look them up myself.
>
> John


Many thanks, this has been usefull. I know what to play with now to get a
better mental picture of this.
Conrad

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class vs. typename Alexander Malkis C++ 7 10-20-2012 09:03 PM
Q: typename or not typename? Jakob Bieling C++ 2 03-14-2006 03:44 PM
implicit typename in template Steve Hill C++ 1 09-06-2003 07:52 AM
typename, typedef, and resolution Gina Yarmel C++ 4 08-13-2003 04:06 PM
Gcc complain about typename Xavier Decoret C++ 1 07-04-2003 05:27 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57