关于C++:模板中的关键词类型和类的区别?

Difference of keywords 'typename' and 'class' in templates?

对于模板,我看到了两个声明:

1
2
template < typename T >
template < class T >

有什么区别?

在下面的例子中,这些关键词究竟意味着什么(摘自德国维基百科关于模板的文章)?

1
2
3
4
5
template < template < typename, typename > class Container, typename Type >
class Example
{
     Container< Type, std::allocator < Type > > baz;
};

(P)EDOCX1和EDOCX1的英文本2是互通的,基本上是一个具体的例子:(p)字母名称(P)and(p)字母名称(P)是相等的。(p)(P)Having said that,there are specific cases where there is a difference between EDOCX1 penal 1 and EDOCX1 penal 2.(p)(P)The first one is in the case of dependent types.EDOCX1 is used to declaration when you are referenceing a nested type that depends on another template parameter,such as the EDOCX1 big 10 in this example:(p)字母名称(P)The second one you actually show in your question,though you might not realize it:(p)字母名称(P)当具体说明一个Template Template,the EDOCX1 plography 2 must be used as above-it is not interchangeable with EDOCX1 indicational 1 in this case(note:since C++17 both keywords are allowed in this case).(p)(P)你也必须使用EDOCX1(音译2)当明确的瞬间(p)字母名称(P)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.(p)


(P)For naming template parameters,EDOCX1 universal 1 and EDOCX1.14.1.2§14.1.2:(p)布尔奇1(P)EDOCX1 sinc 1.However is possible in another context when using templates-to hint at the compiler that you are referring to a dependent type.14.6.2§14.6.2:(p)布尔奇1(P)例如:(p)字母名称(P)Without EDOCX1 inal.The compiler can't tell in general whether you are referring to a type or not.(p)


(P)在没有技术区别的情况下,我发现有两种不同的说法。(p)(P)For a template that should accept any type as t,including built-ins(such as an array)(p)字母名称(P)For a temple that will only work where it is a real class.(p)字母名称(P)但是,请记住,这是一个清除一些人使用。Not mandated by the standard or enforced by compilers(p)


  • 没有区别
  • Template type parameter EDOCX1 plus 0 is itself a template with two type parameters.


    (P)这一PIECE of snippet is from C++First book.我知道这是错的。(p)(P)Each type parameter must be before the keyword class or typename:(p)字母名称(P)这些Keywords have the same meaning and can be used interchangeably inside a temple parameter list.a Template parameter list can use both keywords:(p)字母名称(P)It may seem more intuitive to use the keyword typename rather than class to designate a temple type parameter.在所有情况下,我们可以使用购买-in(Nonclass)类型作为一个Template type argument。Moreover,type name more clearly indicates that the name that follows is a type name.然而,typename was added to C++after templates were already in widespread use;some programmers continue to use class exclusively(p)