Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ Templates

Started by Gorax Jun 5, 2010 at 9:31 AM 2 replies 1.1k views
Original Post
Gorax
Gorax
I was under the impression that templates in C++ were actually handled at compile time to generate classes as necessary, and yet I've managed to come across something that doesn't work...

class Works {  std::map<std::string,int> testMap;    //this doesn't compile, as it shouldn't, since it does nothing  //std::map<std::string,int>::iterator;    //compiles fine  typedef std::map<std::string,int>::iterator TestIterator;};template<typename T>class Broken {  std::map<std::string,T> testMap;    //this compiles, but is completely useless  std::map<std::string,T>::iterator;    //doesn't compile  typedef std::map<std::string,T>::iterator TestIterator;};


So here's my question: is it simply due to the fact that you can't use templates within templated classes (and if so, why do vectors work fine?), or is there something wrong with my understanding of templates, or is there something wrong with GCC?
Antheus
Antheus
typedef typename std::map<std::string,T>::iterator TestIterator;

Welcome to the depths of name resolution.
Gorax
Gorax
I can't believe it was that simple... Thanks for the help though. Greatly appreciated.
wien
wien
To elaborate slightly; what "iterator" is in "std::map::iterator" is dependent on what the template parameter T actually is. The compiler can't tell if "iterator" is a type, value, member function, whatever, until the template is instantiated.

Therefore, to resolve the ambiguity, you have to explicitly tell the compiler that "iterator" is in fact a type, using typename.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.