cpp/language/template specialization

Allows customizing the template code for a given set of template arguments.

Syntax
Any of the following can be fully specialized:
 * 1)  of a class template
 * 2)  of a class template
 * 3)  of a class template
 * 4) member  of a class template
 * 5)  of a class or class template
 * 6)  of a class or class template
 * 1) member  of a class template
 * 2)  of a class or class template
 * 3)  of a class or class template

In detail
Explicit specialization may be declared in any scope where its primary template may be defined (which may be different from the scope where the primary template is defined; such as with out-of-class specialization of a ). Explicit specialization has to appear after the non-specialized template declaration.

Specialization must be declared before the first use that would cause implicit instantiation, in every translation unit where such use occurs:

A template specialization that was declared but not defined can be used just like any other (e.g. pointers and references to it may be used):

Whether an explicit specialization of a function template is is determined by the explicit specialization itself, regardless of whether the primary template is declared with that specifier.

Explicit specializations of function templates
When specializing a function template, its template arguments can be omitted if can provide them from the function arguments:

A function with the same name and the same argument list as a specialization is not a specialization (see template overloading in ).

cannot be specified in explicit specializations of function templates, member function templates, and member functions of class templates when the class is implicitly instantiated.

An explicit specialization cannot be a.

Members of specializations
When defining a member of an explicitly specialized class template outside the body of the class, the syntax is not used, except if it's a member of an explicitly specialized member class template, which is specialized as a class template, because otherwise, the syntax would require such definition to begin with  required by the nested template

An explicit specialization of a static data member of a template is a definition if the declaration includes an initializer; otherwise, it is a declaration. These definitions must use braces for default initialization:

A member or a member template of a class template may be explicitly specialized for a given implicit instantiation of the class template, even if the member or member template is defined in the class template definition.

Member or a member template may be nested within many enclosing class templates. In an explicit specialization for such a member, there's a for every enclosing class template that is explicitly specialized.

In such a nested declaration, some of the levels may remain unspecialized (except that it can't specialize a class member template if its enclosing class is unspecialized). For each of those levels, the declaration needs, because such specializations are themselves templates: