469,159 Members | 1,494 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,159 developers. It's quick & easy.

typename missing

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


Jul 22 '05 #1
4 2182
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.
Jul 22 '05 #2

"Conrad Weyns" <we***@online.no> wrote in message
news:c6***********@news.dataguard.no...
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
Jul 22 '05 #3
>
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
Jul 22 '05 #4

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c6************@ID-196037.news.uni-berlin.de...

"Conrad Weyns" <we***@online.no> wrote in message
news:c6***********@news.dataguard.no...
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

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by I wish | last post: by
3 posts views Thread by Šimon Tóth | last post: by
1 post views Thread by lutorm | last post: by
13 posts views Thread by Staffan Langin | last post: by
8 posts views Thread by xuatla | last post: by
2 posts views Thread by Dilip | last post: by
7 posts views Thread by StephQ | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.