Code is Compiled using GCC. This work without any error in VC++
template <typename T>
void Function(T& A){
T::iterator it; //Error : dependent-name 'T::iterator' is parsed as a non-type,
//but instatiation yields a type.
}
This article states that the compiler can't figure out wether the iterator in T
type is a class or just a static member. So we must use typename
keyword to classify the symbol as a type.
My question is, since T
is known at compile-time then the compiler already knows that iterator
inside T is a class (in my case T is vector<int>
). So why there is an error?
Also is this another use of the typename
keyword beside using it as defining the template parameter T
.
UPDATE:
I read all the answers and other answers from here which really answered all my thoughts. I can sum it up to this :
The correct compiler that handle this right is Gcc. VC++ will let you compile a malformed code. The Error that accure while compiling with Gcc is due to the syntax analysis, since Gcc will try to parse the code of the function template but it will find a syntax error which is T::iterator it;
because Gcc by Deafault treats T::iterator
as a variable (T::iterator
is parsed as non-type) and not as a type, to solve this problem you must tell Gcc explicitly to treat T::iterator
as a type, this is done by adding the keyword typename
.
Now back to VC++. the answer to why this worked is because of existing bug in VC++, it's whether VC++ delay the decision of whether T::iterator
is a variable or a type. or the VC++ supplies the keyword typename
wherever it think it's required.
Useful Article
Note: Feel free to edit the UPDATE if you find something incorrect.