delphij's Chaos

选择chaos这个词是因为~~实在很难找到一个更合适的词来形容这儿了……

25 Apr 2005

How to represent some type inherted from other template, in a C++ template class?

It’s not quite easy to represent a type that is found in base “template class”. Consider the following code:


template <class Key,class T>     class CMyClass: public map<Key,T> { public:     void lookup(Key x)     {         map<Key,T>::iterator iter;         iter = find(x);     } };


You will find that the bold line won’t compile.

To correct this, add “typename” to explicitly specify that map<Key,T>::iterator is a type name. Example:

typename map<Key,T>::iterator