views:

374

answers:

3

What does the "class" part of a template statement do?

Example:

template <class T>
class Something
{
    public:
        Something(const T &something);
}

And what else can go there? I usually only see "class".

+7  A: 

The class keyword means the same thing as the typename keyword for the most part. They both indicates that T is a type.

The only difference between the keywords class and typename is that class can be used to provide class template template arguments to a template, whereas typename can't. Consider:

template<template <class T> class U> // must be "class"
std::string to_string(const U<char>& u)
{
  return std::string(u.begin(),u.end());
}

The only other thing you can put in place of the class or typename keywords is an integral type. For example:

template<std::size_t max>
class Foo{...};
...
Foo<10> f;

For a concrete example of this, take a look at std::bitset<N> in the standard library.

fpsgamer
Just a small point about template template declarations: template <template <typename> class> is allowed (only the name of the template needs to be precededed specifically by 'class'). It's quite a strange rule.
James Hopkin
Actually, "class" and "typename" can both be used to introduce template type arguments. Template template arguments are introduced by "template". The "class" in template template arguments makes it clear you can't instantiate that template with a function template.
MSalters
+3  A: 
e.James
You're missing template template arguments
MSalters
+4  A: 
paercebal
Is that an error in g++?
rlbond