cpp/language/template parameters

Template parameters
Every is parameterized by one or more template parameters, indicated in the  of the template declaration syntax:

Each parameter in may be:
 * a non-type template parameter;
 * a type template parameter;
 * a template template parameter.

Non-type template parameter
@1@ A non-type template parameter with an optional name. @2@ A non-type template parameter with an optional name and a default value. @3@ A non-type template with an optional name. @4@ A non-type template parameter with a placeholder type. may be any type that includes the placeholder (such as plain,  or ), or.

A non-type template parameter must have a structural type, which is one of the following types (optionally cv-qualified, the qualifiers are ignored):
 * (to object or to function);
 * an ;
 * a (to object or to function);
 * a (to member object or to member function);
 * an ;

Array and function types may be written in a template declaration, but they are automatically replaced by pointer to object and pointer to function as appropriate.

When the name of a non-type template parameter is used in an expression within the body of the class template, it is an unmodifiable unless its type was an lvalue reference type.

A template parameter of the form is not an unnamed non-type template parameter of type, even if otherwise  is an  and  declares  to be of type.

Type template parameter
@1@ A type template parameter without a default.

@2@ A type template parameter with a default.

@3@ A type template.

@4@ A constrained type template parameter without a default.

@5@ A constrained type template parameter with a default.

@6@ A constrained type template.

The name of the parameter is optional:

In the body of the template declaration, the name of a type parameter is a typedef-name which aliases the type supplied when the template is instantiated.

Template template parameter
@1@ A template template parameter with an optional name. @2@ A template template parameter with an optional name and a default. @3@ A template template with an optional name.

In the body of the template declaration, the name of this parameter is a template-name (and needs arguments to be instantiated).

Name resolution for template parameters
The name of a template parameter is not allowed to be redeclared within its scope (including nested scopes). A template parameter is not allowed to have the same name as the template name.

In the definition of a member of a class template that appears outside of the class template definition, the name of a member of the class template hides the name of a template parameter of any enclosing class templates, but not a template parameter of the member if the member is a class or function template.

In the definition of a member of a class template that appears outside of the namespace containing the class template definition, the name of a template parameter hides the name of a member of this namespace.

In the definition of a class template or in the definition of a member of such a template that appears outside of the template definition, for each non- base class, if the name of the base class or the name of a member of the base class is the same as the name of a template parameter, the base class name or member name hides the template parameter name.

Template arguments
In order for a template to be instantiated, every template parameter (type, non-type, or template) must be replaced by a corresponding template argument. For s, the arguments are either explicitly provided or defaulted. For s, the arguments are explicitly provided,, or defaulted.

If an argument can be interpreted as both a and an expression, it is always interpreted as a type-id, even if the corresponding template parameter is non-type:

Template non-type arguments
The following limitations apply when instantiating templates that have non-type template parameters:

Template type arguments
A template argument for a type template parameter must be a, which may name an incomplete type:

Template template arguments
A template argument for a template template parameter must be an which names a class template or a template alias.

When the argument is a class template, only the primary template is considered when matching the parameter. The partial specializations, if any, are only considered when a specialization based on this template template parameter happens to be instantiated.

To match a template template argument to a template template parameter,  must be at least as specialized as  (see below).

Formally, a template template-parameter is at least as specialized as a template template argument  if, given the following rewrite to two function templates, the function template corresponding to  is at least as specialized as the function template corresponding to  according to the partial ordering rules for s. Given an invented class template  with the template parameter list of  (including default arguments):
 * Each of the two function templates has the same template parameters, respectively, as or.
 * Each function template has a single function parameter whose type is a specialization of with template arguments corresponding to the template parameters from the respective function template where, for each template parameter  in the template parameter list of the function template, a corresponding template argument  is formed.  is the id-expression.

If the rewrite produces an invalid type, then is not at least as specialized as.

Before the adoption of, each of the template parameters of must match corresponding template parameters of  exactly. This hinders many reasonable template argument from being accepted.

Although it was pointed out very early (CWG#150), by the time it was resolved, the changes were applied to the C++17 working paper and the resolution became a de facto C++17 feature. Many compilers disable it by default:
 * GCC disables it in all language modes prior to C++17 by default, it can only be enabled by setting a compiler flag in these modes.
 * Clang disables it in all language modes by default, it can only be enabled by setting a compiler flag.
 * Microsoft Visual Studio treats it as a normal C++17 feature and only enables it in C++17 and later language modes (i.e. no support in C++14 language mode, which is the default mode).

Default template arguments
Default template arguments are specified in the parameter lists after the sign. Defaults can be specified for any kind of template parameter (type, non-type, or template).

If the default is specified for a template parameter of a primary class template or alias template, each subsequent template parameter must have a default argument. In a function template, there are no restrictions on the parameters that follow a default.

Default parameters are not allowed
 * in the out-of-class definition of a member of a (they have to be provided in the declaration inside the class body). Note that s of non-template classes can use default parameters in their out-of-class definitions (see GCC bug 53856)
 * in declarations

Default template arguments that appear in the declarations are merged similarly to default function arguments:

But the same parameter cannot be given default arguments twice in the same scope:

When parsing a default template argument for a non-type template parameter, the first non-nested is taken as the end of the template parameter list rather than a greater-than operator:

The template parameter lists of template template parameters can have their own default arguments, which are only in effect where the template template parameter itself is in scope:

for the names used in a default template parameter is checked at the declaration, not at the point of use:

Template argument equivalence
Template argument equivalence is used to determine whether two s are same.

Two values are template-argument-equivalent if they are of the same type and
 * they are of integral or enumeration type and their values are the same
 * or they are of pointer type and they have the same pointer value
 * or they are of pointer-to-member type and they refer to the same class member or are both the null member pointer value
 * or they are of lvalue reference type and they refer to the same object or function