1

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 ?

jleahy
  • 16,149
  • 6
  • 47
  • 66
AlexDan
  • 3,203
  • 7
  • 30
  • 46
  • Newer versions of MSVC++ will not accept this. In fact, even VS2005 won't, if I'm not mistaken. – AnT stands with Russia Aug 10 '12 at 04:26
  • @DavidRodríguez-dribeas : My question was about why the lookup happen at template definition time? but from the answer below, I understand that I was confusing between parsing and name lookup. I have a question about MSVC++, from the code above can I understand that MSVC++ doesn't parse the template definition until intialization, and it doesn't perform name lookup on non-dependent name, whereas gcc perform name lookup if the name is non-dependent ? – AlexDan Aug 10 '12 at 04:35

2 Answers2

4

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.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166
  • so template always get checked for syntax error, and if the name is non-dependent they lookup the name too, and VC++ doesn't parse the code at all until instantiation ? – AlexDan Aug 10 '12 at 04:29
  • @AlexDan: Yes, MSVC delays most stuff until instantiation. – Jesse Good Aug 10 '12 at 04:32
  • so if I use MSVC++ , then it wont complain if I used undeclared name (because it doesn't perform name lookup on both dependent & non-dependent name). and I can even do something weird like 1.1+"hi"; and it wont complain (since it doesn't parse the template until initialization). correct me if Im wrong. and thanks for your help. – AlexDan Aug 10 '12 at 04:44
  • @AlexDan: [Read this question](http://stackoverflow.com/questions/10323980/templates-compilation-gcc-vs-vs2010). – Jesse Good Aug 10 '12 at 04:53
  • That question answered all my questions, but I was wondering I read that compiler check only for lexical and syntax errors. but from your link it said that p=i; may be diagnosed even if X::f is not instatiated (p is of type char* and i is an int), which is not lexical error nor syntax error. I guess it's a semantic error. so does the compiler (Gcc) checks for semantics error too if it's possible(e.g "hi"+1.1 will diagnosed at template definition time and it's obviously a semantic error) at template definition time. – AlexDan Aug 10 '12 at 05:12
  • @AlexDan:Yes, it is a semantic error, and yes gcc will probably check it at definition time (although its not required to by the C++ standard). – Jesse Good Aug 10 '12 at 05:54
0

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.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421