From Jason Turner

[temp.class.spec]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpu00rm_oh/{from.md → to.md} +0 -398
tmp/tmpu00rm_oh/{from.md → to.md} RENAMED
@@ -1,398 +0,0 @@
1
- ### Class template partial specializations <a id="temp.class.spec">[[temp.class.spec]]</a>
2
-
3
- A *primary class template* declaration is one in which the class
4
- template name is an identifier. A template declaration in which the
5
- class template name is a *simple-template-id* is a *partial
6
- specialization* of the class template named in the *simple-template-id*.
7
- A partial specialization of a class template provides an alternative
8
- definition of the template that is used instead of the primary
9
- definition when the arguments in a specialization match those given in
10
- the partial specialization [[temp.class.spec.match]]. The primary
11
- template shall be declared before any specializations of that template.
12
- A partial specialization shall be declared before the first use of a
13
- class template specialization that would make use of the partial
14
- specialization as the result of an implicit or explicit instantiation in
15
- every translation unit in which such a use occurs; no diagnostic is
16
- required.
17
-
18
- Each class template partial specialization is a distinct template and
19
- definitions shall be provided for the members of a template partial
20
- specialization [[temp.class.spec.mfunc]].
21
-
22
- [*Example 1*:
23
-
24
- ``` cpp
25
- template<class T1, class T2, int I> class A { };
26
- template<class T, int I> class A<T, T*, I> { };
27
- template<class T1, class T2, int I> class A<T1*, T2, I> { };
28
- template<class T> class A<int, T*, 5> { };
29
- template<class T1, class T2, int I> class A<T1, T2*, I> { };
30
- ```
31
-
32
- The first declaration declares the primary (unspecialized) class
33
- template. The second and subsequent declarations declare partial
34
- specializations of the primary template.
35
-
36
- — *end example*]
37
-
38
- A class template partial specialization may be constrained [[temp.pre]].
39
-
40
- [*Example 2*:
41
-
42
- ``` cpp
43
- template<typename T> concept C = true;
44
-
45
- template<typename T> struct X { };
46
- template<typename T> struct X<T*> { }; // #1
47
- template<C T> struct X<T> { }; // #2
48
- ```
49
-
50
- Both partial specializations are more specialized than the primary
51
- template. \#1 is more specialized because the deduction of its template
52
- arguments from the template argument list of the class template
53
- specialization succeeds, while the reverse does not. \#2 is more
54
- specialized because the template arguments are equivalent, but the
55
- partial specialization is more constrained [[temp.constr.order]].
56
-
57
- — *end example*]
58
-
59
- The template parameters are specified in the angle bracket enclosed list
60
- that immediately follows the keyword `template`. For partial
61
- specializations, the template argument list is explicitly written
62
- immediately following the class template name. For primary templates,
63
- this list is implicitly described by the template parameter list.
64
- Specifically, the order of the template arguments is the sequence in
65
- which they appear in the template parameter list.
66
-
67
- [*Example 3*: The template argument list for the primary template in
68
- the example above is `<T1,` `T2,` `I>`. — *end example*]
69
-
70
- [*Note 1*:
71
-
72
- The template argument list cannot be specified in the primary template
73
- declaration. For example,
74
-
75
- ``` cpp
76
- template<class T1, class T2, int I>
77
- class A<T1, T2, I> { }; // error
78
- ```
79
-
80
- — *end note*]
81
-
82
- A class template partial specialization may be declared in any scope in
83
- which the corresponding primary template may be defined (
84
- [[namespace.memdef]], [[class.mem]], [[temp.mem]]).
85
-
86
- [*Example 4*:
87
-
88
- ``` cpp
89
- template<class T> struct A {
90
- struct C {
91
- template<class T2> struct B { };
92
- template<class T2> struct B<T2**> { }; // partial specialization #1
93
- };
94
- };
95
-
96
- // partial specialization of A<T>::C::B<T2>
97
- template<class T> template<class T2>
98
- struct A<T>::C::B<T2*> { }; // #2
99
-
100
- A<short>::C::B<int*> absip; // uses partial specialization #2
101
- ```
102
-
103
- — *end example*]
104
-
105
- Partial specialization declarations themselves are not found by name
106
- lookup. Rather, when the primary template name is used, any
107
- previously-declared partial specializations of the primary template are
108
- also considered. One consequence is that a *using-declaration* which
109
- refers to a class template does not restrict the set of partial
110
- specializations which may be found through the *using-declaration*.
111
-
112
- [*Example 5*:
113
-
114
- ``` cpp
115
- namespace N {
116
- template<class T1, class T2> class A { }; // primary template
117
- }
118
-
119
- using N::A; // refers to the primary template
120
-
121
- namespace N {
122
- template<class T> class A<T, T*> { }; // partial specialization
123
- }
124
-
125
- A<int,int*> a; // uses the partial specialization, which is found through the using-declaration
126
- // which refers to the primary template
127
- ```
128
-
129
- — *end example*]
130
-
131
- A non-type argument is non-specialized if it is the name of a non-type
132
- parameter. All other non-type arguments are specialized.
133
-
134
- Within the argument list of a class template partial specialization, the
135
- following restrictions apply:
136
-
137
- - The type of a template parameter corresponding to a specialized
138
- non-type argument shall not be dependent on a parameter of the
139
- specialization.
140
- \[*Example 6*:
141
- ``` cpp
142
- template <class T, T t> struct C {};
143
- template <class T> struct C<T, 1>; // error
144
-
145
- template< int X, int (*array_ptr)[X] > class A {};
146
- int array[5];
147
- template< int X > class A<X,&array> { }; // error
148
- ```
149
-
150
- — *end example*]
151
- - The specialization shall be more specialized than the primary template
152
- [[temp.class.order]].
153
- - The template parameter list of a specialization shall not contain
154
- default template argument values.[^8]
155
- - An argument shall not contain an unexpanded pack. If an argument is a
156
- pack expansion [[temp.variadic]], it shall be the last argument in the
157
- template argument list.
158
-
159
- The usual access checking rules do not apply to non-dependent names used
160
- to specify template arguments of the *simple-template-id* of the partial
161
- specialization.
162
-
163
- [*Note 2*: The template arguments may be private types or objects that
164
- would normally not be accessible. Dependent names cannot be checked when
165
- declaring the partial specialization, but will be checked when
166
- substituting into the partial specialization. — *end note*]
167
-
168
- #### Matching of class template partial specializations <a id="temp.class.spec.match">[[temp.class.spec.match]]</a>
169
-
170
- When a class template is used in a context that requires an
171
- instantiation of the class, it is necessary to determine whether the
172
- instantiation is to be generated using the primary template or one of
173
- the partial specializations. This is done by matching the template
174
- arguments of the class template specialization with the template
175
- argument lists of the partial specializations.
176
-
177
- - If exactly one matching specialization is found, the instantiation is
178
- generated from that specialization.
179
- - If more than one matching specialization is found, the partial order
180
- rules [[temp.class.order]] are used to determine whether one of the
181
- specializations is more specialized than the others. If none of the
182
- specializations is more specialized than all of the other matching
183
- specializations, then the use of the class template is ambiguous and
184
- the program is ill-formed.
185
- - If no matches are found, the instantiation is generated from the
186
- primary template.
187
-
188
- A partial specialization matches a given actual template argument list
189
- if the template arguments of the partial specialization can be deduced
190
- from the actual template argument list [[temp.deduct]], and the deduced
191
- template arguments satisfy the associated constraints of the partial
192
- specialization, if any [[temp.constr.decl]].
193
-
194
- [*Example 1*:
195
-
196
- ``` cpp
197
- template<class T1, class T2, int I> class A { }; // #1
198
- template<class T, int I> class A<T, T*, I> { }; // #2
199
- template<class T1, class T2, int I> class A<T1*, T2, I> { }; // #3
200
- template<class T> class A<int, T*, 5> { }; // #4
201
- template<class T1, class T2, int I> class A<T1, T2*, I> { }; // #5
202
-
203
- A<int, int, 1> a1; // uses #1
204
- A<int, int*, 1> a2; // uses #2, T is int, I is 1
205
- A<int, char*, 5> a3; // uses #4, T is char
206
- A<int, char*, 1> a4; // uses #5, T1 is int, T2 is char, I is 1
207
- A<int*, int*, 2> a5; // ambiguous: matches #3 and #5
208
- ```
209
-
210
- — *end example*]
211
-
212
- [*Example 2*:
213
-
214
- ``` cpp
215
- template<typename T> concept C = requires (T t) { t.f(); };
216
-
217
- template<typename T> struct S { }; // #1
218
- template<C T> struct S<T> { }; // #2
219
-
220
- struct Arg { void f(); };
221
-
222
- S<int> s1; // uses #1; the constraints of #2 are not satisfied
223
- S<Arg> s2; // uses #2; both constraints are satisfied but #2 is more specialized
224
- ```
225
-
226
- — *end example*]
227
-
228
- If the template arguments of a partial specialization cannot be deduced
229
- because of the structure of its *template-parameter-list* and the
230
- *template-id*, the program is ill-formed.
231
-
232
- [*Example 3*:
233
-
234
- ``` cpp
235
- template <int I, int J> struct A {};
236
- template <int I> struct A<I+5, I*2> {}; // error
237
-
238
- template <int I> struct A<I, I> {}; // OK
239
-
240
- template <int I, int J, int K> struct B {};
241
- template <int I> struct B<I, I*2, 2> {}; // OK
242
- ```
243
-
244
- — *end example*]
245
-
246
- In a type name that refers to a class template specialization, (e.g.,
247
- `A<int, int, 1>`) the argument list shall match the template parameter
248
- list of the primary template. The template arguments of a specialization
249
- are deduced from the arguments of the primary template.
250
-
251
- #### Partial ordering of class template specializations <a id="temp.class.order">[[temp.class.order]]</a>
252
-
253
- For two class template partial specializations, the first is *more
254
- specialized* than the second if, given the following rewrite to two
255
- function templates, the first function template is more specialized than
256
- the second according to the ordering rules for function templates
257
- [[temp.func.order]]:
258
-
259
- - Each of the two function templates has the same template parameters
260
- and associated constraints [[temp.constr.decl]] as the corresponding
261
- partial specialization.
262
- - Each function template has a single function parameter whose type is a
263
- class template specialization where the template arguments are the
264
- corresponding template parameters from the function template for each
265
- template argument in the *template-argument-list* of the
266
- *simple-template-id* of the partial specialization.
267
-
268
- [*Example 1*:
269
-
270
- ``` cpp
271
- template<int I, int J, class T> class X { };
272
- template<int I, int J> class X<I, J, int> { }; // #1
273
- template<int I> class X<I, I, int> { }; // #2
274
-
275
- template<int I0, int J0> void f(X<I0, J0, int>); // A
276
- template<int I0> void f(X<I0, I0, int>); // B
277
-
278
- template <auto v> class Y { };
279
- template <auto* p> class Y<p> { }; // #3
280
- template <auto** pp> class Y<pp> { }; // #4
281
-
282
- template <auto* p0> void g(Y<p0>); // C
283
- template <auto** pp0> void g(Y<pp0>); // D
284
- ```
285
-
286
- According to the ordering rules for function templates, the function
287
- template *B* is more specialized than the function template *A* and the
288
- function template *D* is more specialized than the function template
289
- *C*. Therefore, the partial specialization \#2 is more specialized than
290
- the partial specialization \#1 and the partial specialization \#4 is
291
- more specialized than the partial specialization \#3.
292
-
293
- — *end example*]
294
-
295
- [*Example 2*:
296
-
297
- ``` cpp
298
- template<typename T> concept C = requires (T t) { t.f(); };
299
- template<typename T> concept D = C<T> && requires (T t) { t.f(); };
300
-
301
- template<typename T> class S { };
302
- template<C T> class S<T> { }; // #1
303
- template<D T> class S<T> { }; // #2
304
-
305
- template<C T> void f(S<T>); // A
306
- template<D T> void f(S<T>); // B
307
- ```
308
-
309
- The partial specialization \#2 is more specialized than \#1 because `B`
310
- is more specialized than `A`.
311
-
312
- — *end example*]
313
-
314
- #### Members of class template specializations <a id="temp.class.spec.mfunc">[[temp.class.spec.mfunc]]</a>
315
-
316
- The template parameter list of a member of a class template partial
317
- specialization shall match the template parameter list of the class
318
- template partial specialization. The template argument list of a member
319
- of a class template partial specialization shall match the template
320
- argument list of the class template partial specialization. A class
321
- template partial specialization is a distinct template. The members of
322
- the class template partial specialization are unrelated to the members
323
- of the primary template. Class template partial specialization members
324
- that are used in a way that requires a definition shall be defined; the
325
- definitions of members of the primary template are never used as
326
- definitions for members of a class template partial specialization. An
327
- explicit specialization of a member of a class template partial
328
- specialization is declared in the same way as an explicit specialization
329
- of the primary template.
330
-
331
- [*Example 1*:
332
-
333
- ``` cpp
334
- // primary class template
335
- template<class T, int I> struct A {
336
- void f();
337
- };
338
-
339
- // member of primary class template
340
- template<class T, int I> void A<T,I>::f() { }
341
-
342
- // class template partial specialization
343
- template<class T> struct A<T,2> {
344
- void f();
345
- void g();
346
- void h();
347
- };
348
-
349
- // member of class template partial specialization
350
- template<class T> void A<T,2>::g() { }
351
-
352
- // explicit specialization
353
- template<> void A<char,2>::h() { }
354
-
355
- int main() {
356
- A<char,0> a0;
357
- A<char,2> a2;
358
- a0.f(); // OK, uses definition of primary template's member
359
- a2.g(); // OK, uses definition of partial specialization's member
360
- a2.h(); // OK, uses definition of explicit specialization's member
361
- a2.f(); // error: no definition of f for A<T,2>; the primary template is not used here
362
- }
363
- ```
364
-
365
- — *end example*]
366
-
367
- If a member template of a class template is partially specialized, the
368
- member template partial specializations are member templates of the
369
- enclosing class template; if the enclosing class template is
370
- instantiated ([[temp.inst]], [[temp.explicit]]), a declaration for
371
- every member template partial specialization is also instantiated as
372
- part of creating the members of the class template specialization. If
373
- the primary member template is explicitly specialized for a given
374
- (implicit) specialization of the enclosing class template, the partial
375
- specializations of the member template are ignored for this
376
- specialization of the enclosing class template. If a partial
377
- specialization of the member template is explicitly specialized for a
378
- given (implicit) specialization of the enclosing class template, the
379
- primary member template and its other partial specializations are still
380
- considered for this specialization of the enclosing class template.
381
-
382
- [*Example 2*:
383
-
384
- ``` cpp
385
- template<class T> struct A {
386
- template<class T2> struct B {}; // #1
387
- template<class T2> struct B<T2*> {}; // #2
388
- };
389
-
390
- template<> template<class T2> struct A<short>::B {}; // #3
391
-
392
- A<char>::B<int*> abcip; // uses #2
393
- A<short>::B<int*> absip; // uses #3
394
- A<char>::B<int> abci; // uses #1
395
- ```
396
-
397
- — *end example*]
398
-