Prawdopodobnie nie rozumiesz nawet o co chodzi.
Przykład:
class foo
{
class iterator
{
};
};
template <typename T>
void func()
{
T::iterator a; // BŁĄD KOMPILACJI
}
int main()
{
func<foo>();
}
Wystąpi błąd kompilacji, bo kompilator nie wie co to jest T::iterator
. Więc trzeba dać mu informacje odnośnie tego czym jest T::iterator
, a jest to typ, czyli typename
.
Zresztą sam kompilator daje jasną informacje, dlaczego typename
jest wymagane:
prog.cpp: In function 'void func()':
prog.cpp2: error: need 'typename' before 'T:: iterator' because 'T' is a dependent scope
T::iterator a;
^
prog.cpp14: error: expected ';' before 'a'
T::iterator a;
^
prog.cpp: In instantiation of 'void func() [with T = foo]':
prog.cpp12: required from here
prog.cpp2: error: dependent-name 'T:: iterator' is parsed as a non-type, but instantiation yields a type
T::iterator a;
^
prog.cpp2: note: say 'typename T:: iterator' if a type is meant
Więc, poprawny kod to:
class foo
{
class iterator
{
};
};
template <typename T>
void func()
{
typename T::iterator a; // OK
}
int main()
{
func<foo>();
}
Więcej możesz poczytać tutaj: http://en.cppreference.com/w/cpp/language/dependent_name