cpp/language/partial specialization

Allows customizing class templates for a given category of template arguments.

Syntax
where identifies the name of a previously declared. This declaration must be in the same or, for s, class scope as the primary template definition which it specializes.

For example,

Examples of partial specializations in the standard library include std, which has a partial specialization for array types.

The argument list
The following restrictions apply to the of a partial template specialization: @1@ The argument list cannot be identical to the non-specialized argument list (it must specialize something):

@2@ Default arguments cannot appear in the argument list @3@ If any argument is a pack expansion, it must be the last argument in the list @4@ Non-type argument expressions can use template parameters as long as the parameter appears at least once outside a (note that only clang supports this feature currently):

@5@ Non-type template argument cannot specialize a template parameter whose type depends on a parameter of the specialization:

Name lookup
Partial template specializations are not found by name lookup. Only if the primary template is found by name lookup, its partial specializations are considered. In particular, a using declaration that makes a primary template visible, makes partial specializations visible as well:

Partial ordering
When a class template is instantiated, and there are partial specializations available, the compiler has to decide if the primary template is going to be used or one of its partial specializations.

@1@ If only one specialization matches the template arguments, that specialization is used @2@ If more than one specialization matches, partial order rules are used to determine which specialization is more specialized. The most specialized specialization is used, if it is unique (if it is not unique, the program cannot be compiled) @3@ If no specializations match, the primary template is used

Informally "A is more specialized than B" means "A accepts a subset of the types that B accepts".

Formally, to establish more-specialized-than relationship between partial specializations, each is first converted to a fictitious function template as follows:
 * the first function template has the same template parameters as the first partial specialization and has just one function parameter, whose type is a class template specialization with all the template arguments from the first partial specialization
 * the second function template has the same template parameters as the second partial specialization and has just one function parameter whose type is a class template specialization with all the template arguments from the second partial specialization.

The function templates are then ranked as if for.

Members of partial specializations
The template parameter list and the template argument list of a member of a partial specialization must match the parameter list and the argument list of the partial specialization.

Just like with members of primary templates, they only need to be defined if used in the program.

Members of partial specializations are not related to the members of the primary template.

Explicit (full) specialization of a member of a partial specialization is declared the same way as an explicit specialization of the primary template.

If a primary template is a member of another class template, its partial specializations are members of the enclosing class template. If the enclosing template is instantiated, the declaration of each member partial specialization is instantiated as well (the same way declarations, but not definitions, of all other members of a template are instantiated).

If the primary member template is explicitly (fully) specialized for a given (implicit) specialization of the enclosing class template, the partial specializations of the member template are ignored for this specialization of the enclosing class template.

If a partial specialization of the member template is explicitly specialized for a given (implicit) specialization of the enclosing class template, the primary member template and its other partial specializations are still considered for this specialization of the enclosing class template.