13

Possible Duplicate:
Use 'class' or 'typename' for template parameters?

I see two different template class declarations:

template <class T> class SampleClass1
{
    // ...
};

and

template <typename T> class SampleClass2
{
    // ...
};

What is the difference between these two codes?

EDIT: I corrected the wrong keyword "typedef" to "typename".

Community
  • 1
  • 1
hkBattousai
  • 10,583
  • 18
  • 76
  • 124
  • 5
    the difference is that first one is correct, while second one is incorrect. Use `typename` instead of `typedef` in the second, then there will be no difference! – Nawaz Dec 26 '10 at 15:28
  • 4
    @Nawaz: Yeah, sorry for the wrong keyword. – hkBattousai Dec 26 '10 at 16:13

2 Answers2

18

If by

template <typedef T> class SampleClass2

you mean

template <typename T> class SampleClass2

then there is no difference. The use of class and typename (in the context of a template parameter that refers to a type) is interchangeable.

The reason that both keywords are allowed here is historical. See this article for a detailed explanation.

Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
15

In case of template template paramater

template <typename T, template <typename> class Wrapper>
class Foo {
    //...
  private:
    Wrapper<T> data;
};

You have to use class before classname. This is wrong:

template <typename T, template <typename> typename Wrapper>

but this is ok:

template <typename T, template <class> class Wrapper>

In other cases they are interchangeable.

  • Update: there is a new proposal in the C++ standardization group that eliminate this difference. N4051: Allow typename in a template template parameter—Richard Smith – Industrial-antidepressant May 28 '14 at 22:24