Use case of typename keyword in c++

160 views Asked by At

I am wondering why we need to had the typename keyword or not in these two situations :

template <T>
void foo(void)
{
   std::vector<T> v;
   typename std::vector<T>::iterator it;
}

In the case of an iterator the compiler send us an error if typename is not specified.

Thank you !

1

There are 1 answers

0
Vlad from Moscow On

From the C++ 17 Standard (17.7 Name resolution)

2 A name used in a template declaration or definition and that is dependent on a template-parameter is assumed not to name a type unless the applicable name lookup finds a type name or the name is qualified by the keyword typename.