[temp.spec.general] (original) (raw)

13 Templates [temp]

13.9 Template instantiation and specialization [temp.spec]

13.9.1 General [temp.spec.general]

The act of instantiating a function, a variable, a class, a member of a class template, or a member template is referred to astemplate instantiation.

A function instantiated from a function template is called an instantiated function.

A class instantiated from a class template is called an instantiated class.

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.

A member function instantiated from a member function template is called an instantiated member function.

A member class instantiated from a member class template is called an instantiated member class.

A variable instantiated from a variable template is called an instantiated variable.

A static data member instantiated from a static data member template is called an instantiated static data member.

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.

An explicit specialization declaration is introduced bytemplate<>.

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.

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.

[Example 1: template<class T = int> struct A { static int x;};template<class U> void g(U) { } template<> struct A<double> { }; template<> struct A<> { }; template<> void g(char) { } template<> void g<int>(int) { } template<> int A<char>::x = 0; template<class T = int> struct B { static int x;};template<> int B<>::x = 1; — _end example_]

An instantiated template specialization can be either implicitly instantiated ([temp.inst]) for a given argument list or be explicitly instantiated ([temp.explicit]).

A specialization is a class, variable, function, or class member that is either instantiated ([temp.inst]) from a templated entity or is an explicit specialization ([temp.expl.spec]) of a templated entity.

For a given template and a given set oftemplate-arguments,

An implementation is not required to diagnose a violation of this rule if neither declaration is reachable from the other.

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, member-specification, enumerator-list, or static data member or variable template initializer.

[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.

— _end note_]

Each class template specialization instantiated from a template has its own copy of any static members.

[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 membersof typeintandX<char*>has a static membersof typechar*.

— _end example_]

If a function declaration acquired its function type through a dependent type without using the syntactic form of a function declarator, the program is ill-formed.

[Example 3: template<class T> struct A { static T t;};typedef int function(); A<function> a; — _end example_]