From Jason Turner

[temp.class.spec]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp7nhroa_t/{from.md → to.md} +111 -57
tmp/tmp7nhroa_t/{from.md → to.md} RENAMED
@@ -1,8 +1,8 @@
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
@@ -17,62 +17,81 @@ 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
  ``` cpp
23
- template<class T1, class T2, int I> class A { }; // #1
24
- template<class T, int I> class A<T, T*, I> { }; // #2
25
- template<class T1, class T2, int I> class A<T1*, T2, I> { }; // #3
26
- template<class T> class A<int, T*, 5> { }; // #4
27
- template<class T1, class T2, int I> class A<T1, T2*, I> { }; // #5
28
  ```
29
 
30
  The first declaration declares the primary (unspecialized) class
31
  template. The second and subsequent declarations declare partial
32
  specializations of the primary template.
33
 
 
 
34
  The template parameters are specified in the angle bracket enclosed list
35
  that immediately follows the keyword `template`. For partial
36
  specializations, the template argument list is explicitly written
37
  immediately following the class template name. For primary templates,
38
  this list is implicitly described by the template parameter list.
39
  Specifically, the order of the template arguments is the sequence in
40
- which they appear in the template parameter list. the template argument
41
- list for the primary template in the example above is `<T1,` `T2,` `I>`.
 
 
 
 
 
42
  The template argument list shall not be specified in the primary
43
  template declaration. For example,
44
 
45
  ``` cpp
46
- template<class T1, class T2, int I> class A<T1, T2, I> { }; // error
 
47
  ```
48
 
49
- A class template partial specialization may be declared or redeclared in
50
- any namespace scope in which its definition may be defined (
51
- [[temp.class]] and  [[temp.mem]]).
 
 
 
 
52
 
53
  ``` cpp
54
  template<class T> struct A {
55
  struct C {
56
  template<class T2> struct B { };
 
57
  };
58
  };
59
 
60
  // partial specialization of A<T>::C::B<T2>
61
  template<class T> template<class T2>
62
- struct A<T>::C::B<T2*> { };
63
 
64
- A<short>::C::B<int*> absip; // uses partial specialization
65
  ```
66
 
 
 
67
  Partial specialization declarations themselves are not found by name
68
  lookup. Rather, when the primary template name is used, any
69
  previously-declared partial specializations of the primary template are
70
  also considered. One consequence is that a *using-declaration* which
71
  refers to a class template does not restrict the set of partial
72
  specializations which may be found through the *using-declaration*.
73
 
 
 
74
  ``` cpp
75
  namespace N {
76
  template<class T1, class T2> class A { }; // primary template
77
  }
78
 
@@ -80,43 +99,36 @@ using N::A; // refers to the primary template
80
 
81
  namespace N {
82
  template<class T> class A<T, T*> { }; // partial specialization
83
  }
84
 
85
- A<int,int*> a; // uses the partial specialization, which is found through
86
- // the using declaration which refers to the primary template
87
  ```
88
 
 
 
89
  A non-type argument is non-specialized if it is the name of a non-type
90
  parameter. All other non-type arguments are specialized.
91
 
92
  Within the argument list of a class template partial specialization, the
93
  following restrictions apply:
94
 
95
- - A partially specialized non-type argument expression shall not involve
96
- a template parameter of the partial specialization except when the
97
- argument expression is a simple *identifier*.
98
- ``` cpp
99
- template <int I, int J> struct A {};
100
- template <int I> struct A<I+5, I*2> {}; // error
101
-
102
- template <int I, int J> struct B {};
103
- template <int I> struct B<I, I> {}; // OK
104
- ```
105
  - The type of a template parameter corresponding to a specialized
106
  non-type argument shall not be dependent on a parameter of the
107
  specialization.
 
108
  ``` cpp
109
  template <class T, T t> struct C {};
110
  template <class T> struct C<T, 1>; // error
111
 
112
  template< int X, int (*array_ptr)[X] > class A {};
113
  int array[5];
114
  template< int X > class A<X,&array> { }; // error
115
  ```
116
- - The argument list of the specialization shall not be identical to the
117
- implicit argument list of the primary template.
118
  - The specialization shall be more specialized than the primary
119
  template ([[temp.class.order]]).
120
  - The template parameter list of a specialization shall not contain
121
  default template argument values.[^4]
122
  - An argument shall not contain an unexpanded parameter pack. If an
@@ -145,57 +157,93 @@ argument lists of the partial specializations.
145
 
146
  A partial specialization matches a given actual template argument list
147
  if the template arguments of the partial specialization can be deduced
148
  from the actual template argument list ([[temp.deduct]]).
149
 
 
 
150
  ``` cpp
 
 
 
 
 
 
151
  A<int, int, 1> a1; // uses #1
152
  A<int, int*, 1> a2; // uses #2, T is int, I is 1
153
  A<int, char*, 5> a3; // uses #4, T is char
154
  A<int, char*, 1> a4; // uses #5, T1 is int, T2 is char, I is 1
155
  A<int*, int*, 2> a5; // ambiguous: matches #3 and #5
156
  ```
157
 
158
- A non-type template argument can also be deduced from the value of an
159
- actual template argument of a non-type parameter of the primary
160
- template. the declaration of `a2` above.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
 
162
  In a type name that refers to a class template specialization, (e.g.,
163
  `A<int, int, 1>`) the argument list shall match the template parameter
164
  list of the primary template. The template arguments of a specialization
165
  are deduced from the arguments of the primary template.
166
 
167
  #### Partial ordering of class template specializations <a id="temp.class.order">[[temp.class.order]]</a>
168
 
169
- For two class template partial specializations, the first is at least as
170
- specialized as the second if, given the following rewrite to two
171
- function templates, the first function template is at least as
172
- specialized as the second according to the ordering rules for function
173
- templates ([[temp.func.order]]):
174
 
175
- - the first function template has the same template parameters as the
176
- first partial specialization and has a single function parameter whose
177
- type is a class template specialization with the template arguments of
178
- the first partial specialization, and
179
- - the second function template has the same template parameters as the
180
- second partial specialization and has a single function parameter
181
- whose type is a class template specialization with the template
182
- arguments of the second partial specialization.
 
183
 
184
  ``` cpp
185
  template<int I, int J, class T> class X { };
186
  template<int I, int J> class X<I, J, int> { }; // #1
187
  template<int I> class X<I, I, int> { }; // #2
188
 
189
- template<int I, int J> void f(X<I, J, int>); // A
190
- template<int I> void f(X<I, I, int>); // B
 
 
 
 
 
 
 
191
  ```
192
 
193
- The partial specialization `#2` is more specialized than the partial
194
- specialization `#1` because the function template `B` is more
195
- specialized than the function template `A` according to the ordering
196
- rules for function templates.
 
 
 
 
197
 
198
  #### Members of class template specializations <a id="temp.class.spec.mfunc">[[temp.class.spec.mfunc]]</a>
199
 
200
  The template parameter list of a member of a class template partial
201
  specialization shall match the template parameter list of the class
@@ -210,16 +258,19 @@ definitions of members of the primary template are never used as
210
  definitions for members of a class template partial specialization. An
211
  explicit specialization of a member of a class template partial
212
  specialization is declared in the same way as an explicit specialization
213
  of the primary template.
214
 
 
 
215
  ``` cpp
216
- // primary template
217
  template<class T, int I> struct A {
218
  void f();
219
  };
220
 
 
221
  template<class T, int I> void A<T,I>::f() { }
222
 
223
  // class template partial specialization
224
  template<class T> struct A<T,2> {
225
  void f();
@@ -235,19 +286,18 @@ template<> void A<char,2>::h() { }
235
 
236
  int main() {
237
  A<char,0> a0;
238
  A<char,2> a2;
239
  a0.f(); // OK, uses definition of primary template's member
240
- a2.g(); // OK, uses definition of
241
- // partial specialization's member
242
- a2.h(); // OK, uses definition of
243
- // explicit specialization's member
244
- a2.f(); // ill-formed, no definition of f for A<T,2>
245
- // the primary template is not used here
246
  }
247
  ```
248
 
 
 
249
  If a member template of a class template is partially specialized, the
250
  member template partial specializations are member templates of the
251
  enclosing class template; if the enclosing class template is
252
  instantiated ([[temp.inst]], [[temp.explicit]]), a declaration for
253
  every member template partial specialization is also instantiated as
@@ -259,10 +309,12 @@ specialization of the enclosing class template. If a partial
259
  specialization of the member template is explicitly specialized for a
260
  given (implicit) specialization of the enclosing class template, the
261
  primary member template and its other partial specializations are still
262
  considered for this specialization of the enclosing class template.
263
 
 
 
264
  ``` cpp
265
  template<class T> struct A {
266
  template<class T2> struct B {}; // #1
267
  template<class T2> struct B<T2*> {}; // #2
268
  };
@@ -272,5 +324,7 @@ template<> template<class T2> struct A<short>::B {}; // #3
272
  A<char>::B<int*> abcip; // uses #2
273
  A<short>::B<int*> absip; // uses #3
274
  A<char>::B<int> abci; // uses #1
275
  ```
276
 
 
 
 
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
 
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
  The template parameters are specified in the angle bracket enclosed list
39
  that immediately follows the keyword `template`. For partial
40
  specializations, the template argument list is explicitly written
41
  immediately following the class template name. For primary templates,
42
  this list is implicitly described by the template parameter list.
43
  Specifically, the order of the template arguments is the sequence in
44
+ which they appear in the template parameter list.
45
+
46
+ [*Example 2*: The template argument list for the primary template in
47
+ the example above is `<T1,` `T2,` `I>`. — *end example*]
48
+
49
+ [*Note 1*:
50
+
51
  The template argument list shall not be specified in the primary
52
  template declaration. For example,
53
 
54
  ``` cpp
55
+ template<class T1, class T2, int I>
56
+ class A<T1, T2, I> { }; // error
57
  ```
58
 
59
+ *end note*]
60
+
61
+ A class template partial specialization may be declared in any scope in
62
+ which the corresponding primary template may be defined (
63
+ [[namespace.memdef]], [[class.mem]], [[temp.mem]]).
64
+
65
+ [*Example 3*:
66
 
67
  ``` cpp
68
  template<class T> struct A {
69
  struct C {
70
  template<class T2> struct B { };
71
+ template<class T2> struct B<T2**> { }; // partial specialization #1
72
  };
73
  };
74
 
75
  // partial specialization of A<T>::C::B<T2>
76
  template<class T> template<class T2>
77
+ struct A<T>::C::B<T2*> { }; // #2
78
 
79
+ A<short>::C::B<int*> absip; // uses partial specialization #2
80
  ```
81
 
82
+ — *end example*]
83
+
84
  Partial specialization declarations themselves are not found by name
85
  lookup. Rather, when the primary template name is used, any
86
  previously-declared partial specializations of the primary template are
87
  also considered. One consequence is that a *using-declaration* which
88
  refers to a class template does not restrict the set of partial
89
  specializations which may be found through the *using-declaration*.
90
 
91
+ [*Example 4*:
92
+
93
  ``` cpp
94
  namespace N {
95
  template<class T1, class T2> class A { }; // primary template
96
  }
97
 
 
99
 
100
  namespace N {
101
  template<class T> class A<T, T*> { }; // partial specialization
102
  }
103
 
104
+ A<int,int*> a; // uses the partial specialization, which is found through the using-declaration
105
+ // which refers to the primary template
106
  ```
107
 
108
+ — *end example*]
109
+
110
  A non-type argument is non-specialized if it is the name of a non-type
111
  parameter. All other non-type arguments are specialized.
112
 
113
  Within the argument list of a class template partial specialization, the
114
  following restrictions apply:
115
 
 
 
 
 
 
 
 
 
 
 
116
  - The type of a template parameter corresponding to a specialized
117
  non-type argument shall not be dependent on a parameter of the
118
  specialization.
119
+ \[*Example 5*:
120
  ``` cpp
121
  template <class T, T t> struct C {};
122
  template <class T> struct C<T, 1>; // error
123
 
124
  template< int X, int (*array_ptr)[X] > class A {};
125
  int array[5];
126
  template< int X > class A<X,&array> { }; // error
127
  ```
128
+
129
+ *end example*]
130
  - The specialization shall be more specialized than the primary
131
  template ([[temp.class.order]]).
132
  - The template parameter list of a specialization shall not contain
133
  default template argument values.[^4]
134
  - An argument shall not contain an unexpanded parameter pack. If an
 
157
 
158
  A partial specialization matches a given actual template argument list
159
  if the template arguments of the partial specialization can be deduced
160
  from the actual template argument list ([[temp.deduct]]).
161
 
162
+ [*Example 1*:
163
+
164
  ``` cpp
165
+ template<class T1, class T2, int I> class A { }; // #1
166
+ template<class T, int I> class A<T, T*, I> { }; // #2
167
+ template<class T1, class T2, int I> class A<T1*, T2, I> { }; // #3
168
+ template<class T> class A<int, T*, 5> { }; // #4
169
+ template<class T1, class T2, int I> class A<T1, T2*, I> { }; // #5
170
+
171
  A<int, int, 1> a1; // uses #1
172
  A<int, int*, 1> a2; // uses #2, T is int, I is 1
173
  A<int, char*, 5> a3; // uses #4, T is char
174
  A<int, char*, 1> a4; // uses #5, T1 is int, T2 is char, I is 1
175
  A<int*, int*, 2> a5; // ambiguous: matches #3 and #5
176
  ```
177
 
178
+ *end example*]
179
+
180
+ If the template arguments of a partial specialization cannot be deduced
181
+ because of the structure of its *template-parameter-list* and the
182
+ *template-id*, the program is ill-formed.
183
+
184
+ [*Example 2*:
185
+
186
+ ``` cpp
187
+ template <int I, int J> struct A {};
188
+ template <int I> struct A<I+5, I*2> {}; // error
189
+
190
+ template <int I> struct A<I, I> {}; // OK
191
+
192
+ template <int I, int J, int K> struct B {};
193
+ template <int I> struct B<I, I*2, 2> {}; // OK
194
+ ```
195
+
196
+ — *end example*]
197
 
198
  In a type name that refers to a class template specialization, (e.g.,
199
  `A<int, int, 1>`) the argument list shall match the template parameter
200
  list of the primary template. The template arguments of a specialization
201
  are deduced from the arguments of the primary template.
202
 
203
  #### Partial ordering of class template specializations <a id="temp.class.order">[[temp.class.order]]</a>
204
 
205
+ For two class template partial specializations, the first is *more
206
+ specialized* than the second if, given the following rewrite to two
207
+ function templates, the first function template is more specialized than
208
+ the second according to the ordering rules for function templates (
209
+ [[temp.func.order]]):
210
 
211
+ - Each of the two function templates has the same template parameters as
212
+ the corresponding partial specialization.
213
+ - Each function template has a single function parameter whose type is a
214
+ class template specialization where the template arguments are the
215
+ corresponding template parameters from the function template for each
216
+ template argument in the *template-argument-list* of the
217
+ *simple-template-id* of the partial specialization.
218
+
219
+ [*Example 1*:
220
 
221
  ``` cpp
222
  template<int I, int J, class T> class X { };
223
  template<int I, int J> class X<I, J, int> { }; // #1
224
  template<int I> class X<I, I, int> { }; // #2
225
 
226
+ template<int I0, int J0> void f(X<I0, J0, int>); // A
227
+ template<int I0> void f(X<I0, I0, int>); // B
228
+
229
+ template <auto v> class Y { };
230
+ template <auto* p> class Y<p> { }; // #3
231
+ template <auto** pp> class Y<pp> { }; // #4
232
+
233
+ template <auto* p0> void g(Y<p0>); // C
234
+ template <auto** pp0> void g(Y<pp0>); // D
235
  ```
236
 
237
+ According to the ordering rules for function templates, the function
238
+ template *B* is more specialized than the function template *A* and the
239
+ function template *D* is more specialized than the function template
240
+ *C*. Therefore, the partial specialization \#2 is more specialized than
241
+ the partial specialization \#1 and the partial specialization \#4 is
242
+ more specialized than the partial specialization \#3.
243
+
244
+ — *end example*]
245
 
246
  #### Members of class template specializations <a id="temp.class.spec.mfunc">[[temp.class.spec.mfunc]]</a>
247
 
248
  The template parameter list of a member of a class template partial
249
  specialization shall match the template parameter list of the class
 
258
  definitions for members of a class template partial specialization. An
259
  explicit specialization of a member of a class template partial
260
  specialization is declared in the same way as an explicit specialization
261
  of the primary template.
262
 
263
+ [*Example 1*:
264
+
265
  ``` cpp
266
+ // primary class template
267
  template<class T, int I> struct A {
268
  void f();
269
  };
270
 
271
+ // member of primary class template
272
  template<class T, int I> void A<T,I>::f() { }
273
 
274
  // class template partial specialization
275
  template<class T> struct A<T,2> {
276
  void f();
 
286
 
287
  int main() {
288
  A<char,0> a0;
289
  A<char,2> a2;
290
  a0.f(); // OK, uses definition of primary template's member
291
+ a2.g(); // OK, uses definition of partial specialization's member
292
+ a2.h(); // OK, uses definition of explicit specialization's member
293
+ a2.f(); // ill-formed, no definition of f for A<T,2>; the primary template is not used here
 
 
 
294
  }
295
  ```
296
 
297
+ — *end example*]
298
+
299
  If a member template of a class template is partially specialized, the
300
  member template partial specializations are member templates of the
301
  enclosing class template; if the enclosing class template is
302
  instantiated ([[temp.inst]], [[temp.explicit]]), a declaration for
303
  every member template partial specialization is also instantiated as
 
309
  specialization of the member template is explicitly specialized for a
310
  given (implicit) specialization of the enclosing class template, the
311
  primary member template and its other partial specializations are still
312
  considered for this specialization of the enclosing class template.
313
 
314
+ [*Example 2*:
315
+
316
  ``` cpp
317
  template<class T> struct A {
318
  template<class T2> struct B {}; // #1
319
  template<class T2> struct B<T2*> {}; // #2
320
  };
 
324
  A<char>::B<int*> abcip; // uses #2
325
  A<short>::B<int*> absip; // uses #3
326
  A<char>::B<int> abci; // uses #1
327
  ```
328
 
329
+ — *end example*]
330
+