AlexDan
AlexDan

Reputation: 3341

dependent name and non-dependent name in template

Code:

template <class T>
void f(){
T::iterator a; // will work using Gcc if we add typename
//...
}

The above code will work using MSVC++ and will not work using gcc, because MSVC++ will delay parsing.
I know that the compiler at the template definition time will only perform lookups for non-dependent names, and since T::iterator obviously depends on T, why does the lookup happen at template defintion time ?

Upvotes: 1

Views: 276

Answers (2)

Potatoswatter
Potatoswatter

Reputation: 137800

The purpose of the typename keyword is to allow the compiler to defer lookup. Thus it is only used in contexts where the lookup does not happen at template definition time.

Lookup would resolve whether the name is a type or an object, which is required to check the syntax of the template definition. typename specifies this explicitly. If there is no typename keyword then the compiler assumes that it is an object, for syntax purposes.

Lookup at instantiation time must find a type if and only if typename is applied to the dependent name.

Upvotes: 0

Jesse Good
Jesse Good

Reputation: 52365

It doesn't. Dependent names are looked up at instantiation time. At definition time, it only checks for syntax errors, etc. for dependent names. The typename keyword is used to help the compiler parse the expression.

Upvotes: 4

Related Questions