Iterators with unresolved template parameters

O

Ole Nielsby

Porting a project from VC8 to GCC, I stumbled on this:

I have certain templates that use STL collections with a
value type that is a pointer to a template parameter type.
When I declare iterators for these parametrized collections,
GCC chokes.

template class MapWrapper<T> {
std::map<MyKey, T*> myMap; // OK with both compilers
myMethod(){
std::map<MyKey, T*>::iterator i; // GCC chokes
}
}
Did I get the syntax wrong, or is GCC buggy here?

I circumvented it by using B* as the map value type,
where B is a common base class for the T classes used,
combined with unchecked (C-style) casts - but this isn't
supposed to be necessary when working with templates.
 
S

Salt_Peter

Ole said:
Porting a project from VC8 to GCC, I stumbled on this:

I have certain templates that use STL collections with a
value type that is a pointer to a template parameter type.
When I declare iterators for these parametrized collections,
GCC chokes.

template class MapWrapper<T> {
std::map<MyKey, T*> myMap; // OK with both compilers
myMethod(){
std::map<MyKey, T*>::iterator i; // GCC chokes
}
}
Did I get the syntax wrong, or is GCC buggy here?

Nope, its doing its job. the map iterator is a dependent type.

#include <map>

template< typename Key, typename T >
class Map {
std::map< Key, T*> m_map;
void myMethod() {
typedef typename std::map< Key, T*>::iterator Miter; // dependent
type
MIter miter = m_map.begin();
}
};
 
O

Ole Nielsby

Salt_Peter said:
Nope, its doing its job. the map iterator is a dependent type.

#include <map>

template< typename Key, typename T >
class Map {
std::map< Key, T*> m_map;
void myMethod() {
typedef typename std::map< Key, T*>::iterator Miter; // dependent
type
MIter miter = m_map.begin();
}
};

Thanks - seems I forgot the 'typename' keyword in the original
version, and VC8 didn't require it.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top