tmp/tmp79fy28jb/{from.md → to.md}
RENAMED
|
@@ -28,18 +28,18 @@ other words, `Array` is a parameterized type with `T` as its parameter.
|
|
| 28 |
— *end example*]
|
| 29 |
|
| 30 |
When a member function, a member class, a member enumeration, a static
|
| 31 |
data member or a member template of a class template is defined outside
|
| 32 |
of the class template definition, the member definition is defined as a
|
| 33 |
-
template definition in which the *template-
|
| 34 |
-
class template. The names of the template
|
| 35 |
-
definition of the member may be different from
|
| 36 |
-
names used in the class template definition. The
|
| 37 |
-
following the class template name in the member
|
| 38 |
-
the parameters in the same order as the one used
|
| 39 |
-
parameter list of the member. Each template parameter
|
| 40 |
-
expanded with an ellipsis in the template argument list.
|
| 41 |
|
| 42 |
[*Example 2*:
|
| 43 |
|
| 44 |
``` cpp
|
| 45 |
template<class T1, class T2> struct A {
|
|
@@ -59,16 +59,36 @@ template<class ... Types> struct B {
|
|
| 59 |
|
| 60 |
template<class ... Types> void B<Types ...>::f3() { } // OK
|
| 61 |
template<class ... Types> void B<Types>::f4() { } // error
|
| 62 |
```
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
— *end example*]
|
| 65 |
|
| 66 |
In a redeclaration, partial specialization, explicit specialization or
|
| 67 |
explicit instantiation of a class template, the *class-key* shall agree
|
| 68 |
-
in kind with the original class template declaration
|
| 69 |
-
[[dcl.type.elab]]).
|
| 70 |
|
| 71 |
#### Member functions of class templates <a id="temp.mem.func">[[temp.mem.func]]</a>
|
| 72 |
|
| 73 |
A member function of a class template may be defined outside of the
|
| 74 |
class template definition in which it is declared.
|
|
@@ -84,50 +104,110 @@ public:
|
|
| 84 |
T& operator[](int);
|
| 85 |
T& elem(int i) { return v[i]; }
|
| 86 |
};
|
| 87 |
```
|
| 88 |
|
| 89 |
-
declares three
|
| 90 |
-
defined like this:
|
| 91 |
|
| 92 |
``` cpp
|
| 93 |
template<class T> T& Array<T>::operator[](int i) {
|
| 94 |
if (i<0 || sz<=i) error("Array: range error");
|
| 95 |
return v[i];
|
| 96 |
}
|
| 97 |
```
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
— *end example*]
|
| 100 |
|
| 101 |
The *template-argument*s for a member function of a class template are
|
| 102 |
determined by the *template-argument*s of the type of the object for
|
| 103 |
which the member function is called.
|
| 104 |
|
| 105 |
[*Example 2*:
|
| 106 |
|
| 107 |
-
The *template-argument* for `Array<T>::operator[]
|
| 108 |
-
|
| 109 |
|
| 110 |
``` cpp
|
| 111 |
Array<int> v1(20);
|
| 112 |
Array<dcomplex> v2(30);
|
| 113 |
|
| 114 |
-
v1[3] = 7;
|
| 115 |
-
v2[3] = dcomplex(7,8);
|
| 116 |
```
|
| 117 |
|
| 118 |
— *end example*]
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
#### Member classes of class templates <a id="temp.mem.class">[[temp.mem.class]]</a>
|
| 121 |
|
| 122 |
A member class of a class template may be defined outside the class
|
| 123 |
template definition in which it is declared.
|
| 124 |
|
| 125 |
[*Note 1*:
|
| 126 |
|
| 127 |
The member class must be defined before its first use that requires an
|
| 128 |
-
instantiation
|
| 129 |
|
| 130 |
``` cpp
|
| 131 |
template<class T> struct A {
|
| 132 |
class B;
|
| 133 |
};
|
|
|
|
| 28 |
— *end example*]
|
| 29 |
|
| 30 |
When a member function, a member class, a member enumeration, a static
|
| 31 |
data member or a member template of a class template is defined outside
|
| 32 |
of the class template definition, the member definition is defined as a
|
| 33 |
+
template definition in which the *template-head* is equivalent to that
|
| 34 |
+
of the class template [[temp.over.link]]. The names of the template
|
| 35 |
+
parameters used in the definition of the member may be different from
|
| 36 |
+
the template parameter names used in the class template definition. The
|
| 37 |
+
template argument list following the class template name in the member
|
| 38 |
+
definition shall name the parameters in the same order as the one used
|
| 39 |
+
in the template parameter list of the member. Each template parameter
|
| 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 {
|
|
|
|
| 59 |
|
| 60 |
template<class ... Types> void B<Types ...>::f3() { } // OK
|
| 61 |
template<class ... Types> void B<Types>::f4() { } // error
|
| 62 |
```
|
| 63 |
|
| 64 |
+
``` cpp
|
| 65 |
+
template<typename T> concept C = true;
|
| 66 |
+
template<typename T> concept D = true;
|
| 67 |
+
|
| 68 |
+
template<C T> struct S {
|
| 69 |
+
void f();
|
| 70 |
+
void g();
|
| 71 |
+
void h();
|
| 72 |
+
template<D U> struct Inner;
|
| 73 |
+
};
|
| 74 |
+
|
| 75 |
+
template<C A> void S<A>::f() { } // OK: template-head{s} match
|
| 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 |
+
|
| 81 |
+
template<C X> template<D Y>
|
| 82 |
+
struct S<X>::Inner { }; // OK
|
| 83 |
+
```
|
| 84 |
+
|
| 85 |
— *end example*]
|
| 86 |
|
| 87 |
In a redeclaration, partial specialization, explicit specialization or
|
| 88 |
explicit instantiation of a class template, the *class-key* shall agree
|
| 89 |
+
in kind with the original class template declaration [[dcl.type.elab]].
|
|
|
|
| 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.
|
|
|
|
| 104 |
T& operator[](int);
|
| 105 |
T& elem(int i) { return v[i]; }
|
| 106 |
};
|
| 107 |
```
|
| 108 |
|
| 109 |
+
declares three member functions of a class template. The subscript
|
| 110 |
+
function might be defined like this:
|
| 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];
|
| 116 |
}
|
| 117 |
```
|
| 118 |
|
| 119 |
+
A constrained member function can be defined out of line:
|
| 120 |
+
|
| 121 |
+
``` cpp
|
| 122 |
+
template<typename T> concept C = requires {
|
| 123 |
+
typename T::type;
|
| 124 |
+
};
|
| 125 |
+
|
| 126 |
+
template<typename T> struct S {
|
| 127 |
+
void f() requires C<T>;
|
| 128 |
+
void g() requires C<T>;
|
| 129 |
+
};
|
| 130 |
+
|
| 131 |
+
template<typename T>
|
| 132 |
+
void S<T>::f() requires C<T> { } // OK
|
| 133 |
+
template<typename T>
|
| 134 |
+
void S<T>::g() { } // error: no matching function in S<T>
|
| 135 |
+
```
|
| 136 |
+
|
| 137 |
— *end example*]
|
| 138 |
|
| 139 |
The *template-argument*s for a member function of a class template are
|
| 140 |
determined by the *template-argument*s of the type of the object for
|
| 141 |
which the member function is called.
|
| 142 |
|
| 143 |
[*Example 2*:
|
| 144 |
|
| 145 |
+
The *template-argument* for `Array<T>::operator[]` will be determined by
|
| 146 |
+
the `Array` to which the subscripting operation is applied.
|
| 147 |
|
| 148 |
``` cpp
|
| 149 |
Array<int> v1(20);
|
| 150 |
Array<dcomplex> v2(30);
|
| 151 |
|
| 152 |
+
v1[3] = 7; // Array<int>::operator[]
|
| 153 |
+
v2[3] = dcomplex(7,8); // Array<dcomplex>::operator[]
|
| 154 |
```
|
| 155 |
|
| 156 |
— *end example*]
|
| 157 |
|
| 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]], any deduction
|
| 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 |
+
```
|
| 170 |
+
|
| 171 |
+
[*Example 1*:
|
| 172 |
+
|
| 173 |
+
``` cpp
|
| 174 |
+
template<class T, class D = int>
|
| 175 |
+
struct S {
|
| 176 |
+
T data;
|
| 177 |
+
};
|
| 178 |
+
template<class U>
|
| 179 |
+
S(U) -> S<typename U::type>;
|
| 180 |
+
|
| 181 |
+
struct A {
|
| 182 |
+
using type = short;
|
| 183 |
+
operator type();
|
| 184 |
+
};
|
| 185 |
+
S x{A()}; // x is of type S<short, int>
|
| 186 |
+
```
|
| 187 |
+
|
| 188 |
+
— *end example*]
|
| 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 be declared in the
|
| 195 |
+
same scope as the corresponding class template and, for a member class
|
| 196 |
+
template, with the same access. Two deduction guide declarations in the
|
| 197 |
+
same translation unit for the same class template shall not have
|
| 198 |
+
equivalent *parameter-declaration-clause*s.
|
| 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.
|
| 204 |
|
| 205 |
[*Note 1*:
|
| 206 |
|
| 207 |
The member class must be defined before its first use that requires an
|
| 208 |
+
instantiation [[temp.inst]]. For example,
|
| 209 |
|
| 210 |
``` cpp
|
| 211 |
template<class T> struct A {
|
| 212 |
class B;
|
| 213 |
};
|