Files
2025-10-25 03:02:53 +03:00

81 lines
3.8 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[temp.class.general]
# 13 Templates [[temp]](./#temp)
## 13.7 Template declarations [[temp.decls]](temp.decls#temp.class.general)
### 13.7.2 Class templates [[temp.class]](temp.class#general)
#### 13.7.2.1 General [temp.class.general]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L2650)
A[*class template*](#def:template,class "13.7.2.1General[temp.class.general]") defines the layout and operations
for an unbounded set of related types[.](#1.sentence-1)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L2656)
[*Example [1](#example-1)*:
It is possible for a single class templateList to provide an unbounded set of class definitions:
one class List<T> for every type T,
each describing a linked list of elements of type T[.](#2.sentence-1)
Similarly, a class template Array describing a contiguous,
dynamic array can be defined like this:template<class T> class Array { T* v; int sz;public:explicit Array(int);
T& operator[](int);
T& elem(int i) { return v[i]; }};
The prefix template<class T> specifies that a template is being declared and that a[*type-name*](dcl.type.simple#nt:type-name "9.2.9.3Simple type specifiers[dcl.type.simple]") T can be used in the declaration[.](#2.sentence-3)
In other words,Array is a parameterized type withT as its parameter[.](#2.sentence-4)
— *end example*]
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L2686)
[*Note [1](#note-1)*:
When a member of a class
template is defined outside of the class template definition,
the member definition is defined as a template definition with the[*template-head*](temp.pre#nt:template-head "13.1Preamble[temp.pre]") equivalent to that
of the class template[.](#3.sentence-1)
The names of the template parameters used in the definition of the member
can differ from the template parameter names used in the class
template definition[.](#3.sentence-2)
The class template name in the member definition is followed by
the template argument list of the [*template-head*](temp.pre#nt:template-head "13.1Preamble[temp.pre]") ([[temp.arg]](temp.arg "13.4Template arguments"))[.](#3.sentence-3)
[*Example [2](#example-2)*: template<class T1, class T2> struct A {void f1(); void f2();};
template<class T2, class T1> void A<T2,T1>::f1() { } // OKtemplate<class T2, class T1> void A<T1,T2>::f2() { } // error
template<class ... Types> struct B {void f3(); void f4();};
template<class ... Types> void B<Types ...>::f3() { } // OKtemplate<class ... Types> void B<Types>::f4() { } // error
template<typename T> concept C = true;template<typename T> concept D = true;
template<C T> struct S {void f(); void g(); void h(); template<D U> struct Inner;};
template<C A> void S<A>::f() { } // OK, [*template-head*](temp.pre#nt:template-head "13.1Preamble[temp.pre]")*s* matchtemplate<typename T> void S<T>::g() { } // error: no matching declaration for S<T>template<typename T> requires C<T> // ill-formed, no diagnostic required: [*template-head*](temp.pre#nt:template-head "13.1Preamble[temp.pre]")*s* arevoid S<T>::h() { } // functionally equivalent but not equivalenttemplate<C X> template<D Y>struct S<X>::Inner { }; // OK — *end example*]
— *end note*]
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/templates.tex#L2742)
In a partial specialization,
explicit specialization or
explicit instantiation of a class template,
the [*class-key*](class.pre#nt:class-key "11.1Preamble[class.pre]") shall agree in kind with the original class template declaration ([[dcl.type.elab]](dcl.type.elab "9.2.9.5Elaborated type specifiers"))[.](#4.sentence-1)