1

Possible Duplicate:
Where and why do I have to put "template" and "typename" on dependent names?

I know only two cases where they are used (this is template parameter dependency i think but i don't know more than these two forms) :

  • In parameter list i.e <typename T>
  • like typename X::Sub_type Xx

So there are any more cases in general , or there is some theory behind all this?

Community
  • 1
  • 1

2 Answers2

3

You need the typename keyword to refer to types whose name is dependent on a template argument. The exact rules for dependency are not trivial, but basically any type that is internal to any of your type or template arguments, or to the instantiation of any template with one of the arguments to your own template is dependent.

template <typename T, int I>
struct test {
   typename T::x x;                  // internal to your argument
   typename other_template<T>::y y;  // to the instantiation of your template with an argument
   typename another_tmpl<I>::z z;    // even if the template argument is not a type
};
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • Some further details are in this FAQ: [What is the template `typename` keyword used for?](http://www.comeaucomputing.com/techtalk/templates/#typename) – ildjarn Sep 01 '11 at 18:03
0

1>This is not the real reason as you can simply write <class T> as a replacement.

<typename T>

2> This is the real reason behind typename where it's needed before a qualified dependent type.

template <class T>
void foo() {
   typename T::iterator * iter;
   ...
}

Without typename here, there is a C++ parsing rule that says that qualified dependent names should be parsed as non-types even if it leads to a syntax error.

RoundPi
  • 5,819
  • 7
  • 49
  • 75