简体   繁体   English

c ++,typename和class中的模板

[英]Templates in c++,typename and class

What is the difference between writing a template in c++ in the below both ways 以下两种方式在c ++中编写模板有什么区别

template<typename T> & template<class T> template<typename T>template<class T>

and when do we prefer them above each other? 我们什么时候比他们更喜欢他们? If both are same,then why do we have them both if one is enough? 如果两者都相同,那么为什么如果一个就够了我们两个都有?

No difference at all. 没有任何区别。 I prefer first one (mostly), but that is my personal taste. 我更喜欢第一个(大多数),但那是我的个人品味。 The language doesn't make any difference between them. 这种语言在它们之间没有任何区别。

For template parameters, the keywords typename and class are equivalent. 对于模板参数,关键字typenameclass是等效的。 §14.1.2 says: §14.1.2说:

There is no semantic difference between class and typename in a template-parameter. template-parameter中的class和typename之间没有语义差异。


If both are same,then why do we have them both if one is enough? 如果两者都相同,那么为什么如果一个就够了我们两个都有?

Stan Lippman explains this in his article: Stan Lippman在他的文章中解释了这一点:

Why C++ Supports both Class and Typename for Type Parameters? 为什么C ++支持类型参数的类和类型名称?

These keywords are there because of historical reason. 由于历史原因,这些关键字存在。 There's no difference between them. 它们之间没有区别。 Read this: http://blogs.msdn.com/b/slippman/archive/2004/08/11/212768.aspx 阅读本文: http//blogs.msdn.com/b/slippman/archive/2004/08/11/212768.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM