views:

76

answers:

1

Looking through the source code of a binary tree,I find the following function:

//definition of BTR,in case you'd want to know
template< class Type>
struct BTR 
{
    // The item saved to that specifiec position into the tree
    Type  value;    

    // Points to the left leaf
    BTR<Type>*  left;

    // Points to the right leaf
    BTR<Type>*  right;  
};

//why typename?
template< class Type>
BTR<Type>* CreateEx(typename const std::vector<Type>::iterator start,typename const std::vector<Type>::iterator end) 
{
    //definition
}

Now,what's confusing me about this function, is its parameters. Why does it need the keyword typename? Because if I remove both the typenames,my compiler starts complaining and says I should put a ')' before identifier 'start'. And if I changed the parameters so that the function took two vectors instead of two iterators and removed the typenames,my compiler stops complaining(although of course,the function doesn't work any more).

// perfectly acceptable!
template< class Type>
BTR<Type>* CreateEx( const std::vector<Type> start, const std::vector<Type> end)

So it seems I need the keyword because the function wants two iterators. But why is this keyword necessary in a situation like this?

+2  A: 

Because the compiler doesn't know if std::vector< Type >::iterator is a type or a member of std::vector< Type >, and thus needs a little help in the form of typename.

ronag