Team LiB   Previous Section   Next Section
template keyword Declares a template, specialization, or instantiation

declaration := template-decl | explicit-instantiation | explicit-specialization
template-decl ::= [export] template < template-parm-list > declaration
template-parm-list ::= template-parm | template-parm-list , template-parm
template-parm ::= type-parm | parm-decl
type-parm ::= class [identifier] [= type-id] | typename [identifier] [= type-id] | 
    template < template-parm-list > class [identifier] [= id-expr]
template-id ::= template-name < [template-arg-list] >
typename-name ::= identifier
template-arg-list ::= template-arg | template-arg-list , template-arg
template-arg ::= assignment-expr | type-id | id-expr
explicit-instantiation ::= template declaration
explicit-specialization ::= template < > declaration
elaborated-type-specifier := class-key [::] [nested-name ::] [template] 
    template-id | typename [::] nested-name :: [template] template-id
simple-type-specifier := [::] nested-name :: template template-id
postfix-expr := postfix-expr . [template] id-expr | 
    postfix-expr -> [template] id-expr
pseudo-dtor-name := [::] nested-name :: template template-id :: ~ class-name
nested-name := nested-name [:: template class-name]
qualified-id := [::] nested-name :: [template] unqualified-id

The template keyword declares a template, a specialization of a template, or an instance of a template. The declaration can be a function declaration, function definition, class declaration, or class definition.

A template instance provides arguments for the template parameters, enclosing the arguments in angle brackets (<>). If the template is a member of a class, and the . or -> operator is used to access the member template, you must use the template keyword to tell the compiler to interpret the < symbol as the start of the template arguments. Similarly, use the template keyword in a qualified name when the name is used to instantiate the template in a context in which the compiler would interpret the < symbol as the less-than operator.

Example

template<typename T>
T min(const T& a, const T& b) { return a < b ? a : b; }
typedef complex<float> cfloat;
template<> min<cfloat>(const cfloat& a, const cfloat& b)
{
  return cfloat(min(a.real(), b.real(  )), min(a.imag(), b.imag(  )));
}
template int min<int>(const int&, const int&);

See Also

class, expression, identifier, type, Chapter 7

    Team LiB   Previous Section   Next Section