[temp.spec.general]

# 13 Templates [[temp]](./#temp)

## 13.9 Template instantiation and specialization [[temp.spec]](temp.spec#general)

### 13.9.1 General [temp.spec.general]

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L6345)

The act of instantiating a function, a variable,
a class, a member of a class template, or
a member template is referred to as[*template instantiation*](#def:template_instantiation "13.9.1 General [temp.spec.general]")[.](#1.sentence-1)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L6352)

A function instantiated from a function template is called an instantiated
function[.](#2.sentence-1)

A class instantiated from a class template is called an instantiated class[.](#2.sentence-2)

A member function, a member class, a member enumeration, or a static data member of a class template
instantiated from the member definition of the class template is called,
respectively, an instantiated member function, member class, member enumeration, or static data
member[.](#2.sentence-3)

A member function instantiated from a member function template is called an
instantiated member function[.](#2.sentence-4)

A member class instantiated from a member class template is called an
instantiated member class[.](#2.sentence-5)

A variable instantiated from a variable template is called an
instantiated variable[.](#2.sentence-6)

A static data member instantiated from a static data member template
is called an instantiated static data member[.](#2.sentence-7)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L6369)

An explicit specialization may be declared for a function template,
a variable template, a class template, a member of a class template, or
a member template[.](#3.sentence-1)

An explicit specialization declaration is introduced bytemplate<>[.](#3.sentence-2)

In an explicit specialization declaration for
a variable template, a class template,
a member of a class template, or a class member template,
the variable or class that is explicitly specialized
shall be specified with a [*simple-template-id*](temp.names#nt:simple-template-id "13.3 Names of template specializations [temp.names]")[.](#3.sentence-3)

In the explicit specialization declaration for a function template or
a member function template,
the function or member function explicitly specialized may be specified
using a [*template-id*](temp.names#nt:template-id "13.3 Names of template specializations [temp.names]")[.](#3.sentence-4)

[*Example [1](#example-1)*: template<class T = int> struct A {static int x;};template<class U> void g(U) { }template<> struct A<double> { }; // specialize for T == doubletemplate<> struct A<> { }; // specialize for T == inttemplate<> void g(char) { } // specialize for U == char// U is deduced from the parameter typetemplate<> void g<int>(int) { } // specialize for U == inttemplate<> int A<char>::x = 0; // specialize for T == chartemplate<class T = int> struct B {static int x;};template<> int B<>::x = 1; // specialize for T == int â *end example*]

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L6405)

An instantiated template specialization can be either implicitly
instantiated ([[temp.inst]](temp.inst "13.9.2 Implicit instantiation")) for a given argument list or be explicitly
instantiated ([[temp.explicit]](temp.explicit "13.9.3 Explicit instantiation"))[.](#4.sentence-1)

A [*specialization*](#def:specialization "13.9.1 General [temp.spec.general]") is a class, variable, function, or class member that is either
instantiated ([[temp.inst]](temp.inst "13.9.2 Implicit instantiation")) from a templated entity or is an
explicit specialization ([[temp.expl.spec]](temp.expl.spec "13.9.4 Explicit specialization")) of a templated entity[.](#4.sentence-2)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L6413)

For a given template and a given set of[*template-argument*](temp.names#nt:template-argument "13.3 Names of template specializations [temp.names]")*s*,

- [(5.1)](#5.1)

an explicit instantiation definition shall appear at most once in a program,

- [(5.2)](#5.2)

an explicit specialization shall be defined at most once
in a program, as specified in [[basic.def.odr]](basic.def.odr "6.3 One-definition rule"), and

- [(5.3)](#5.3)

both an explicit instantiation and a declaration of an
explicit specialization shall not appear in a program unless
the explicit specialization is reachable from the explicit instantiation[.](#5.sentence-1)

An implementation is not required to diagnose a violation of this rule
if neither declaration is reachable from the other[.](#5.sentence-2)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L6430)

The usual access checking rules do not apply to names
in a declaration of an explicit instantiation or explicit specialization,
with the exception of names appearing in a function body,
default argument, [*base-clause*](class.derived.general#nt:base-clause "11.7.1 General [class.derived.general]"), [*member-specification*](class.mem.general#nt:member-specification "11.4.1 General [class.mem.general]"), [*enumerator-list*](dcl.enum#nt:enumerator-list "9.8.1 Enumeration declarations [dcl.enum]"),
or static data member or variable template initializer[.](#6.sentence-1)

[*Note [1](#note-1)*:

In particular, the template arguments and names
used in the function declarator
(including parameter types, return types and exception specifications)
can be private types or objects that would normally not be accessible[.](#6.sentence-2)

â *end note*]

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L6443)

Each class template specialization instantiated from a template has its own
copy of any static members[.](#7.sentence-1)

[*Example [2](#example-2)*: template<class T> class X {static T s;};template<class T> T X<T>::s = 0;
X<int> aa;
X<char*> bb;

X<int> has a static members of typeint andX<char*> has a static members of typechar*[.](#7.sentence-2)

â *end example*]

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L6469)

If a function declaration acquired its function type through
a [dependent type](temp.dep.type "13.8.3.2 Dependent types [temp.dep.type]") without using the syntactic form of
a function declarator, the program is ill-formed[.](#8.sentence-1)

[*Example [3](#example-3)*: template<class T> struct A {static T t;};typedef int function();
A<function> a; // error: would declare A<function>​::​t as a static member function â *end example*]
