views:

641

answers:

4

For templates I have seen both declarations:

template < typename T >

And:

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;
};
+3  A: 
Nikolai N Fetissov
there is a difference in general.
Hassan Syed
could those two parameters the container is templated with also be named? in the example they don't have any names. And also - in this example it's written 'class Container' - could there also be written 'typename Container' instead?
Mat
@Mat: yes, the term to search for is *template template parameters/arguments*. E.g.: `template<template<class U> class V> struct C {};`
Georg Fritzsche
+14  A: 

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<T>::some_type

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

Georg Fritzsche
+2  A: 

While there is no technical difference, I have seen the two used to denote slightly different things.

For a template that should accept any type as T, including built-ins (such as an array )

template<typename T>
class Foo { ... }

For a template that will only work where T is a real class.

template<class T>
class Foo { ... }

But keep in mind that this is purely a style thing some people use. Not mandated by the standard or enforced by compilers

Michael Anderson
I don't blame you for mentioning it, but I think this policy is pretty misguided, since programmers end up taking time thinking about something that doesn't matter ("have I used the right one?") to indicate something that doesn't matter ("does there exist a built-in type which implements the interface required of this template parameter?"). If any members of the template parameter are used (`T t; int i = t.toInt();`) then you need a "real class", and your code won't compile if you supply `int` for `T`...
Steve Jessop
If you want to limit the use to actual classes, you're better off adding a specialization to throw/cause an error for non-class types. If you want to limit use to particular classes, only specialize for them. In any case, such a stylistic distinction is too subtle to get the message across.
Potatoswatter
+12  A: 

typename and class are interchangeable in the basic case of specifying a template:

template<class T>
class Foo
{
};

and

template<typename T>
class Foo
{
};

are equivalent.

Having said that, there are cases specific where there is a difference between typename and class.

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<typename param_t>
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.

You also must use class when explicitly instantiating a template:

template class Foo<int>;

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.

Aaron Klotz
That last one is pretty much a special case of the fact that you must use class or struct, not typename, to define a class. Obviously neither of your first two bits of code could be replaced with `template <typename T> typename Foo {};`, because Foo<T> is most definitely a class.
Steve Jessop
`std::vector<int>::value_type` is not a dependent type, you don't need `typename` there - you only need it if a type depends on a template parameter, say `template<class T> struct C { typedef typename std::vector<T>::value_type type; };`
Georg Fritzsche
And again, `param_t` is not a dependent type. Dependent types are *names that are dependent on a template parameter*, e.g. `foo<param_t>::some_type`, not template parameters themselves.
Georg Fritzsche