Pretty straight-forward:
template <class T>
void foo() {} //compiles
template <struct T>
void goo() {} //doesn't
Why?
Pretty straight-forward:
template <class T>
void foo() {} //compiles
template <struct T>
void goo() {} //doesn't
Why?
class
is only a keyword to indicate that T
is a type. It does not actually mean T
must be a class. (e.g. you can call foo<int>()
.)
The class
keyword was used because typename
didn't exist when the template feature was added. See Templates in c++,typename and class.
Because a template has to be declared with either class
or...
template <typename T>
void foo() {}
Because, the grammar forbids it:
template-declaration:
export_opt template< template-parameter-list > declaration
template-parameter-list:
template-parameter
parameter-declaration
type-parameter:
class identifier
class identifier = type-id
typename identifier
typename identifier = type-id
template < template-parameter-list > class identifier
template < template-parameter-list > class identifier = template-name
Alternatively , you can think that struct is a special case of a class where the members are all public. Thus, allowing it in the template-parameter-list would not be as general.