Template malarky
Hi, what's the deal with a typedef'd typename's scope? If I try to declare a function that returns a typedef typename outside of the class it doesn't see it at all? For example, something similar to what I have now.
template<class x, class y>
class MyClass
{
typedef pair<x, y> IndexedPair;
typedef list< IndexedPair > IndexList;
typedef typename IndexList::iterator IndexList_It
IndexList_It FindIndexPair(const x& index);
};
template<class x, class y>
IndexList_It MyClass<x, y>::FindIndexPair(const x& index)
{
//Do something...
}
That'll result in an error because it can't find the return type, however it works if I define that function actually inside of the class. Anyone know of a way around that?
[862 byte] By [
mortiz] at [2007-11-11 10:23:54]

# 1 Re: Template malarky
The scope of typdefs declared inside a class is restricted to the class itself. To refer to them from another scope, such as the enclosing scope, you need to use qualified names, e.g., MyClass<x,y>::IndexList_It
This shouldn't surprise since you already know that static data members for example are accessed in a similar way.
The problem is that MyClass is a template. Therefore, it's not a type or a class. You need to use a specialization:
MyClass<x,y>::IndexList_It
Now comes the big problem: the compiler can't always know what IndexList_It is -- is it a type or an object. By default, it assumes that it's an object. To override this, you have to use the typename keyword:
typename MyClass<x,y>::IndexList_It MyClass<x, y>::FindIndexPair(const x& index)
{
//Do something...
}
BTW, you forgot a ; after typedef typename IndexList::iterator IndexList_It
Danny at 2007-11-11 20:59:03 >
