For templates I have seen both declarations:
template < typename T >
template < class T >
What’s the difference?
And what exactly do those keywords mean in the following example (taken from the German Wikipedia article about templates)?
template < template < typename, typename > class Container, typename Type >
class Example
{
Container< Type, std::allocator < Type > > baz;
};
typenameandclassare interchangeable in the basic case of specifying a template:and
are equivalent.
Having said that, there are specific cases where there is a difference between
typenameandclass.The first one is in the case of dependent types.
typenameis used to declare when you are referencing a nested type that depends on another template parameter, such as thetypedefin this example:The second one you actually show in your question, though you might not realize it:
When specifying a template template, the
classkeyword MUST be used as above — it is not interchangeable withtypenamein this case (note: since C++17 both keywords are allowed in this case).You also must use
classwhen explicitly instantiating a template: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.