Let’s say I’m using a templated class with something simple like:
template <class T>
class MyClass
I want to use elements from T’s namespace, for example T could be string, and I wanted to use
T::const_iterator myIterator;
…or something like that. How do I achieve that?
Probably, it’s either not possible or very simple, but I have no idea.
Thanks for answers!
By default if
Tis a template parameter like in your example, theT::some_memberis assumed not to name a type. You have to explicitly specify that it is, by prefixing it withtypename:This resolves some parsing problems like in the following example
So that the compiler can parse this even before instantiating the template, you have to give it a hand and use
typename, including in those cases where it wouldn’t be ambiguous, like in the first case above. The Template FAQ has more insight into this.