Question

template <typename dataType>
**typename** List<dataType>::Node* List<dataType>::find(int index){
    Node *cur = head;
    for(int i = 0; i < index; i++){
        cur = cur->next;
    }
    return cur;
}

If I don't use that I get following error:

need ‘typename’ before ‘List<dataType>::Node’ because ‘List<dataType>’ is a dependent scope

So I read the wiki entry but I do not get the reason behind that particular example.

Was it helpful?

Solution

Because, like the error message says, List<dataType>::Node is a dependent name, that is, one that depends on the template parameter. Imagine you had something like this:

struct A {
    typedef float t;
};

struct B {
    static int t;
};

template <typename T>
void func()
{
    T::t * x; // pointer declaration, or multiplication? It depends...
}

Here, A::t and B::t are both valid C++ expressions, but mean completely different things: the first is a type, and the second is a value. Now, when the compiler first parses the template function func() -- to check whether it's syntactically correct, before doing any type substitution -- it has to try and decide what T::t * x means. Clearly, there are two possible ways to parse it, so the compiler has to decide which to take.

In some circumstances there is enough "context" for the compiler to work out whether you're referring to a type or not, but in many cases (such as this example), there isn't. When it can't decide, it assumes you're talking about a value -- so the above would (hopefully) give you an error about the variable x being unknown. If you wanted T::t to be treated as a type, you'd need to tell it so explicitly, using the typename keyword.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top