typename missing

C

Conrad Weyns

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
 
J

Jeff Schwab

Conrad said:
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.
 
J

John Harrison

Conrad Weyns said:
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
 
J

John Harrison

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
 
C

Conrad Weyns

John Harrison said:
Conrad Weyns said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top