首页 > 编程语言 > 详细

C++ template typename 和 class 的区别

时间:2020-05-03 11:05:04      阅读:48      评论:0      收藏:0      [点我收藏+]

在比较基础的情况,typenameclass 是可以交换的,也就是没什么差别:


template<class T>
class Foo
{
};


template<typename T>
class Foo
{
};

是等价的。
但也意味着,有些特殊的情况typenameclass 是有区别的。

The first one is in the case of dependent types. typename is used to declare when you are referencing a nested type that depends on another template parameter, such as the typedef in this example:

template
class Foo
{
typedef typename param_t::baz sub_t;
};
The second one you actually show in your question, though you might not realize it:

template < template < typename, typename > class Container, typename Type >
When specifying a template template, the class keyword MUST be used as above -- it is not interchangeable with typename in this case (note: since C++17 both keywords are allowed in this case).

You also must use class when explicitly instantiating a template:

template class Foo;
I‘m sure that there are other cases that I‘ve missed, but the bottom line is: these two keywords are not equivalent, and these are some common cases where you need to use one or the other.

C++ template typename 和 class 的区别

原文:https://www.cnblogs.com/drunknbeard/p/12821054.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!