What is the difference between "template <class T>" and "template <typename T>"? [duplicate]
Possible Duplicate:
Use 'class' or 'typename' for template parameters?
I see two different template class declarations:
template <class T> class SampleClass1
{开发者_JAVA技巧
// ...
};
and
template <typename T> class SampleClass2
{
// ...
};
What is the difference between these two codes?
EDIT: I corrected the wrong keyword "typedef" to "typename".
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.
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.
精彩评论