Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | language
(Undo revision 149773 by SebastianWilke (talk))
m (extend navbar, fmt.)
Line 1: Line 1:
 
{{title|Class template}}
 
{{title|Class template}}
{{cpp/language/templates/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|num=1|{{ttb|template}} {{ttb|<}} {{spar|parameter-list}} {{ttb|>}} {{spar|class-declaration}} }}
+
{{sdsc|num=1|{{ttb|template}} {{ttb|<}} {{spar|parameter-list}} {{ttb|>}} {{spar|class-declaration}}}}
 
{{sdsc|num=2|{{ttb|export}} {{ttb|template}} {{ttb|<}} {{spar|parameter-list}} {{ttb|>}} {{spar|class-declaration}}|notes={{mark until c++11|removed=yes}}}}
 
{{sdsc|num=2|{{ttb|export}} {{ttb|template}} {{ttb|<}} {{spar|parameter-list}} {{ttb|>}} {{spar|class-declaration}}|notes={{mark until c++11|removed=yes}}}}
 
{{sdsc end}}
 
{{sdsc end}}
Line 12: Line 12:
 
===Explanation===
 
===Explanation===
 
{{par begin}}
 
{{par begin}}
{{par | {{spar|class-declaration}} | a {{rlp|class|class declaration}}. The class name declared becomes a template name. }}
+
{{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|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 end}}
 
{{par end}}
  
 
{{rev begin}}
 
{{rev begin}}
{{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.
+
{{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.
 
}}
 
}}
 
{{rev end}}
 
{{rev end}}
Line 25: Line 25:
 
====Explicit instantiation====
 
====Explicit instantiation====
 
{{sdsc begin}}
 
{{sdsc begin}}
{{sdsc|num=1|{{ttb|template}} {{spar|class-key}} {{spar|template-name}} {{ttb|<}} {{spar|argument-list}} {{ttb|>}} {{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-key}} {{spar|template-name}} {{ttb|<}} {{spar|argument-list}} {{ttb|>}} {{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 begin}}
{{par | {{spar|class-key}} | {{ttb|class}}, {{ttb|struct}} or {{ttb|union}} }}
+
{{par|{{spar|class-key}}|{{ttb|class}}, {{ttb|struct}} or {{ttb|union}}}}
 
{{par end}}
 
{{par end}}
  
Line 42: Line 42:
 
}}
 
}}
  
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.
+
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.
  
 
Explicit instantiation can only appear in the enclosing namespace of the template, unless it uses qualified-id:
 
Explicit instantiation can only appear in the enclosing namespace of the template, unless it uses qualified-id:
 
{{source|1=
 
{{source|1=
namespace N  
+
namespace N
 
{
 
{
     template<class T>  
+
     template<class T>
 
     class Y // template definition
 
     class Y // template definition
     {  
+
     {
         void mf() {}  
+
         void mf() {}
     };  
+
     };
 
}
 
}
  
 
// template class Y<int>; // error: class template Y not visible in the global namespace
 
// template class Y<int>; // error: class template Y not visible in the global namespace
 
using N::Y;
 
using N::Y;
// template class Y<int>; // error: explicit instantiation outside  
+
// template class Y<int>; // error: explicit instantiation outside
 
                           // of the namespace of the template
 
                           // of the namespace of the template
 
template class N::Y<char*>;      // OK: explicit instantiation
 
template class N::Y<char*>;      // OK: explicit instantiation
Line 65: Line 65:
 
Explicit instantiation has no effect if an {{rlp|template specialization|explicit specialization}} appeared before for the same set of template arguments.
 
Explicit instantiation has no effect if an {{rlp|template specialization|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,{{rev inl|since=c++14| a variable template,}} a member function or static data member of a class template,  
+
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,
 
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
 
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, unless an explicit specialization with the same template arguments appeared before.
  
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.
+
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.
  
 
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.
 
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.
Line 81: Line 81:
  
 
{{source|1=
 
{{source|1=
template<class T>  
+
template<class T>
 
struct Z // template definition
 
struct Z // template definition
 
{
 
{
 
     void f() {}
 
     void f() {}
 
     void g(); // never defined
 
     void g(); // never defined
};  
+
};
  
 
template struct Z<double>; // explicit instantiation of Z<double>
 
template struct Z<double>; // explicit instantiation of Z<double>
Line 99: Line 99:
 
If a class template has been declared, but not defined, at the point of instantiation, the instantiation yields an incomplete class type:
 
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=
template<class T>  
+
template<class T>
 
class X;    // declaration, not definition
 
class X;    // declaration, not definition
  
Line 107: Line 107:
 
{{rev begin}}
 
{{rev begin}}
 
{{rev|since=c++17|
 
{{rev|since=c++17|
{{rlp|class#Local_classes|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.}}
+
{{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}}
 
{{rev end}}
  
 
===See also===
 
===See also===
* {{rlp|template_parameters | template parameters and arguments}} allow templates to be parameterized
+
* {{rlp|template_parameters|template parameters and arguments}} allow templates to be parameterized
* {{rlp|function_template | function template declaration}} declares a function template
+
* {{rlp|function_template|function template declaration}} declares a function template
* {{rlp|template_specialization | template specialization}} defines an existing template for a specific type
+
* {{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}}
+
* {{rlp|parameter_pack|parameter packs}} allows the use of lists of types in templates {{mark since c++11}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Revision as of 20:35, 29 December 2023

 
 
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

Syntax

template < parameter-list > class-declaration (1)
export template < parameter-list > class-declaration (2) (removed in C++11)

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

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

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.

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)

See also