c ++中的模板,typename和类 [英] Templates in c++,typename and class
问题描述
使用以下两种方式在c ++中编写模板有什么区别
What is the difference between writing a template in c++ in the below both ways
模板< typename T>
& 模板< 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.
对于模板参数,关键字 typename
和 class
是等价的。 §14.1.2说:
For template parameters, the keywords typename
and class
are equivalent. §14.1.2 says:
模板参数中类和typename之间没有语义差异
。
There is no semantic difference between class and typename in a template-parameter.
如果两者相同,
If both are same,then why do we have them both if one is enough?
Stan Lippman在他的文章中解释了这一点:
Stan Lippman explains this in his article:
这篇关于c ++中的模板,typename和类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!