From Jason Turner

[temp.arg]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpzyzoje51/{from.md → to.md} +146 -74
tmp/tmpzyzoje51/{from.md → to.md} RENAMED
@@ -1,17 +1,15 @@
1
  ## Template arguments <a id="temp.arg">[[temp.arg]]</a>
2
 
3
  ### General <a id="temp.arg.general">[[temp.arg.general]]</a>
4
 
5
- There are three forms of *template-argument*, corresponding to the three
6
- forms of *template-parameter*: type, non-type and template. The type and
7
- form of each *template-argument* specified in a *template-id* shall
8
- match the type and form specified for the corresponding parameter
9
- declared by the template in its *template-parameter-list*. When the
10
- parameter declared by the template is a template parameter pack
11
- [[temp.variadic]], it will correspond to zero or more
12
- *template-argument*s.
13
 
14
  [*Example 1*:
15
 
16
  ``` cpp
17
  template<class T> class Array {
@@ -43,11 +41,11 @@ is a template parameter pack [[temp.variadic]], the nᵗʰ template
43
  argument is a pack expansion whose pattern is the name of the template
44
  parameter pack.
45
 
46
  In a *template-argument*, an ambiguity between a *type-id* and an
47
  expression is resolved to a *type-id*, regardless of the form of the
48
- corresponding *template-parameter*.[^3]
49
 
50
  [*Example 2*:
51
 
52
  ``` cpp
53
  template<class T> void f();
@@ -59,12 +57,13 @@ void g() {
59
  ```
60
 
61
  — *end example*]
62
 
63
  [*Note 1*: Names used in a *template-argument* are subject to access
64
- control where they appear. Because a *template-parameter* is not a class
65
- member, no access control applies. *end note*]
 
66
 
67
  [*Example 3*:
68
 
69
  ``` cpp
70
  template<class T> class X {
@@ -82,13 +81,13 @@ private:
82
  X<Y::S> y; // error: S not accessible
83
  ```
84
 
85
  — *end example*]
86
 
87
- For a *template-argument* that is a class type or a class template, the
88
  template definition has no special access rights to the members of the
89
- *template-argument*.
90
 
91
  [*Example 4*:
92
 
93
  ``` cpp
94
  template <template <class TT> class T> class A {
@@ -103,11 +102,11 @@ private:
103
  A<B> b; // error: A has no access to B::S
104
  ```
105
 
106
  — *end example*]
107
 
108
- When template argument packs or default *template-argument*s are used, a
109
  *template-argument* list can be empty. In that case the empty `<>`
110
  brackets shall still be used as the *template-argument-list*.
111
 
112
  [*Example 5*:
113
 
@@ -138,25 +137,26 @@ void f(A<int>* p, A<int>* q) {
138
  }
139
  ```
140
 
141
  — *end example*]
142
 
143
- If the use of a *template-argument* gives rise to an ill-formed
144
- construct in the instantiation of a template specialization, the program
145
- is ill-formed.
146
 
147
  When name lookup for the component name of a *template-id* finds an
148
  overload set, both non-template functions in the overload set and
149
  function templates in the overload set for which the
150
  *template-argument*s do not match the *template-parameter*s are ignored.
151
 
152
  [*Note 2*: If none of the function templates have matching
153
  *template-parameter*s, the program is ill-formed. — *end note*]
154
 
155
- When a *simple-template-id* does not name a function, a default
156
- *template-argument* is implicitly instantiated [[temp.inst]] when the
157
- value of that default argument is needed.
 
158
 
159
  [*Example 7*:
160
 
161
  ``` cpp
162
  template<typename T, typename U = int> struct S { };
@@ -169,14 +169,14 @@ The default argument for `U` is instantiated to form the type
169
  — *end example*]
170
 
171
  A *template-argument* followed by an ellipsis is a pack expansion
172
  [[temp.variadic]].
173
 
174
- ### Template type arguments <a id="temp.arg.type">[[temp.arg.type]]</a>
175
 
176
- A *template-argument* for a *template-parameter* which is a type shall
177
- be a *type-id*.
178
 
179
  [*Example 1*:
180
 
181
  ``` cpp
182
  template <class T> class X { };
@@ -200,45 +200,92 @@ void f() {
200
  — *end example*]
201
 
202
  [*Note 1*: A template type argument can be an incomplete type
203
  [[term.incomplete.type]]. — *end note*]
204
 
205
- ### Template non-type arguments <a id="temp.arg.nontype">[[temp.arg.nontype]]</a>
206
 
207
- If the type `T` of a *template-parameter* [[temp.param]] contains a
208
- placeholder type [[dcl.spec.auto]] or a placeholder for a deduced class
209
- type [[dcl.type.class.deduct]], the type of the parameter is the type
210
- deduced for the variable `x` in the invented declaration
211
 
212
  ``` cpp
213
- T x = template-argument ;
214
  ```
215
 
216
- If a deduced parameter type is not permitted for a *template-parameter*
217
- declaration [[temp.param]], the program is ill-formed.
 
 
 
218
 
219
- A *template-argument* for a non-type *template-parameter* shall be a
220
- converted constant expression [[expr.const]] of the type of the
221
- *template-parameter*.
222
 
223
- [*Note 1*: If the *template-argument* is an overload set (or the
224
- address of such, including forming a pointer-to-member), the matching
225
- function is selected from the set [[over.over]]. — *end note*]
226
 
227
- For a non-type *template-parameter* of reference or pointer type, or for
228
- each non-static data member of reference or pointer type in a non-type
229
- *template-parameter* of class type or subobject thereof, the reference
230
- or pointer value shall not refer to or be the address of (respectively):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
 
232
  - a temporary object [[class.temporary]],
233
  - a string literal object [[lex.string]],
234
  - the result of a `typeid` expression [[expr.typeid]],
235
  - a predefined `__func__` variable [[dcl.fct.def.general]], or
236
  - a subobject [[intro.object]] of one of the above.
237
 
238
  [*Example 1*:
239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
  ``` cpp
241
  template<const int* pci> struct X { ... };
242
  int ai[10];
243
  X<ai> xi; // array to pointer and qualification conversions
244
 
@@ -261,20 +308,35 @@ A<&f> a; // selects f(int)
261
  template<auto n> struct B { ... };
262
  B<5> b1; // OK, template parameter type is int
263
  B<'a'> b2; // OK, template parameter type is char
264
  B<2.5> b3; // OK, template parameter type is double
265
  B<void(0)> b4; // error: template parameter type cannot be void
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266
  ```
267
 
268
  — *end example*]
269
 
270
  [*Note 2*:
271
 
272
  A *string-literal* [[lex.string]] is not an acceptable
273
- *template-argument* for a *template-parameter* of non-class type.
274
 
275
- [*Example 2*:
276
 
277
  ``` cpp
278
  template<class T, T p> class X {
279
  ...
280
  };
@@ -297,13 +359,13 @@ X<A, "Pyrophoricity"> z; // OK, string-literal is a constructor argument
297
  — *end note*]
298
 
299
  [*Note 3*:
300
 
301
  A temporary object is not an acceptable *template-argument* when the
302
- corresponding *template-parameter* has reference type.
303
 
304
- [*Example 3*:
305
 
306
  ``` cpp
307
  template<const int& CRI> struct B { ... };
308
 
309
  B<1> b1; // error: temporary would be required for template argument
@@ -322,21 +384,23 @@ C<Y{X{1}.n}> c; // error: subobject of temporary object used to
322
 
323
  — *end note*]
324
 
325
  ### Template template arguments <a id="temp.arg.template">[[temp.arg.template]]</a>
326
 
327
- A *template-argument* for a template *template-parameter* shall be the
328
- name of a class template or an alias template, expressed as
329
- *id-expression*. Only primary templates are considered when matching the
330
- template template argument with the corresponding parameter; partial
331
- specializations are not considered even if their parameter lists match
332
- that of the template template parameter.
 
 
333
 
334
  Any partial specializations [[temp.spec.partial]] associated with the
335
  primary template are considered when a specialization based on the
336
- template *template-parameter* is instantiated. If a specialization is
337
- not reachable from the point of instantiation, and it would have been
338
  selected had it been reachable, the program is ill-formed, no diagnostic
339
  required.
340
 
341
  [*Example 1*:
342
 
@@ -355,26 +419,34 @@ C<A> c; // V<int> within C<A> uses the primary template, so c.y.x ha
355
  // V<int*> within C<A> uses the partial specialization, so c.z.x has type long
356
  ```
357
 
358
  — *end example*]
359
 
360
- A *template-argument* matches a template *template-parameter* `P` when
361
- `P` is at least as specialized as the *template-argument* `A`. In this
362
- comparison, if `P` is unconstrained, the constraints on `A` are not
363
- considered. If `P` contains a template parameter pack, then `A` also
364
- matches `P` if each of `A`’s template parameters matches the
365
- corresponding template parameter in the *template-head* of `P`. Two
366
- template parameters match if they are of the same kind (type, non-type,
367
- template), for non-type *template-parameter*s, their types are
368
- equivalent [[temp.over.link]], and for template *template-parameter*s,
369
- each of their corresponding *template-parameter*s matches, recursively.
370
- When `P`’s *template-head* contains a template parameter pack
371
- [[temp.variadic]], the template parameter pack will match zero or more
372
- template parameters or template parameter packs in the *template-head*
373
- of `A` with the same type and form as the template parameter pack in `P`
374
- (ignoring whether those template parameters are template parameter
375
- packs).
 
 
 
 
 
 
 
 
376
 
377
  [*Example 2*:
378
 
379
  ``` cpp
380
  template<class T> class A { ... };
@@ -436,11 +508,11 @@ S<Y> s2; // error: P is not at least as specialized as Y
436
  S<Z> s3; // OK, P is at least as specialized as Z
437
  ```
438
 
439
  — *end example*]
440
 
441
- A template *template-parameter* `P` is at least as specialized as a
442
  template *template-argument* `A` if, given the following rewrite to two
443
  function templates, the function template corresponding to `P` is at
444
  least as specialized as the function template corresponding to `A`
445
  according to the partial ordering rules for function templates
446
  [[temp.func.order]]. Given an invented class template `X` with the
@@ -450,14 +522,14 @@ according to the partial ordering rules for function templates
450
  - Each of the two function templates has the same template parameters
451
  and *requires-clause* (if any), respectively, as `P` or `A`.
452
  - Each function template has a single function parameter whose type is a
453
  specialization of `X` with template arguments corresponding to the
454
  template parameters from the respective function template where, for
455
- each template parameter `PP` in the *template-head* of the function
456
- template, a corresponding template argument `AA` is formed. If `PP`
457
  declares a template parameter pack, then `AA` is the pack expansion
458
- `PP...` [[temp.variadic]]; otherwise, `AA` is the *id-expression*
459
- `PP`.
460
 
461
  If the rewrite produces an invalid type, then `P` is not at least as
462
  specialized as `A`.
463
 
 
1
  ## Template arguments <a id="temp.arg">[[temp.arg]]</a>
2
 
3
  ### General <a id="temp.arg.general">[[temp.arg.general]]</a>
4
 
5
+ The type and form of each *template-argument* specified in a
6
+ *template-id* or in a *splice-specialization-specifier* shall match the
7
+ type and form specified for the corresponding parameter declared by the
8
+ template in its *template-parameter-list*. When the parameter declared
9
+ by the template is a template parameter pack [[temp.variadic]], it will
10
+ correspond to zero or more *template-argument*s.
 
 
11
 
12
  [*Example 1*:
13
 
14
  ``` cpp
15
  template<class T> class Array {
 
41
  argument is a pack expansion whose pattern is the name of the template
42
  parameter pack.
43
 
44
  In a *template-argument*, an ambiguity between a *type-id* and an
45
  expression is resolved to a *type-id*, regardless of the form of the
46
+ corresponding *template-parameter*.[^2]
47
 
48
  [*Example 2*:
49
 
50
  ``` cpp
51
  template<class T> void f();
 
57
  ```
58
 
59
  — *end example*]
60
 
61
  [*Note 1*: Names used in a *template-argument* are subject to access
62
+ control where they appear. Because a template parameter is not a class
63
+ member, no access control applies where the template parameter is
64
+ used. — *end note*]
65
 
66
  [*Example 3*:
67
 
68
  ``` cpp
69
  template<class T> class X {
 
81
  X<Y::S> y; // error: S not accessible
82
  ```
83
 
84
  — *end example*]
85
 
86
+ For a template argument that is a class type or a class template, the
87
  template definition has no special access rights to the members of the
88
+ template argument.
89
 
90
  [*Example 4*:
91
 
92
  ``` cpp
93
  template <template <class TT> class T> class A {
 
102
  A<B> b; // error: A has no access to B::S
103
  ```
104
 
105
  — *end example*]
106
 
107
+ When template argument packs or default template arguments are used, a
108
  *template-argument* list can be empty. In that case the empty `<>`
109
  brackets shall still be used as the *template-argument-list*.
110
 
111
  [*Example 5*:
112
 
 
137
  }
138
  ```
139
 
140
  — *end example*]
141
 
142
+ If the use of a template argument gives rise to an ill-formed construct
143
+ in the instantiation of a template specialization, the program is
144
+ ill-formed.
145
 
146
  When name lookup for the component name of a *template-id* finds an
147
  overload set, both non-template functions in the overload set and
148
  function templates in the overload set for which the
149
  *template-argument*s do not match the *template-parameter*s are ignored.
150
 
151
  [*Note 2*: If none of the function templates have matching
152
  *template-parameter*s, the program is ill-formed. — *end note*]
153
 
154
+ When a *simple-template-id* or *splice-specialization-specifier* does
155
+ not designate a function, a default *template-argument* is implicitly
156
+ instantiated [[temp.inst]] when the value of that default argument is
157
+ needed.
158
 
159
  [*Example 7*:
160
 
161
  ``` cpp
162
  template<typename T, typename U = int> struct S { };
 
169
  — *end example*]
170
 
171
  A *template-argument* followed by an ellipsis is a pack expansion
172
  [[temp.variadic]].
173
 
174
+ ### Type template arguments <a id="temp.arg.type">[[temp.arg.type]]</a>
175
 
176
+ A *template-argument* for a type template parameter shall be a
177
+ *type-id*.
178
 
179
  [*Example 1*:
180
 
181
  ``` cpp
182
  template <class T> class X { };
 
200
  — *end example*]
201
 
202
  [*Note 1*: A template type argument can be an incomplete type
203
  [[term.incomplete.type]]. — *end note*]
204
 
205
+ ### Constant template arguments <a id="temp.arg.nontype">[[temp.arg.nontype]]</a>
206
 
207
+ A template argument E for a constant template parameter with declared
208
+ type `T` shall be such that the invented declaration
 
 
209
 
210
  ``` cpp
211
+ T x = E ;
212
  ```
213
 
214
+ satisfies the semantic constraints for the definition of a `constexpr`
215
+ variable with static storage duration [[dcl.constexpr]]. If `T` contains
216
+ a placeholder type [[dcl.spec.auto]] or a placeholder for a deduced
217
+ class type [[dcl.type.class.deduct]], the type of the parameter is
218
+ deduced from the above declaration.
219
 
220
+ [*Note 1*: E is a *template-argument* or (for a default template
221
+ argument) an *initializer-clause*. *end note*]
 
222
 
223
+ If the parameter type thus deduced is not permitted for a constant
224
+ template parameter [[temp.param]], the program is ill-formed.
 
225
 
226
+ The value of a constant template parameter P of (possibly deduced) type
227
+ `T` is determined from its template argument A as follows. If `T` is not
228
+ a class type and A is not a *braced-init-list*, A shall be a converted
229
+ constant expression [[expr.const]] of type `T`; the value of P is A (as
230
+ converted).
231
+
232
+ Otherwise, a temporary variable
233
+
234
+ ``` cpp
235
+ constexpr T v = A;
236
+ ```
237
+
238
+ is introduced. The lifetime of `v` ends immediately after initializing
239
+ it and any template parameter object (see below). For each such
240
+ variable, the *id-expression* `v` is termed a *candidate initializer*.
241
+
242
+ If `T` is a class type, a template parameter object [[temp.param]]
243
+ exists that is constructed so as to be template-argument-equivalent to
244
+ `v`; P denotes that template parameter object. P is copy-initialized
245
+ from an unspecified candidate initializer that is
246
+ template-argument-equivalent to `v`. If, for the initialization from any
247
+ candidate initializer,
248
+
249
+ - the initialization would be ill-formed, or
250
+ - the full-expression of an invented *init-declarator* for the
251
+ initialization would not be a constant expression when interpreted as
252
+ a *constant-expression* [[expr.const]], or
253
+ - the initialization would cause P to not be
254
+ template-argument-equivalent [[temp.type]] to `v`,
255
+
256
+ the program is ill-formed.
257
+
258
+ Otherwise, the value of P is that of v.
259
+
260
+ For a constant template parameter of reference or pointer type, or for
261
+ each non-static data member of reference or pointer type in a constant
262
+ template parameter of class type or subobject thereof, the reference or
263
+ pointer value shall not refer or point to (respectively):
264
 
265
  - a temporary object [[class.temporary]],
266
  - a string literal object [[lex.string]],
267
  - the result of a `typeid` expression [[expr.typeid]],
268
  - a predefined `__func__` variable [[dcl.fct.def.general]], or
269
  - a subobject [[intro.object]] of one of the above.
270
 
271
  [*Example 1*:
272
 
273
+ ``` cpp
274
+ template <int& r> class A{};
275
+ extern int x;
276
+ A<x> a; // OK
277
+ void f(int p) {
278
+ constexpr int& r = p; // OK
279
+ A<r> a; // error: a static constexpr int& variable cannot be initialized to refer to p here
280
+ }
281
+ ```
282
+
283
+ — *end example*]
284
+
285
+ [*Example 2*:
286
+
287
  ``` cpp
288
  template<const int* pci> struct X { ... };
289
  int ai[10];
290
  X<ai> xi; // array to pointer and qualification conversions
291
 
 
308
  template<auto n> struct B { ... };
309
  B<5> b1; // OK, template parameter type is int
310
  B<'a'> b2; // OK, template parameter type is char
311
  B<2.5> b3; // OK, template parameter type is double
312
  B<void(0)> b4; // error: template parameter type cannot be void
313
+
314
+ template<int i> struct C { /* ... */ };
315
+ C<{ 42 }> c1; // OK
316
+
317
+ struct J1 {
318
+ J1 *self = this;
319
+ };
320
+ B<J1{}> j1; // error: initialization of template parameter object is not a constant expression
321
+
322
+ struct J2 {
323
+ J2 *self = this;
324
+ constexpr J2() {}
325
+ constexpr J2(const J2&) {}
326
+ };
327
+ B<J2{}> j2; // error: template parameter object not template-argument-equivalent to introduced temporary
328
  ```
329
 
330
  — *end example*]
331
 
332
  [*Note 2*:
333
 
334
  A *string-literal* [[lex.string]] is not an acceptable
335
+ *template-argument* for a constant template parameter of non-class type.
336
 
337
+ [*Example 3*:
338
 
339
  ``` cpp
340
  template<class T, T p> class X {
341
  ...
342
  };
 
359
  — *end note*]
360
 
361
  [*Note 3*:
362
 
363
  A temporary object is not an acceptable *template-argument* when the
364
+ corresponding template parameter has reference type.
365
 
366
+ [*Example 4*:
367
 
368
  ``` cpp
369
  template<const int& CRI> struct B { ... };
370
 
371
  B<1> b1; // error: temporary would be required for template argument
 
384
 
385
  — *end note*]
386
 
387
  ### Template template arguments <a id="temp.arg.template">[[temp.arg.template]]</a>
388
 
389
+ A *template-argument* for a template template parameter shall be the
390
+ name of a template. For a *type-tt-parameter*, the name shall denote a
391
+ class template or alias template. For a *variable-tt-parameter*, the
392
+ name shall denote a variable template. For a *concept-tt-parameter*, the
393
+ name shall denote a concept. Only primary templates are considered when
394
+ matching the template template argument with the corresponding
395
+ parameter; partial specializations are not considered even if their
396
+ parameter lists match that of the template template parameter.
397
 
398
  Any partial specializations [[temp.spec.partial]] associated with the
399
  primary template are considered when a specialization based on the
400
+ template template parameter is instantiated. If a specialization is not
401
+ reachable from the point of instantiation, and it would have been
402
  selected had it been reachable, the program is ill-formed, no diagnostic
403
  required.
404
 
405
  [*Example 1*:
406
 
 
419
  // V<int*> within C<A> uses the partial specialization, so c.z.x has type long
420
  ```
421
 
422
  — *end example*]
423
 
424
+ A template template parameter `P` and a *template-argument* `A` are
425
+ *compatible* if
426
+
427
+ - `A` denotes a class template or an alias template and `P` is a type
428
+ template template parameter,
429
+ - `A` denotes a variable template and `P` is a variable template
430
+ template parameter, or
431
+ - `A` denotes a concept and `P` is a concept template parameter.
432
+
433
+ A template *template-argument* `A` matches a template template parameter
434
+ `P` when `A` and `P` are compatible and `P` is at least as specialized
435
+ as `A`, ignoring constraints on `A` if `P` is unconstrained. If `P`
436
+ contains a template parameter pack, then `A` also matches `P` if each of
437
+ `A`’s template parameters matches the corresponding template parameter
438
+ declared in the *template-head* of `P`. Two template parameters match if
439
+ they are of the same kind, for constant template parameters, their types
440
+ are equivalent [[temp.over.link]], and for template template parameters,
441
+ each of their corresponding template parameters matches, recursively.
442
+ When `P`’s *template-head* contains a *template-parameter* that declares
443
+ a template parameter pack [[temp.variadic]], the template parameter pack
444
+ will match zero or more template parameters or template parameter packs
445
+ declared in the *template-head* of `A` with the same type and form as
446
+ the template parameter pack declared in `P` (ignoring whether those
447
+ template parameters are template parameter packs).
448
 
449
  [*Example 2*:
450
 
451
  ``` cpp
452
  template<class T> class A { ... };
 
508
  S<Z> s3; // OK, P is at least as specialized as Z
509
  ```
510
 
511
  — *end example*]
512
 
513
+ A template template parameter `P` is at least as specialized as a
514
  template *template-argument* `A` if, given the following rewrite to two
515
  function templates, the function template corresponding to `P` is at
516
  least as specialized as the function template corresponding to `A`
517
  according to the partial ordering rules for function templates
518
  [[temp.func.order]]. Given an invented class template `X` with the
 
522
  - Each of the two function templates has the same template parameters
523
  and *requires-clause* (if any), respectively, as `P` or `A`.
524
  - Each function template has a single function parameter whose type is a
525
  specialization of `X` with template arguments corresponding to the
526
  template parameters from the respective function template where, for
527
+ each *template-parameter* `PP` in the *template-head* of the function
528
+ template, a corresponding *template-argument* `AA` is formed. If `PP`
529
  declares a template parameter pack, then `AA` is the pack expansion
530
+ `PP...` [[temp.variadic]]; otherwise, `AA` is an *id-expression*
531
+ denoting `PP`.
532
 
533
  If the rewrite produces an invalid type, then `P` is not at least as
534
  specialized as `A`.
535