Sunday, 13 March 2011

Typename v/s class

The key words 'typename' and 'class' are a matter of choice in below case.

template<typename T>
class New{....}

whereas i prefer using typename as the typename was introduced specifically to name a type.

It is not matter of choice always, for line number 6 and 7, class cannot be used as a substitute for typename.
  - i is of independent type ( its not dependent on type T ).
  - d is a dependent type, it depends on the template parameter T.
  - begin is of Nested dependent type, as it type depends on the nested class iterator present within the dependent type T.

typename has to be prefixed on the Nested dependent type.

  1. template <typename Type>
  2. bool doSomeCrap
  3. {
  4.       int i;
  5.       T d;
  6.      typename Type::iterator begin = cont.begin();  
  7.      typename Type::iterator end = cont.end() - 1;  
  8.      return *begin > *end;
  9. }

Stan Lippman speaks about the historical reasons for supporting both  here. I found it interesting...

No comments:

Post a Comment