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

后端 未结 5 635
南方客
南方客 2020-11-22 16:37

For templates I have seen both declarations:

template < typename T >
template < class T >

What\'s the difference?

And

5条回答
  •  青春惊慌失措
    2020-11-22 17:33

    For naming template parameters, typename and class are equivalent. §14.1.2:

    There is no semantic difference between class and typename in a template-parameter.

    typename 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:

    A name used in a template declaration or definition and that is dependent on a template-parameter is assumed not to name a type unless the applicable name lookup finds a type name or the name is qualified by the keyword typename.

    Example:

    typename some_template::some_type
    

    Without typename the compiler can't tell in general whether you are referring to a type or not.

提交回复
热议问题