tmp/tmpl2ya5yh0/{from.md → to.md}
RENAMED
|
@@ -1,16 +1,19 @@
|
|
| 1 |
### Class templates <a id="temp.class">[[temp.class]]</a>
|
| 2 |
|
|
|
|
|
|
|
| 3 |
A *class template* defines the layout and operations for an unbounded
|
| 4 |
set of related types.
|
| 5 |
|
| 6 |
[*Example 1*:
|
| 7 |
|
| 8 |
-
|
| 9 |
-
definitions: one class `List<T>` for every type
|
| 10 |
-
linked list of elements of type `T`. Similarly, a
|
| 11 |
-
describing a contiguous, dynamic array
|
|
|
|
| 12 |
|
| 13 |
``` cpp
|
| 14 |
template<class T> class Array {
|
| 15 |
T* v;
|
| 16 |
int sz;
|
|
@@ -20,26 +23,25 @@ public:
|
|
| 20 |
T& elem(int i) { return v[i]; }
|
| 21 |
};
|
| 22 |
```
|
| 23 |
|
| 24 |
The prefix `template<class T>` specifies that a template is being
|
| 25 |
-
declared and that a *type-name* `T`
|
| 26 |
other words, `Array` is a parameterized type with `T` as its parameter.
|
| 27 |
|
| 28 |
— *end example*]
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
of
|
| 33 |
-
template definition
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
the template parameter names used in the
|
| 37 |
-
template
|
| 38 |
-
definition
|
| 39 |
-
|
| 40 |
-
pack shall be expanded with an ellipsis in the template argument list.
|
| 41 |
|
| 42 |
[*Example 2*:
|
| 43 |
|
| 44 |
``` cpp
|
| 45 |
template<class T1, class T2> struct A {
|
|
@@ -70,11 +72,11 @@ template<C T> struct S {
|
|
| 70 |
void g();
|
| 71 |
void h();
|
| 72 |
template<D U> struct Inner;
|
| 73 |
};
|
| 74 |
|
| 75 |
-
template<C A> void S<A>::f() { } // OK
|
| 76 |
template<typename T> void S<T>::g() { } // error: no matching declaration for S<T>
|
| 77 |
|
| 78 |
template<typename T> requires C<T> // ill-formed, no diagnostic required: template-head{s} are
|
| 79 |
void S<T>::h() { } // functionally equivalent but not equivalent
|
| 80 |
|
|
@@ -82,13 +84,15 @@ template<C X> template<D Y>
|
|
| 82 |
struct S<X>::Inner { }; // OK
|
| 83 |
```
|
| 84 |
|
| 85 |
— *end example*]
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
| 90 |
|
| 91 |
#### Member functions of class templates <a id="temp.mem.func">[[temp.mem.func]]</a>
|
| 92 |
|
| 93 |
A member function of a class template may be defined outside of the
|
| 94 |
class template definition in which it is declared.
|
|
@@ -105,11 +109,11 @@ public:
|
|
| 105 |
T& elem(int i) { return v[i]; }
|
| 106 |
};
|
| 107 |
```
|
| 108 |
|
| 109 |
declares three member functions of a class template. The subscript
|
| 110 |
-
function
|
| 111 |
|
| 112 |
``` cpp
|
| 113 |
template<class T> T& Array<T>::operator[](int i) {
|
| 114 |
if (i<0 || sz<=i) error("Array: range error");
|
| 115 |
return v[i];
|
|
@@ -158,12 +162,12 @@ v2[3] = dcomplex(7,8); // Array<dcomplex>::operator[]
|
|
| 158 |
#### Deduction guides <a id="temp.deduct.guide">[[temp.deduct.guide]]</a>
|
| 159 |
|
| 160 |
Deduction guides are used when a *template-name* appears as a type
|
| 161 |
specifier for a deduced class type [[dcl.type.class.deduct]]. Deduction
|
| 162 |
guides are not found by name lookup. Instead, when performing class
|
| 163 |
-
template argument deduction [[over.match.class.deduct]],
|
| 164 |
-
guides declared for the class template are considered.
|
| 165 |
|
| 166 |
``` bnf
|
| 167 |
deduction-guide:
|
| 168 |
explicit-specifierₒₚₜ template-name '(' parameter-declaration-clause ')' '->' simple-template-id ';'
|
| 169 |
```
|
|
@@ -189,15 +193,15 @@ S x{A()}; // x is of type S<short, int>
|
|
| 189 |
|
| 190 |
The same restrictions apply to the *parameter-declaration-clause* of a
|
| 191 |
deduction guide as in a function declaration [[dcl.fct]]. The
|
| 192 |
*simple-template-id* shall name a class template specialization. The
|
| 193 |
*template-name* shall be the same *identifier* as the *template-name* of
|
| 194 |
-
the *simple-template-id*. A *deduction-guide* shall
|
| 195 |
-
|
| 196 |
-
template,
|
| 197 |
-
same
|
| 198 |
-
|
| 199 |
|
| 200 |
#### Member classes of class templates <a id="temp.mem.class">[[temp.mem.class]]</a>
|
| 201 |
|
| 202 |
A member class of a class template may be defined outside the class
|
| 203 |
template definition in which it is declared.
|
|
@@ -209,13 +213,13 @@ instantiation [[temp.inst]]. For example,
|
|
| 209 |
|
| 210 |
``` cpp
|
| 211 |
template<class T> struct A {
|
| 212 |
class B;
|
| 213 |
};
|
| 214 |
-
A<int>::B* b1; // OK
|
| 215 |
template<class T> class A<T>::B { };
|
| 216 |
-
A<int>::B b2; // OK
|
| 217 |
```
|
| 218 |
|
| 219 |
— *end note*]
|
| 220 |
|
| 221 |
#### Static data members of class templates <a id="temp.static">[[temp.static]]</a>
|
|
@@ -251,11 +255,11 @@ of unknown bound can have a different bound from its definition, if any.
|
|
| 251 |
``` cpp
|
| 252 |
template <class T> struct A {
|
| 253 |
static int i[];
|
| 254 |
};
|
| 255 |
template <class T> int A<T>::i[4]; // 4 elements
|
| 256 |
-
template <> int A<int>::i[] = { 1 }; // OK
|
| 257 |
```
|
| 258 |
|
| 259 |
— *end example*]
|
| 260 |
|
| 261 |
#### Enumeration members of class templates <a id="temp.mem.enum">[[temp.mem.enum]]</a>
|
|
|
|
| 1 |
### Class templates <a id="temp.class">[[temp.class]]</a>
|
| 2 |
|
| 3 |
+
#### General <a id="temp.class.general">[[temp.class.general]]</a>
|
| 4 |
+
|
| 5 |
A *class template* defines the layout and operations for an unbounded
|
| 6 |
set of related types.
|
| 7 |
|
| 8 |
[*Example 1*:
|
| 9 |
|
| 10 |
+
It is possible for a single class template `List` to provide an
|
| 11 |
+
unbounded set of class definitions: one class `List<T>` for every type
|
| 12 |
+
`T`, each describing a linked list of elements of type `T`. Similarly, a
|
| 13 |
+
class template `Array` describing a contiguous, dynamic array can be
|
| 14 |
+
defined like this:
|
| 15 |
|
| 16 |
``` cpp
|
| 17 |
template<class T> class Array {
|
| 18 |
T* v;
|
| 19 |
int sz;
|
|
|
|
| 23 |
T& elem(int i) { return v[i]; }
|
| 24 |
};
|
| 25 |
```
|
| 26 |
|
| 27 |
The prefix `template<class T>` specifies that a template is being
|
| 28 |
+
declared and that a *type-name* `T` can be used in the declaration. In
|
| 29 |
other words, `Array` is a parameterized type with `T` as its parameter.
|
| 30 |
|
| 31 |
— *end example*]
|
| 32 |
|
| 33 |
+
[*Note 1*:
|
| 34 |
+
|
| 35 |
+
When a member of a class template is defined outside of the class
|
| 36 |
+
template definition, the member definition is defined as a template
|
| 37 |
+
definition with the *template-head* equivalent to that of the class
|
| 38 |
+
template. The names of the template parameters used in the definition of
|
| 39 |
+
the member can differ from the template parameter names used in the
|
| 40 |
+
class template definition. The class template name in the member
|
| 41 |
+
definition is followed by the template argument list of the
|
| 42 |
+
*template-head* [[temp.arg]].
|
|
|
|
| 43 |
|
| 44 |
[*Example 2*:
|
| 45 |
|
| 46 |
``` cpp
|
| 47 |
template<class T1, class T2> struct A {
|
|
|
|
| 72 |
void g();
|
| 73 |
void h();
|
| 74 |
template<D U> struct Inner;
|
| 75 |
};
|
| 76 |
|
| 77 |
+
template<C A> void S<A>::f() { } // OK, template-head{s} match
|
| 78 |
template<typename T> void S<T>::g() { } // error: no matching declaration for S<T>
|
| 79 |
|
| 80 |
template<typename T> requires C<T> // ill-formed, no diagnostic required: template-head{s} are
|
| 81 |
void S<T>::h() { } // functionally equivalent but not equivalent
|
| 82 |
|
|
|
|
| 84 |
struct S<X>::Inner { }; // OK
|
| 85 |
```
|
| 86 |
|
| 87 |
— *end example*]
|
| 88 |
|
| 89 |
+
— *end note*]
|
| 90 |
+
|
| 91 |
+
In a partial specialization, explicit specialization or explicit
|
| 92 |
+
instantiation of a class template, the *class-key* shall agree in kind
|
| 93 |
+
with the original class template declaration [[dcl.type.elab]].
|
| 94 |
|
| 95 |
#### Member functions of class templates <a id="temp.mem.func">[[temp.mem.func]]</a>
|
| 96 |
|
| 97 |
A member function of a class template may be defined outside of the
|
| 98 |
class template definition in which it is declared.
|
|
|
|
| 109 |
T& elem(int i) { return v[i]; }
|
| 110 |
};
|
| 111 |
```
|
| 112 |
|
| 113 |
declares three member functions of a class template. The subscript
|
| 114 |
+
function can be defined like this:
|
| 115 |
|
| 116 |
``` cpp
|
| 117 |
template<class T> T& Array<T>::operator[](int i) {
|
| 118 |
if (i<0 || sz<=i) error("Array: range error");
|
| 119 |
return v[i];
|
|
|
|
| 162 |
#### Deduction guides <a id="temp.deduct.guide">[[temp.deduct.guide]]</a>
|
| 163 |
|
| 164 |
Deduction guides are used when a *template-name* appears as a type
|
| 165 |
specifier for a deduced class type [[dcl.type.class.deduct]]. Deduction
|
| 166 |
guides are not found by name lookup. Instead, when performing class
|
| 167 |
+
template argument deduction [[over.match.class.deduct]], all reachable
|
| 168 |
+
deduction guides declared for the class template are considered.
|
| 169 |
|
| 170 |
``` bnf
|
| 171 |
deduction-guide:
|
| 172 |
explicit-specifierₒₚₜ template-name '(' parameter-declaration-clause ')' '->' simple-template-id ';'
|
| 173 |
```
|
|
|
|
| 193 |
|
| 194 |
The same restrictions apply to the *parameter-declaration-clause* of a
|
| 195 |
deduction guide as in a function declaration [[dcl.fct]]. The
|
| 196 |
*simple-template-id* shall name a class template specialization. The
|
| 197 |
*template-name* shall be the same *identifier* as the *template-name* of
|
| 198 |
+
the *simple-template-id*. A *deduction-guide* shall inhabit the scope to
|
| 199 |
+
which the corresponding class template belongs and, for a member class
|
| 200 |
+
template, have the same access. Two deduction guide declarations for the
|
| 201 |
+
same class template shall not have equivalent
|
| 202 |
+
*parameter-declaration-clause*s if either is reachable from the other.
|
| 203 |
|
| 204 |
#### Member classes of class templates <a id="temp.mem.class">[[temp.mem.class]]</a>
|
| 205 |
|
| 206 |
A member class of a class template may be defined outside the class
|
| 207 |
template definition in which it is declared.
|
|
|
|
| 213 |
|
| 214 |
``` cpp
|
| 215 |
template<class T> struct A {
|
| 216 |
class B;
|
| 217 |
};
|
| 218 |
+
A<int>::B* b1; // OK, requires A to be defined but not A::B
|
| 219 |
template<class T> class A<T>::B { };
|
| 220 |
+
A<int>::B b2; // OK, requires A::B to be defined
|
| 221 |
```
|
| 222 |
|
| 223 |
— *end note*]
|
| 224 |
|
| 225 |
#### Static data members of class templates <a id="temp.static">[[temp.static]]</a>
|
|
|
|
| 255 |
``` cpp
|
| 256 |
template <class T> struct A {
|
| 257 |
static int i[];
|
| 258 |
};
|
| 259 |
template <class T> int A<T>::i[4]; // 4 elements
|
| 260 |
+
template <> int A<int>::i[] = { 1 }; // OK, 1 element
|
| 261 |
```
|
| 262 |
|
| 263 |
— *end example*]
|
| 264 |
|
| 265 |
#### Enumeration members of class templates <a id="temp.mem.enum">[[temp.mem.enum]]</a>
|