Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/class template"

From cppreference.com
< cpp‎ | language
(added missing lcass keyword in template-template-param, and addressed some of the todos)
m (Undo revision 175084 by Xmcgcg (talk); ongoing vandalisms)
 
(39 intermediate revisions by 17 users not shown)
Line 1: Line 1:
 
{{title|Class template}}
 
{{title|Class template}}
{{cpp/language/navbar}}
+
{{cpp/language/declarations/expressions/templates/navbar}}
  
 
A class template defines a family of classes.
 
A class template defines a family of classes.
Line 6: Line 6:
 
===Syntax===
 
===Syntax===
 
{{sdsc begin}}
 
{{sdsc begin}}
{{sdsc|{{ttb|template}} {{ttb|<}} {{spar|parameter-list}} {{ttb|>}} {{spar|declaration}} }}
+
{{sdsc|num=1|{{ttb|template}} {{ttb|<}} {{spar|parameter-list}} {{ttb|>}} {{spar|class-declaration}}}}
 +
{{sdsc|num=2|notes={{mark since c++20}}|{{ttb|template}} {{ttb|<}} {{spar|parameter-list}} {{ttb|>}} {{ttb|requires}} {{spar|constraint}} {{spar|class-declaration}}}}
 +
{{sdsc|num=3|{{ttb|export}} {{ttb|template}} {{ttb|<}} {{spar|parameter-list}} {{ttb|>}} {{spar|class-declaration}}|notes={{mark until c++11|removed=yes}}}}
 
{{sdsc end}}
 
{{sdsc end}}
  
 
===Explanation===
 
===Explanation===
{{spar|declaration}} defines or declares a class (including struct and union), a member class or member enumeration type, a [[cpp/language/function_template|function or member function]], a static data member of a class template, or a [[cpp/language/type_alias|type alias]]. It may also define a [[cpp/language/template_specialization|template specialization]]. This page focuses on class templates.
+
{{par begin}}
 +
{{par|{{spar|class-declaration}}|a {{rlp|class|class declaration}}. The class name declared becomes a template name.}}
 +
{{par|{{spar|parameter-list}}|a non-empty comma-separated list of the {{rlp|template parameters}}, each of which is either a [[cpp/language/template parameters#Non-type template parameter|non-type parameter]], a [[cpp/language/template parameters#Type template parameter|type parameter]], a [[cpp/language/template parameters#Template template parameter|template parameter]], or a [[cpp/language/parameter pack|parameter pack]] of any of those.}}
 +
{{par|{{spar|constraint}}|a {{rlp|constraints|constraint expression}} which restricts the template parameters accepted by this class template}}
 +
{{par end}}
  
{{spar|parameter-list}} is a non-empty comma-separated list of the template parameters, each of which is either non-type parameter, a type parameter, a template parameter, or a [[cpp/language/parameter_pack|parameter pack]] of any of those. This page focuses on the parameters that are not parameter packs.
+
{{rev begin}}
====Non-type template parameter====
+
{{rev|until=c++11|{{tt|export}} was an optional modifier which declared the template as ''exported'' (when used with a class template, it declared all of its members exported as well). Files that instantiated exported templates did not need to include their definitions: the declaration was sufficient. Implementations of {{tt|export}} were rare and disagreed with each other on details.
{{sdsc begin}}
+
{{sdsc|num=1|{{spar|type}} {{spar|name}}{{mark optional}} }}
+
{{sdsc|num=2|{{spar|type}} {{spar|name}}{{mark optional}} {{ttb|{{=}} }} {{spar|default}} }}
+
{{sdsc|num=3|{{spar|type}} {{ttb|...}} {{spar|name}}{{mark optional}} | notes={{mark since c++11}}}}
+
{{sdsc end}}
+
@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 [[cpp/language/parameter_pack|parameter pack]] with an optional name
+
 
+
{{spar|type}} is one of the following types (optionally cv-qualified, the qualifiers are ignored)
+
* integral type
+
* enumeration
+
* pointer to object or to function
+
* lvalue reference to object or to function
+
* pointer to member object or to member function
+
* {{lc|std::nullptr_t}} {{mark since c++11}}
+
 
+
Array and function types may be written in a template declaration, but they are automatically replaced by pointer to data 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 prvalue unless its type was an lvalue reference type.
+
 
+
====Type template parameter====
+
{{sdsc begin}}
+
{{sdsc|num=1|{{ttb|typename}} {{spar|name}}{{mark optional}} }}
+
{{sdsc|num=2|{{ttb|class}} {{spar|name}}{{mark optional}} }}
+
{{sdsc|num=3|{{ttb|typename{{!}}class}} {{spar|name}}{{mark optional}} {{ttb|{{=}} }} {{spar|default}}  }}
+
{{sdsc|num=4|{{ttb|typename{{!}}class}} {{ttb|...}} {{spar|name}}{{mark optional}} | notes={{mark since c++11}}}}
+
{{sdsc end}}
+
@1@ A type template parameter with an optional name
+
@2@ Exactly the same as 1)
+
@3@ A type template parameter with an optional name and a default
+
@5@ A type template [[cpp/language/parameter_pack|parameter pack]] with an optional name
+
 
+
A type template parameter declares its identifier to be a type alias to some type that will be provided when the template is instantiated.
+
 
+
There is no difference between the keywords {{ttb|class}} and {{ttb|typename}} in a type template parameter declaration.
+
 
+
====Template template parameter====
+
{{sdsc begin}}
+
{{sdsc|num=1|{{ttb|template}} {{ttb|<}} {{spar|parameter-list}} {{ttb|>}} {{ttb|class}} {{spar|name}}{{mark optional}} }}
+
{{sdsc|num=2|{{ttb|template}} {{ttb|<}} {{spar|parameter-list}} {{ttb|>}} {{ttb|class}} {{spar|name}}{{mark optional}} {{ttb|{{=}}}} {{spar|default}} }}
+
{{sdsc|num=3|{{ttb|template}} {{ttb|<}} {{spar|parameter-list}} {{ttb|>}} {{ttb|class}} {{ttb|...}} {{spar|name}}{{mark optional}} | notes={{mark since c++11}}}}
+
{{sdsc end}}
+
@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 [[cpp/language/parameter_pack|parameter pack]] with an optional name
+
 
+
Unlike type template parameter declaration, template template parameter declaration can only use the keyword {{ttb|class}} and not {{ttb|typename}}.
+
 
+
{{source|1=
+
template<class T> class myarray {};
+
 
+
// two type template parameters and one template template parameter:
+
template<class K, class V, template<typename> class C = myarray>
+
class Map {
+
  C<K> key;
+
  C<V> value;
+
};
+
 
}}
 
}}
 +
{{rev end}}
  
 
===Class template instantiation===
 
===Class template instantiation===
Line 78: Line 27:
 
====Explicit instantiation====
 
====Explicit instantiation====
 
{{sdsc begin}}
 
{{sdsc begin}}
{{sdsc|num=1|{{ttb|template}} {{spar|class}} {{spar|name}} {{ttb|<}} {{spar|argument-list}} {{ttb|> ;}} }}
+
{{sdsc|num=1|{{ttb|template}} {{spar|class-key}} {{spar|template-name}} {{ttb|<}} {{spar|argument-list}} {{ttb|>}} {{ttb|;}}}}
{{sdsc|num=2|{{ttb|extern}} {{ttb|template}} {{spar|class}} {{spar|name}} {{ttb|<}} {{spar|argument-list}} {{ttb|> ;}}|notes={{mark since c++11}}}}
+
{{sdsc|num=2|{{ttb|extern}} {{ttb|template}} {{spar|class-key}} {{spar|template-name}} {{ttb|<}} {{spar|argument-list}} {{ttb|>}} {{ttb|;}}|notes={{mark since c++11}}}}
 
{{sdsc end}}
 
{{sdsc end}}
 +
 +
{{par begin}}
 +
{{par|{{spar|class-key}}|{{ttb|class}}, {{ttb|struct}} or {{ttb|union}}}}
 +
{{par end}}
 +
 
@1@ Explicit instantiation definition
 
@1@ Explicit instantiation definition
 
@2@ Explicit instantiation declaration
 
@2@ Explicit instantiation declaration
  
An explicit instantiation definition forces instantiation of the class, struct, or union they refer to. It may appear in the program anywhere after the template definition, and for a given argument-list, is only allowed to appear once in the program.
+
An explicit instantiation definition forces instantiation of the class, struct, or union they refer to. It may appear in the program anywhere after the template definition, and for a given {{spar|argument-list}}, is only allowed to appear once in the entire program, no diagnostic required.
  
An explicit instantiation declaration (an extern template) prevents implicit instantiations: the code that would otherwise cause an implicit instantiation has to use the explicit instantiation definition provided somewhere else in the program.
+
{{rrev|since=c++11|
 
+
An explicit instantiation declaration (an extern template) skips implicit instantiation step: the code that would otherwise cause an implicit instantiation instead uses the explicit instantiation definition provided elsewhere (resulting in link errors if no such instantiation exists). This can be used to reduce compilation times by explicitly declaring a template instantiation in all but one of the source files using it, and explicitly defining it in the remaining file.
====Implicit instantiation====
+
When code refers to a template in context that requires a completely defined type, or when the completeness of the type affects the code, and this particular type has not been explicitly instantiated, implicit instantiation occurs. For example, when an object of this type is constructed, but not when a pointer to this type is constructed.
+
 
+
This applies to the members of the class template: unless the member is used in the program, it is not instantiated, and does not require a definition.
+
 
+
{{c|
+
template<class T> struct Z {
+
    void f() {}
+
    void g(); // never defined
+
}; // template definition
+
template struct Z<double>; // explicit instantiation of Z<double>
+
Z<int> a; // implicit instantiation of Z<int>
+
Z<char>* p; // nothing is instantiated here
+
p->f(); // implicit instantiation of Z<char> and Z<char>::f() occurs here.
+
// Z<char>::g() is never needed and never instantiated: it does not have to be defined
+
 
}}
 
}}
  
====Template Non-type arguments====
+
Classes, functions{{rev inl|since=c++14|, variables}}, and member template specializations can be explicitly instantiated from their templates. Member functions, member classes, and static data members of class templates can be explicitly instantiated from their member definitions.
The following limitations apply when instantiating class templates that have non-type template parameters:
+
* For integral and arithmetic types, the template argument provided during instantiation must be a {{rlp|constexpr|converted constant expression}} of the template parameter's type.
+
* For pointers to objects, the template arguments have to designate the address of an object with static storage duration and a linkage (either internal or external), or a constant expression that evaluates to the appropriate null pointer value.
+
* For pointers to functions, the valid arguments are pointers to functions with linkage (or constant expressions that evaluate to null pointer values).
+
* For lvalue reference parameters, the argument provided at instantiation cannot be a temporary, an unnamed lvalue, or a named lvalue with no linkage.
+
* For pointers to members, the argument has to be a pointer to member expressed as {{c|&Class::Member}} or a constant expression that evaluates to null pointer value.
+
  
In particular, this implies that string literals, addresses of array elements, and addresses of non-static members cannot be used as template arguments to instantiate templates whose corresponding non-type template parameters are pointers to data.
+
Explicit instantiation can only appear in the enclosing namespace of the template, unless it uses qualified-id:
 
+
{{source|1=
====Template type arguments====
+
namespace N
A template argument for a type template parameter must be a {{spar|type-id}}, which may name an incomplete type:
+
{
{{source|
+
    template<class T>
template <class T> class X { }; // class template
+
    class Y // template definition
 
+
    {
struct A; // incomplete type
+
        void mf() {}
typedef struct {} B; // type alias to an unnamed type
+
    };
int main() {
+
  X<A> x1; // OK, 'A' names a type
+
  X<A*> x2; // OK, 'A*' names a type
+
  X<B> x3; // OK, 'B' names a type
+
 
}
 
}
}}
 
  
====Template template arguments====
+
// template class Y<int>; // error: class template Y not visible in the global namespace
A template argument for a template template parameter must be an {{spar|id-expression}} which names a class template or a template alias.
+
using N::Y;
 
+
// template class Y<int>; // error: explicit instantiation outside
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.
+
                          // of the namespace of the template
 
+
template class N::Y<char*>;       // OK: explicit instantiation
{{source|1=
+
template void N::Y<double>::mf(); // OK: explicit instantiation
template<class T> class A { // primary template
+
    int x;
+
};
+
template<class T> class A<T*> { // partial specialization
+
    long x;
+
};
+
 
+
// class template with a template template parameter V
+
template< template<typename> class V> class C {
+
    V<int> y; // uses the primary template
+
    V<int*> z; // uses the partial specialization
+
};
+
 
+
C<A> c; // c.y.x has type int, c.z.x has type long
+
 
}}
 
}}
  
To match a template template argument {{tt|A}} to a template template parameter {{tt|P}}, each of the template parameters of {{tt|A}} must match corresponding template parameters of {{tt|P}}. If {{tt|P}}'s parameter list includes a {{rlp|parameter_pack|parameter pack}}, zero or more template parameters (or parameter packs) from {{tt|A}}'s template parameter list are matched by it.
+
Explicit instantiation has no effect if an {{rlp|template specialization|explicit specialization}} appeared before for the same set of template arguments.
  
{{source|1=
+
Only the declaration is required to be visible when explicitly instantiating a function template{{rev inl|since=c++14|, a variable template}}, a member function or static data member of a class template,
template<class T> struct eval; // primary template  
+
or a member function template. The complete definition must appear before the explicit instantiation of a class template, a member class of a class template, or a member class
 +
template, unless an explicit specialization with the same template arguments appeared before.
  
template< template<class, class...> class TT, class T1, class... Rest>
+
If a function template{{rev inl|since=c++14|, variable template}}, member function template, or member function or static data member of a class template is explicitly instantiated with an explicit instantiation definition, the template definition must be present in the same translation unit.
struct eval<TT<T1, Rest...>> {}; // partial specialization of eval
+
  
template <class T1> struct A;
+
When an explicit instantiation names a class template specialization, it serves as an explicit instantiation of the same kind (declaration or definition) of each of its non-inherited non-template members that has not been previously explicitly specialized in the translation unit. If this explicit instantiation is a definition, it is also an explicit instantiation definition only for the members that have been defined at this point.
template <class T1, class T2> struct B;
+
template <int N> struct C;
+
template <class T1, int N> struct D;
+
template <class T1, class T2, int N = 17> struct E;
+
  
eval<A<int>> eA; // OK: matches partial specialization of eval
+
Explicit instantiation definitions ignore member access specifiers: parameter types and return types may be private.
eval<B<int, float>> eB; // OK: matches partial specialization of eval
+
eval<C<17>> eC; // error: C does not match TT in partial specialization because
+
                // TT's first parameter is a type template parameter,
+
                // while 17 does not name a type
+
eval<D<int, 17>> eD; // error: D does not match TT in partial specialization
+
                    // because TT's second parameter is a type parameter pack,
+
                    // while 17 does not name a type
+
eval<E<int, float>> eE; // error: E does not match TT in partial specialization
+
                        // because E's third (default) parameter is a non-type
+
}}
+
  
====Default template arguments====
+
====Implicit instantiation====
Default template arguments are specified in the parameter lists after the {{c|{{=}}}} sign. Defaults can be specified for any kind of template parameter (type, non-type, or template), but not to parameter packs.
+
When code refers to a template in context that requires a completely defined type, or when the completeness of the type affects the code, and this particular type has not been explicitly instantiated, implicit instantiation occurs. For example, when an object of this type is constructed, but not when a pointer to this type is constructed.
  
If the default is specified for a template parameter of a primary class template, each subsequent template parameter must have a default argument, except the very last one may be a template parameter pack. In a function template, a parameter pack may be followed by more type parameters only if they have defaults or can be deduced from the function arguments.
+
This applies to the members of the class template: unless the member is used in the program, it is not instantiated, and does not require a definition.
  
Default parameters are not allowed
+
{{source|1=
* in the out-of-class definition of a member template (they have to be provided in the declaration inside the class body)
+
template<class T>
* in friend class template declarations
+
struct Z // template definition
 +
{
 +
    void f() {}
 +
    void g(); // never defined
 +
};
  
On a friend function template declaration, default template arguments are allowed only if the declaration is a definition, and no other declarations of this function appear in this translation unit.
+
template struct Z<double>; // explicit instantiation of Z<double>
 +
Z<int> a;                  // implicit instantiation of Z<int>
 +
Z<char>* p;                // nothing is instantiated here
  
Default template arguments that appear in the declarations and the definition are merged similarly to default function arguments:
+
p->f(); // implicit instantiation of Z<char> and Z<char>::f() occurs here.
{{source|1=
+
        // Z<char>::g() is never needed and never instantiated:
template<class T1, class T2 = int> class A;
+
        // it does not have to be defined
template<class T1 = int, class T2> class A;
+
// the above is the same as the following:
+
template<class T1 = int, class T2 = int> class A;
+
}}
+
But the same parameter cannot be given default arguments twice in the same scope
+
{{source|1=
+
template<class T = int> class X;
+
template<class T = int> class X { /�... �/ }; // error
+
 
}}
 
}}
  
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:
+
If a class template has been declared, but not defined, at the point of instantiation, the instantiation yields an incomplete class type:
 
+
 
{{source|1=
 
{{source|1=
// class template, with a type template parameter with a default
+
template<class T>
template <class T = float> struct B {};
+
class X;   // declaration, not definition
  
// template template parameter T has a parameter list, which
+
X<char> ch; // error: incomplete type X<char>
// consists of one type template parameter with a default
+
template <template <class = float> class T> struct A {
+
    void f();
+
    void g();
+
};
+
// out-of-body member function template definitions
+
template <template <class TT> class T>
+
void A<T>::f() {
+
    T<> t; // error - TT has no default in scope
+
}
+
template <template <class TT = char> class T>
+
void A<T>::g() {
+
    T<> t; // OK, t is T<char>
+
}
+
 
}}
 
}}
  
 +
{{rev begin}}
 +
{{rev|since=c++17|
 +
{{rlps|class#Local classes}} and any templates used in their members are instantiated as part of the instantiation of the entity within which the local class or enumeration is declared.}}
 +
{{rev end}}
  
 
+
===Keywords===
===Examples===
+
{{rev inl|until=c++11|{{ltt|cpp/keyword/export}}}}{{rev inl|since=c++11|{{ltt|cpp/keyword/extern}}}}
====Non-type template parameters====
+
{{example
+
|code=
+
#include <iostream>
+
 
+
// simple non-type template parameter
+
template<int N>
+
struct S {
+
    int a[N];
+
};
+
 
+
template<const char*>
+
struct S2 {};
+
 
+
// complicated non-type example
+
template <
+
    char c, // integral type
+
    int (&ra)[5], // lvalue reference to object (of array type)
+
    int (*pf)(int), // pointer to function
+
    int (S<10>::*a)[10] // pointer to member object (of type int[10])
+
> struct Complicated {
+
    // calls the function selected at compile time
+
    // and stores the result in the array selected at compile time
+
    void foo(char base) {
+
        ra[4] = pf(c - base);
+
    }
+
};
+
 
+
//  S2<"fail"> s2; // Error: string literal cannot be used
+
char okay[] = "okay"; // static object with linkage
+
// S2< &okay[0] > s2; // Error: array element has no linkage
+
S2<okay> s2; // works
+
 
+
int a[5];
+
int f(int n) { return n;}
+
int main()
+
{
+
    S<10> s; // s.a is an array of 10 int
+
    s.a[9] = 4;
+
 
+
    Complicated<'2', a, f, &S<10>::a> c;
+
    c.foo('0');
+
 
+
    std::cout << s.a[9] << a[4] << '\n';
+
}
+
|output=
+
42
+
}}
+
  
 
===See also===
 
===See also===
* {{rlp|function_template | function template declaration}} declares a function template
+
* {{rlp|template_parameters|template parameters and arguments}} allow templates to be parameterized
* {{rlp|template_specialization | template specialization}} defines an existing template for a specific type
+
* {{rlp|function_template|function template declaration}} declares a function template
* {{rlp|parameter_pack | parameter packs}} allows the use of lists of types in templates {{mark since c++11}}
+
* {{rlp|template_specialization|template specialization}} defines an existing template for a specific type
 +
* {{rlp|parameter_pack|parameter packs}} allows the use of lists of types in templates {{mark since c++11}}
  
[[de:cpp/language/class template]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/language/class template]]
+
[[fr:cpp/language/class template]]
+
[[it:cpp/language/class template]]
+
[[ja:cpp/language/class template]]
+
[[pt:cpp/language/class template]]
+
[[ru:cpp/language/class template]]
+
[[zh:cpp/language/class template]]
+

Latest revision as of 02:55, 14 August 2024

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Class template
Function template
Miscellaneous
 
 
 
 

A class template defines a family of classes.

Contents

[edit] Syntax

template < parameter-list > class-declaration (1)
template < parameter-list > requires constraint class-declaration (2) (since C++20)
export template < parameter-list > class-declaration (3) (removed in C++11)

[edit] Explanation

class-declaration - a class declaration. The class name declared becomes a template name.
parameter-list - a non-empty comma-separated list of the template parameters, each of which is either a non-type parameter, a type parameter, a template parameter, or a parameter pack of any of those.
constraint - a constraint expression which restricts the template parameters accepted by this class template
export was an optional modifier which declared the template as exported (when used with a class template, it declared all of its members exported as well). Files that instantiated exported templates did not need to include their definitions: the declaration was sufficient. Implementations of export were rare and disagreed with each other on details. (until C++11)

[edit] Class template instantiation

A class template by itself is not a type, or an object, or any other entity. No code is generated from a source file that contains only template definitions. In order for any code to appear, a template must be instantiated: the template arguments must be provided so that the compiler can generate an actual class (or function, from a function template).

[edit] Explicit instantiation

template class-key template-name < argument-list > ; (1)
extern template class-key template-name < argument-list > ; (2) (since C++11)
class-key - class, struct or union
1) Explicit instantiation definition
2) Explicit instantiation declaration

An explicit instantiation definition forces instantiation of the class, struct, or union they refer to. It may appear in the program anywhere after the template definition, and for a given argument-list, is only allowed to appear once in the entire program, no diagnostic required.

An explicit instantiation declaration (an extern template) skips implicit instantiation step: the code that would otherwise cause an implicit instantiation instead uses the explicit instantiation definition provided elsewhere (resulting in link errors if no such instantiation exists). This can be used to reduce compilation times by explicitly declaring a template instantiation in all but one of the source files using it, and explicitly defining it in the remaining file.

(since C++11)

Classes, functions, variables(since C++14), and member template specializations can be explicitly instantiated from their templates. Member functions, member classes, and static data members of class templates can be explicitly instantiated from their member definitions.

Explicit instantiation can only appear in the enclosing namespace of the template, unless it uses qualified-id:

namespace N
{
    template<class T>
    class Y // template definition
    {
        void mf() {}
    };
}
 
// template class Y<int>; // error: class template Y not visible in the global namespace
using N::Y;
// template class Y<int>; // error: explicit instantiation outside
                          // of the namespace of the template
template class N::Y<char*>;       // OK: explicit instantiation
template void N::Y<double>::mf(); // OK: explicit instantiation

Explicit instantiation has no effect if an explicit specialization appeared before for the same set of template arguments.

Only the declaration is required to be visible when explicitly instantiating a function template, a variable template(since C++14), a member function or static data member of a class template, or a member function template. The complete definition must appear before the explicit instantiation of a class template, a member class of a class template, or a member class template, unless an explicit specialization with the same template arguments appeared before.

If a function template, variable template(since C++14), member function template, or member function or static data member of a class template is explicitly instantiated with an explicit instantiation definition, the template definition must be present in the same translation unit.

When an explicit instantiation names a class template specialization, it serves as an explicit instantiation of the same kind (declaration or definition) of each of its non-inherited non-template members that has not been previously explicitly specialized in the translation unit. If this explicit instantiation is a definition, it is also an explicit instantiation definition only for the members that have been defined at this point.

Explicit instantiation definitions ignore member access specifiers: parameter types and return types may be private.

[edit] Implicit instantiation

When code refers to a template in context that requires a completely defined type, or when the completeness of the type affects the code, and this particular type has not been explicitly instantiated, implicit instantiation occurs. For example, when an object of this type is constructed, but not when a pointer to this type is constructed.

This applies to the members of the class template: unless the member is used in the program, it is not instantiated, and does not require a definition.

template<class T>
struct Z // template definition
{
    void f() {}
    void g(); // never defined
};
 
template struct Z<double>; // explicit instantiation of Z<double>
Z<int> a;                  // implicit instantiation of Z<int>
Z<char>* p;                // nothing is instantiated here
 
p->f(); // implicit instantiation of Z<char> and Z<char>::f() occurs here.
        // Z<char>::g() is never needed and never instantiated:
        // it does not have to be defined

If a class template has been declared, but not defined, at the point of instantiation, the instantiation yields an incomplete class type:

template<class T>
class X;    // declaration, not definition
 
X<char> ch; // error: incomplete type X<char>
Local classes and any templates used in their members are instantiated as part of the instantiation of the entity within which the local class or enumeration is declared. (since C++17)

[edit] Keywords

export(until C++11)extern(since C++11)

[edit] See also