“template< class T>”之间的区别是什么,和“template< typename T>” [英] What is the difference between "template <class T>" and "template <typename T>"?

查看:383
本文介绍了“template< class T>”之间的区别是什么,和“template< typename T>”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

对模板参数使用'class'或'typename'?





b $ b

我看到两个不同的模板类声明:

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

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

这两个代码有什么区别?



编辑:我将错误的关键字typedef更正为typename。

解决方案

If by

  template< typedef T& class SampleClass2 

您的意思是

  template< typename T> class SampleClass2 

那么没有区别。使用 class typename (在引用类型的模板参数的上下文中)是可互换的。 / p>

这里允许两个关键字的原因是历史。有关详细说明,请参见本文


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".

解决方案

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.

这篇关于“template&lt; class T&gt;”之间的区别是什么,和“template&lt; typename T&gt;”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆