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".
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".
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.