"Conrad Weyns" <we***@online.n o> wrote in message
news:c6******** ***@news.datagu ard.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<typen ame T*>::iterator it;
2. Comeau:
"ComeauTest .c", line 5: error: nontype "std::list< _Tp, _Alloc>::iterat or
[with
_Tp=T *, _Alloc=std::all ocator<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