开发者

What is the difference between "template <class T>" and "template <typename T>"? [duplicate]

This question already has answers here: Closed 10 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜