From Jason Turner

[temp]

Large diff (277.3 KB) - rendering may be slow on some devices

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmphe3nu1_o/{from.md → to.md} +2742 -1137
tmp/tmphe3nu1_o/{from.md → to.md} RENAMED
@@ -1,40 +1,63 @@
1
  # Templates <a id="temp">[[temp]]</a>
2
 
3
- A *template* defines a family of classes, functions, or variables, or an
4
- alias for a family of types.
 
 
5
 
6
  ``` bnf
7
  template-declaration:
8
- 'template <' template-parameter-list '>' declaration
 
 
 
 
 
 
9
  ```
10
 
11
  ``` bnf
12
  template-parameter-list:
13
  template-parameter
14
  template-parameter-list ',' template-parameter
15
  ```
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  [*Note 1*: The `>` token following the *template-parameter-list* of a
18
  *template-declaration* may be the product of replacing a `>{>}` token by
19
- two consecutive `>` tokens ([[temp.names]]). — *end note*]
20
 
21
- The *declaration* in a *template-declaration* shall
22
 
23
  - declare or define a function, a class, or a variable, or
24
  - define a member function, a member class, a member enumeration, or a
25
  static data member of a class template or of a class nested within a
26
  class template, or
27
  - define a member template of a class or class template, or
28
  - be a *deduction-guide*, or
29
  - be an *alias-declaration*.
30
 
31
- A *template-declaration* is a *declaration*. A *template-declaration* is
32
- also a definition if its *declaration* defines a function, a class, a
33
- variable, or a static data member. A declaration introduced by a
34
- template declaration of a variable is a *variable template*. A variable
35
- template at class scope is a *static data member template*.
36
 
37
  [*Example 1*:
38
 
39
  ``` cpp
40
  template<class T>
@@ -45,79 +68,105 @@ T circular_area(T r) {
45
  }
46
  struct matrix_constants {
47
  template<class T>
48
  using pauli = hermitian_matrix<T, 2>;
49
  template<class T>
50
- constexpr pauli<T> sigma1 = { { 0, 1 }, { 1, 0 } };
51
  template<class T>
52
- constexpr pauli<T> sigma2 = { { 0, -1i }, { 1i, 0 } };
53
  template<class T>
54
- constexpr pauli<T> sigma3 = { { 1, 0 }, { 0, -1 } };
55
  };
56
  ```
57
 
58
  — *end example*]
59
 
60
  A *template-declaration* can appear only as a namespace scope or class
61
- scope declaration. In a function template declaration, the last
 
62
  component of the *declarator-id* shall not be a *template-id*.
63
 
64
  [*Note 2*: That last component may be an *identifier*, an
65
  *operator-function-id*, a *conversion-function-id*, or a
66
  *literal-operator-id*. In a class template declaration, if the class
67
  name is a *simple-template-id*, the declaration declares a class
68
- template partial specialization ([[temp.class.spec]]). — *end note*]
69
 
70
  In a *template-declaration*, explicit specialization, or explicit
71
  instantiation the *init-declarator-list* in the declaration shall
72
  contain at most one declarator. When such a declaration is used to
73
  declare a class template, no declarator is permitted.
74
 
75
- A template name has linkage ([[basic.link]]). Specializations (explicit
76
- or implicit) of a template that has internal linkage are distinct from
77
- all specializations in other translation units. A template, a template
78
- explicit specialization ([[temp.expl.spec]]), and a class template
79
- partial specialization shall not have C linkage. Use of a linkage
80
- specification other than `"C"` or `"C++"` with any of these constructs
81
- is conditionally-supported, with *implementation-defined* semantics.
82
- Template definitions shall obey the one-definition rule (
83
- [[basic.def.odr]]).
84
 
85
  [*Note 3*: Default arguments for function templates and for member
86
  functions of class templates are considered definitions for the purpose
87
- of template instantiation ([[temp.decls]]) and must also obey the
88
  one-definition rule. — *end note*]
89
 
90
  A class template shall not have the same name as any other template,
91
  class, function, variable, enumeration, enumerator, namespace, or type
92
- in the same scope ([[basic.scope]]), except as specified in 
93
  [[temp.class.spec]]. Except that a function template can be overloaded
94
- either by non-template functions ([[dcl.fct]]) with the same name or by
95
- other function templates with the same name ([[temp.over]]), a template
96
  name declared in namespace scope or in class scope shall be unique in
97
  that scope.
98
 
99
- A *templated entity* is
100
 
101
  - a template,
102
- - an entity defined ([[basic.def]]) or created ([[class.temporary]])
103
- in a templated entity,
104
  - a member of a templated entity,
105
  - an enumerator for an enumeration that is a templated entity, or
106
- - the closure type of a *lambda-expression* (
107
- [[expr.prim.lambda.closure]]) appearing in the declaration of a
108
- templated entity.
109
 
110
  [*Note 4*: A local class, a local variable, or a friend function
111
  defined in a templated entity is a templated entity. — *end note*]
112
 
113
- A function template, member function of a class template, variable
114
- template, or static data member of a class template shall be defined in
115
- every translation unit in which it is implicitly instantiated (
116
- [[temp.inst]]) unless the corresponding specialization is explicitly
117
- instantiated ([[temp.explicit]]) in some translation unit; no
118
- diagnostic is required.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
  ## Template parameters <a id="temp.param">[[temp.param]]</a>
121
 
122
  The syntax for *template-parameter*s is:
123
 
@@ -129,23 +178,31 @@ template-parameter:
129
 
130
  ``` bnf
131
  type-parameter:
132
  type-parameter-key '...'ₒₚₜ identifierₒₚₜ
133
  type-parameter-key identifierₒₚₜ '=' type-id
134
- 'template <' template-parameter-list '>' type-parameter-key '...'ₒₚₜ identifierₒₚₜ
135
- 'template <' template-parameter-list '>' type-parameter-key identifierₒₚₜ '=' id-expression
 
 
136
  ```
137
 
138
  ``` bnf
139
  type-parameter-key:
140
- 'class'
141
- 'typename'
 
 
 
 
 
 
142
  ```
143
 
144
  [*Note 1*: The `>` token following the *template-parameter-list* of a
145
  *type-parameter* may be the product of replacing a `>{>}` token by two
146
- consecutive `>` tokens ([[temp.names]]). — *end note*]
147
 
148
  There is no semantic difference between `class` and `typename` in a
149
  *type-parameter-key*. `typename` followed by an *unqualified-id* names a
150
  template type parameter. `typename` followed by a *qualified-id* denotes
151
  the type in a non-type [^1] *parameter-declaration*. A
@@ -193,66 +250,112 @@ class Map {
193
  };
194
  ```
195
 
196
  — *end note*]
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
  A non-type *template-parameter* shall have one of the following
199
- (optionally cv-qualified) types:
200
 
201
- - integral or enumeration type,
202
- - pointer to object or pointer to function,
203
- - lvalue reference to object or lvalue reference to function,
204
- - pointer to member,
205
- - `std::nullptr_t`, or
206
- - a type that contains a placeholder type ([[dcl.spec.auto]]).
207
-
208
- [*Note 3*: Other types are disallowed either explicitly below or
209
- implicitly by the rules governing the form of *template-argument*s (
210
- [[temp.arg]]). — *end note*]
211
 
212
  The top-level *cv-qualifier*s on the *template-parameter* are ignored
213
  when determining its type.
214
 
215
- A non-type non-reference *template-parameter* is a prvalue. It shall not
216
- be assigned to or in any other way have its value changed. A non-type
217
- non-reference *template-parameter* cannot have its address taken. When a
218
- non-type non-reference *template-parameter* is used as an initializer
219
- for a reference, a temporary is always used.
220
 
221
- [*Example 2*:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
 
223
  ``` cpp
224
- template<const X& x, int i> void f() {
 
 
225
  i++; // error: change of template-parameter value
226
 
227
  &x; // OK
228
  &i; // error: address of non-reference template-parameter
229
-
230
  int& ri = i; // error: non-const reference bound to temporary
231
  const int& cri = i; // OK: const reference bound to temporary
 
232
  }
233
  ```
234
 
235
  — *end example*]
236
 
237
- A non-type *template-parameter* shall not be declared to have
238
- floating-point, class, or void type.
239
 
240
- [*Example 3*:
 
 
 
241
 
242
  ``` cpp
243
- template<double d> class X; // error
244
- template<double* pd> class Y; // OK
245
- template<double& rd> class Z; // OK
246
  ```
247
 
248
  — *end example*]
249
 
 
 
250
  A non-type *template-parameter* of type “array of `T`” or of function
251
  type `T` is adjusted to be of type “pointer to `T`”.
252
 
253
- [*Example 4*:
254
 
255
  ``` cpp
256
  template<int* a> struct R { ... };
257
  template<int b[5]> struct S { ... };
258
  int p;
@@ -263,30 +366,35 @@ R<v> y; // OK due to implicit argument conversion
263
  S<v> z; // OK due to both adjustment and conversion
264
  ```
265
 
266
  — *end example*]
267
 
268
- A *default template-argument* is a *template-argument* ([[temp.arg]])
 
 
 
 
 
269
  specified after `=` in a *template-parameter*. A default
270
  *template-argument* may be specified for any kind of
271
  *template-parameter* (type, non-type, template) that is not a template
272
- parameter pack ([[temp.variadic]]). A default *template-argument* may
273
- be specified in a template declaration. A default *template-argument*
274
- shall not be specified in the *template-parameter-list*s of the
275
- definition of a member of a class template that appears outside of the
276
- member’s class. A default *template-argument* shall not be specified in
277
- a friend class template declaration. If a friend function template
278
- declaration specifies a default *template-argument*, that declaration
279
- shall be a definition and shall be the only declaration of the function
280
- template in the translation unit.
281
 
282
  The set of default *template-argument*s available for use is obtained by
283
  merging the default arguments from all prior declarations of the
284
- template in the same way default function arguments are (
285
- [[dcl.fct.default]]).
286
 
287
- [*Example 5*:
288
 
289
  ``` cpp
290
  template<class T1, class T2 = int> class A;
291
  template<class T1 = int, class T2> class A;
292
  ```
@@ -305,17 +413,17 @@ alias template has a default *template-argument*, each subsequent
305
  supplied or be a template parameter pack. If a *template-parameter* of a
306
  primary class template, primary variable template, or alias template is
307
  a template parameter pack, it shall be the last *template-parameter*. A
308
  template parameter pack of a function template shall not be followed by
309
  another template parameter unless that template parameter can be deduced
310
- from the parameter-type-list ([[dcl.fct]]) of the function template or
311
- has a default argument ([[temp.deduct]]). A template parameter of a
312
- deduction guide template ([[temp.deduct.guide]]) that does not have a
313
- default argument shall be deducible from the parameter-type-list of the
314
  deduction guide template.
315
 
316
- [*Example 6*:
317
 
318
  ``` cpp
319
  template<class T1 = int, class T2> class B; // error
320
 
321
  // U can be neither deduced from the parameter-type-list nor specified
@@ -326,11 +434,11 @@ template<class... T, class U> void g() { } // error
326
  — *end example*]
327
 
328
  A *template-parameter* shall not be given default arguments by two
329
  different declarations in the same scope.
330
 
331
- [*Example 7*:
332
 
333
  ``` cpp
334
  template<class T = int> class X;
335
  template<class T = int> class X { ... }; // error
336
  ```
@@ -339,11 +447,11 @@ template<class T = int> class X { ... }; // error
339
 
340
  When parsing a default *template-argument* for a non-type
341
  *template-parameter*, the first non-nested `>` is taken as the end of
342
  the *template-parameter-list* rather than a greater-than operator.
343
 
344
- [*Example 8*:
345
 
346
  ``` cpp
347
  template<int i = 3 > 4 > // syntax error
348
  class X { ... };
349
 
@@ -356,60 +464,63 @@ class Y { ... };
356
  A *template-parameter* of a template *template-parameter* is permitted
357
  to have a default *template-argument*. When such default arguments are
358
  specified, they apply to the template *template-parameter* in the scope
359
  of the template *template-parameter*.
360
 
361
- [*Example 9*:
362
 
363
  ``` cpp
364
- template <class T = float> struct B {};
365
  template <template <class TT = float> class T> struct A {
366
  inline void f();
367
  inline void g();
368
  };
369
  template <template <class TT> class T> void A<T>::f() {
370
- T<> t; // error - TT has no default template argument
371
  }
372
  template <template <class TT = char> class T> void A<T>::g() {
373
- T<> t; // OK - T<char>
374
  }
375
  ```
376
 
377
  — *end example*]
378
 
379
  If a *template-parameter* is a *type-parameter* with an ellipsis prior
380
  to its optional *identifier* or is a *parameter-declaration* that
381
- declares a parameter pack ([[dcl.fct]]), then the *template-parameter*
382
- is a template parameter pack ([[temp.variadic]]). A template parameter
383
- pack that is a *parameter-declaration* whose type contains one or more
384
- unexpanded parameter packs is a pack expansion. Similarly, a template
385
- parameter pack that is a *type-parameter* with a
386
- *template-parameter-list* containing one or more unexpanded parameter
387
- packs is a pack expansion. A template parameter pack that is a pack
388
- expansion shall not expand a parameter pack declared in the same
 
389
  *template-parameter-list*.
390
 
391
- [*Example 10*:
392
 
393
  ``` cpp
394
- template <class... Types> class Tuple; // Types is a template type parameter pack
395
- // but not a pack expansion
396
- template <class T, int... Dims> struct multi_array; // Dims is a non-type template parameter pack
397
- // but not a pack expansion
398
- template<class... T> struct value_holder {
 
 
 
399
  template <T... Values> struct apply { }; // Values is a non-type template parameter pack
400
- // and a pack expansion
401
- };
402
- template<class... T, T... Values> struct static_array;// error: Values expands template type parameter
403
- // pack T within the same template parameter list
404
  ```
405
 
406
  — *end example*]
407
 
408
  ## Names of template specializations <a id="temp.names">[[temp.names]]</a>
409
 
410
- A template specialization ([[temp.spec]]) can be referred to by a
411
  *template-id*:
412
 
413
  ``` bnf
414
  simple-template-id:
415
  template-name '<' template-argument-listₒₚₜ '>'
@@ -438,32 +549,39 @@ template-argument:
438
  constant-expression
439
  type-id
440
  id-expression
441
  ```
442
 
443
- [*Note 1*: The name lookup rules ([[basic.lookup]]) are used to
444
- associate the use of a name with a template declaration; that is, to
445
- identify a name as a *template-name*. — *end note*]
446
 
447
  For a *template-name* to be explicitly qualified by the template
448
- arguments, the name must be known to refer to a template.
449
-
450
- After name lookup ([[basic.lookup]]) finds that a name is a
451
- *template-name* or that an *operator-function-id* or a
452
- *literal-operator-id* refers to a set of overloaded functions any member
453
- of which is a function template, if this is followed by a `<`, the `<`
454
- is always taken as the delimiter of a *template-argument-list* and never
455
- as the less-than operator. When parsing a *template-argument-list*, the
456
- first non-nested `>`[^2] is taken as the ending delimiter rather than a
457
- greater-than operator. Similarly, the first non-nested `>{>}` is treated
458
- as two consecutive but distinct `>` tokens, the first of which is taken
459
- as the end of the *template-argument-list* and completes the
460
- *template-id*.
461
-
462
- [*Note 2*: The second `>` token produced by this replacement rule may
 
 
 
 
 
 
 
463
  terminate an enclosing *template-id* construct or it may be part of a
464
- different construct (e.g. a cast). — *end note*]
465
 
466
  [*Example 1*:
467
 
468
  ``` cpp
469
  template<int i> class X { ... };
@@ -480,46 +598,46 @@ Y<X<(6>>1)>> x5; // OK
480
  — *end example*]
481
 
482
  The keyword `template` is said to appear at the top level in a
483
  *qualified-id* if it appears outside of a *template-argument-list* or
484
  *decltype-specifier*. In a *qualified-id* of a *declarator-id* or in a
485
- *qualified-id* formed by a *class-head-name* (Clause  [[class]]) or
486
- *enum-head-name* ([[dcl.enum]]), the keyword `template` shall not
487
- appear at the top level. In a *qualified-id* used as the name in a
488
- *typename-specifier* ([[temp.res]]), *elaborated-type-specifier* (
489
- [[dcl.type.elab]]), *using-declaration* ([[namespace.udecl]]), or
490
- *class-or-decltype* (Clause  [[class.derived]]), an optional keyword
491
- `template` appearing at the top level is ignored. In these contexts, a
492
- `<` token is always assumed to introduce a *template-argument-list*. In
493
- all other contexts, when naming a template specialization of a member of
494
- an unknown specialization ([[temp.dep.type]]), the member template name
495
- shall be prefixed by the keyword `template`.
496
 
497
  [*Example 2*:
498
 
499
  ``` cpp
500
  struct X {
501
  template<std::size_t> X* alloc();
502
  template<std::size_t> static X* adjust();
503
  };
504
  template<class T> void f(T* p) {
505
- T* p1 = p->alloc<200>(); // ill-formed: < means less than
506
  T* p2 = p->template alloc<200>(); // OK: < starts template argument list
507
- T::adjust<100>(); // ill-formed: < means less than
508
  T::template adjust<100>(); // OK: < starts template argument list
509
  }
510
  ```
511
 
512
  — *end example*]
513
 
514
  A name prefixed by the keyword `template` shall be a *template-id* or
515
  the name shall refer to a class template or an alias template.
516
 
517
- [*Note 3*: The keyword `template` may not be applied to non-template
518
  members of class templates. — *end note*]
519
 
520
- [*Note 4*: As is the case with the `typename` prefix, the `template`
521
  prefix is allowed in cases where it is not strictly necessary; i.e.,
522
  when the *nested-name-specifier* or the expression on the left of the
523
  `->` or `.` is not dependent on a *template-parameter*, or the use does
524
  not appear in the scope of a template. — *end note*]
525
 
@@ -546,25 +664,114 @@ template <class T, template <class X> class TT = T::template C> struct D { };
546
  D<B<int> > db;
547
  ```
548
 
549
  — *end example*]
550
 
551
- A *simple-template-id* that names a class template specialization is a
552
- *class-name* (Clause  [[class]]).
553
 
554
- A *template-id* that names an alias template specialization is a
555
- *type-name*.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
556
 
557
  ## Template arguments <a id="temp.arg">[[temp.arg]]</a>
558
 
559
  There are three forms of *template-argument*, corresponding to the three
560
  forms of *template-parameter*: type, non-type and template. The type and
561
  form of each *template-argument* specified in a *template-id* shall
562
  match the type and form specified for the corresponding parameter
563
  declared by the template in its *template-parameter-list*. When the
564
- parameter declared by the template is a template parameter pack (
565
- [[temp.variadic]]), it will correspond to zero or more
566
  *template-argument*s.
567
 
568
  [*Example 1*:
569
 
570
  ``` cpp
@@ -649,11 +856,11 @@ template <template <class TT> class T> class A {
649
  template <class U> class B {
650
  private:
651
  struct S { ... };
652
  };
653
 
654
- A<B> b; // ill-formed: A has no access to B::S
655
  ```
656
 
657
  — *end example*]
658
 
659
  When template argument packs or default *template-argument*s are used, a
@@ -671,12 +878,12 @@ Tuple<>* t; // OK: Elements is empty
671
  Tuple* u; // syntax error
672
  ```
673
 
674
  — *end example*]
675
 
676
- An explicit destructor call ([[class.dtor]]) for an object that has a
677
- type that is a class template specialization may explicitly specify the
678
  *template-argument*s.
679
 
680
  [*Example 6*:
681
 
682
  ``` cpp
@@ -693,18 +900,18 @@ void f(A<int>* p, A<int>* q) {
693
 
694
  If the use of a *template-argument* gives rise to an ill-formed
695
  construct in the instantiation of a template specialization, the program
696
  is ill-formed.
697
 
698
- When the template in a *template-id* is an overloaded function template,
699
  both non-template functions in the overload set and function templates
700
  in the overload set for which the *template-argument*s do not match the
701
  *template-parameter*s are ignored. If none of the function templates
702
  have matching *template-parameter*s, the program is ill-formed.
703
 
704
  When a *simple-template-id* does not name a function, a default
705
- *template-argument* is implicitly instantiated ([[temp.inst]]) when the
706
  value of that default argument is needed.
707
 
708
  [*Example 7*:
709
 
710
  ``` cpp
@@ -715,12 +922,12 @@ S<bool>* p; // the type of p is S<bool, int>*
715
  The default argument for `U` is instantiated to form the type
716
  `S<bool, int>*`.
717
 
718
  — *end example*]
719
 
720
- A *template-argument* followed by an ellipsis is a pack expansion (
721
- [[temp.variadic]]).
722
 
723
  ### Template type arguments <a id="temp.arg.type">[[temp.arg.type]]</a>
724
 
725
  A *template-argument* for a *template-parameter* which is a type shall
726
  be a *type-id*.
@@ -746,37 +953,45 @@ void f() {
746
  }
747
  ```
748
 
749
  — *end example*]
750
 
751
- [*Note 1*: A template type argument may be an incomplete type (
752
- [[basic.types]]). — *end note*]
753
 
754
  ### Template non-type arguments <a id="temp.arg.nontype">[[temp.arg.nontype]]</a>
755
 
756
- If the type of a *template-parameter* contains a placeholder type (
757
- [[dcl.spec.auto]], [[temp.param]]), the deduced parameter type is
758
- determined from the type of the *template-argument* by placeholder type
759
- deduction ([[dcl.type.auto.deduct]]). If a deduced parameter type is
760
- not permitted for a *template-parameter* declaration ([[temp.param]]),
761
- the program is ill-formed.
 
 
 
 
 
762
 
763
  A *template-argument* for a non-type *template-parameter* shall be a
764
- converted constant expression ([[expr.const]]) of the type of the
765
- *template-parameter*. For a non-type *template-parameter* of reference
766
- or pointer type, the value of the constant expression shall not refer to
767
- (or for a pointer type, shall not be the address of):
768
 
769
- - a subobject ([[intro.object]]),
770
- - a temporary object ([[class.temporary]]),
771
- - a string literal ([[lex.string]]),
772
- - the result of a `typeid` expression ([[expr.typeid]]), or
773
- - a predefined `__func__` variable ([[dcl.fct.def.general]]).
774
 
775
- [*Note 1*: If the *template-argument* represents a set of overloaded
776
- functions (or a pointer or member pointer to such), the matching
777
- function is selected from the set ([[over.over]]). *end note*]
 
 
 
 
 
 
 
778
 
779
  [*Example 1*:
780
 
781
  ``` cpp
782
  template<const int* pci> struct X { ... };
@@ -798,76 +1013,67 @@ void f(int);
798
  template<void (*pf)(int)> struct A { ... };
799
 
800
  A<&f> a; // selects f(int)
801
 
802
  template<auto n> struct B { ... };
803
- B<5> b1; // OK: template parameter type is int
804
- B<'a'> b2; // OK: template parameter type is char
805
- B<2.5> b3; // error: template parameter type cannot be double
 
806
  ```
807
 
808
  — *end example*]
809
 
810
  [*Note 2*:
811
 
812
- A string literal ([[lex.string]]) is not an acceptable
813
- *template-argument*.
814
 
815
  [*Example 2*:
816
 
817
  ``` cpp
818
- template<class T, const char* p> class X {
819
  ...
820
  };
821
 
822
- X<int, "Studebaker"> x1; // error: string literal as template-argument
 
823
 
824
  const char p[] = "Vivisectionist";
825
- X<int,p> x2; // OK
 
 
 
 
 
 
826
  ```
827
 
828
  — *end example*]
829
 
830
  — *end note*]
831
 
832
  [*Note 3*:
833
 
834
- The address of an array element or non-static data member is not an
835
- acceptable *template-argument*.
836
-
837
- [*Example 3*:
838
-
839
- ``` cpp
840
- template<int* p> class X { };
841
-
842
- int a[10];
843
- struct S { int m; static int s; } s;
844
-
845
- X<&a[2]> x3; // error: address of array element
846
- X<&s.m> x4; // error: address of non-static member
847
- X<&s.s> x5; // OK: address of static member
848
- X<&S::s> x6; // OK: address of static member
849
- ```
850
-
851
- — *end example*]
852
-
853
- — *end note*]
854
-
855
- [*Note 4*:
856
-
857
  A temporary object is not an acceptable *template-argument* when the
858
  corresponding *template-parameter* has reference type.
859
 
860
- [*Example 4*:
861
 
862
  ``` cpp
863
  template<const int& CRI> struct B { ... };
864
 
865
- B<1> b2; // error: temporary would be required for template argument
866
 
867
  int c = 1;
868
- B<c> b1; // OK
 
 
 
 
 
 
869
  ```
870
 
871
  — *end example*]
872
 
873
  — *end note*]
@@ -880,11 +1086,11 @@ name of a class template or an alias template, expressed as
880
  only primary class templates are considered when matching the template
881
  template argument with the corresponding parameter; partial
882
  specializations are not considered even if their parameter lists match
883
  that of the template template parameter.
884
 
885
- Any partial specializations ([[temp.class.spec]]) associated with the
886
  primary class template or primary variable template are considered when
887
  a specialization based on the template *template-parameter* is
888
  instantiated. If a specialization is not visible at the point of
889
  instantiation, and it would have been selected had it been visible, the
890
  program is ill-formed, no diagnostic required.
@@ -907,24 +1113,25 @@ C<A> c; // V<int> within C<A> uses the primary template, so c.y.x ha
907
  ```
908
 
909
  — *end example*]
910
 
911
  A *template-argument* matches a template *template-parameter* `P` when
912
- `P` is at least as specialized as the *template-argument* `A`. If `P`
913
- contains a parameter pack, then `A` also matches `P` if each of `A`’s
914
- template parameters matches the corresponding template parameter in the
915
- *template-parameter-list* of `P`. Two template parameters match if they
916
- are of the same kind (type, non-type, template), for non-type
917
- *template-parameter*s, their types are equivalent ([[temp.over.link]]),
918
- and for template *template-parameter*s, each of their corresponding
919
- *template-parameter*s matches, recursively. When `P`’s
920
- *template-parameter-list* contains a template parameter pack (
921
- [[temp.variadic]]), the template parameter pack will match zero or more
922
- template parameters or template parameter packs in the
923
- *template-parameter-list* of `A` with the same type and form as the
924
- template parameter pack in `P` (ignoring whether those template
925
- parameters are template parameter packs).
 
926
 
927
  [*Example 2*:
928
 
929
  ``` cpp
930
  template<class T> class A { ... };
@@ -967,52 +1174,551 @@ eval<D<int, 17>> eD; // error: D does not match TT in partial special
967
  eval<E<int, float>> eE; // error: E does not match TT in partial specialization
968
  ```
969
 
970
  — *end example*]
971
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
972
  A template *template-parameter* `P` is at least as specialized as a
973
  template *template-argument* `A` if, given the following rewrite to two
974
  function templates, the function template corresponding to `P` is at
975
  least as specialized as the function template corresponding to `A`
976
- according to the partial ordering rules for function templates (
977
- [[temp.func.order]]). Given an invented class template `X` with the
978
- template parameter list of `A` (including default arguments):
 
979
 
980
- - Each of the two function templates has the same template parameters,
981
- respectively, as `P` or `A`.
982
  - Each function template has a single function parameter whose type is a
983
  specialization of `X` with template arguments corresponding to the
984
  template parameters from the respective function template where, for
985
- each template parameter `PP` in the template parameter list of the
986
- function template, a corresponding template argument `AA` is formed.
987
- If `PP` declares a parameter pack, then `AA` is the pack expansion
988
- `PP...` ([[temp.variadic]]); otherwise, `AA` is the *id-expression*
989
  `PP`.
990
 
991
  If the rewrite produces an invalid type, then `P` is not at least as
992
  specialized as `A`.
993
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
994
  ## Type equivalence <a id="temp.type">[[temp.type]]</a>
995
 
996
- Two *template-id*s refer to the same class, function, or variable if
997
 
998
  - their *template-name*s, *operator-function-id*s, or
999
- *literal-operator-id*s refer to the same template and
1000
- - their corresponding type *template-argument*s are the same type and
1001
- - their corresponding non-type template arguments of integral or
1002
- enumeration type have identical values and
1003
- - their corresponding non-type *template-argument*s of pointer type
1004
- refer to the same object or function or are both the null pointer
1005
- value and
1006
- - their corresponding non-type *template-argument*s of pointer-to-member
1007
- type refer to the same class member or are both the null member
1008
- pointer value and
1009
- - their corresponding non-type *template-argument*s of reference type
1010
- refer to the same object or function and
1011
  - their corresponding template *template-argument*s refer to the same
1012
  template.
1013
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1014
  [*Example 1*:
1015
 
1016
  ``` cpp
1017
  template<class E, int size> class buffer { ... };
1018
  buffer<char,2*512> x;
@@ -1042,14 +1748,14 @@ X<Z<int> > z;
1042
 
1043
  declares `y` and `z` to be of the same type.
1044
 
1045
  — *end example*]
1046
 
1047
- If an expression e is type-dependent ([[temp.dep.expr]]), `decltype(e)`
1048
  denotes a unique dependent type. Two such *decltype-specifier*s refer to
1049
- the same type only if their *expression*s are equivalent (
1050
- [[temp.over.link]]).
1051
 
1052
  [*Note 1*: However, such a type may be aliased, e.g., by a
1053
  *typedef-name*. — *end note*]
1054
 
1055
  ## Template declarations <a id="temp.decls">[[temp.decls]]</a>
@@ -1066,20 +1772,22 @@ template<class T1, int I> void sort<T1, I>(T1 data[I]); // error
1066
  ```
1067
 
1068
  — *end example*]
1069
 
1070
  [*Note 1*: However, this syntax is allowed in class template partial
1071
- specializations ([[temp.class.spec]]). — *end note*]
1072
 
1073
- For purposes of name lookup and instantiation, default arguments and
1074
- *noexcept-specifier*s of function templates and default arguments and
1075
- *noexcept-specifier*s of member functions of class templates are
1076
- considered definitions; each default argument or *noexcept-specifier* is
1077
- a separate definition which is unrelated to the function template
1078
- definition or to any other default arguments or *noexcept-specifier*s.
1079
- For the purpose of instantiation, the substatements of a constexpr if
1080
- statement ([[stmt.if]]) are considered definitions.
 
 
1081
 
1082
  Because an *alias-declaration* cannot declare a *template-id*, it is not
1083
  possible to partially or explicitly specialize an alias template.
1084
 
1085
  ### Class templates <a id="temp.class">[[temp.class]]</a>
@@ -1112,18 +1820,18 @@ other words, `Array` is a parameterized type with `T` as its parameter.
1112
  — *end example*]
1113
 
1114
  When a member function, a member class, a member enumeration, a static
1115
  data member or a member template of a class template is defined outside
1116
  of the class template definition, the member definition is defined as a
1117
- template definition in which the *template-parameter*s are those of the
1118
- class template. The names of the template parameters used in the
1119
- definition of the member may be different from the template parameter
1120
- names used in the class template definition. The template argument list
1121
- following the class template name in the member definition shall name
1122
- the parameters in the same order as the one used in the template
1123
- parameter list of the member. Each template parameter pack shall be
1124
- expanded with an ellipsis in the template argument list.
1125
 
1126
  [*Example 2*:
1127
 
1128
  ``` cpp
1129
  template<class T1, class T2> struct A {
@@ -1143,16 +1851,36 @@ template<class ... Types> struct B {
1143
 
1144
  template<class ... Types> void B<Types ...>::f3() { } // OK
1145
  template<class ... Types> void B<Types>::f4() { } // error
1146
  ```
1147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1148
  — *end example*]
1149
 
1150
  In a redeclaration, partial specialization, explicit specialization or
1151
  explicit instantiation of a class template, the *class-key* shall agree
1152
- in kind with the original class template declaration (
1153
- [[dcl.type.elab]]).
1154
 
1155
  #### Member functions of class templates <a id="temp.mem.func">[[temp.mem.func]]</a>
1156
 
1157
  A member function of a class template may be defined outside of the
1158
  class template definition in which it is declared.
@@ -1168,50 +1896,110 @@ public:
1168
  T& operator[](int);
1169
  T& elem(int i) { return v[i]; }
1170
  };
1171
  ```
1172
 
1173
- declares three function templates. The subscript function might be
1174
- defined like this:
1175
 
1176
  ``` cpp
1177
  template<class T> T& Array<T>::operator[](int i) {
1178
  if (i<0 || sz<=i) error("Array: range error");
1179
  return v[i];
1180
  }
1181
  ```
1182
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1183
  — *end example*]
1184
 
1185
  The *template-argument*s for a member function of a class template are
1186
  determined by the *template-argument*s of the type of the object for
1187
  which the member function is called.
1188
 
1189
  [*Example 2*:
1190
 
1191
- The *template-argument* for `Array<T>::operator[]()` will be determined
1192
- by the `Array` to which the subscripting operation is applied.
1193
 
1194
  ``` cpp
1195
  Array<int> v1(20);
1196
  Array<dcomplex> v2(30);
1197
 
1198
- v1[3] = 7; // Array<int>::operator[]()
1199
- v2[3] = dcomplex(7,8); // Array<dcomplex>::operator[]()
1200
  ```
1201
 
1202
  — *end example*]
1203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1204
  #### Member classes of class templates <a id="temp.mem.class">[[temp.mem.class]]</a>
1205
 
1206
  A member class of a class template may be defined outside the class
1207
  template definition in which it is declared.
1208
 
1209
  [*Note 1*:
1210
 
1211
  The member class must be defined before its first use that requires an
1212
- instantiation ([[temp.inst]]). For example,
1213
 
1214
  ``` cpp
1215
  template<class T> struct A {
1216
  class B;
1217
  };
@@ -1284,13 +2072,13 @@ A<int>::E e = A<int>::e1;
1284
 
1285
  A template can be declared within a class or class template; such a
1286
  template is called a member template. A member template can be defined
1287
  within or outside its class definition or class template definition. A
1288
  member template of a class template that is defined outside of its class
1289
- template definition shall be specified with the *template-parameter*s of
1290
- the class template followed by the *template-parameter*s of the member
1291
- template.
1292
 
1293
  [*Example 1*:
1294
 
1295
  ``` cpp
1296
  template<class T> struct string {
@@ -1302,20 +2090,39 @@ template<class T> template<class T2> int string<T>::compare(const T2& s) {
1302
  }
1303
  ```
1304
 
1305
  — *end example*]
1306
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1307
  A local class of non-closure type shall not have member templates.
1308
- Access control rules (Clause  [[class.access]]) apply to member template
1309
- names. A destructor shall not be a member template. A non-template
1310
- member function ([[dcl.fct]]) with a given name and type and a member
1311
- function template of the same name, which could be used to generate a
1312
  specialization of the same type, can both be declared in a class. When
1313
  both exist, a use of that name and type refers to the non-template
1314
  member unless an explicit template argument list is supplied.
1315
 
1316
- [*Example 2*:
1317
 
1318
  ``` cpp
1319
  template <class T> struct A {
1320
  void f(int);
1321
  template <class T2> void f(T2);
@@ -1334,11 +2141,11 @@ int main() {
1334
 
1335
  — *end example*]
1336
 
1337
  A member function template shall not be virtual.
1338
 
1339
- [*Example 3*:
1340
 
1341
  ``` cpp
1342
  template <class T> struct AA {
1343
  template <class C> virtual void g(C); // error
1344
  virtual void f(); // OK
@@ -1348,11 +2155,11 @@ template <class T> struct AA {
1348
  — *end example*]
1349
 
1350
  A specialization of a member function template does not override a
1351
  virtual function from a base class.
1352
 
1353
- [*Example 4*:
1354
 
1355
  ``` cpp
1356
  class B {
1357
  virtual void f(int);
1358
  };
@@ -1367,11 +2174,11 @@ class D : public B {
1367
 
1368
  A specialization of a conversion function template is referenced in the
1369
  same way as a non-template conversion function that converts to the same
1370
  type.
1371
 
1372
- [*Example 5*:
1373
 
1374
  ``` cpp
1375
  struct A {
1376
  template <class T> operator T*();
1377
  };
@@ -1386,27 +2193,25 @@ int main() {
1386
  }
1387
  ```
1388
 
1389
  — *end example*]
1390
 
1391
- [*Note 1*: Because the explicit template argument list follows the
1392
- function template name, and because conversion member function templates
1393
- and constructor member function templates are called without using a
1394
- function name, there is no way to provide an explicit template argument
1395
- list for these function templates. — *end note*]
1396
 
1397
  A specialization of a conversion function template is not found by name
1398
  lookup. Instead, any conversion function templates visible in the
1399
  context of the use are considered. For each such operator, if argument
1400
- deduction succeeds ([[temp.deduct.conv]]), the resulting specialization
1401
- is used as if found by name lookup.
1402
 
1403
  A *using-declaration* in a derived class cannot refer to a
1404
  specialization of a conversion function template in a base class.
1405
 
1406
- Overload resolution ([[over.ics.rank]]) and partial ordering (
1407
- [[temp.func.order]]) are used to select the best conversion function
1408
  among multiple specializations of conversion function templates and/or
1409
  non-template conversion functions.
1410
 
1411
  ### Variadic templates <a id="temp.variadic">[[temp.variadic]]</a>
1412
 
@@ -1432,84 +2237,103 @@ more function arguments.
1432
  [*Example 2*:
1433
 
1434
  ``` cpp
1435
  template<class ... Types> void f(Types ... args);
1436
 
1437
- f(); // OK: args contains no arguments
1438
- f(1); // OK: args contains one argument: int
1439
- f(2, 1.0); // OK: args contains two arguments: int and double
1440
  ```
1441
 
1442
  — *end example*]
1443
 
1444
- A *parameter pack* is either a template parameter pack or a function
1445
- parameter pack.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1446
 
1447
  A *pack expansion* consists of a *pattern* and an ellipsis, the
1448
  instantiation of which produces zero or more instantiations of the
1449
  pattern in a list (described below). The form of the pattern depends on
1450
  the context in which the expansion occurs. Pack expansions can occur in
1451
  the following contexts:
1452
 
1453
- - In a function parameter pack ([[dcl.fct]]); the pattern is the
1454
  *parameter-declaration* without the ellipsis.
1455
- - In a *using-declaration* ([[namespace.udecl]]); the pattern is a
1456
  *using-declarator*.
1457
- - In a template parameter pack that is a pack expansion (
1458
- [[temp.param]]):
1459
  - if the template parameter pack is a *parameter-declaration*; the
1460
  pattern is the *parameter-declaration* without the ellipsis;
1461
- - if the template parameter pack is a *type-parameter* with a
1462
- *template-parameter-list*; the pattern is the corresponding
1463
- *type-parameter* without the ellipsis.
1464
- - In an *initializer-list* ([[dcl.init]]); the pattern is an
1465
  *initializer-clause*.
1466
- - In a *base-specifier-list* (Clause  [[class.derived]]); the pattern is
1467
- a *base-specifier*.
1468
- - In a *mem-initializer-list* ([[class.base.init]]) for a
1469
  *mem-initializer* whose *mem-initializer-id* denotes a base class; the
1470
  pattern is the *mem-initializer*.
1471
- - In a *template-argument-list* ([[temp.arg]]); the pattern is a
1472
  *template-argument*.
1473
- - In an *attribute-list* ([[dcl.attr.grammar]]); the pattern is an
1474
  *attribute*.
1475
- - In an *alignment-specifier* ([[dcl.align]]); the pattern is the
1476
  *alignment-specifier* without the ellipsis.
1477
- - In a *capture-list* ([[expr.prim.lambda]]); the pattern is a
1478
- *capture*.
1479
- - In a `sizeof...` expression ([[expr.sizeof]]); the pattern is an
1480
  *identifier*.
1481
- - In a *fold-expression* ([[expr.prim.fold]]); the pattern is the
1482
- *cast-expression* that contains an unexpanded parameter pack.
1483
 
1484
- [*Example 3*:
1485
 
1486
  ``` cpp
1487
  template<class ... Types> void f(Types ... rest);
1488
  template<class ... Types> void g(Types ... rest) {
1489
  f(&rest ...); // ``&rest ...'' is a pack expansion; ``&rest'' is its pattern
1490
  }
1491
  ```
1492
 
1493
  — *end example*]
1494
 
1495
- For the purpose of determining whether a parameter pack satisfies a rule
1496
- regarding entities other than parameter packs, the parameter pack is
1497
- considered to be the entity that would result from an instantiation of
1498
- the pattern in which it appears.
1499
 
1500
- A parameter pack whose name appears within the pattern of a pack
1501
- expansion is expanded by that pack expansion. An appearance of the name
1502
- of a parameter pack is only expanded by the innermost enclosing pack
1503
- expansion. The pattern of a pack expansion shall name one or more
1504
- parameter packs that are not expanded by a nested pack expansion; such
1505
- parameter packs are called *unexpanded parameter packs* in the pattern.
1506
- All of the parameter packs expanded by a pack expansion shall have the
1507
- same number of arguments specified. An appearance of a name of a
1508
- parameter pack that is not expanded is ill-formed.
1509
 
1510
- [*Example 4*:
1511
 
1512
  ``` cpp
1513
  template<typename...> struct Tuple {};
1514
  template<typename T1, typename T2> struct Pair {};
1515
 
@@ -1525,36 +2349,40 @@ typedef zip<short>::with<unsigned short, unsigned>::type T2;
1525
  // error: different number of arguments specified for Args1 and Args2
1526
 
1527
  template<class ... Args>
1528
  void g(Args ... args) { // OK: Args is expanded by the function parameter pack args
1529
  f(const_cast<const Args*>(&args)...); // OK: ``Args'' and ``args'' are expanded
1530
- f(5 ...); // error: pattern does not contain any parameter packs
1531
- f(args); // error: parameter pack ``args'' is not expanded
1532
  f(h(args ...) + args ...); // OK: first ``args'' expanded within h,
1533
  // second ``args'' expanded within f
1534
  }
1535
  ```
1536
 
1537
  — *end example*]
1538
 
1539
  The instantiation of a pack expansion that is neither a `sizeof...`
1540
- expression nor a *fold-expression* produces a list
1541
- $\mathtt{E}_1, \mathtt{E}_2, \dotsc, \mathtt{E}_N$, where N is the
1542
- number of elements in the pack expansion parameters. Each Eᵢ is
1543
- generated by instantiating the pattern and replacing each pack expansion
1544
- parameter with its ith element. Such an element, in the context of the
1545
- instantiation, is interpreted as follows:
1546
 
1547
  - if the pack is a template parameter pack, the element is a template
1548
- parameter ([[temp.param]]) of the corresponding kind (type or
1549
- non-type) designating the type or value from the template argument;
1550
- otherwise,
1551
  - if the pack is a function parameter pack, the element is an
1552
- *id-expression* designating the function parameter that resulted from
1553
- the instantiation of the pattern where the pack is declared.
 
 
 
 
 
1554
 
1555
- All of the Eᵢ become elements in the enclosing list.
1556
 
1557
  [*Note 1*: The variety of list varies with the context:
1558
  *expression-list*, *base-specifier-list*, *template-argument-list*,
1559
  etc. — *end note*]
1560
 
@@ -1562,11 +2390,11 @@ When N is zero, the instantiation of the expansion produces an empty
1562
  list. Such an instantiation does not alter the syntactic interpretation
1563
  of the enclosing construct, even in cases where omitting the list
1564
  entirely would otherwise be ill-formed or would result in an ambiguity
1565
  in the grammar.
1566
 
1567
- [*Example 5*:
1568
 
1569
  ``` cpp
1570
  template<class... T> struct X : T... { };
1571
  template<class... T> void f(T... values) {
1572
  X<T...> x(values...);
@@ -1576,13 +2404,13 @@ template void f<>(); // OK: X<> has no base classes
1576
  // x is a variable of type X<> that is value-initialized
1577
  ```
1578
 
1579
  — *end example*]
1580
 
1581
- The instantiation of a `sizeof...` expression ([[expr.sizeof]])
1582
- produces an integral constant containing the number of elements in the
1583
- parameter pack it expands.
1584
 
1585
  The instantiation of a *fold-expression* produces:
1586
 
1587
  - `((`E₁ *op* E₂`)` *op* ⋯`)` *op* $\mathtt{E}_N$ for a unary left fold,
1588
  - E₁ *op* `(`⋯ *op* `(`$\mathtt{E}_{N-1}$ *op* $\mathtt{E}_N$`))` for a
@@ -1593,15 +2421,15 @@ The instantiation of a *fold-expression* produces:
1593
  E`)))` for a binary right fold.
1594
 
1595
  In each case, *op* is the *fold-operator*, N is the number of elements
1596
  in the pack expansion parameters, and each Eᵢ is generated by
1597
  instantiating the pattern and replacing each pack expansion parameter
1598
- with its ith element. For a binary fold-expression, E is generated by
1599
  instantiating the *cast-expression* that did not contain an unexpanded
1600
- parameter pack.
1601
 
1602
- [*Example 6*:
1603
 
1604
  ``` cpp
1605
  template<typename ...Args>
1606
  bool all(Args ...args) { return (... && args); }
1607
 
@@ -1612,17 +2440,17 @@ Within the instantiation of `all`, the returned expression expands to
1612
  `((true && true) && true) && false`, which evaluates to `false`.
1613
 
1614
  — *end example*]
1615
 
1616
  If N is zero for a unary fold-expression, the value of the expression is
1617
- shown in Table  [[tab:fold.empty]]; if the operator is not listed in
1618
- Table  [[tab:fold.empty]], the instantiation is ill-formed.
1619
 
1620
- **Table: Value of folding empty sequences** <a id="tab:fold.empty">[tab:fold.empty]</a>
1621
 
1622
- | Operator | Value when parameter pack is empty |
1623
- | -------- | ---------------------------------- |
1624
  | `&&` | `true` |
1625
  | `||` | `false` |
1626
  | `,` | `void()` |
1627
 
1628
 
@@ -1640,11 +2468,11 @@ declaration that is not a template declaration:
1640
  non-template function is found in the specified class or namespace,
1641
  the friend declaration refers to that function, otherwise,
1642
  - if the name of the friend is a *qualified-id* and a matching function
1643
  template is found in the specified class or namespace, the friend
1644
  declaration refers to the deduced specialization of that function
1645
- template ([[temp.deduct.decl]]), otherwise,
1646
  - the name shall be an *unqualified-id* that declares (or redeclares) a
1647
  non-template function.
1648
 
1649
  [*Example 1*:
1650
 
@@ -1695,13 +2523,13 @@ class A {
1695
  ```
1696
 
1697
  — *end example*]
1698
 
1699
  A template friend declaration specifies that all specializations of that
1700
- template, whether they are implicitly instantiated ([[temp.inst]]),
1701
- partially specialized ([[temp.class.spec]]) or explicitly specialized (
1702
- [[temp.expl.spec]]), are friends of the class containing the template
1703
  friend declaration.
1704
 
1705
  [*Example 3*:
1706
 
1707
  ``` cpp
@@ -1714,52 +2542,63 @@ template<class T> struct A { X::Y ab; }; // OK
1714
  template<class T> struct A<T*> { X::Y ab; }; // OK
1715
  ```
1716
 
1717
  — *end example*]
1718
 
1719
- A member of a class template may be declared to be a friend of a
1720
- non-template class. In this case, the corresponding member of every
1721
- specialization of the primary class template and class template partial
1722
- specializations thereof is a friend of the class granting friendship.
1723
- For explicit specializations and specializations of partial
1724
- specializations, the corresponding member is the member (if any) that
1725
- has the same name, kind (type, function, class template, or function
1726
- template), template parameters, and signature as the member of the class
1727
- template instantiation that would otherwise have been generated.
 
 
 
1728
 
1729
  [*Example 4*:
1730
 
1731
  ``` cpp
1732
  template<class T> struct A {
1733
  struct B { };
1734
  void f();
1735
  struct D {
1736
  void g();
1737
  };
 
 
1738
  };
1739
  template<> struct A<int> {
1740
  struct B { };
1741
  int f();
1742
  struct D {
1743
  void g();
1744
  };
 
 
 
 
1745
  };
1746
 
1747
  class C {
1748
  template<class T> friend struct A<T>::B; // grants friendship to A<int>::B even though
1749
  // it is not a specialization of A<T>::B
1750
  template<class T> friend void A<T>::f(); // does not grant friendship to A<int>::f()
1751
  // because its return type does not match
1752
- template<class T> friend void A<T>::D::g(); // does not grant friendship to A<int>::D::g()
1753
- // because A<int>::D is not a specialization of A<T>::D
1754
- };
 
 
1755
  ```
1756
 
1757
  — *end example*]
1758
 
1759
  [*Note 1*: A friend declaration may first declare a member of an
1760
- enclosing namespace scope ([[temp.inject]]). — *end note*]
1761
 
1762
  A friend template shall not be declared in a local class.
1763
 
1764
  Friend declarations shall not declare partial specializations.
1765
 
@@ -1774,32 +2613,40 @@ class X {
1774
 
1775
  — *end example*]
1776
 
1777
  When a friend declaration refers to a specialization of a function
1778
  template, the function parameter declarations shall not include default
1779
- arguments, nor shall the inline specifier be used in such a declaration.
 
 
 
 
 
 
 
 
1780
 
1781
  ### Class template partial specializations <a id="temp.class.spec">[[temp.class.spec]]</a>
1782
 
1783
  A *primary class template* declaration is one in which the class
1784
  template name is an identifier. A template declaration in which the
1785
  class template name is a *simple-template-id* is a *partial
1786
  specialization* of the class template named in the *simple-template-id*.
1787
  A partial specialization of a class template provides an alternative
1788
  definition of the template that is used instead of the primary
1789
  definition when the arguments in a specialization match those given in
1790
- the partial specialization ([[temp.class.spec.match]]). The primary
1791
  template shall be declared before any specializations of that template.
1792
  A partial specialization shall be declared before the first use of a
1793
  class template specialization that would make use of the partial
1794
  specialization as the result of an implicit or explicit instantiation in
1795
  every translation unit in which such a use occurs; no diagnostic is
1796
  required.
1797
 
1798
  Each class template partial specialization is a distinct template and
1799
  definitions shall be provided for the members of a template partial
1800
- specialization ([[temp.class.spec.mfunc]]).
1801
 
1802
  [*Example 1*:
1803
 
1804
  ``` cpp
1805
  template<class T1, class T2, int I> class A { };
@@ -1813,25 +2660,46 @@ The first declaration declares the primary (unspecialized) class
1813
  template. The second and subsequent declarations declare partial
1814
  specializations of the primary template.
1815
 
1816
  — *end example*]
1817
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1818
  The template parameters are specified in the angle bracket enclosed list
1819
  that immediately follows the keyword `template`. For partial
1820
  specializations, the template argument list is explicitly written
1821
  immediately following the class template name. For primary templates,
1822
  this list is implicitly described by the template parameter list.
1823
  Specifically, the order of the template arguments is the sequence in
1824
  which they appear in the template parameter list.
1825
 
1826
- [*Example 2*: The template argument list for the primary template in
1827
  the example above is `<T1,` `T2,` `I>`. — *end example*]
1828
 
1829
  [*Note 1*:
1830
 
1831
- The template argument list shall not be specified in the primary
1832
- template declaration. For example,
1833
 
1834
  ``` cpp
1835
  template<class T1, class T2, int I>
1836
  class A<T1, T2, I> { }; // error
1837
  ```
@@ -1840,11 +2708,11 @@ class A<T1, T2, I> { }; // error
1840
 
1841
  A class template partial specialization may be declared in any scope in
1842
  which the corresponding primary template may be defined (
1843
  [[namespace.memdef]], [[class.mem]], [[temp.mem]]).
1844
 
1845
- [*Example 3*:
1846
 
1847
  ``` cpp
1848
  template<class T> struct A {
1849
  struct C {
1850
  template<class T2> struct B { };
@@ -1866,11 +2734,11 @@ lookup. Rather, when the primary template name is used, any
1866
  previously-declared partial specializations of the primary template are
1867
  also considered. One consequence is that a *using-declaration* which
1868
  refers to a class template does not restrict the set of partial
1869
  specializations which may be found through the *using-declaration*.
1870
 
1871
- [*Example 4*:
1872
 
1873
  ``` cpp
1874
  namespace N {
1875
  template<class T1, class T2> class A { }; // primary template
1876
  }
@@ -1894,28 +2762,37 @@ Within the argument list of a class template partial specialization, the
1894
  following restrictions apply:
1895
 
1896
  - The type of a template parameter corresponding to a specialized
1897
  non-type argument shall not be dependent on a parameter of the
1898
  specialization.
1899
- \[*Example 5*:
1900
  ``` cpp
1901
  template <class T, T t> struct C {};
1902
  template <class T> struct C<T, 1>; // error
1903
 
1904
  template< int X, int (*array_ptr)[X] > class A {};
1905
  int array[5];
1906
  template< int X > class A<X,&array> { }; // error
1907
  ```
1908
 
1909
  — *end example*]
1910
- - The specialization shall be more specialized than the primary
1911
- template ([[temp.class.order]]).
1912
  - The template parameter list of a specialization shall not contain
1913
- default template argument values.[^4]
1914
- - An argument shall not contain an unexpanded parameter pack. If an
1915
- argument is a pack expansion ([[temp.variadic]]), it shall be the
1916
- last argument in the template argument list.
 
 
 
 
 
 
 
 
 
1917
 
1918
  #### Matching of class template partial specializations <a id="temp.class.spec.match">[[temp.class.spec.match]]</a>
1919
 
1920
  When a class template is used in a context that requires an
1921
  instantiation of the class, it is necessary to determine whether the
@@ -1925,21 +2802,23 @@ arguments of the class template specialization with the template
1925
  argument lists of the partial specializations.
1926
 
1927
  - If exactly one matching specialization is found, the instantiation is
1928
  generated from that specialization.
1929
  - If more than one matching specialization is found, the partial order
1930
- rules ([[temp.class.order]]) are used to determine whether one of the
1931
  specializations is more specialized than the others. If none of the
1932
  specializations is more specialized than all of the other matching
1933
  specializations, then the use of the class template is ambiguous and
1934
  the program is ill-formed.
1935
  - If no matches are found, the instantiation is generated from the
1936
  primary template.
1937
 
1938
  A partial specialization matches a given actual template argument list
1939
  if the template arguments of the partial specialization can be deduced
1940
- from the actual template argument list ([[temp.deduct]]).
 
 
1941
 
1942
  [*Example 1*:
1943
 
1944
  ``` cpp
1945
  template<class T1, class T2, int I> class A { }; // #1
@@ -1955,15 +2834,31 @@ A<int, char*, 1> a4; // uses #5, T1 is int, T2 is char, I is 1
1955
  A<int*, int*, 2> a5; // ambiguous: matches #3 and #5
1956
  ```
1957
 
1958
  — *end example*]
1959
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1960
  If the template arguments of a partial specialization cannot be deduced
1961
  because of the structure of its *template-parameter-list* and the
1962
  *template-id*, the program is ill-formed.
1963
 
1964
- [*Example 2*:
1965
 
1966
  ``` cpp
1967
  template <int I, int J> struct A {};
1968
  template <int I> struct A<I+5, I*2> {}; // error
1969
 
@@ -1983,15 +2878,16 @@ are deduced from the arguments of the primary template.
1983
  #### Partial ordering of class template specializations <a id="temp.class.order">[[temp.class.order]]</a>
1984
 
1985
  For two class template partial specializations, the first is *more
1986
  specialized* than the second if, given the following rewrite to two
1987
  function templates, the first function template is more specialized than
1988
- the second according to the ordering rules for function templates (
1989
- [[temp.func.order]]):
1990
 
1991
- - Each of the two function templates has the same template parameters as
1992
- the corresponding partial specialization.
 
1993
  - Each function template has a single function parameter whose type is a
1994
  class template specialization where the template arguments are the
1995
  corresponding template parameters from the function template for each
1996
  template argument in the *template-argument-list* of the
1997
  *simple-template-id* of the partial specialization.
@@ -2021,21 +2917,40 @@ function template *D* is more specialized than the function template
2021
  the partial specialization \#1 and the partial specialization \#4 is
2022
  more specialized than the partial specialization \#3.
2023
 
2024
  — *end example*]
2025
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2026
  #### Members of class template specializations <a id="temp.class.spec.mfunc">[[temp.class.spec.mfunc]]</a>
2027
 
2028
  The template parameter list of a member of a class template partial
2029
  specialization shall match the template parameter list of the class
2030
  template partial specialization. The template argument list of a member
2031
  of a class template partial specialization shall match the template
2032
  argument list of the class template partial specialization. A class
2033
- template specialization is a distinct template. The members of the class
2034
- template partial specialization are unrelated to the members of the
2035
- primary template. Class template partial specialization members that are
2036
- used in a way that requires a definition shall be defined; the
2037
  definitions of members of the primary template are never used as
2038
  definitions for members of a class template partial specialization. An
2039
  explicit specialization of a member of a class template partial
2040
  specialization is declared in the same way as an explicit specialization
2041
  of the primary template.
@@ -2068,11 +2983,11 @@ int main() {
2068
  A<char,0> a0;
2069
  A<char,2> a2;
2070
  a0.f(); // OK, uses definition of primary template's member
2071
  a2.g(); // OK, uses definition of partial specialization's member
2072
  a2.h(); // OK, uses definition of explicit specialization's member
2073
- a2.f(); // ill-formed, no definition of f for A<T,2>; the primary template is not used here
2074
  }
2075
  ```
2076
 
2077
  — *end example*]
2078
 
@@ -2122,14 +3037,14 @@ template<class T> void sort(Array<T>&);
2122
  ```
2123
 
2124
  — *end example*]
2125
 
2126
  A function template can be overloaded with other function templates and
2127
- with non-template functions ([[dcl.fct]]). A non-template function is
2128
- not related to a function template (i.e., it is never considered to be a
2129
  specialization), even if it has the same name and type as a potentially
2130
- generated function template specialization.[^5]
2131
 
2132
  #### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
2133
 
2134
  It is possible to overload function templates so that two different
2135
  function template specializations have the same type.
@@ -2155,16 +3070,16 @@ void h(int* p) {
2155
  ```
2156
 
2157
  — *end example*]
2158
 
2159
  Such specializations are distinct functions and do not violate the
2160
- one-definition rule ([[basic.def.odr]]).
2161
 
2162
- The signature of a function template is defined in Clause 
2163
- [[intro.defs]]. The names of the template parameters are significant
2164
- only for establishing the relationship between the template parameters
2165
- and the rest of the signature.
2166
 
2167
  [*Note 1*:
2168
 
2169
  Two distinct function templates may have identical function return types
2170
  and function parameter lists, even if overload resolution alone cannot
@@ -2202,120 +3117,186 @@ template parameters, but it is possible for an expression to reference a
2202
  type parameter. For example, a template type parameter can be used in
2203
  the `sizeof` operator. — *end note*]
2204
 
2205
  Two expressions involving template parameters are considered
2206
  *equivalent* if two function definitions containing the expressions
2207
- would satisfy the one-definition rule ([[basic.def.odr]]), except that
2208
- the tokens used to name the template parameters may differ as long as a
2209
  token used to name a template parameter in one expression is replaced by
2210
  another token that names the same template parameter in the other
2211
- expression. For determining whether two dependent names ([[temp.dep]])
2212
- are equivalent, only the name itself is considered, not the result of
2213
- name lookup in the context of the template. If multiple declarations of
2214
- the same function template differ in the result of this name lookup, the
2215
- result for the first declaration is used.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2216
 
2217
  [*Example 3*:
2218
 
2219
  ``` cpp
2220
  template <int I, int J> void f(A<I+J>); // #1
2221
  template <int K, int L> void f(A<K+L>); // same as #1
2222
 
2223
  template <class T> decltype(g(T())) h();
2224
  int g(int);
2225
- template <class T> decltype(g(T())) h() // redeclaration of h() uses the earlier lookup
2226
- { return g(T()); } // ...although the lookup here does find g(int)
2227
  int i = h<int>(); // template argument substitution fails; g(int)
2228
  // was not in scope at the first declaration of h()
 
 
 
 
 
 
 
 
2229
  ```
2230
 
2231
  — *end example*]
2232
 
2233
- Two expressions involving template parameters that are not equivalent
2234
- are *functionally equivalent* if, for any given set of template
2235
- arguments, the evaluation of the expression results in the same value.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2236
 
2237
  Two function templates are *equivalent* if they are declared in the same
2238
- scope, have the same name, have identical template parameter lists, and
2239
- have return types and parameter lists that are equivalent using the
2240
- rules described above to compare expressions involving template
2241
- parameters. Two function templates are *functionally equivalent* if they
2242
- are equivalent except that one or more expressions that involve template
2243
- parameters in the return types and parameter lists are functionally
2244
- equivalent using the rules described above to compare expressions
2245
- involving template parameters. If a program contains declarations of
2246
- function templates that are functionally equivalent but not equivalent,
2247
- the program is ill-formed, no diagnostic required.
 
 
2248
 
2249
- [*Note 3*:
2250
 
2251
  This rule guarantees that equivalent declarations will be linked with
2252
  one another, while not requiring implementations to use heroic efforts
2253
  to guarantee that functionally equivalent declarations will be treated
2254
  as distinct. For example, the last two declarations are functionally
2255
  equivalent and would cause a program to be ill-formed:
2256
 
2257
  ``` cpp
2258
- // Guaranteed to be the same
2259
  template <int I> void f(A<I>, A<I+10>);
2260
  template <int I> void f(A<I>, A<I+10>);
2261
 
2262
- // Guaranteed to be different
2263
  template <int I> void f(A<I>, A<I+10>);
2264
  template <int I> void f(A<I>, A<I+11>);
2265
 
2266
- // Ill-formed, no diagnostic required
2267
  template <int I> void f(A<I>, A<I+10>);
2268
  template <int I> void f(A<I>, A<I+1+2+3+4>);
2269
  ```
2270
 
2271
  — *end note*]
2272
 
2273
  #### Partial ordering of function templates <a id="temp.func.order">[[temp.func.order]]</a>
2274
 
2275
  If a function template is overloaded, the use of a function template
2276
- specialization might be ambiguous because template argument deduction (
2277
- [[temp.deduct]]) may associate the function template specialization with
2278
  more than one function template declaration. *Partial ordering* of
2279
  overloaded function template declarations is used in the following
2280
  contexts to select the function template to which a function template
2281
  specialization refers:
2282
 
2283
  - during overload resolution for a call to a function template
2284
- specialization ([[over.match.best]]);
2285
  - when the address of a function template specialization is taken;
2286
  - when a placement operator delete that is a function template
2287
  specialization is selected to match a placement operator new (
2288
  [[basic.stc.dynamic.deallocation]], [[expr.new]]);
2289
- - when a friend function declaration ([[temp.friend]]), an explicit
2290
- instantiation ([[temp.explicit]]) or an explicit specialization (
2291
- [[temp.expl.spec]]) refers to a function template specialization.
2292
 
2293
  Partial ordering selects which of two function templates is more
2294
  specialized than the other by transforming each template in turn (see
2295
  next paragraph) and performing template argument deduction using the
2296
  function type. The deduction process determines whether one of the
2297
  templates is more specialized than the other. If so, the more
2298
  specialized template is the one chosen by the partial ordering process.
 
 
2299
 
2300
  To produce the transformed template, for each type, non-type, or
2301
- template template parameter (including template parameter packs (
2302
- [[temp.variadic]]) thereof) synthesize a unique type, value, or class
2303
  template respectively and substitute it for each occurrence of that
2304
  parameter in the function type of the template.
2305
 
2306
  [*Note 1*: The type replacing the placeholder in the type of the value
2307
  synthesized for a non-type template parameter is also a unique
2308
  synthesized type. — *end note*]
2309
 
2310
- If only one of the function templates *M* is a non-static member of some
2311
- class *A*, *M* is considered to have a new first parameter inserted in
2312
- its function parameter list. Given cv as the cv-qualifiers of *M* (if
2313
- any), the new parameter is of type “rvalue reference to cv *A*” if the
2314
- optional *ref-qualifier* of *M* is `&&` or if *M* has no *ref-qualifier*
2315
- and the first parameter of the other template has rvalue reference type.
2316
- Otherwise, the new parameter is of type “lvalue reference to cv *A*”.
 
 
 
 
 
 
 
 
 
2317
 
2318
  [*Note 2*: This allows a non-static member to be ordered with respect
2319
  to a non-member function and for the results to be equivalent to the
2320
  ordering of two equivalent non-members. — *end note*]
2321
 
@@ -2333,11 +3314,11 @@ template<class T, class R> int operator*(T&, R&); // #2
2333
  // template<class R> int operator*(B<A>&, R&);\quad\quad\quad// #1a
2334
 
2335
  int main() {
2336
  A a;
2337
  B<A> b;
2338
- b * a; // calls #1a
2339
  }
2340
  ```
2341
 
2342
  — *end example*]
2343
 
@@ -2374,12 +3355,12 @@ void m() {
2374
 
2375
  — *end example*]
2376
 
2377
  [*Note 3*:
2378
 
2379
- Since partial ordering in a call context considers only parameters for
2380
- which there are explicit call arguments, some parameters are ignored
2381
  (namely, function parameter packs, parameters with default arguments,
2382
  and ellipsis parameters).
2383
 
2384
  [*Example 3*:
2385
 
@@ -2426,30 +3407,91 @@ template<class T, class... U> void f(T, U...); // #1
2426
  template<class T > void f(T); // #2
2427
  template<class T, class... U> void g(T*, U...); // #3
2428
  template<class T > void g(T); // #4
2429
 
2430
  void h(int i) {
2431
- f(&i); // error: ambiguous
2432
  g(&i); // OK: calls #3
2433
  }
2434
  ```
2435
 
2436
  — *end example*]
2437
 
2438
  — *end note*]
2439
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2440
  ### Alias templates <a id="temp.alias">[[temp.alias]]</a>
2441
 
2442
  A *template-declaration* in which the *declaration* is an
2443
- *alias-declaration* (Clause  [[dcl.dcl]]) declares the *identifier* to
2444
- be an *alias template*. An alias template is a name for a family of
2445
- types. The name of the alias template is a *template-name*.
2446
 
2447
  When a *template-id* refers to the specialization of an alias template,
2448
  it is equivalent to the associated type obtained by substitution of its
2449
- *template-argument*s for the *template-parameter*s in the *type-id* of
2450
- the alias template.
2451
 
2452
  [*Note 1*: An alias template name is never deduced. — *end note*]
2453
 
2454
  [*Example 1*:
2455
 
@@ -2484,19 +3526,19 @@ substitution still applies to the *template-id*.
2484
  [*Example 2*:
2485
 
2486
  ``` cpp
2487
  template<typename...> using void_t = void;
2488
  template<typename T> void_t<typename T::foo> f();
2489
- f<int>(); // error, int does not have a nested type foo
2490
  ```
2491
 
2492
  — *end example*]
2493
 
2494
- The *type-id* in an alias template declaration shall not refer to the
2495
- alias template being declared. The type produced by an alias template
2496
- specialization shall not directly or indirectly make use of that
2497
- specialization.
2498
 
2499
  [*Example 3*:
2500
 
2501
  ``` cpp
2502
  template <class T> struct A;
@@ -2507,17 +3549,84 @@ template <class T> struct A {
2507
  B<short> b; // error: instantiation of B<short> uses own type via A<short>::U
2508
  ```
2509
 
2510
  — *end example*]
2511
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2512
  ## Name resolution <a id="temp.res">[[temp.res]]</a>
2513
 
2514
  Three kinds of names can be used within a template definition:
2515
 
2516
  - The name of the template itself, and names declared within the
2517
  template itself.
2518
- - Names dependent on a *template-parameter* ([[temp.dep]]).
2519
  - Names from scopes which are visible within the template definition.
2520
 
2521
  A name used in a template declaration or definition and that is
2522
  dependent on a *template-parameter* is assumed not to name a type unless
2523
  the applicable name lookup finds a type name or the name is qualified by
@@ -2539,38 +3648,31 @@ template<class T> class Y {
2539
  Y* a3; // declare pointer to Y<T>
2540
  Z* a4; // declare pointer to Z
2541
  typedef typename T::A TA;
2542
  TA* a5; // declare pointer to T's A
2543
  typename T::A* a6; // declare pointer to T's A
2544
- T::A* a7; // T::A is not a type name:
2545
- // multiplication of T::A by a7; ill-formed, no visible declaration of a7
2546
- B* a8; // B is not a type name:
2547
- // multiplication of B by a8; ill-formed, no visible declarations of B and a8
2548
  }
2549
  };
2550
  ```
2551
 
2552
  — *end example*]
2553
 
2554
- When a *qualified-id* is intended to refer to a type that is not a
2555
- member of the current instantiation ([[temp.dep.type]]) and its
2556
- *nested-name-specifier* refers to a dependent type, it shall be prefixed
2557
- by the keyword `typename`, forming a *typename-specifier*. If the
2558
- *qualified-id* in a *typename-specifier* does not denote a type or a
2559
- class template, the program is ill-formed.
2560
-
2561
  ``` bnf
2562
  typename-specifier:
2563
- 'typename' nested-name-specifier identifier
2564
- 'typename' nested-name-specifier 'template'ₒₚₜ simple-template-id
2565
  ```
2566
 
2567
- If a specialization of a template is instantiated for a set of
2568
- *template-argument*s such that the *qualified-id* prefixed by `typename`
2569
- does not denote a type or a class template, the specialization is
2570
- ill-formed. The usual qualified name lookup ([[basic.lookup.qual]]) is
2571
- used to find the *qualified-id* even in the presence of `typename`.
2572
 
2573
  [*Example 2*:
2574
 
2575
  ``` cpp
2576
  struct A {
@@ -2591,33 +3693,75 @@ void foo() {
2591
  }
2592
  ```
2593
 
2594
  — *end example*]
2595
 
2596
- A qualified name used as the name in a *class-or-decltype* (Clause 
2597
- [[class.derived]]) or an *elaborated-type-specifier* is implicitly
2598
  assumed to name a type, without the use of the `typename` keyword. In a
2599
  *nested-name-specifier* that immediately contains a
2600
  *nested-name-specifier* that depends on a template parameter, the
2601
  *identifier* or *simple-template-id* is implicitly assumed to name a
2602
  type, without the use of the `typename` keyword.
2603
 
2604
  [*Note 1*: The `typename` keyword is not permitted by the syntax of
2605
  these constructs. — *end note*]
2606
 
2607
- If, for a given set of template arguments, a specialization of a
2608
- template is instantiated that refers to a *qualified-id* that denotes a
2609
- type or a class template, and the *qualified-id* refers to a member of
2610
- an unknown specialization, the *qualified-id* shall either be prefixed
2611
- by `typename` or shall be used in a context in which it implicitly names
2612
- a type as described above.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2613
 
2614
  [*Example 3*:
2615
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2616
  ``` cpp
2617
  template <class T> void f(int i) {
2618
- T::x * i; // T::x must not be a type
2619
  }
2620
 
2621
  struct Foo {
2622
  typedef int x;
2623
  };
@@ -2634,38 +3778,36 @@ int main() {
2634
 
2635
  — *end example*]
2636
 
2637
  Within the definition of a class template or within the definition of a
2638
  member of a class template following the *declarator-id*, the keyword
2639
- `typename` is not required when referring to the name of a previously
2640
- declared member of the class template that declares a type or a class
2641
- template.
2642
 
2643
- [*Note 2*: Such names can be found using unqualified name lookup (
2644
- [[basic.lookup.unqual]]), class member lookup ([[class.qual]]) into the
2645
- current instantiation ([[temp.dep.type]]), or class member access
2646
- expression lookup ([[basic.lookup.classref]]) when the type of the
2647
- object expression is the current instantiation (
2648
- [[temp.dep.expr]]). — *end note*]
2649
-
2650
- [*Example 4*:
2651
 
2652
  ``` cpp
2653
  template<class T> struct A {
2654
  typedef int B;
2655
  B b; // OK, no typename required
2656
  };
2657
  ```
2658
 
2659
  — *end example*]
2660
 
2661
- Knowing which names are type names allows the syntax of every template
2662
- to be checked. The program is ill-formed, no diagnostic required, if:
 
 
 
 
2663
 
2664
  - no valid specialization can be generated for a template or a
2665
- substatement of a constexpr if statement ([[stmt.if]]) within a
2666
- template and the template is not instantiated, or
 
 
2667
  - every valid specialization of a variadic template requires an empty
2668
  template parameter pack, or
2669
  - a hypothetical instantiation of a template immediately following its
2670
  definition would be ill-formed due to a construct that does not depend
2671
  on a template parameter, or
@@ -2683,13 +3825,13 @@ to be checked. The program is ill-formed, no diagnostic required, if:
2683
  *using-declaration* was a pack expansion and the corresponding pack
2684
  is empty, or
2685
  - an instantiation uses a default argument or default template
2686
  argument that had not been defined at the point at which the
2687
  template was defined, or
2688
- - constant expression evaluation ([[expr.const]]) within the template
2689
  instantiation uses
2690
- - the value of a `const` object of integral or unscoped enumeration
2691
  type or
2692
  - the value of a `constexpr` object or
2693
  - the value of a reference or
2694
  - the definition of a constexpr function,
2695
 
@@ -2705,15 +3847,14 @@ to be checked. The program is ill-formed, no diagnostic required, if:
2705
 
2706
  Otherwise, no diagnostic shall be issued for a template for which a
2707
  valid specialization can be generated.
2708
 
2709
  [*Note 4*: If a template is instantiated, errors will be diagnosed
2710
- according to the other rules in this International Standard. Exactly
2711
- when these errors are diagnosed is a quality of implementation
2712
- issue. — *end note*]
2713
 
2714
- [*Example 5*:
2715
 
2716
  ``` cpp
2717
  int j;
2718
  template<class T> class X {
2719
  void f(T t, int i, char* p) {
@@ -2737,13 +3878,13 @@ template<class... T> struct A : T..., T... { }; // error: duplicate base cla
2737
 
2738
  When looking for the declaration of a name used in a template
2739
  definition, the usual lookup rules ([[basic.lookup.unqual]],
2740
  [[basic.lookup.argdep]]) are used for non-dependent names. The lookup of
2741
  names dependent on the template parameters is postponed until the actual
2742
- template argument is known ([[temp.dep]]).
2743
 
2744
- [*Example 6*:
2745
 
2746
  ``` cpp
2747
  #include <iostream>
2748
  using namespace std;
2749
 
@@ -2758,30 +3899,30 @@ public:
2758
  cout << p[i] << '\n';
2759
  }
2760
  };
2761
  ```
2762
 
2763
- in the example, `i` is the local variable `i` declared in `printall`,
2764
  `cnt` is the member `cnt` declared in `Set`, and `cout` is the standard
2765
  output stream declared in `iostream`. However, not every declaration can
2766
  be found this way; the resolution of some names must be postponed until
2767
  the actual *template-argument*s are known. For example, even though the
2768
  name `operator<<` is known within the definition of `printall()` and a
2769
  declaration of it can be found in `<iostream>`, the actual declaration
2770
  of `operator<<` needed to print `p[i]` cannot be known until it is known
2771
- what type `T` is ([[temp.dep]]).
2772
 
2773
  — *end example*]
2774
 
2775
  If a name does not depend on a *template-parameter* (as defined in 
2776
  [[temp.dep]]), a declaration (or set of declarations) for that name
2777
  shall be in scope at the point where the name appears in the template
2778
  definition; the name is bound to the declaration (or declarations) found
2779
  at that point and this binding is not affected by declarations that are
2780
  visible at the point of instantiation.
2781
 
2782
- [*Example 7*:
2783
 
2784
  ``` cpp
2785
  void f(char);
2786
 
2787
  template<class T> void g(T t) {
@@ -2804,23 +3945,24 @@ void h() {
2804
  — *end example*]
2805
 
2806
  [*Note 5*: For purposes of name lookup, default arguments and
2807
  *noexcept-specifier*s of function templates and default arguments and
2808
  *noexcept-specifier*s of member functions of class templates are
2809
- considered definitions ([[temp.decls]]). — *end note*]
2810
 
2811
  ### Locally declared names <a id="temp.local">[[temp.local]]</a>
2812
 
2813
  Like normal (non-template) classes, class templates have an
2814
- injected-class-name (Clause  [[class]]). The injected-class-name can be
2815
- used as a *template-name* or a *type-name*. When it is used with a
2816
  *template-argument-list*, as a *template-argument* for a template
2817
  *template-parameter*, or as the final identifier in the
2818
  *elaborated-type-specifier* of a friend class template declaration, it
2819
- refers to the class template itself. Otherwise, it is equivalent to the
2820
- *template-name* followed by the *template-parameter*s of the class
2821
- template enclosed in `<>`.
 
2822
 
2823
  Within the scope of a class template specialization or partial
2824
  specialization, when the injected-class-name is used as a *type-name*,
2825
  it is equivalent to the *template-name* followed by the
2826
  *template-argument*s of the class template specialization or partial
@@ -2842,11 +3984,11 @@ template<> class Y<int> {
2842
  ```
2843
 
2844
  — *end example*]
2845
 
2846
  The injected-class-name of a class template or class template
2847
- specialization can be used either as a *template-name* or a *type-name*
2848
  wherever it is in scope.
2849
 
2850
  [*Example 2*:
2851
 
2852
  ``` cpp
@@ -2857,20 +3999,20 @@ template <class T> struct Base {
2857
  template <class T> struct Derived: public Base<T> {
2858
  typename Derived::Base* p; // meaning Derived::Base<T>
2859
  };
2860
 
2861
  template<class T, template<class> class U = T::template Base> struct Third { };
2862
- Third<Base<int> > t; // OK: default argument uses injected-class-name as a template
2863
  ```
2864
 
2865
  — *end example*]
2866
 
2867
- A lookup that finds an injected-class-name ([[class.member.lookup]])
2868
- can result in an ambiguity in certain cases (for example, if it is found
2869
- in more than one base class). If all of the injected-class-names that
2870
- are found refer to specializations of the same class template, and if
2871
- the name is used as a *template-name*, the reference refers to the class
2872
  template itself and not a specialization thereof, and is not ambiguous.
2873
 
2874
  [*Example 3*:
2875
 
2876
  ``` cpp
@@ -2899,13 +4041,13 @@ template<class T> class X {
2899
  };
2900
  ```
2901
 
2902
  — *end example*]
2903
 
2904
- A *template-parameter* shall not be redeclared within its scope
2905
- (including nested scopes). A *template-parameter* shall not have the
2906
- same name as the template name.
2907
 
2908
  [*Example 5*:
2909
 
2910
  ``` cpp
2911
  template<class T, int i> class Y {
@@ -2968,14 +4110,14 @@ template<class C> void N::B<C>::f(C) {
2968
 
2969
  — *end example*]
2970
 
2971
  In the definition of a class template or in the definition of a member
2972
  of such a template that appears outside of the template definition, for
2973
- each non-dependent base class ([[temp.dep.type]]), if the name of the
2974
- base class or the name of a member of the base class is the same as the
2975
- name of a *template-parameter*, the base class name or member name hides
2976
- the *template-parameter* name ([[basic.scope.hiding]]).
2977
 
2978
  [*Example 8*:
2979
 
2980
  ``` cpp
2981
  struct A {
@@ -2997,31 +4139,38 @@ template<class B, class a> struct X : A {
2997
  Inside a template, some constructs have semantics which may differ from
2998
  one instantiation to another. Such a construct *depends* on the template
2999
  parameters. In particular, types and expressions may depend on the type
3000
  and/or value of template parameters (as determined by the template
3001
  arguments) and this determines the context for name lookup for certain
3002
- names. An expressions may be *type-dependent* (that is, its type may
3003
  depend on a template parameter) or *value-dependent* (that is, its value
3004
- when evaluated as a constant expression ([[expr.const]]) may depend on
3005
- a template parameter) as described in this subclause. In an expression
3006
- of the form:
 
 
 
 
 
3007
 
3008
  where the *postfix-expression* is an *unqualified-id*, the
3009
  *unqualified-id* denotes a *dependent name* if
3010
 
3011
- - any of the expressions in the *expression-list* is a pack expansion (
3012
- [[temp.variadic]]),
3013
  - any of the expressions or *braced-init-list*s in the *expression-list*
3014
- is type-dependent ([[temp.dep.expr]]), or
3015
  - the *unqualified-id* is a *template-id* in which any of the template
3016
  arguments depends on a template parameter.
3017
 
3018
  If an operand of an operator is a type-dependent expression, the
3019
- operator also denotes a dependent name. Such names are unbound and are
3020
- looked up at the point of the template instantiation ([[temp.point]])
3021
- in both the context of the template definition and the context of the
3022
- point of instantiation.
 
 
3023
 
3024
  [*Example 1*:
3025
 
3026
  ``` cpp
3027
  template<class T> struct X : B<T> {
@@ -3031,17 +4180,17 @@ template<class T> struct X : B<T> {
3031
  pb->j++;
3032
  }
3033
  };
3034
  ```
3035
 
3036
- the base class name `B<T>`, the type name `T::A`, the names `B<T>::i`
3037
  and `pb->j` explicitly depend on the *template-parameter*.
3038
 
3039
  — *end example*]
3040
 
3041
  In the definition of a class or class template, the scope of a dependent
3042
- base class ([[temp.dep.type]]) is not examined during unqualified name
3043
  lookup either at the point of definition of the class template or member
3044
  or during an instantiation of the class template or member.
3045
 
3046
  [*Example 2*:
3047
 
@@ -3091,41 +4240,50 @@ not affect the binding of names in `Y<A>`.
3091
 
3092
  A name refers to the *current instantiation* if it is
3093
 
3094
  - in the definition of a class template, a nested class of a class
3095
  template, a member of a class template, or a member of a nested class
3096
- of a class template, the injected-class-name (Clause  [[class]]) of
3097
- the class template or nested class,
3098
  - in the definition of a primary class template or a member of a primary
3099
  class template, the name of the class template followed by the
3100
  template argument list of the primary template (as described below)
3101
  enclosed in `<>` (or an equivalent template alias specialization),
3102
  - in the definition of a nested class of a class template, the name of
3103
  the nested class referenced as a member of the current instantiation,
3104
  or
3105
  - in the definition of a partial specialization or a member of a partial
3106
  specialization, the name of the class template followed by the
3107
  template argument list of the partial specialization enclosed in `<>`
3108
- (or an equivalent template alias specialization). If the *n*th
3109
- template parameter is a parameter pack, the *n*th template argument is
3110
- a pack expansion ([[temp.variadic]]) whose pattern is the name of the
3111
- parameter pack.
3112
 
3113
  The template argument list of a primary template is a template argument
3114
- list in which the *n*th template argument has the value of the *n*th
3115
- template parameter of the class template. If the *n*th template
3116
- parameter is a template parameter pack ([[temp.variadic]]), the *n*th
3117
- template argument is a pack expansion ([[temp.variadic]]) whose pattern
3118
- is the name of the template parameter pack.
3119
 
3120
- A template argument that is equivalent to a template parameter (i.e.,
3121
- has the same constant value or the same type as the template parameter)
3122
- can be used in place of that template parameter in a reference to the
3123
- current instantiation. In the case of a non-type template argument, the
3124
- argument must have been given the value of the template parameter and
3125
- not an expression in which the template parameter appears as a
3126
- subexpression.
 
 
 
 
 
 
 
 
 
3127
 
3128
  [*Example 1*:
3129
 
3130
  ``` cpp
3131
  template <class T> class A {
@@ -3150,22 +4308,26 @@ template <class T1, class T2, int I> struct B {
3150
  B<T2, T1, I>* b2; // not the current instantiation
3151
  typedef T1 my_T1;
3152
  static const int my_I = I;
3153
  static const int my_I2 = I+0;
3154
  static const int my_I3 = my_I;
 
 
3155
  B<my_T1, T2, my_I>* b3; // refers to the current instantiation
3156
  B<my_T1, T2, my_I2>* b4; // not the current instantiation
3157
  B<my_T1, T2, my_I3>* b5; // refers to the current instantiation
 
 
3158
  };
3159
  ```
3160
 
3161
  — *end example*]
3162
 
3163
  A *dependent base class* is a base class that is a dependent type and is
3164
  not the current instantiation.
3165
 
3166
- [*Note 1*:
3167
 
3168
  A base class can be the current instantiation in the case of a nested
3169
  class naming an enclosing class as a base.
3170
 
3171
  [*Example 2*:
@@ -3190,26 +4352,26 @@ template<class T> struct A<T>::B::C : A<T> {
3190
 
3191
  A name is a *member of the current instantiation* if it is
3192
 
3193
  - An unqualified name that, when looked up, refers to at least one
3194
  member of a class that is the current instantiation or a non-dependent
3195
- base class thereof. \[*Note 2*: This can only occur when looking up a
3196
  name in a scope enclosed by the definition of a class
3197
  template. — *end note*]
3198
  - A *qualified-id* in which the *nested-name-specifier* refers to the
3199
  current instantiation and that, when looked up, refers to at least one
3200
  member of a class that is the current instantiation or a non-dependent
3201
- base class thereof. \[*Note 3*: If no such member is found, and the
3202
  current instantiation has any dependent base classes, then the
3203
  *qualified-id* is a member of an unknown specialization; see
3204
  below. — *end note*]
3205
  - An *id-expression* denoting the member in a class member access
3206
- expression ([[expr.ref]]) for which the type of the object expression
3207
- is the current instantiation, and the *id-expression*, when looked
3208
- up ([[basic.lookup.classref]]), refers to at least one member of a
3209
- class that is the current instantiation or a non-dependent base class
3210
- thereof. \[*Note 4*: If no such member is found, and the current
3211
  instantiation has any dependent base classes, then the *id-expression*
3212
  is a member of an unknown specialization; see below. — *end note*]
3213
 
3214
  [*Example 3*:
3215
 
@@ -3241,18 +4403,18 @@ A name is a *member of an unknown specialization* if it is
3241
  current instantiation, the current instantiation has at least one
3242
  dependent base class, and name lookup of the *qualified-id* does not
3243
  find any member of a class that is the current instantiation or a
3244
  non-dependent base class thereof.
3245
  - An *id-expression* denoting the member in a class member access
3246
- expression ([[expr.ref]]) in which either
3247
  - the type of the object expression is the current instantiation, the
3248
  current instantiation has at least one dependent base class, and
3249
  name lookup of the *id-expression* does not find a member of a class
3250
  that is the current instantiation or a non-dependent base class
3251
  thereof; or
3252
- - the type of the object expression is dependent and is not the
3253
- current instantiation.
3254
 
3255
  If a *qualified-id* in which the *nested-name-specifier* refers to the
3256
  current instantiation is not a member of the current instantiation or a
3257
  member of an unknown specialization, the program is ill-formed even if
3258
  the template containing the *qualified-id* is not instantiated; no
@@ -3319,20 +4481,20 @@ A type is dependent if it is
3319
  - a cv-qualified type where the cv-unqualified type is dependent,
3320
  - a compound type constructed from any dependent type,
3321
  - an array type whose element type is dependent or whose bound (if any)
3322
  is value-dependent,
3323
  - a function type whose exception specification is value-dependent,
3324
- - a *simple-template-id* in which either the template name is a template
3325
- parameter or any of the template arguments is a dependent type or an
3326
- expression that is type-dependent or value-dependent or is a pack
3327
- expansion \[*Note 5*: This includes an injected-class-name (Clause
3328
- [[class]]) of a class template used without a
3329
  *template-argument-list*. — *end note*] , or
3330
  - denoted by `decltype(`*expression*`)`, where *expression* is
3331
- type-dependent ([[temp.dep.expr]]).
3332
 
3333
- [*Note 6*: Because typedefs do not introduce new types, but instead
3334
  simply refer to other types, a name that refers to a typedef that is a
3335
  member of the current instantiation is dependent only if the type
3336
  referred to is dependent. — *end note*]
3337
 
3338
  #### Type-dependent expressions <a id="temp.dep.expr">[[temp.dep.expr]]</a>
@@ -3341,49 +4503,77 @@ Except as described below, an expression is type-dependent if any
3341
  subexpression is type-dependent.
3342
 
3343
  `this`
3344
 
3345
  is type-dependent if the class type of the enclosing member function is
3346
- dependent ([[temp.dep.type]]).
3347
 
3348
- An *id-expression* is type-dependent if it contains
 
3349
 
3350
  - an *identifier* associated by name lookup with one or more
3351
  declarations declared with a dependent type,
3352
  - an *identifier* associated by name lookup with a non-type
3353
  *template-parameter* declared with a type that contains a placeholder
3354
- type ([[dcl.spec.auto]]),
 
 
 
3355
  - an *identifier* associated by name lookup with one or more
3356
  declarations of member functions of the current instantiation declared
3357
  with a return type that contains a placeholder type,
3358
  - an *identifier* associated by name lookup with a structured binding
3359
- declaration ([[dcl.struct.bind]]) whose *brace-or-equal-initializer*
3360
- is type-dependent,
3361
- - the *identifier* `__func__` ([[dcl.fct.def.general]]), where any
3362
  enclosing function is a template, a member of a class template, or a
3363
  generic lambda,
3364
  - a *template-id* that is dependent,
3365
  - a *conversion-function-id* that specifies a dependent type, or
3366
  - a *nested-name-specifier* or a *qualified-id* that names a member of
3367
  an unknown specialization;
3368
 
3369
  or if it names a dependent member of the current instantiation that is a
3370
- static data member of type “array of unknown bound of `T`” for some
3371
- `T` ([[temp.static]]). Expressions of the following forms are
3372
- type-dependent only if the type specified by the *type-id*,
3373
- *simple-type-specifier* or *new-type-id* is dependent, even if any
3374
- subexpression is type-dependent:
 
 
 
 
 
 
 
 
 
 
3375
 
3376
  Expressions of the following forms are never type-dependent (because the
3377
  type of the expression cannot be dependent):
3378
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3379
  [*Note 1*: For the standard library macro `offsetof`, see 
3380
  [[support.types]]. — *end note*]
3381
 
3382
- A class member access expression ([[expr.ref]]) is type-dependent if
3383
- the expression refers to a member of the current instantiation and the
3384
- type of the referenced member is dependent, or the class member access
3385
  expression refers to a member of an unknown specialization.
3386
 
3387
  [*Note 2*: In an expression of the form `x.y` or `xp->y` the type of
3388
  the expression is usually the type of the member `y` of the class of `x`
3389
  (or the class pointed to by `xp`). However, if `x` or `xp` refers to a
@@ -3403,37 +4593,60 @@ Except as described below, an expression used in a context where a
3403
  constant expression is required is value-dependent if any subexpression
3404
  is value-dependent.
3405
 
3406
  An *id-expression* is value-dependent if:
3407
 
 
3408
  - it is type-dependent,
3409
  - it is the name of a non-type template parameter,
3410
  - it names a static data member that is a dependent member of the
3411
  current instantiation and is not initialized in a *member-declarator*,
3412
  - it names a static member function that is a dependent member of the
3413
  current instantiation, or
3414
- - it is a constant with literal type and is initialized with an
3415
- expression that is value-dependent.
3416
 
3417
  Expressions of the following form are value-dependent if the
3418
  *unary-expression* or *expression* is type-dependent or the *type-id* is
3419
  dependent:
3420
 
 
 
 
 
 
 
 
 
 
3421
  [*Note 1*: For the standard library macro `offsetof`, see 
3422
  [[support.types]]. — *end note*]
3423
 
3424
  Expressions of the following form are value-dependent if either the
3425
  *type-id* or *simple-type-specifier* is dependent or the *expression* or
3426
  *cast-expression* is value-dependent:
3427
 
 
 
 
 
 
 
 
 
3428
  Expressions of the following form are value-dependent:
3429
 
 
 
 
 
 
3430
  An expression of the form `&`*qualified-id* where the *qualified-id*
3431
  names a dependent member of the current instantiation is
3432
  value-dependent. An expression of the form `&`*cast-expression* is also
3433
  value-dependent if evaluating *cast-expression* as a core constant
3434
- expression ([[expr.const]]) succeeds and the result of the evaluation
3435
  refers to a templated entity that is an object with static or thread
3436
  storage duration or a member function.
3437
 
3438
  #### Dependent template arguments <a id="temp.dep.temp">[[temp.dep.temp]]</a>
3439
 
@@ -3477,19 +4690,10 @@ void g(int); // not in scope at the point of the template definition, not
3477
 
3478
  — *end example*]
3479
 
3480
  ### Dependent name resolution <a id="temp.dep.res">[[temp.dep.res]]</a>
3481
 
3482
- In resolving dependent names, names from the following sources are
3483
- considered:
3484
-
3485
- - Declarations that are visible at the point of definition of the
3486
- template.
3487
- - Declarations from namespaces associated with the types of the function
3488
- arguments both from the instantiation context ([[temp.point]]) and
3489
- from the definition context.
3490
-
3491
  #### Point of instantiation <a id="temp.point">[[temp.point]]</a>
3492
 
3493
  For a function template specialization, a member function template
3494
  specialization, or a specialization for a member function or static data
3495
  member of a class template, if the specialization is implicitly
@@ -3535,61 +4739,260 @@ enclosing class template specialization.
3535
 
3536
  An explicit instantiation definition is an instantiation point for the
3537
  specialization or specializations specified by the explicit
3538
  instantiation.
3539
 
3540
- The instantiation context of an expression that depends on the template
3541
- arguments is the set of declarations with external linkage declared
3542
- prior to the point of instantiation of the template specialization in
3543
- the same translation unit.
3544
-
3545
  A specialization for a function template, a member function template, or
3546
  of a member function or static data member of a class template may have
3547
  multiple points of instantiations within a translation unit, and in
3548
- addition to the points of instantiation described above, for any such
3549
- specialization that has a point of instantiation within the translation
3550
- unit, the end of the translation unit is also considered a point of
3551
- instantiation. A specialization for a class template has at most one
3552
- point of instantiation within a translation unit. A specialization for
3553
- any template may have points of instantiation in multiple translation
3554
- units. If two different points of instantiation give a template
3555
- specialization different meanings according to the one-definition rule (
3556
- [[basic.def.odr]]), the program is ill-formed, no diagnostic required.
 
 
 
 
 
 
 
 
3557
 
3558
  #### Candidate functions <a id="temp.dep.candidate">[[temp.dep.candidate]]</a>
3559
 
3560
  For a function call where the *postfix-expression* is a dependent name,
3561
- the candidate functions are found using the usual lookup rules (
3562
- [[basic.lookup.unqual]], [[basic.lookup.argdep]]) except that:
 
3563
 
3564
- - For the part of the lookup using unqualified name lookup (
3565
- [[basic.lookup.unqual]]), only function declarations from the template
3566
- definition context are found.
3567
- - For the part of the lookup using associated namespaces (
3568
- [[basic.lookup.argdep]]), only function declarations found in either
3569
- the template definition context or the template instantiation context
3570
- are found.
3571
 
3572
  If the call would be ill-formed or would find a better match had the
3573
  lookup within the associated namespaces considered all the function
3574
  declarations with external linkage introduced in those namespaces in all
3575
  translation units, not just considering those declarations found in the
3576
  template definition and template instantiation contexts, then the
3577
  program has undefined behavior.
3578
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3579
  ### Friend names declared within a class template <a id="temp.inject">[[temp.inject]]</a>
3580
 
3581
  Friend classes or functions can be declared within a class template.
3582
  When a template is instantiated, the names of its friends are treated as
3583
  if the specialization had been explicitly declared at its point of
3584
  instantiation.
3585
 
3586
  As with non-template classes, the names of namespace-scope friend
3587
  functions of a class template specialization are not visible during an
3588
- ordinary lookup unless explicitly declared at namespace scope (
3589
- [[class.friend]]). Such names may be found under the rules for
3590
- associated classes ([[basic.lookup.argdep]]).[^6]
3591
 
3592
  [*Example 1*:
3593
 
3594
  ``` cpp
3595
  template<typename T> struct number {
@@ -3599,20 +5002,20 @@ template<typename T> struct number {
3599
 
3600
  void g() {
3601
  number<double> a(3), b(4);
3602
  a = gcd(a,b); // finds gcd because number<double> is an associated class,
3603
  // making gcd visible in its namespace (global scope)
3604
- b = gcd(3,4); // ill-formed; gcd is not visible
3605
  }
3606
  ```
3607
 
3608
  — *end example*]
3609
 
3610
  ## Template instantiation and specialization <a id="temp.spec">[[temp.spec]]</a>
3611
 
3612
- The act of instantiating a function, a class, a member of a class
3613
- template or a member template is referred to as *template
3614
  instantiation*.
3615
 
3616
  A function instantiated from a function template is called an
3617
  instantiated function. A class instantiated from a class template is
3618
  called an instantiated class. A member function, a member class, a
@@ -3620,18 +5023,22 @@ member enumeration, or a static data member of a class template
3620
  instantiated from the member definition of the class template is called,
3621
  respectively, an instantiated member function, member class, member
3622
  enumeration, or static data member. A member function instantiated from
3623
  a member function template is called an instantiated member function. A
3624
  member class instantiated from a member class template is called an
3625
- instantiated member class.
 
 
 
3626
 
3627
  An explicit specialization may be declared for a function template, a
3628
- class template, a member of a class template or a member template. An
3629
- explicit specialization declaration is introduced by `template<>`. In an
3630
- explicit specialization declaration for a class template, a member of a
3631
- class template or a class member template, the name of the class that is
3632
- explicitly specialized shall be a *simple-template-id*. In the explicit
 
3633
  specialization declaration for a function template or a member function
3634
  template, the name of the function or member function explicitly
3635
  specialized may be a *template-id*.
3636
 
3637
  [*Example 1*:
@@ -3656,27 +5063,39 @@ template<> int B<>::x = 1; // specialize for T == int
3656
  ```
3657
 
3658
  — *end example*]
3659
 
3660
  An instantiated template specialization can be either implicitly
3661
- instantiated ([[temp.inst]]) for a given argument list or be explicitly
3662
- instantiated ([[temp.explicit]]). A specialization is a class,
3663
- function, or class member that is either instantiated or explicitly
3664
- specialized ([[temp.expl.spec]]).
 
3665
 
3666
  For a given template and a given set of *template-argument*s,
3667
 
3668
  - an explicit instantiation definition shall appear at most once in a
3669
  program,
3670
- - an explicit specialization shall be defined at most once in a program
3671
- (according to  [[basic.def.odr]]), and
3672
  - both an explicit instantiation and a declaration of an explicit
3673
  specialization shall not appear in a program unless the explicit
3674
  instantiation follows a declaration of the explicit specialization.
3675
 
3676
  An implementation is not required to diagnose a violation of this rule.
3677
 
 
 
 
 
 
 
 
 
 
 
 
3678
  Each class template specialization instantiated from a template has its
3679
  own copy of any static members.
3680
 
3681
  [*Example 2*:
3682
 
@@ -3695,35 +5114,50 @@ has a static member `s` of type `int` and `X<char*>` has a static member
3695
  `s` of type `char*`.
3696
 
3697
  — *end example*]
3698
 
3699
  If a function declaration acquired its function type through a dependent
3700
- type ([[temp.dep.type]]) without using the syntactic form of a function
3701
  declarator, the program is ill-formed.
3702
 
3703
  [*Example 3*:
3704
 
3705
  ``` cpp
3706
  template<class T> struct A {
3707
  static T t;
3708
  };
3709
  typedef int function();
3710
- A<function> a; // ill-formed: would declare A<function>::t as a static member function
3711
  ```
3712
 
3713
  — *end example*]
3714
 
3715
  ### Implicit instantiation <a id="temp.inst">[[temp.inst]]</a>
3716
 
3717
- Unless a class template specialization has been explicitly
3718
- instantiated ([[temp.explicit]]) or explicitly specialized (
3719
- [[temp.expl.spec]]), the class template specialization is implicitly
3720
- instantiated when the specialization is referenced in a context that
3721
- requires a completely-defined object type or when the completeness of
3722
- the class type affects the semantics of the program.
3723
-
3724
- [*Note 1*: In particular, if the semantics of an expression depend on
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3725
  the member or base class lists of a class template specialization, the
3726
  class template specialization is implicitly generated. For instance,
3727
  deleting a pointer to class type depends on whether or not the class
3728
  declares a destructor, and a conversion between pointers to class type
3729
  depends on the inheritance relationship between the two classes
@@ -3746,44 +5180,65 @@ void g(D<int>* p, D<char>* pp, D<double>* ppp) {
3746
  ```
3747
 
3748
  — *end example*]
3749
 
3750
  If a class template has been declared, but not defined, at the point of
3751
- instantiation ([[temp.point]]), the instantiation yields an incomplete
3752
- class type ([[basic.types]]).
3753
 
3754
  [*Example 2*:
3755
 
3756
  ``` cpp
3757
  template<class T> class X;
3758
  X<char> ch; // error: incomplete type X<char>
3759
  ```
3760
 
3761
  — *end example*]
3762
 
3763
- [*Note 2*: Within a template declaration, a local class (
3764
- [[class.local]]) or enumeration and the members of a local class are
3765
- never considered to be entities that can be separately instantiated
3766
- (this includes their default arguments, *noexcept-specifier*s, and
3767
- non-static data member initializers, if any). As a result, the dependent
3768
- names are looked up, the semantic constraints are checked, and any
3769
- templates used are instantiated as part of the instantiation of the
3770
- entity within which the local class or enumeration is
3771
- declared. — *end note*]
3772
 
3773
- The implicit instantiation of a class template specialization causes the
3774
- implicit instantiation of the declarations, but not of the definitions,
3775
- default arguments, or *noexcept-specifier*s of the class member
3776
- functions, member classes, scoped member enumerations, static data
3777
- members, member templates, and friends; and it causes the implicit
3778
- instantiation of the definitions of unscoped member enumerations and
3779
- member anonymous unions. However, for the purpose of determining whether
3780
- an instantiated redeclaration is valid according to  [[basic.def.odr]]
3781
- and [[class.mem]], a declaration that corresponds to a definition in the
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3782
  template is considered to be a definition.
3783
 
3784
- [*Example 3*:
3785
 
3786
  ``` cpp
3787
  template<class T, class U>
3788
  struct Outer {
3789
  template<class X, class Y> struct Inner;
@@ -3804,38 +5259,47 @@ same partial specialization.
3804
  ``` cpp
3805
  template<typename T> struct Friendly {
3806
  template<typename U> friend int f(U) { return sizeof(T); }
3807
  };
3808
  Friendly<char> fc;
3809
- Friendly<float> ff; // ill-formed: produces second definition of f(U)
3810
  ```
3811
 
3812
  — *end example*]
3813
 
3814
- Unless a member of a class template or a member template has been
3815
- explicitly instantiated or explicitly specialized, the specialization of
3816
- the member is implicitly instantiated when the specialization is
3817
- referenced in a context that requires the member definition to exist; in
 
3818
  particular, the initialization (and any associated side effects) of a
3819
  static data member does not occur unless the static data member is
3820
  itself used in a way that requires the definition of the static data
3821
  member to exist.
3822
 
3823
- Unless a function template specialization has been explicitly
3824
- instantiated or explicitly specialized, the function template
3825
- specialization is implicitly instantiated when the specialization is
3826
- referenced in a context that requires a function definition to exist. A
3827
- function whose declaration was instantiated from a friend function
3828
- definition is implicitly instantiated when it is referenced in a context
3829
- that requires a function definition to exist. Unless a call is to a
3830
- function template explicit specialization or to a member function of an
3831
- explicitly specialized class template, a default argument for a function
3832
- template or a member function of a class template is implicitly
3833
- instantiated when the function is called in a context that requires the
3834
- value of the default argument.
 
3835
 
3836
- [*Example 4*:
 
 
 
 
 
 
 
3837
 
3838
  ``` cpp
3839
  template<class T> struct Z {
3840
  void f();
3841
  void g();
@@ -3855,22 +5319,46 @@ void h() {
3855
  Nothing in this example requires `class` `Z<double>`, `Z<int>::g()`, or
3856
  `Z<char>::f()` to be implicitly instantiated.
3857
 
3858
  — *end example*]
3859
 
3860
- Unless a variable template specialization has been explicitly
3861
- instantiated or explicitly specialized, the variable template
3862
- specialization is implicitly instantiated when the specialization is
3863
- used. A default template argument for a variable template is implicitly
3864
- instantiated when the variable template is referenced in a context that
3865
- requires the value of the default argument.
3866
-
3867
- If the function selected by overload resolution ([[over.match]]) can be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3868
  determined without instantiating a class template definition, it is
3869
  unspecified whether that instantiation actually takes place.
3870
 
3871
- [*Example 5*:
3872
 
3873
  ``` cpp
3874
  template <class T> struct S {
3875
  operator int();
3876
  };
@@ -3887,34 +5375,41 @@ void g(S<int>& sr) {
3887
 
3888
  — *end example*]
3889
 
3890
  If a function template or a member function template specialization is
3891
  used in a way that involves overload resolution, a declaration of the
3892
- specialization is implicitly instantiated ([[temp.over]]).
3893
 
3894
  An implementation shall not implicitly instantiate a function template,
3895
  a variable template, a member template, a non-virtual member function, a
3896
  member class, a static data member of a class template, or a
3897
- substatement of a constexpr if statement ([[stmt.if]]), unless such
3898
- instantiation is required. It is unspecified whether or not an
3899
- implementation implicitly instantiates a virtual member function of a
3900
- class template if the virtual member function would not otherwise be
3901
- instantiated. The use of a template specialization in a default argument
3902
- shall not cause the template to be implicitly instantiated except that a
3903
- class template may be instantiated where its complete type is needed to
3904
- determine the correctness of the default argument. The use of a default
3905
- argument in a function call causes specializations in the default
3906
- argument to be implicitly instantiated.
 
 
 
 
 
 
 
3907
 
3908
  Implicitly instantiated class, function, and variable template
3909
  specializations are placed in the namespace where the template is
3910
  defined. Implicitly instantiated specializations for members of a class
3911
  template are placed in the namespace where the enclosing class template
3912
  is defined. Implicitly instantiated member templates are placed in the
3913
  namespace where the enclosing class or class template is defined.
3914
 
3915
- [*Example 6*:
3916
 
3917
  ``` cpp
3918
  namespace N {
3919
  template<class T> class List {
3920
  public:
@@ -3931,11 +5426,11 @@ public:
3931
  void g(Map<const char*,int>& m) {
3932
  int i = m.get("Nicholas");
3933
  }
3934
  ```
3935
 
3936
- a call of `lt.get()` from `Map<const char*,int>::get()` would place
3937
  `List<int>::get()` in the namespace `N` rather than in the global
3938
  namespace.
3939
 
3940
  — *end example*]
3941
 
@@ -3944,19 +5439,19 @@ argument to be used, the dependent names are looked up, the semantics
3944
  constraints are checked, and the instantiation of any template used in
3945
  the default argument is done as if the default argument had been an
3946
  initializer used in a function template specialization with the same
3947
  scope, the same template parameters and the same access as that of the
3948
  function template `f` used at that point, except that the scope in which
3949
- a closure type is declared ([[expr.prim.lambda.closure]]) – and
3950
- therefore its associated namespaces – remain as determined from the
3951
- context of the definition for the default argument. This analysis is
3952
- called *default argument instantiation*. The instantiated default
3953
- argument is then used as the argument of `f`.
3954
 
3955
  Each default argument is instantiated independently.
3956
 
3957
- [*Example 7*:
3958
 
3959
  ``` cpp
3960
  template<class T> void f(T x, T y = ydef(T()), T z = zdef(T()));
3961
 
3962
  class A { };
@@ -3964,34 +5459,34 @@ class A { };
3964
  A zdef(A);
3965
 
3966
  void g(A a, A b, A c) {
3967
  f(a, b, c); // no default argument instantiation
3968
  f(a, b); // default argument z = zdef(T()) instantiated
3969
- f(a); // ill-formed; ydef is not declared
3970
  }
3971
  ```
3972
 
3973
  — *end example*]
3974
 
3975
  The *noexcept-specifier* of a function template specialization is not
3976
  instantiated along with the function declaration; it is instantiated
3977
- when needed ([[except.spec]]). If such an *noexcept-specifier* is
3978
- needed but has not yet been instantiated, the dependent names are looked
3979
- up, the semantics constraints are checked, and the instantiation of any
3980
  template used in the *noexcept-specifier* is done as if it were being
3981
  done as part of instantiating the declaration of the specialization at
3982
  that point.
3983
 
3984
- [*Note 3*: [[temp.point]] defines the point of instantiation of a
3985
  template specialization. — *end note*]
3986
 
3987
  There is an *implementation-defined* quantity that specifies the limit
3988
- on the total depth of recursive instantiations ([[implimits]]), which
3989
  could involve more than one template. The result of an infinite
3990
  recursion in instantiation is undefined.
3991
 
3992
- [*Example 8*:
3993
 
3994
  ``` cpp
3995
  template<class T> class X {
3996
  X<T>* p; // OK
3997
  X<T*> a; // implicit generation of X<T> requires
@@ -4000,53 +5495,119 @@ template<class T> class X {
4000
  };
4001
  ```
4002
 
4003
  — *end example*]
4004
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4005
  ### Explicit instantiation <a id="temp.explicit">[[temp.explicit]]</a>
4006
 
4007
  A class, function, variable, or member template specialization can be
4008
  explicitly instantiated from its template. A member function, member
4009
  class or static data member of a class template can be explicitly
4010
  instantiated from the member definition associated with its class
4011
- template. An explicit instantiation of a function template or member
4012
- function of a class template shall not use the `inline` or `constexpr`
4013
- specifiers.
4014
 
4015
  The syntax for explicit instantiation is:
4016
 
4017
  ``` bnf
4018
  explicit-instantiation:
4019
- 'extern'ₒₚₜ 'template' declaration
4020
  ```
4021
 
4022
  There are two forms of explicit instantiation: an explicit instantiation
4023
  definition and an explicit instantiation declaration. An explicit
4024
  instantiation declaration begins with the `extern` keyword.
4025
 
 
 
 
 
 
 
 
4026
  If the explicit instantiation is for a class or member class, the
4027
  *elaborated-type-specifier* in the *declaration* shall include a
4028
- *simple-template-id*. If the explicit instantiation is for a function or
4029
- member function, the *unqualified-id* in the *declaration* shall be
4030
- either a *template-id* or, where all template arguments can be deduced,
4031
- a *template-name* or *operator-function-id*.
 
 
 
4032
 
4033
  [*Note 1*: The declaration may declare a *qualified-id*, in which case
4034
  the *unqualified-id* of the *qualified-id* must be a
4035
  *template-id*. — *end note*]
4036
 
4037
  If the explicit instantiation is for a member function, a member class
4038
  or a static data member of a class template specialization, the name of
4039
  the class template specialization in the *qualified-id* for the member
4040
  name shall be a *simple-template-id*. If the explicit instantiation is
4041
- for a variable, the *unqualified-id* in the declaration shall be a
4042
- *template-id*. An explicit instantiation shall appear in an enclosing
4043
- namespace of its template. If the name declared in the explicit
4044
- instantiation is an unqualified name, the explicit instantiation shall
4045
- appear in the namespace where its template is declared or, if that
4046
- namespace is inline ([[namespace.def]]), any namespace from its
4047
- enclosing namespace set.
4048
 
4049
  [*Note 2*: Regarding qualified names in declarators, see 
4050
  [[dcl.meaning]]. — *end note*]
4051
 
4052
  [*Example 1*:
@@ -4074,17 +5635,50 @@ instantiation of that entity. A definition of a class template, a member
4074
  class of a class template, or a member class template of a class or
4075
  class template shall precede an explicit instantiation of that entity
4076
  unless the explicit instantiation is preceded by an explicit
4077
  specialization of the entity with the same template arguments. If the
4078
  *declaration* of the explicit instantiation names an implicitly-declared
4079
- special member function (Clause  [[special]]), the program is
4080
- ill-formed.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4081
 
4082
  For a given set of template arguments, if an explicit instantiation of a
4083
  template appears after a declaration of an explicit specialization for
4084
  that template, the explicit instantiation has no effect. Otherwise, for
4085
- an explicit instantiation definition the definition of a function
4086
  template, a variable template, a member function template, or a member
4087
  function or static data member of a class template shall be present in
4088
  every translation unit in which it is explicitly instantiated.
4089
 
4090
  An explicit instantiation of a class, function template, or variable
@@ -4092,11 +5686,11 @@ template specialization is placed in the namespace in which the template
4092
  is defined. An explicit instantiation for a member of a class template
4093
  is placed in the namespace where the enclosing class template is
4094
  defined. An explicit instantiation for a member template is placed in
4095
  the namespace where the enclosing class or class template is defined.
4096
 
4097
- [*Example 2*:
4098
 
4099
  ``` cpp
4100
  namespace N {
4101
  template<class T> class Y { void mf() { } };
4102
  }
@@ -4113,13 +5707,13 @@ template void N::Y<double>::mf(); // OK: explicit instantiation in namespa
4113
  — *end example*]
4114
 
4115
  A trailing *template-argument* can be left unspecified in an explicit
4116
  instantiation of a function template specialization or of a member
4117
  function template specialization provided it can be deduced from the
4118
- type of a function parameter ([[temp.deduct]]).
4119
 
4120
- [*Example 3*:
4121
 
4122
  ``` cpp
4123
  template<class T> class Array { ... };
4124
  template<class T> void sort(Array<T>& v) { ... }
4125
 
@@ -4127,70 +5721,63 @@ template<class T> void sort(Array<T>& v) { ... }
4127
  template void sort<>(Array<int>&);
4128
  ```
4129
 
4130
  — *end example*]
4131
 
 
 
 
 
 
 
 
 
 
4132
  An explicit instantiation that names a class template specialization is
4133
  also an explicit instantiation of the same kind (declaration or
4134
  definition) of each of its members (not including members inherited from
4135
  base classes and members that are templates) that has not been
4136
  previously explicitly specialized in the translation unit containing the
4137
- explicit instantiation, except as described below.
 
 
 
4138
 
4139
- [*Note 3*: In addition, it will typically be an explicit instantiation
4140
  of certain implementation-dependent data about the class. — *end note*]
4141
 
4142
  An explicit instantiation definition that names a class template
4143
  specialization explicitly instantiates the class template specialization
4144
  and is an explicit instantiation definition of only those members that
4145
  have been defined at the point of instantiation.
4146
 
4147
- Except for inline functions and variables, declarations with types
4148
- deduced from their initializer or return value ([[dcl.spec.auto]]),
4149
- `const` variables of literal types, variables of reference types, and
4150
- class template specializations, explicit instantiation declarations have
4151
- the effect of suppressing the implicit instantiation of the entity to
4152
- which they refer.
4153
-
4154
- [*Note 4*: The intent is that an inline function that is the subject of
4155
- an explicit instantiation declaration will still be implicitly
4156
- instantiated when odr-used ([[basic.def.odr]]) so that the body can be
4157
- considered for inlining, but that no out-of-line copy of the inline
4158
- function would be generated in the translation unit. — *end note*]
4159
 
4160
  If an entity is the subject of both an explicit instantiation
4161
  declaration and an explicit instantiation definition in the same
4162
  translation unit, the definition shall follow the declaration. An entity
4163
  that is the subject of an explicit instantiation declaration and that is
4164
- also used in a way that would otherwise cause an implicit
4165
- instantiation ([[temp.inst]]) in the translation unit shall be the
4166
- subject of an explicit instantiation definition somewhere in the
4167
- program; otherwise the program is ill-formed, no diagnostic required.
4168
 
4169
- [*Note 5*: This rule does apply to inline functions even though an
4170
  explicit instantiation declaration of such an entity has no other
4171
  normative effect. This is needed to ensure that if the address of an
4172
  inline function is taken in a translation unit in which the
4173
  implementation chose to suppress the out-of-line body, another
4174
  translation unit will supply the body. — *end note*]
4175
 
4176
  An explicit instantiation declaration shall not name a specialization of
4177
  a template with internal linkage.
4178
 
4179
- The usual access checking rules do not apply to names used to specify
4180
- explicit instantiations.
4181
-
4182
- [*Note 6*: In particular, the template arguments and names used in the
4183
- function declarator (including parameter types, return types and
4184
- exception specifications) may be private types or objects which would
4185
- normally not be accessible and the template may be a member template or
4186
- member function which would not normally be accessible. — *end note*]
4187
-
4188
  An explicit instantiation does not constitute a use of a default
4189
  argument, so default argument instantiation is not done.
4190
 
4191
- [*Example 4*:
4192
 
4193
  ``` cpp
4194
  char* p = 0;
4195
  template<class T> T g(T x = &p) { return x; }
4196
  template int g<int>(int); // OK even though &p isn't an int.
@@ -4214,11 +5801,11 @@ An explicit specialization of any of the following:
4214
 
4215
  can be declared by a declaration introduced by `template<>`; that is:
4216
 
4217
  ``` bnf
4218
  explicit-specialization:
4219
- 'template < >' declaration
4220
  ```
4221
 
4222
  [*Example 1*:
4223
 
4224
  ``` cpp
@@ -4239,10 +5826,13 @@ specializations instantiated from the class template. Similarly,
4239
  `Array<char*>`; other `Array` types will be sorted by functions
4240
  generated from the template.
4241
 
4242
  — *end example*]
4243
 
 
 
 
4244
  An explicit specialization may be declared in any scope in which the
4245
  corresponding primary template may be defined ([[namespace.memdef]],
4246
  [[class.mem]], [[temp.mem]]).
4247
 
4248
  A declaration of a function template, class template, or variable
@@ -4273,12 +5863,12 @@ enumeration, a member class template, a static data member, or a static
4273
  data member template of a class template may be explicitly specialized
4274
  for a class specialization that is implicitly instantiated; in this
4275
  case, the definition of the class template shall precede the explicit
4276
  specialization for the member of the class template. If such an explicit
4277
  specialization for the member of a class template names an
4278
- implicitly-declared special member function (Clause  [[special]]), the
4279
- program is ill-formed.
4280
 
4281
  A member of an explicitly specialized class is not implicitly
4282
  instantiated from the member declaration of the class template; instead,
4283
  the member of the class template specialization shall itself be
4284
  explicitly defined if its definition is required. In this case, the
@@ -4372,11 +5962,11 @@ template<class T> struct A {
4372
  };
4373
  template<> enum A<int>::E : int { eint }; // OK
4374
  template<> enum class A<int>::S : int { sint }; // OK
4375
  template<class T> enum A<T>::E : T { eT };
4376
  template<class T> enum class A<T>::S : T { sT };
4377
- template<> enum A<char>::E : char { echar }; // ill-formed, A<char>::E was instantiated
4378
  // when A<char> was instantiated
4379
  template<> enum class A<char>::S : char { schar }; // OK
4380
  ```
4381
 
4382
  — *end example*]
@@ -4420,12 +6010,12 @@ template<> class N::Y<short> { ... }; // OK: specialization in enclosing nam
4420
 
4421
  — *end example*]
4422
 
4423
  A *simple-template-id* that names a class template explicit
4424
  specialization that has been declared but not defined can be used
4425
- exactly like the names of other incompletely-defined classes (
4426
- [[basic.types]]).
4427
 
4428
  [*Example 6*:
4429
 
4430
  ``` cpp
4431
  template<class T> class X; // X is a class template
@@ -4452,18 +6042,27 @@ template<class T> void sort(Array<T>& v);
4452
  template<> void sort(Array<int>&);
4453
  ```
4454
 
4455
  — *end example*]
4456
 
 
 
 
 
 
 
 
 
 
4457
  A function with the same name as a template and a type that exactly
4458
  matches that of a template specialization is not an explicit
4459
- specialization ([[temp.fct]]).
4460
 
4461
- An explicit specialization of a function or variable template is inline
4462
- only if it is declared with the `inline` specifier or defined as
4463
- deleted, and independently of whether its function or variable template
4464
- is inline.
4465
 
4466
  [*Example 8*:
4467
 
4468
  ``` cpp
4469
  template<class T> void f(T) { ... }
@@ -4478,11 +6077,11 @@ template<> int g<>(int) { ... } // OK: not inline
4478
  An explicit specialization of a static data member of a template or an
4479
  explicit specialization of a static data member template is a definition
4480
  if the declaration includes an initializer; otherwise, it is a
4481
  declaration.
4482
 
4483
- [*Note 2*:
4484
 
4485
  The definition of a static data member of a template that requires
4486
  default-initialization must use a *braced-init-list*:
4487
 
4488
  ``` cpp
@@ -4552,11 +6151,11 @@ template<> template<> void A<char>::B<char>::mf();
4552
  In an explicit specialization declaration for a member of a class
4553
  template or a member template that appears in namespace scope, the
4554
  member template and some of its enclosing class templates may remain
4555
  unspecialized, except that the declaration shall not explicitly
4556
  specialize a class member template if its enclosing class templates are
4557
- not explicitly specialized as well. In such explicit specialization
4558
  declaration, the keyword `template` followed by a
4559
  *template-parameter-list* shall be provided instead of the `template<>`
4560
  preceding the explicit specialization declaration of the member. The
4561
  types of the *template-parameter*s in the *template-parameter-list*
4562
  shall be the same as those specified in the primary template definition.
@@ -4575,11 +6174,11 @@ template <> template <class X>
4575
  template <class T> void mf1(T);
4576
  };
4577
  template <> template <> template<class T>
4578
  void A<int>::B<double>::mf1(T t) { }
4579
  template <class Y> template <>
4580
- void A<Y>::B<double>::mf2() { } // ill-formed; B<double> is specialized but
4581
  // its enclosing class template A is not
4582
  ```
4583
 
4584
  — *end example*]
4585
 
@@ -4595,11 +6194,11 @@ definition for one of the following explicit specializations:
4595
 
4596
  - the explicit specialization of a function template;
4597
  - the explicit specialization of a member function template;
4598
  - the explicit specialization of a member function of a class template
4599
  where the class template specialization to which the member function
4600
- specialization belongs is implicitly instantiated. \[*Note 3*: Default
4601
  function arguments may be specified in the declaration or definition
4602
  of a member function of a class template specialization that is
4603
  explicitly specialized. — *end note*]
4604
 
4605
  ## Function template specializations <a id="temp.fct.spec">[[temp.fct.spec]]</a>
@@ -4634,13 +6233,14 @@ Here `f<int>(int*)` has a static variable `s` of type `int` and
4634
  — *end example*]
4635
 
4636
  ### Explicit template argument specification <a id="temp.arg.explicit">[[temp.arg.explicit]]</a>
4637
 
4638
  Template arguments can be specified when referring to a function
4639
- template specialization by qualifying the function template name with
4640
- the list of *template-argument*s in the same way as *template-argument*s
4641
- are specified in uses of a class template specialization.
 
4642
 
4643
  [*Example 1*:
4644
 
4645
  ``` cpp
4646
  template<class T> void sort(Array<T>& v);
@@ -4661,24 +6261,28 @@ void g(double d) {
4661
  }
4662
  ```
4663
 
4664
  — *end example*]
4665
 
 
 
 
 
4666
  A template argument list may be specified when referring to a
4667
  specialization of a function template
4668
 
4669
  - when a function is called,
4670
  - when the address of a function is taken, when a function initializes a
4671
  reference to function, or when a pointer to member function is formed,
4672
  - in an explicit specialization,
4673
  - in an explicit instantiation, or
4674
  - in a friend declaration.
4675
 
4676
- Trailing template arguments that can be deduced ([[temp.deduct]]) or
4677
  obtained from default *template-argument*s may be omitted from the list
4678
- of explicit *template-argument*s. A trailing template parameter pack (
4679
- [[temp.variadic]]) not otherwise deduced will be deduced to an empty
4680
  sequence of template arguments. If all of the template arguments can be
4681
  deduced, they may all be omitted; in this case, the empty template
4682
  argument list `<>` itself may also be omitted. In contexts where
4683
  deduction is done and fails, or in contexts where deduction is not done,
4684
  if a template argument list is specified and it, along with any default
@@ -4690,27 +6294,27 @@ template specialization.
4690
 
4691
  ``` cpp
4692
  template<class X, class Y> X f(Y);
4693
  template<class X, class Y, class ... Z> X g(Y);
4694
  void h() {
4695
- int i = f<int>(5.6); // Y is deduced to be double
4696
- int j = f(5.6); // ill-formed: X cannot be deduced
4697
- f<void>(f<int, bool>); // Y for outer f deduced to be int (*)(bool)
4698
- f<void>(f<int>); // ill-formed: f<int> does not denote a single function template specialization
4699
- int k = g<int>(5.6); // Y is deduced to be double, Z is deduced to an empty sequence
4700
- f<void>(g<int, bool>); // Y for outer f is deduced to be int (*)(bool),
4701
- // Z is deduced to an empty sequence
4702
  }
4703
  ```
4704
 
4705
  — *end example*]
4706
 
4707
  [*Note 1*:
4708
 
4709
  An empty template argument list can be used to indicate that a given use
4710
  refers to a specialization of a function template even when a
4711
- non-template function ([[dcl.fct]]) is visible that would otherwise be
4712
  used. For example:
4713
 
4714
  ``` cpp
4715
  template <class T> int f(T); // #1
4716
  int f(int); // #2
@@ -4731,23 +6335,23 @@ there are corresponding *template-parameter*s unless one of the
4731
  ``` cpp
4732
  template<class X, class Y, class Z> X f(Y,Z);
4733
  template<class ... Args> void f2();
4734
  void g() {
4735
  f<int,const char*,double>("aa",3.0);
4736
- f<int,const char*>("aa",3.0); // Z is deduced to be double
4737
- f<int>("aa",3.0); // Y is deduced to be const char*, and Z is deduced to be double
4738
  f("aa",3.0); // error: X cannot be deduced
4739
  f2<char, short, int, long>(); // OK
4740
  }
4741
  ```
4742
 
4743
  — *end example*]
4744
 
4745
- Implicit conversions (Clause  [[conv]]) will be performed on a function
4746
- argument to convert it to the type of the corresponding function
4747
- parameter if the parameter type contains no *template-parameter*s that
4748
- participate in template argument deduction.
4749
 
4750
  [*Note 2*:
4751
 
4752
  Template parameters do not participate in template argument deduction if
4753
  they are explicitly specified. For example,
@@ -4765,63 +6369,26 @@ void g() {
4765
  ```
4766
 
4767
  — *end note*]
4768
 
4769
  [*Note 3*: Because the explicit template argument list follows the
4770
- function template name, and because conversion member function templates
4771
- and constructor member function templates are called without using a
4772
- function name, there is no way to provide an explicit template argument
4773
- list for these function templates. — *end note*]
4774
-
4775
- [*Note 4*:
4776
-
4777
- For simple function names, argument dependent lookup (
4778
- [[basic.lookup.argdep]]) applies even when the function name is not
4779
- visible within the scope of the call. This is because the call still has
4780
- the syntactic form of a function call ([[basic.lookup.unqual]]). But
4781
- when a function template with explicit template arguments is used, the
4782
- call does not have the correct syntactic form unless there is a function
4783
- template with that name visible at the point of the call. If no such
4784
- name is visible, the call is not syntactically well-formed and
4785
- argument-dependent lookup does not apply. If some such name is visible,
4786
- argument dependent lookup applies and additional function templates may
4787
- be found in other namespaces.
4788
-
4789
- [*Example 4*:
4790
-
4791
- ``` cpp
4792
- namespace A {
4793
- struct B { };
4794
- template<int X> void f(B);
4795
- }
4796
- namespace C {
4797
- template<class T> void f(T t);
4798
- }
4799
- void g(A::B b) {
4800
- f<3>(b); // ill-formed: not a function call
4801
- A::f<3>(b); // well-formed
4802
- C::f<3>(b); // ill-formed; argument dependent lookup applies only to unqualified names
4803
- using C::f;
4804
- f<3>(b); // well-formed because C::f is visible; then A::f is found by argument dependent lookup
4805
- }
4806
- ```
4807
-
4808
- — *end example*]
4809
-
4810
- — *end note*]
4811
 
4812
  Template argument deduction can extend the sequence of template
4813
  arguments corresponding to a template parameter pack, even when the
4814
  sequence contains explicitly specified template arguments.
4815
 
4816
- [*Example 5*:
4817
 
4818
  ``` cpp
4819
  template<class ... Types> void f(Types ... values);
4820
 
4821
  void g() {
4822
- f<int*, float*>(0, 0, 0); // Types is deduced to the sequence int*, float*, int
4823
  }
4824
  ```
4825
 
4826
  — *end example*]
4827
 
@@ -4850,28 +6417,14 @@ void g(double d) {
4850
  }
4851
  ```
4852
 
4853
  — *end example*]
4854
 
4855
- When an explicit template argument list is specified, the template
4856
- arguments must be compatible with the template parameter list and must
4857
- result in a valid function type as described below; otherwise type
4858
- deduction fails. Specifically, the following steps are performed when
4859
- evaluating an explicitly specified template argument list with respect
4860
- to a given function template:
4861
-
4862
- - The specified template arguments must match the template parameters in
4863
- kind (i.e., type, non-type, template). There must not be more
4864
- arguments than there are parameters unless at least one parameter is a
4865
- template parameter pack, and there shall be an argument for each
4866
- non-pack parameter. Otherwise, type deduction fails.
4867
- - Non-type arguments must match the types of the corresponding non-type
4868
- template parameters, or must be convertible to the types of the
4869
- corresponding non-type parameters as specified in 
4870
- [[temp.arg.nontype]], otherwise type deduction fails.
4871
- - The specified template argument values are substituted for the
4872
- corresponding template parameters as specified below.
4873
 
4874
  After this substitution is performed, the function parameter type
4875
  adjustments described in  [[dcl.fct]] are performed.
4876
 
4877
  [*Example 2*: A parameter type of “`void (const int, int[5])`” becomes
@@ -4940,10 +6493,13 @@ void g() {
4940
  When all template arguments have been deduced or obtained from default
4941
  template arguments, all uses of template parameters in the template
4942
  parameter list of the template and the function type are replaced with
4943
  the corresponding deduced or default argument values. If the
4944
  substitution results in an invalid type, as described above, type
 
 
 
4945
  deduction fails.
4946
 
4947
  At certain points in the template argument deduction process it is
4948
  necessary to take a function type that makes use of template parameters
4949
  and replace those template parameters with the corresponding template
@@ -4958,11 +6514,14 @@ the function type and in template parameter declarations. The
4958
  expressions include not only constant expressions such as those that
4959
  appear in array bounds or as nontype template arguments but also general
4960
  expressions (i.e., non-constant expressions) inside `sizeof`,
4961
  `decltype`, and other contexts that allow non-constant expressions. The
4962
  substitution proceeds in lexical order and stops when a condition that
4963
- causes deduction to fail is encountered.
 
 
 
4964
 
4965
  [*Note 3*: The equivalent substitution in exception specifications is
4966
  done only when the *noexcept-specifier* is instantiated, at which point
4967
  a program is ill-formed if the substitution results in an invalid type
4968
  or expression. — *end note*]
@@ -4973,14 +6532,18 @@ or expression. — *end note*]
4973
  template <class T> struct A { using X = typename T::X; };
4974
  template <class T> typename T::X f(typename A<T>::X);
4975
  template <class T> void f(...) { }
4976
  template <class T> auto g(typename A<T>::X) -> typename T::X;
4977
  template <class T> void g(...) { }
 
 
 
4978
 
4979
- void h() {
4980
  f<int>(0); // OK, substituting return type causes deduction to fail
4981
  g<int>(0); // error, substituting parameter type instantiates A<int>
 
4982
  }
4983
  ```
4984
 
4985
  — *end example*]
4986
 
@@ -4992,22 +6555,64 @@ arguments.
4992
  [*Note 4*: If no diagnostic is required, the program is still
4993
  ill-formed. Access checking is done as part of the substitution
4994
  process. — *end note*]
4995
 
4996
  Only invalid types and expressions in the immediate context of the
4997
- function type and its template parameter types can result in a deduction
4998
- failure.
4999
 
5000
  [*Note 5*: The substitution into types and expressions can result in
5001
  effects such as the instantiation of class template specializations
5002
  and/or function template specializations, the generation of
5003
  implicitly-defined functions, etc. Such effects are not in the
5004
  “immediate context” and can result in the program being
5005
  ill-formed. — *end note*]
5006
 
 
 
 
 
 
 
 
 
 
5007
  [*Example 6*:
5008
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5009
  ``` cpp
5010
  struct X { };
5011
  struct Y {
5012
  Y(X){}
5013
  };
@@ -5019,30 +6624,30 @@ X x1, x2;
5019
  X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
5020
  ```
5021
 
5022
  — *end example*]
5023
 
5024
- [*Note 6*:
5025
 
5026
  Type deduction may fail for the following reasons:
5027
 
5028
- - Attempting to instantiate a pack expansion containing multiple
5029
- parameter packs of differing lengths.
5030
  - Attempting to create an array with an element type that is `void`, a
5031
- function type, a reference type, or an abstract class type, or
5032
- attempting to create an array with a size that is zero or negative.
5033
- \[*Example 7*:
5034
  ``` cpp
5035
  template <class T> int f(T[5]);
5036
  int I = f<int>(0);
5037
  int j = f<void>(0); // invalid array
5038
  ```
5039
 
5040
  — *end example*]
5041
  - Attempting to use a type that is not a class or enumeration type in a
5042
  qualified name.
5043
- \[*Example 8*:
5044
  ``` cpp
5045
  template <class T> int f(typename T::B*);
5046
  int i = f<int>(0);
5047
  ```
5048
 
@@ -5053,11 +6658,11 @@ Type deduction may fail for the following reasons:
5053
  - the specified member is not a type where a type is required, or
5054
  - the specified member is not a template where a template is required,
5055
  or
5056
  - the specified member is not a non-type where a non-type is required.
5057
 
5058
- \[*Example 9*:
5059
  ``` cpp
5060
  template <int I> struct X { };
5061
  template <template <class T> class> struct Z { };
5062
  template <class T> void f(typename T::Y*){}
5063
  template <class T> void g(X<T::N>*){}
@@ -5083,19 +6688,19 @@ Type deduction may fail for the following reasons:
5083
  — *end example*]
5084
  - Attempting to create a pointer to reference type.
5085
  - Attempting to create a reference to `void`.
5086
  - Attempting to create “pointer to member of `T`” when `T` is not a
5087
  class type.
5088
- \[*Example 10*:
5089
  ``` cpp
5090
  template <class T> int f(int T::*);
5091
  int i = f<int>(0);
5092
  ```
5093
 
5094
  — *end example*]
5095
  - Attempting to give an invalid type to a non-type template parameter.
5096
- \[*Example 11*:
5097
  ``` cpp
5098
  template <class T, T> struct S {};
5099
  template <class T> int f(S<T, T()>*);
5100
  struct X {};
5101
  int i0 = f<X>(0);
@@ -5103,32 +6708,30 @@ Type deduction may fail for the following reasons:
5103
 
5104
  — *end example*]
5105
  - Attempting to perform an invalid conversion in either a template
5106
  argument expression, or an expression used in the function
5107
  declaration.
5108
- \[*Example 12*:
5109
  ``` cpp
5110
  template <class T, T*> int f(int);
5111
  int i2 = f<int,1>(0); // can't conv 1 to int*
5112
  ```
5113
 
5114
  — *end example*]
5115
  - Attempting to create a function type in which a parameter has a type
5116
  of `void`, or in which the return type is a function type or array
5117
  type.
5118
- - Attempting to create a function type in which a parameter type or the
5119
- return type is an abstract class type ([[class.abstract]]).
5120
 
5121
  — *end note*]
5122
 
5123
- [*Example 13*:
5124
 
5125
  In the following example, assuming a `signed char` cannot represent the
5126
- value 1000, a narrowing conversion ([[dcl.init.list]]) would be
5127
- required to convert the *template-argument* of type `int` to
5128
- `signed char`, therefore substitution fails for the second template (
5129
- [[temp.arg.nontype]]).
5130
 
5131
  ``` cpp
5132
  template <int> int f(int);
5133
  template <signed char> int f(int);
5134
  int i1 = f<1000>(0); // OK
@@ -5142,84 +6745,93 @@ int i2 = f<1>(0); // ambiguous; not narrowing
5142
  Template argument deduction is done by comparing each function template
5143
  parameter type (call it `P`) that contains *template-parameter*s that
5144
  participate in template argument deduction with the type of the
5145
  corresponding argument of the call (call it `A`) as described below. If
5146
  removing references and cv-qualifiers from `P` gives
5147
- `std::initializer_list<P'>` or `P'[N]` for some `P'` and `N` and the
5148
- argument is a non-empty initializer list ([[dcl.init.list]]), then
5149
- deduction is performed instead for each element of the initializer list,
5150
- taking `P'` as a function template parameter type and the initializer
5151
- element as its argument, and in the `P'[N]` case, if `N` is a non-type
5152
- template parameter, `N` is deduced from the length of the initializer
5153
- list. Otherwise, an initializer list argument causes the parameter to be
5154
- considered a non-deduced context ([[temp.deduct.type]]).
 
5155
 
5156
  [*Example 1*:
5157
 
5158
  ``` cpp
5159
  template<class T> void f(std::initializer_list<T>);
5160
- f({1,2,3}); // T deduced to int
5161
- f({1,"asdf"}); // error: T deduced to both int and const char*
5162
 
5163
  template<class T> void g(T);
5164
  g({1,2,3}); // error: no argument deduced for T
5165
 
5166
  template<class T, int N> void h(T const(&)[N]);
5167
- h({1,2,3}); // T deduced to int, N deduced to 3
5168
 
5169
  template<class T> void j(T const(&)[3]);
5170
- j({42}); // T deduced to int, array bound not considered
5171
 
5172
  struct Aggr { int i; int j; };
5173
  template<int N> void k(Aggr const(&)[N]);
5174
  k({1,2,3}); // error: deduction fails, no conversion from int to Aggr
5175
- k({{1},{2},{3}}); // OK, N deduced to 3
5176
 
5177
  template<int M, int N> void m(int const(&)[M][N]);
5178
- m({{1,2},{3,4}}); // M and N both deduced to 2
5179
 
5180
  template<class T, int N> void n(T const(&)[N], T);
5181
  n({{1},{2},{3}},Aggr()); // OK, T is Aggr, N is 3
 
 
 
 
 
 
 
 
5182
  ```
5183
 
5184
  — *end example*]
5185
 
5186
  For a function parameter pack that occurs at the end of the
5187
  *parameter-declaration-list*, deduction is performed for each remaining
5188
  argument of the call, taking the type `P` of the *declarator-id* of the
5189
  function parameter pack as the corresponding function template parameter
5190
  type. Each deduction deduces template arguments for subsequent positions
5191
  in the template parameter packs expanded by the function parameter pack.
5192
- When a function parameter pack appears in a non-deduced context (
5193
- [[temp.deduct.type]]), the type of that parameter pack is never deduced.
5194
 
5195
  [*Example 2*:
5196
 
5197
  ``` cpp
5198
  template<class ... Types> void f(Types& ...);
5199
  template<class T1, class ... Types> void g(T1, Types ...);
5200
  template<class T1, class ... Types> void g1(Types ..., T1);
5201
 
5202
  void h(int x, float& y) {
5203
  const int z = x;
5204
- f(x, y, z); // Types is deduced to int, float, const int
5205
- g(x, y, z); // T1 is deduced to int; Types is deduced to float, int
5206
  g1(x, y, z); // error: Types is not deduced
5207
  g1<int, int, int>(x, y, z); // OK, no deduction occurs
5208
  }
5209
  ```
5210
 
5211
  — *end example*]
5212
 
5213
  If `P` is not a reference type:
5214
 
5215
  - If `A` is an array type, the pointer type produced by the
5216
- array-to-pointer standard conversion ([[conv.array]]) is used in
5217
- place of `A` for type deduction; otherwise,
5218
  - If `A` is a function type, the pointer type produced by the
5219
- function-to-pointer standard conversion ([[conv.func]]) is used in
5220
- place of `A` for type deduction; otherwise,
5221
  - If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s
5222
  type are ignored for type deduction.
5223
 
5224
  If `P` is a cv-qualified type, the top-level cv-qualifiers of `P`’s type
5225
  are ignored for type deduction. If `P` is a reference type, the type
@@ -5238,12 +6850,12 @@ int n3 = g(i); // calls g<const int>(const volatile int&)
5238
 
5239
  — *end example*]
5240
 
5241
  A *forwarding reference* is an rvalue reference to a cv-unqualified
5242
  template parameter that does not represent a template parameter of a
5243
- class template (during class template argument deduction (
5244
- [[over.match.class.deduct]])). If `P` is a forwarding reference and the
5245
  argument is an lvalue, the type “lvalue reference to `A`” is used in
5246
  place of `A` for type deduction.
5247
 
5248
  [*Example 4*:
5249
 
@@ -5279,19 +6891,38 @@ values that will make the deduced `A` identical to `A` (after the type
5279
  that allow a difference:
5280
 
5281
  - If the original `P` is a reference type, the deduced `A` (i.e., the
5282
  type referred to by the reference) can be more cv-qualified than the
5283
  transformed `A`.
5284
- - The transformed `A` can be another pointer or pointer to member type
5285
  that can be converted to the deduced `A` via a function pointer
5286
- conversion ([[conv.fctptr]]) and/or qualification conversion (
5287
- [[conv.qual]]).
5288
  - If `P` is a class and `P` has the form *simple-template-id*, then the
5289
- transformed `A` can be a derived class of the deduced `A`. Likewise,
5290
- if `P` is a pointer to a class of the form *simple-template-id*, the
5291
- transformed `A` can be a pointer to a derived class pointed to by the
5292
- deduced `A`.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5293
 
5294
  These alternatives are considered only if type deduction would otherwise
5295
  fail. If they yield more than one possible deduced `A`, the type
5296
  deduction fails.
5297
 
@@ -5299,23 +6930,23 @@ deduction fails.
5299
  parameters of a function template, or is used only in a non-deduced
5300
  context, its corresponding *template-argument* cannot be deduced from a
5301
  function call and the *template-argument* must be explicitly
5302
  specified. — *end note*]
5303
 
5304
- When `P` is a function type, function pointer type, or pointer to member
5305
- function type:
5306
 
5307
  - If the argument is an overload set containing one or more function
5308
  templates, the parameter is treated as a non-deduced context.
5309
  - If the argument is an overload set (not containing function
5310
  templates), trial argument deduction is attempted using each of the
5311
  members of the set. If deduction succeeds for only one of the overload
5312
  set members, that member is used as the argument value for the
5313
  deduction. If deduction succeeds for more than one member of the
5314
  overload set the parameter is treated as a non-deduced context.
5315
 
5316
- [*Example 5*:
5317
 
5318
  ``` cpp
5319
  // Only one function of an overload set matches the call so the function parameter is a deduced context.
5320
  template <class T> int f(T (*p)(T));
5321
  int g(int);
@@ -5323,11 +6954,11 @@ int g(char);
5323
  int i = f(g); // calls f(int (*)(int))
5324
  ```
5325
 
5326
  — *end example*]
5327
 
5328
- [*Example 6*:
5329
 
5330
  ``` cpp
5331
  // Ambiguous deduction causes the second function parameter to be a non-deduced context.
5332
  template <class T> int f(T, T (*p)(T));
5333
  int g(int);
@@ -5335,11 +6966,11 @@ char g(char);
5335
  int i = f(1, g); // calls f(int, int (*)(int))
5336
  ```
5337
 
5338
  — *end example*]
5339
 
5340
- [*Example 7*:
5341
 
5342
  ``` cpp
5343
  // The overload set contains a template, causing the second function parameter to be a non-deduced context.
5344
  template <class T> int f(T, T (*p)(T));
5345
  char g(char);
@@ -5362,11 +6993,11 @@ explicitly-specified template arguments, if the corresponding argument
5362
  *template-parameter*s participate in template argument deduction, and
5363
  parameters that became non-dependent due to substitution of
5364
  explicitly-specified template arguments, will be checked during overload
5365
  resolution. — *end note*]
5366
 
5367
- [*Example 8*:
5368
 
5369
  ``` cpp
5370
  template <class T> struct Z {
5371
  typedef typename T::x xx;
5372
  };
@@ -5381,16 +7012,17 @@ resolution. — *end note*]
5381
  — *end example*]
5382
 
5383
  #### Deducing template arguments taking the address of a function template <a id="temp.deduct.funcaddr">[[temp.deduct.funcaddr]]</a>
5384
 
5385
  Template arguments can be deduced from the type specified when taking
5386
- the address of an overloaded function ([[over.over]]). The function
5387
- template’s function type and the specified type are used as the types of
5388
- `P` and `A`, and the deduction is done as described in 
5389
- [[temp.deduct.type]].
 
5390
 
5391
- A placeholder type ([[dcl.spec.auto]]) in the return type of a function
5392
  template is a non-deduced context. If template argument deduction
5393
  succeeds for such a function, the return type is determined from
5394
  instantiation of the function body.
5395
 
5396
  #### Deducing conversion function template arguments <a id="temp.deduct.conv">[[temp.deduct.conv]]</a>
@@ -5401,20 +7033,20 @@ required as the result of the conversion (call it `A`; see 
5401
  [[dcl.init]], [[over.match.conv]], and [[over.match.ref]] for the
5402
  determination of that type) as described in  [[temp.deduct.type]].
5403
 
5404
  If `P` is a reference type, the type referred to by `P` is used in place
5405
  of `P` for type deduction and for any further references to or
5406
- transformations of `P` in the remainder of this section.
5407
 
5408
  If `A` is not a reference type:
5409
 
5410
  - If `P` is an array type, the pointer type produced by the
5411
- array-to-pointer standard conversion ([[conv.array]]) is used in
5412
- place of `P` for type deduction; otherwise,
5413
  - If `P` is a function type, the pointer type produced by the
5414
- function-to-pointer standard conversion ([[conv.func]]) is used in
5415
- place of `P` for type deduction; otherwise,
5416
  - If `P` is a cv-qualified type, the top-level cv-qualifiers of `P`’s
5417
  type are ignored for type deduction.
5418
 
5419
  If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s type
5420
  are ignored for type deduction. If `A` is a reference type, the type
@@ -5423,46 +7055,23 @@ referred to by `A` is used for type deduction.
5423
  In general, the deduction process attempts to find template argument
5424
  values that will make the deduced `A` identical to `A`. However, there
5425
  are four cases that allow a difference:
5426
 
5427
  - If the original `A` is a reference type, `A` can be more cv-qualified
5428
- than the deduced `A` (i.e., the type referred to by the reference)
5429
  - If the original `A` is a function pointer type, `A` can be “pointer to
5430
- function” even if the deduced `A` is “pointer to noexcept function”.
5431
- - If the original `A` is a pointer to member function type, `A` can be
5432
  “pointer to member of type function” even if the deduced `A` is
5433
- “pointer to member of type noexcept function”.
5434
- - The deduced `A` can be another pointer or pointer to member type that
5435
  can be converted to `A` via a qualification conversion.
5436
 
5437
  These alternatives are considered only if type deduction would otherwise
5438
  fail. If they yield more than one possible deduced `A`, the type
5439
  deduction fails.
5440
 
5441
- When the deduction process requires a qualification conversion for a
5442
- pointer or pointer to member type as described above, the following
5443
- process is used to determine the deduced template argument values:
5444
-
5445
- If `A` is a type
5446
-
5447
- and `P` is a type
5448
-
5449
- then the cv-unqualified `T1` and `T2` are used as the types of `A` and
5450
- `P` respectively for type deduction.
5451
-
5452
- [*Example 1*:
5453
-
5454
- ``` cpp
5455
- struct A {
5456
- template <class T> operator T***();
5457
- };
5458
- A a;
5459
- const int * const * const * p1 = a; // T is deduced as int, not const int
5460
- ```
5461
-
5462
- — *end example*]
5463
-
5464
  #### Deducing template arguments during partial ordering <a id="temp.deduct.partial">[[temp.deduct.partial]]</a>
5465
 
5466
  Template argument deduction is done by comparing certain types
5467
  associated with the two function templates being compared.
5468
 
@@ -5483,21 +7092,19 @@ as the parameter template.
5483
 
5484
  The types used to determine the ordering depend on the context in which
5485
  the partial ordering is done:
5486
 
5487
  - In the context of a function call, the types used are those function
5488
- parameter types for which the function call has arguments.[^7]
5489
  - In the context of a call to a conversion function, the return types of
5490
  the conversion function templates are used.
5491
- - In other contexts ([[temp.func.order]]) the function template’s
5492
- function type is used.
5493
 
5494
  Each type nominated above from the parameter template and the
5495
  corresponding type from the argument template are used as the types of
5496
- `P` and `A`. If a particular `P` contains no *template-parameter*s that
5497
- participate in template argument deduction, that `P` is not used to
5498
- determine the ordering.
5499
 
5500
  Before the partial ordering is done, certain transformations are
5501
  performed on the types used for partial ordering:
5502
 
5503
  - If `P` is a reference type, `P` is replaced by the type referred to.
@@ -5541,14 +7148,13 @@ f(1, 2); // calls #3; non-variadic template #3 is more specialized
5541
  // than the variadic templates #1 and #2
5542
  ```
5543
 
5544
  — *end example*]
5545
 
5546
- If, for a given type, deduction succeeds in both directions (i.e., the
5547
- types are identical after the transformations above) and both `P` and
5548
- `A` were reference types (before being replaced with the type referred
5549
- to above):
5550
 
5551
  - if the type from the argument template was an lvalue reference and the
5552
  type from the parameter template was not, the parameter type is not
5553
  considered to be at least as specialized as the argument type;
5554
  otherwise,
@@ -5563,18 +7169,18 @@ from `F` is at least as specialized as the type from `G`. `F` is *more
5563
  specialized than* `G` if `F` is at least as specialized as `G` and `G`
5564
  is not at least as specialized as `F`.
5565
 
5566
  If, after considering the above, function template `F` is at least as
5567
  specialized as function template `G` and vice-versa, and if `G` has a
5568
- trailing parameter pack for which `F` does not have a corresponding
5569
- parameter, and if `F` does not have a trailing parameter pack, then `F`
5570
- is more specialized than `G`.
5571
 
5572
- In most cases, all template parameters must have values in order for
5573
- deduction to succeed, but for partial ordering purposes a template
5574
- parameter may remain without a value provided it is not used in the
5575
- types being used for partial ordering.
5576
 
5577
  [*Note 2*: A template parameter used in a non-deduced context is
5578
  considered used. — *end note*]
5579
 
5580
  [*Example 2*:
@@ -5633,32 +7239,32 @@ array bound if it is not otherwise deduced.
5633
  A given type `P` can be composed from a number of other types,
5634
  templates, and non-type values:
5635
 
5636
  - A function type includes the types of each of the function parameters
5637
  and the return type.
5638
- - A pointer to member type includes the type of the class object pointed
5639
  to and the type of the member pointed to.
5640
  - A type that is a specialization of a class template (e.g., `A<int>`)
5641
  includes the types, templates, and non-type values referenced by the
5642
  template argument list of the specialization.
5643
  - An array type includes the array element type and the value of the
5644
  array bound.
5645
 
5646
  In most cases, the types, templates, and non-type values that are used
5647
  to compose `P` participate in template argument deduction. That is, they
5648
- may be used to determine the value of a template argument, and the value
5649
- so determined must be consistent with the values determined elsewhere.
5650
- In certain contexts, however, the value does not participate in type
5651
- deduction, but instead uses the values of template arguments that were
5652
- either deduced elsewhere or explicitly specified. If a template
5653
- parameter is used only in non-deduced contexts and is not explicitly
5654
- specified, template argument deduction fails.
 
5655
 
5656
- [*Note 1*: Under [[temp.deduct.call]] and [[temp.deduct.partial]], if
5657
- `P` contains no *template-parameter*s that appear in deduced contexts,
5658
- no deduction is done, so `P` and `A` need not have the same
5659
- form. — *end note*]
5660
 
5661
  The non-deduced contexts are:
5662
 
5663
  - The *nested-name-specifier* of a type that was specified using a
5664
  *qualified-id*.
@@ -5666,23 +7272,21 @@ The non-deduced contexts are:
5666
  - A non-type template argument or an array bound in which a
5667
  subexpression references a template parameter.
5668
  - A template parameter used in the parameter type of a function
5669
  parameter that has a default argument that is being used in the call
5670
  for which argument deduction is being done.
5671
- - A function parameter for which argument deduction cannot be done
5672
- because the associated function argument is a function, or a set of
5673
- overloaded functions ([[over.over]]), and one or more of the
5674
- following apply:
5675
  - more than one function matches the function parameter type
5676
  (resulting in an ambiguous deduction), or
5677
  - no function matches the function parameter type, or
5678
- - the set of functions supplied as an argument contains one or more
5679
  function templates.
5680
  - A function parameter for which the associated argument is an
5681
- initializer list ([[dcl.init.list]]) but the parameter does not have
5682
- a type for which deduction from an initializer list is specified (
5683
- [[temp.deduct.call]]).
5684
  \[*Example 1*:
5685
  ``` cpp
5686
  template<class T> void g(T);
5687
  g({1,2,3}); // error: no argument deduced for T
5688
  ```
@@ -5772,11 +7376,11 @@ A template type argument `T`, a template template argument `TT` or a
5772
  template non-type argument `i` can be deduced if `P` and `A` have one of
5773
  the following forms:
5774
 
5775
  ``` cpp
5776
  T
5777
- cv-list T
5778
  T*
5779
  T&
5780
  T&&
5781
  T[integer-constant]
5782
  template-name<T> (where template-name refers to a class template)
@@ -5798,12 +7402,12 @@ template-name<i> (where template-name refers to a class template)
5798
  TT<T>
5799
  TT<i>
5800
  TT<>
5801
  ```
5802
 
5803
- where `(T)` represents a parameter-type-list ([[dcl.fct]]) where at
5804
- least one parameter type contains a `T`, and `()` represents a
5805
  parameter-type-list where no parameter type contains a `T`. Similarly,
5806
  `<T>` represents template argument lists where at least one argument
5807
  contains a `T`, `<i>` represents template argument lists where at least
5808
  one argument contains an `i` and `<>` represents template argument lists
5809
  where no argument contains a `T` or an `i`.
@@ -5815,12 +7419,12 @@ corresponding argument Aᵢ of the corresponding template argument list of
5815
  is not the last template argument, the entire template argument list is
5816
  a non-deduced context. If `Pᵢ` is a pack expansion, then the pattern of
5817
  `Pᵢ` is compared with each remaining argument in the template argument
5818
  list of `A`. Each comparison deduces template arguments for subsequent
5819
  positions in the template parameter packs expanded by `Pᵢ`. During
5820
- partial ordering ([[temp.deduct.partial]]), if `Aᵢ` was originally a
5821
- pack expansion:
5822
 
5823
  - if `P` does not contain a template argument corresponding to `Aᵢ` then
5824
  `Aᵢ` is ignored;
5825
  - otherwise, if `Pᵢ` is not a pack expansion, template argument
5826
  deduction fails.
@@ -5840,19 +7444,19 @@ template struct A<int, int*>; // selects #2
5840
  ```
5841
 
5842
  — *end example*]
5843
 
5844
  Similarly, if `P` has a form that contains `(T)`, then each parameter
5845
- type `Pᵢ` of the respective parameter-type-list ([[dcl.fct]]) of `P` is
5846
  compared with the corresponding parameter type `Aᵢ` of the corresponding
5847
  parameter-type-list of `A`. If `P` and `A` are function types that
5848
- originated from deduction when taking the address of a function
5849
- template ([[temp.deduct.funcaddr]]) or when deducing template arguments
5850
- from a function declaration ([[temp.deduct.decl]]) and `Pᵢ` and `Aᵢ`
5851
- are parameters of the top-level parameter-type-list of `P` and `A`,
5852
- respectively, `Pᵢ` is adjusted if it is a forwarding reference (
5853
- [[temp.deduct.call]]) and `Aᵢ` is an lvalue reference, in which case the
5854
  type of `Pᵢ` is changed to be the template parameter type (i.e., `T&&`
5855
  is changed to simply `T`).
5856
 
5857
  [*Note 2*: As a result, when `Pᵢ` is `T&&` and `Aᵢ` is `X&`, the
5858
  adjusted `Pᵢ` will be `T`, causing `T` to be deduced as
@@ -5875,11 +7479,11 @@ void g(int i) {
5875
  If the *parameter-declaration* corresponding to `Pᵢ` is a function
5876
  parameter pack, then the type of its *declarator-id* is compared with
5877
  each remaining parameter type in the parameter-type-list of `A`. Each
5878
  comparison deduces template arguments for subsequent positions in the
5879
  template parameter packs expanded by the function parameter pack. During
5880
- partial ordering ([[temp.deduct.partial]]), if `Aᵢ` was originally a
5881
  function parameter pack:
5882
 
5883
  - if `P` does not contain a function parameter type corresponding to
5884
  `Aᵢ` then `Aᵢ` is ignored;
5885
  - otherwise, if `Pᵢ` is not a function parameter pack, template argument
@@ -5937,11 +7541,11 @@ template<typename T> struct C;
5937
  template<typename T, T n> struct C<A<n>> {
5938
  using Q = T;
5939
  };
5940
 
5941
  using R = long;
5942
- using R = C<A<2>>::Q; // OK; T was deduced to long from the
5943
  // template argument value in the type A<2>
5944
  ```
5945
 
5946
  — *end example*]
5947
 
@@ -5954,11 +7558,11 @@ template<typename T> struct S;
5954
  template<typename T, T n> struct S<int[n]> {
5955
  using Q = T;
5956
  };
5957
 
5958
  using V = decltype(sizeof 0);
5959
- using V = S<int[42]>::Q; // OK; T was deduced to std::size_t from the type int[42]
5960
  ```
5961
 
5962
  — *end example*]
5963
 
5964
  [*Example 10*:
@@ -5983,15 +7587,15 @@ template<int i> void f1(int a[10][i]);
5983
  template<int i> void f2(int a[i][20]);
5984
  template<int i> void f3(int (&a)[i][20]);
5985
 
5986
  void g() {
5987
  int v[10][20];
5988
- f1(v); // OK: i deduced to be 20
5989
  f1<20>(v); // OK
5990
  f2(v); // error: cannot deduce template-argument i
5991
  f2<10>(v); // OK
5992
- f3(v); // OK: i deduced to be 10
5993
  }
5994
  ```
5995
 
5996
  — *end note*]
5997
 
@@ -6033,21 +7637,21 @@ T deduce(typename A<T>::X x, // T is not deduced here
6033
  typename B<i>::Y y); // i is not deduced here
6034
  A<int> a;
6035
  B<77> b;
6036
 
6037
  int x = deduce<77>(a.xm, 62, b.ym);
6038
- // T is deduced to be int, a.xm must be convertible to A<int>::X
6039
- // i is explicitly specified to be 77, b.ym must be convertible to B<77>::Y
6040
  ```
6041
 
6042
  — *end note*]
6043
 
6044
  If `P` has a form that contains `<i>`, and if the type of `i` differs
6045
  from the type of the corresponding template parameter of the template
6046
  named by the enclosing *simple-template-id*, deduction fails. If `P` has
6047
  a form that contains `[i]`, and if the type of `i` is not an integral
6048
- type, deduction fails.[^8]
6049
 
6050
  [*Example 12*:
6051
 
6052
  ``` cpp
6053
  template<int i> class A { ... };
@@ -6067,11 +7671,11 @@ void k2() {
6067
  ```
6068
 
6069
  — *end example*]
6070
 
6071
  A *template-argument* can be deduced from a function, pointer to
6072
- function, or pointer to member function type.
6073
 
6074
  [*Example 13*:
6075
 
6076
  ``` cpp
6077
  template<class T> void f(void(*)(T,int));
@@ -6120,12 +7724,12 @@ A<B> ab;
6120
  f(ab); // calls f(A<B>)
6121
  ```
6122
 
6123
  — *end example*]
6124
 
6125
- [*Note 6*: Template argument deduction involving parameter packs (
6126
- [[temp.variadic]]) can deduce zero or more arguments for each parameter
6127
  pack. — *end note*]
6128
 
6129
  [*Example 16*:
6130
 
6131
  ``` cpp
@@ -6151,49 +7755,47 @@ int fv = f(g); // OK; Types contains int, float
6151
  #### Deducing template arguments from a function declaration <a id="temp.deduct.decl">[[temp.deduct.decl]]</a>
6152
 
6153
  In a declaration whose *declarator-id* refers to a specialization of a
6154
  function template, template argument deduction is performed to identify
6155
  the specialization to which the declaration refers. Specifically, this
6156
- is done for explicit instantiations ([[temp.explicit]]), explicit
6157
- specializations ([[temp.expl.spec]]), and certain friend declarations (
6158
- [[temp.friend]]). This is also done to determine whether a deallocation
6159
  function template specialization matches a placement `operator new` (
6160
- [[basic.stc.dynamic.deallocation]],  [[expr.new]]). In all these cases,
6161
  `P` is the type of the function template being considered as a potential
6162
  match and `A` is either the function type from the declaration or the
6163
  type of the deallocation function that would match the placement
6164
  `operator new` as described in  [[expr.new]]. The deduction is done as
6165
  described in  [[temp.deduct.type]].
6166
 
6167
  If, for the set of function templates so considered, there is either no
6168
- match or more than one match after partial ordering has been
6169
- considered ([[temp.func.order]]), deduction fails and, in the
6170
- declaration cases, the program is ill-formed.
6171
 
6172
  ### Overload resolution <a id="temp.over">[[temp.over]]</a>
6173
 
6174
- A function template can be overloaded either by (non-template) functions
6175
- of its name or by (other) function templates of the same name. When a
6176
- call to that name is written (explicitly, or implicitly using the
6177
- operator notation), template argument deduction ([[temp.deduct]]) and
6178
- checking of any explicit template arguments ([[temp.arg]]) are
6179
- performed for each function template to find the template argument
6180
- values (if any) that can be used with that function template to
6181
- instantiate a function template specialization that can be invoked with
6182
- the call arguments. For each function template, if the argument
6183
- deduction and checking succeeds, the *template-argument*s (deduced
6184
- and/or explicit) are used to synthesize the declaration of a single
6185
- function template specialization which is added to the candidate
6186
  functions set to be used in overload resolution. If, for a given
6187
  function template, argument deduction fails or the synthesized function
6188
  template specialization would be ill-formed, no such function is added
6189
  to the set of candidate functions for that template. The complete set of
6190
  candidate functions includes all the synthesized declarations and all of
6191
  the non-template overloaded functions of the same name. The synthesized
6192
  declarations are treated like any other functions in the remainder of
6193
  overload resolution, except as explicitly noted in 
6194
- [[over.match.best]].[^9]
6195
 
6196
  [*Example 1*:
6197
 
6198
  ``` cpp
6199
  template<class T> T max(T a, T b) { return a>b?a:b; }
@@ -6275,52 +7877,10 @@ and not defined at the point of the call. The program will be ill-formed
6275
  unless a specialization for `f<const char*>`, either implicitly or
6276
  explicitly generated, is present in some translation unit.
6277
 
6278
  — *end example*]
6279
 
6280
- ## Deduction guides <a id="temp.deduct.guide">[[temp.deduct.guide]]</a>
6281
-
6282
- Deduction guides are used when a *template-name* appears as a type
6283
- specifier for a deduced class type ([[dcl.type.class.deduct]]).
6284
- Deduction guides are not found by name lookup. Instead, when performing
6285
- class template argument deduction ([[over.match.class.deduct]]), any
6286
- deduction guides declared for the class template are considered.
6287
-
6288
- ``` bnf
6289
- deduction-guide:
6290
- 'explicit'ₒₚₜ template-name '(' parameter-declaration-clause ') ->' simple-template-id ';'
6291
- ```
6292
-
6293
- [*Example 1*:
6294
-
6295
- ``` cpp
6296
- template<class T, class D = int>
6297
- struct S {
6298
- T data;
6299
- };
6300
- template<class U>
6301
- S(U) -> S<typename U::type>;
6302
-
6303
- struct A {
6304
- using type = short;
6305
- operator type();
6306
- };
6307
- S x{A()}; // x is of type S<short, int>
6308
- ```
6309
-
6310
- — *end example*]
6311
-
6312
- The same restrictions apply to the *parameter-declaration-clause* of a
6313
- deduction guide as in a function declaration ([[dcl.fct]]). The
6314
- *simple-template-id* shall name a class template specialization. The
6315
- *template-name* shall be the same *identifier* as the *template-name* of
6316
- the *simple-template-id*. A *deduction-guide* shall be declared in the
6317
- same scope as the corresponding class template and, for a member class
6318
- template, with the same access. Two deduction guide declarations in the
6319
- same translation unit for the same class template shall not have
6320
- equivalent *parameter-declaration-clause*s.
6321
-
6322
  <!-- Link reference definitions -->
6323
  [basic.def]: basic.md#basic.def
6324
  [basic.def.odr]: basic.md#basic.def.odr
6325
  [basic.link]: basic.md#basic.link
6326
  [basic.lookup]: basic.md#basic.lookup
@@ -6328,71 +7888,84 @@ equivalent *parameter-declaration-clause*s.
6328
  [basic.lookup.classref]: basic.md#basic.lookup.classref
6329
  [basic.lookup.qual]: basic.md#basic.lookup.qual
6330
  [basic.lookup.unqual]: basic.md#basic.lookup.unqual
6331
  [basic.scope]: basic.md#basic.scope
6332
  [basic.scope.hiding]: basic.md#basic.scope.hiding
 
6333
  [basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
6334
  [basic.types]: basic.md#basic.types
6335
- [class]: class.md#class
6336
- [class.abstract]: class.md#class.abstract
6337
  [class.access]: class.md#class.access
6338
- [class.base.init]: special.md#class.base.init
 
 
 
6339
  [class.derived]: class.md#class.derived
6340
- [class.dtor]: special.md#class.dtor
6341
  [class.friend]: class.md#class.friend
6342
  [class.local]: class.md#class.local
6343
  [class.mem]: class.md#class.mem
6344
  [class.member.lookup]: class.md#class.member.lookup
 
6345
  [class.qual]: basic.md#class.qual
6346
- [class.temporary]: special.md#class.temporary
6347
- [conv]: conv.md#conv
6348
- [conv.array]: conv.md#conv.array
6349
- [conv.fctptr]: conv.md#conv.fctptr
6350
- [conv.func]: conv.md#conv.func
6351
- [conv.qual]: conv.md#conv.qual
 
6352
  [dcl.align]: dcl.md#dcl.align
6353
  [dcl.attr.grammar]: dcl.md#dcl.attr.grammar
6354
- [dcl.dcl]: dcl.md#dcl.dcl
6355
  [dcl.enum]: dcl.md#dcl.enum
6356
  [dcl.fct]: dcl.md#dcl.fct
6357
  [dcl.fct.def.general]: dcl.md#dcl.fct.def.general
6358
  [dcl.fct.default]: dcl.md#dcl.fct.default
6359
  [dcl.init]: dcl.md#dcl.init
6360
  [dcl.init.list]: dcl.md#dcl.init.list
6361
  [dcl.meaning]: dcl.md#dcl.meaning
 
6362
  [dcl.spec.auto]: dcl.md#dcl.spec.auto
 
6363
  [dcl.struct.bind]: dcl.md#dcl.struct.bind
6364
- [dcl.type.auto.deduct]: dcl.md#dcl.type.auto.deduct
6365
  [dcl.type.class.deduct]: dcl.md#dcl.type.class.deduct
6366
  [dcl.type.elab]: dcl.md#dcl.type.elab
 
6367
  [except.spec]: except.md#except.spec
6368
  [expr.const]: expr.md#expr.const
 
 
 
6369
  [expr.new]: expr.md#expr.new
6370
  [expr.prim.fold]: expr.md#expr.prim.fold
6371
- [expr.prim.lambda]: expr.md#expr.prim.lambda
 
 
6372
  [expr.prim.lambda.closure]: expr.md#expr.prim.lambda.closure
6373
  [expr.ref]: expr.md#expr.ref
6374
  [expr.sizeof]: expr.md#expr.sizeof
6375
  [expr.typeid]: expr.md#expr.typeid
 
6376
  [implimits]: limits.md#implimits
6377
  [intro.defs]: intro.md#intro.defs
6378
- [intro.object]: intro.md#intro.object
6379
  [lex.string]: lex.md#lex.string
6380
  [namespace.def]: dcl.md#namespace.def
6381
  [namespace.memdef]: dcl.md#namespace.memdef
6382
  [namespace.udecl]: dcl.md#namespace.udecl
6383
  [over.ics.rank]: over.md#over.ics.rank
6384
  [over.match]: over.md#over.match
6385
  [over.match.best]: over.md#over.match.best
6386
  [over.match.class.deduct]: over.md#over.match.class.deduct
6387
  [over.match.conv]: over.md#over.match.conv
 
6388
  [over.match.ref]: over.md#over.match.ref
 
6389
  [over.over]: over.md#over.over
6390
- [special]: special.md#special
6391
  [stmt.if]: stmt.md#stmt.if
6392
- [support.types]: language.md#support.types
6393
- [tab:fold.empty]: #tab:fold.empty
6394
  [temp]: #temp
6395
  [temp.alias]: #temp.alias
6396
  [temp.arg]: #temp.arg
6397
  [temp.arg.explicit]: #temp.arg.explicit
6398
  [temp.arg.nontype]: #temp.arg.nontype
@@ -6401,10 +7974,18 @@ equivalent *parameter-declaration-clause*s.
6401
  [temp.class]: #temp.class
6402
  [temp.class.order]: #temp.class.order
6403
  [temp.class.spec]: #temp.class.spec
6404
  [temp.class.spec.match]: #temp.class.spec.match
6405
  [temp.class.spec.mfunc]: #temp.class.spec.mfunc
 
 
 
 
 
 
 
 
6406
  [temp.decls]: #temp.decls
6407
  [temp.deduct]: #temp.deduct
6408
  [temp.deduct.call]: #temp.deduct.call
6409
  [temp.deduct.conv]: #temp.deduct.conv
6410
  [temp.deduct.decl]: #temp.deduct.decl
@@ -6421,10 +8002,11 @@ equivalent *parameter-declaration-clause*s.
6421
  [temp.dep.type]: #temp.dep.type
6422
  [temp.expl.spec]: #temp.expl.spec
6423
  [temp.explicit]: #temp.explicit
6424
  [temp.fct]: #temp.fct
6425
  [temp.fct.spec]: #temp.fct.spec
 
6426
  [temp.friend]: #temp.friend
6427
  [temp.func.order]: #temp.func.order
6428
  [temp.inject]: #temp.inject
6429
  [temp.inst]: #temp.inst
6430
  [temp.local]: #temp.local
@@ -6436,10 +8018,11 @@ equivalent *parameter-declaration-clause*s.
6436
  [temp.nondep]: #temp.nondep
6437
  [temp.over]: #temp.over
6438
  [temp.over.link]: #temp.over.link
6439
  [temp.param]: #temp.param
6440
  [temp.point]: #temp.point
 
6441
  [temp.res]: #temp.res
6442
  [temp.spec]: #temp.spec
6443
  [temp.static]: #temp.static
6444
  [temp.type]: #temp.type
6445
  [temp.variadic]: #temp.variadic
@@ -6456,31 +8039,53 @@ equivalent *parameter-declaration-clause*s.
6456
 
6457
  [^3]: There is no such ambiguity in a default *template-argument*
6458
  because the form of the *template-parameter* determines the
6459
  allowable forms of the *template-argument*.
6460
 
6461
- [^4]: There is no way in which they could be used.
 
 
6462
 
6463
- [^5]: That is, declarations of non-template functions do not merely
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6464
  guide overload resolution of function template specializations with
6465
- the same name. If such a non-template function is odr-used (
6466
- [[basic.def.odr]]) in a program, it must be defined; it will not be
6467
  implicitly instantiated using the function template definition.
6468
 
6469
- [^6]: Friend declarations do not introduce new names into any scope,
 
 
6470
  either when the template is declared or when it is instantiated.
6471
 
6472
- [^7]: Default arguments are not considered to be arguments in this
6473
  context; they only become arguments after a function has been
6474
  selected.
6475
 
6476
- [^8]: Although the *template-argument* corresponding to a
6477
  *template-parameter* of type `bool` may be deduced from an array
6478
  bound, the resulting value will always be `true` because the array
6479
  bound will be nonzero.
6480
 
6481
- [^9]: The parameters of function template specializations contain no
6482
  template parameter types. The set of conversions allowed on deduced
6483
  arguments is limited, because the argument deduction process
6484
  produces function templates with parameters that either match the
6485
  call arguments exactly or differ only in ways that can be bridged by
6486
  the allowed limited conversions. Non-deduced arguments allow the
 
1
  # Templates <a id="temp">[[temp]]</a>
2
 
3
+ ## Preamble <a id="temp.pre">[[temp.pre]]</a>
4
+
5
+ A *template* defines a family of classes, functions, or variables, an
6
+ alias for a family of types, or a concept.
7
 
8
  ``` bnf
9
  template-declaration:
10
+ template-head declaration
11
+ template-head concept-definition
12
+ ```
13
+
14
+ ``` bnf
15
+ template-head:
16
+ template '<' template-parameter-list '>' requires-clauseₒₚₜ
17
  ```
18
 
19
  ``` bnf
20
  template-parameter-list:
21
  template-parameter
22
  template-parameter-list ',' template-parameter
23
  ```
24
 
25
+ ``` bnf
26
+ requires-clause:
27
+ requires constraint-logical-or-expression
28
+ ```
29
+
30
+ ``` bnf
31
+ constraint-logical-or-expression:
32
+ constraint-logical-and-expression
33
+ constraint-logical-or-expression '||' constraint-logical-and-expression
34
+ ```
35
+
36
+ ``` bnf
37
+ constraint-logical-and-expression:
38
+ primary-expression
39
+ constraint-logical-and-expression '&&' primary-expression
40
+ ```
41
+
42
  [*Note 1*: The `>` token following the *template-parameter-list* of a
43
  *template-declaration* may be the product of replacing a `>{>}` token by
44
+ two consecutive `>` tokens [[temp.names]]. — *end note*]
45
 
46
+ The *declaration* in a *template-declaration* (if any) shall
47
 
48
  - declare or define a function, a class, or a variable, or
49
  - define a member function, a member class, a member enumeration, or a
50
  static data member of a class template or of a class nested within a
51
  class template, or
52
  - define a member template of a class or class template, or
53
  - be a *deduction-guide*, or
54
  - be an *alias-declaration*.
55
 
56
+ A *template-declaration* is a *declaration*. A declaration introduced by
57
+ a template declaration of a variable is a *variable template*. A
58
+ variable template at class scope is a *static data member template*.
 
 
59
 
60
  [*Example 1*:
61
 
62
  ``` cpp
63
  template<class T>
 
68
  }
69
  struct matrix_constants {
70
  template<class T>
71
  using pauli = hermitian_matrix<T, 2>;
72
  template<class T>
73
+ constexpr static pauli<T> sigma1 = { { 0, 1 }, { 1, 0 } };
74
  template<class T>
75
+ constexpr static pauli<T> sigma2 = { { 0, -1i }, { 1i, 0 } };
76
  template<class T>
77
+ constexpr static pauli<T> sigma3 = { { 1, 0 }, { 0, -1 } };
78
  };
79
  ```
80
 
81
  — *end example*]
82
 
83
  A *template-declaration* can appear only as a namespace scope or class
84
+ scope declaration. Its *declaration* shall not be an
85
+ *export-declaration*. In a function template declaration, the last
86
  component of the *declarator-id* shall not be a *template-id*.
87
 
88
  [*Note 2*: That last component may be an *identifier*, an
89
  *operator-function-id*, a *conversion-function-id*, or a
90
  *literal-operator-id*. In a class template declaration, if the class
91
  name is a *simple-template-id*, the declaration declares a class
92
+ template partial specialization [[temp.class.spec]]. — *end note*]
93
 
94
  In a *template-declaration*, explicit specialization, or explicit
95
  instantiation the *init-declarator-list* in the declaration shall
96
  contain at most one declarator. When such a declaration is used to
97
  declare a class template, no declarator is permitted.
98
 
99
+ A template name has linkage [[basic.link]]. Specializations (explicit or
100
+ implicit) of a template that has internal linkage are distinct from all
101
+ specializations in other translation units. A template, a template
102
+ explicit specialization [[temp.expl.spec]], and a class template partial
103
+ specialization shall not have C linkage. Use of a linkage specification
104
+ other than `"C"` or `"C++"` with any of these constructs is
105
+ conditionally-supported, with *implementation-defined* semantics.
106
+ Template definitions shall obey the one-definition rule
107
+ [[basic.def.odr]].
108
 
109
  [*Note 3*: Default arguments for function templates and for member
110
  functions of class templates are considered definitions for the purpose
111
+ of template instantiation [[temp.decls]] and must also obey the
112
  one-definition rule. — *end note*]
113
 
114
  A class template shall not have the same name as any other template,
115
  class, function, variable, enumeration, enumerator, namespace, or type
116
+ in the same scope [[basic.scope]], except as specified in 
117
  [[temp.class.spec]]. Except that a function template can be overloaded
118
+ either by non-template functions [[dcl.fct]] with the same name or by
119
+ other function templates with the same name [[temp.over]], a template
120
  name declared in namespace scope or in class scope shall be unique in
121
  that scope.
122
 
123
+ An entity is *templated* if it is
124
 
125
  - a template,
126
+ - an entity defined [[basic.def]] or created [[class.temporary]] in a
127
+ templated entity,
128
  - a member of a templated entity,
129
  - an enumerator for an enumeration that is a templated entity, or
130
+ - the closure type of a *lambda-expression* [[expr.prim.lambda.closure]]
131
+ appearing in the declaration of a templated entity.
 
132
 
133
  [*Note 4*: A local class, a local variable, or a friend function
134
  defined in a templated entity is a templated entity. — *end note*]
135
 
136
+ A *template-declaration* is written in terms of its template parameters.
137
+ The optional *requires-clause* following a *template-parameter-list*
138
+ allows the specification of constraints [[temp.constr.decl]] on template
139
+ arguments [[temp.arg]]. The *requires-clause* introduces the
140
+ *constraint-expression* that results from interpreting the
141
+ *constraint-logical-or-expression* as a *constraint-expression*. The
142
+ *constraint-logical-or-expression* of a *requires-clause* is an
143
+ unevaluated operand [[expr.context]].
144
+
145
+ [*Note 5*:
146
+
147
+ The expression in a *requires-clause* uses a restricted grammar to avoid
148
+ ambiguities. Parentheses can be used to specify arbitrary expressions in
149
+ a *requires-clause*.
150
+
151
+ [*Example 2*:
152
+
153
+ ``` cpp
154
+ template<int N> requires N == sizeof new unsigned short
155
+ int f(); // error: parentheses required around == expression
156
+ ```
157
+
158
+ — *end example*]
159
+
160
+ — *end note*]
161
+
162
+ A definition of a function template, member function of a class
163
+ template, variable template, or static data member of a class template
164
+ shall be reachable from the end of every definition domain
165
+ [[basic.def.odr]] in which it is implicitly instantiated [[temp.inst]]
166
+ unless the corresponding specialization is explicitly instantiated
167
+ [[temp.explicit]] in some translation unit; no diagnostic is required.
168
 
169
  ## Template parameters <a id="temp.param">[[temp.param]]</a>
170
 
171
  The syntax for *template-parameter*s is:
172
 
 
178
 
179
  ``` bnf
180
  type-parameter:
181
  type-parameter-key '...'ₒₚₜ identifierₒₚₜ
182
  type-parameter-key identifierₒₚₜ '=' type-id
183
+ type-constraint '...'ₒₚₜ identifierₒₚₜ
184
+ type-constraint identifierₒₚₜ '=' type-id
185
+ template-head type-parameter-key '...'ₒₚₜ identifierₒₚₜ
186
+ template-head type-parameter-key identifierₒₚₜ '=' id-expression
187
  ```
188
 
189
  ``` bnf
190
  type-parameter-key:
191
+ class
192
+ typename
193
+ ```
194
+
195
+ ``` bnf
196
+ type-constraint:
197
+ nested-name-specifierₒₚₜ concept-name
198
+ nested-name-specifierₒₚₜ concept-name '<' template-argument-listₒₚₜ '>'
199
  ```
200
 
201
  [*Note 1*: The `>` token following the *template-parameter-list* of a
202
  *type-parameter* may be the product of replacing a `>{>}` token by two
203
+ consecutive `>` tokens [[temp.names]]. — *end note*]
204
 
205
  There is no semantic difference between `class` and `typename` in a
206
  *type-parameter-key*. `typename` followed by an *unqualified-id* names a
207
  template type parameter. `typename` followed by a *qualified-id* denotes
208
  the type in a non-type [^1] *parameter-declaration*. A
 
250
  };
251
  ```
252
 
253
  — *end note*]
254
 
255
+ A *type-constraint* `Q` that designates a concept `C` can be used to
256
+ constrain a contextually-determined type or template type parameter pack
257
+ `T` with a *constraint-expression* `E` defined as follows. If `Q` is of
258
+ the form `C<A₁, ⋯, Aₙ>`, then let `E'` be `C<T, A₁, ⋯, Aₙ>`. Otherwise,
259
+ let `E'` be `C<T>`. If `T` is not a pack, then `E` is `E'`, otherwise
260
+ `E` is `(E' && ...)`. This *constraint-expression* `E` is called the
261
+ *immediately-declared constraint* of `Q` for `T`. The concept designated
262
+ by a *type-constraint* shall be a type concept [[temp.concept]].
263
+
264
+ A *type-parameter* that starts with a *type-constraint* introduces the
265
+ immediately-declared constraint of the *type-constraint* for the
266
+ parameter.
267
+
268
+ [*Example 2*:
269
+
270
+ ``` cpp
271
+ template<typename T> concept C1 = true;
272
+ template<typename... Ts> concept C2 = true;
273
+ template<typename T, typename U> concept C3 = true;
274
+
275
+ template<C1 T> struct s1; // associates C1<T>
276
+ template<C1... T> struct s2; // associates (C1<T> && ...)
277
+ template<C2... T> struct s3; // associates (C2<T> && ...)
278
+ template<C3<int> T> struct s4; // associates C3<T, int>
279
+ template<C3<int>... T> struct s5; // associates (C3<T, int> && ...)
280
+ ```
281
+
282
+ — *end example*]
283
+
284
  A non-type *template-parameter* shall have one of the following
285
+ (possibly cv-qualified) types:
286
 
287
+ - a structural type (see below),
288
+ - a type that contains a placeholder type [[dcl.spec.auto]], or
289
+ - a placeholder for a deduced class type [[dcl.type.class.deduct]].
 
 
 
 
 
 
 
290
 
291
  The top-level *cv-qualifier*s on the *template-parameter* are ignored
292
  when determining its type.
293
 
294
+ A *structural type* is one of the following:
 
 
 
 
295
 
296
+ - a scalar type, or
297
+ - an lvalue reference type, or
298
+ - a literal class type with the following properties:
299
+ - all base classes and non-static data members are public and
300
+ non-mutable and
301
+ - the types of all bases classes and non-static data members are
302
+ structural types or (possibly multi-dimensional) array thereof.
303
+
304
+ An *id-expression* naming a non-type *template-parameter* of class type
305
+ `T` denotes a static storage duration object of type `const T`, known as
306
+ a *template parameter object*, whose value is that of the corresponding
307
+ template argument after it has been converted to the type of the
308
+ *template-parameter*. All such template parameters in the program of the
309
+ same type with the same value denote the same template parameter object.
310
+ A template parameter object shall have constant destruction
311
+ [[expr.const]].
312
+
313
+ [*Note 3*: If an *id-expression* names a non-type non-reference
314
+ *template-parameter*, then it is a prvalue if it has non-class type.
315
+ Otherwise, if it is of class type `T`, it is an lvalue and has type
316
+ `const T` [[expr.prim.id.unqual]]. — *end note*]
317
+
318
+ [*Example 3*:
319
 
320
  ``` cpp
321
+ using X = int;
322
+ struct A {};
323
+ template<const X& x, int i, A a> void f() {
324
  i++; // error: change of template-parameter value
325
 
326
  &x; // OK
327
  &i; // error: address of non-reference template-parameter
328
+ &a; // OK
329
  int& ri = i; // error: non-const reference bound to temporary
330
  const int& cri = i; // OK: const reference bound to temporary
331
+ const A& ra = a; // OK: const reference bound to a template parameter object
332
  }
333
  ```
334
 
335
  — *end example*]
336
 
337
+ [*Note 4*:
 
338
 
339
+ A non-type *template-parameter* cannot be declared to have type cv
340
+ `void`.
341
+
342
+ [*Example 4*:
343
 
344
  ``` cpp
345
+ template<void v> class X; // error
346
+ template<void* pv> class Y; // OK
 
347
  ```
348
 
349
  — *end example*]
350
 
351
+ — *end note*]
352
+
353
  A non-type *template-parameter* of type “array of `T`” or of function
354
  type `T` is adjusted to be of type “pointer to `T`”.
355
 
356
+ [*Example 5*:
357
 
358
  ``` cpp
359
  template<int* a> struct R { ... };
360
  template<int b[5]> struct S { ... };
361
  int p;
 
366
  S<v> z; // OK due to both adjustment and conversion
367
  ```
368
 
369
  — *end example*]
370
 
371
+ A non-type template parameter declared with a type that contains a
372
+ placeholder type with a *type-constraint* introduces the
373
+ immediately-declared constraint of the *type-constraint* for the
374
+ invented type corresponding to the placeholder [[dcl.fct]].
375
+
376
+ A *default template-argument* is a *template-argument* [[temp.arg]]
377
  specified after `=` in a *template-parameter*. A default
378
  *template-argument* may be specified for any kind of
379
  *template-parameter* (type, non-type, template) that is not a template
380
+ parameter pack [[temp.variadic]]. A default *template-argument* may be
381
+ specified in a template declaration. A default *template-argument* shall
382
+ not be specified in the *template-parameter-list*s of the definition of
383
+ a member of a class template that appears outside of the member’s class.
384
+ A default *template-argument* shall not be specified in a friend class
385
+ template declaration. If a friend function template declaration
386
+ specifies a default *template-argument*, that declaration shall be a
387
+ definition and shall be the only declaration of the function template in
388
+ the translation unit.
389
 
390
  The set of default *template-argument*s available for use is obtained by
391
  merging the default arguments from all prior declarations of the
392
+ template in the same way default function arguments are
393
+ [[dcl.fct.default]].
394
 
395
+ [*Example 6*:
396
 
397
  ``` cpp
398
  template<class T1, class T2 = int> class A;
399
  template<class T1 = int, class T2> class A;
400
  ```
 
413
  supplied or be a template parameter pack. If a *template-parameter* of a
414
  primary class template, primary variable template, or alias template is
415
  a template parameter pack, it shall be the last *template-parameter*. A
416
  template parameter pack of a function template shall not be followed by
417
  another template parameter unless that template parameter can be deduced
418
+ from the parameter-type-list [[dcl.fct]] of the function template or has
419
+ a default argument [[temp.deduct]]. A template parameter of a deduction
420
+ guide template [[temp.deduct.guide]] that does not have a default
421
+ argument shall be deducible from the parameter-type-list of the
422
  deduction guide template.
423
 
424
+ [*Example 7*:
425
 
426
  ``` cpp
427
  template<class T1 = int, class T2> class B; // error
428
 
429
  // U can be neither deduced from the parameter-type-list nor specified
 
434
  — *end example*]
435
 
436
  A *template-parameter* shall not be given default arguments by two
437
  different declarations in the same scope.
438
 
439
+ [*Example 8*:
440
 
441
  ``` cpp
442
  template<class T = int> class X;
443
  template<class T = int> class X { ... }; // error
444
  ```
 
447
 
448
  When parsing a default *template-argument* for a non-type
449
  *template-parameter*, the first non-nested `>` is taken as the end of
450
  the *template-parameter-list* rather than a greater-than operator.
451
 
452
+ [*Example 9*:
453
 
454
  ``` cpp
455
  template<int i = 3 > 4 > // syntax error
456
  class X { ... };
457
 
 
464
  A *template-parameter* of a template *template-parameter* is permitted
465
  to have a default *template-argument*. When such default arguments are
466
  specified, they apply to the template *template-parameter* in the scope
467
  of the template *template-parameter*.
468
 
469
+ [*Example 10*:
470
 
471
  ``` cpp
 
472
  template <template <class TT = float> class T> struct A {
473
  inline void f();
474
  inline void g();
475
  };
476
  template <template <class TT> class T> void A<T>::f() {
477
+ T<> t; // error: TT has no default template argument
478
  }
479
  template <template <class TT = char> class T> void A<T>::g() {
480
+ T<> t; // OK, T<char>
481
  }
482
  ```
483
 
484
  — *end example*]
485
 
486
  If a *template-parameter* is a *type-parameter* with an ellipsis prior
487
  to its optional *identifier* or is a *parameter-declaration* that
488
+ declares a pack [[dcl.fct]], then the *template-parameter* is a template
489
+ parameter pack [[temp.variadic]]. A template parameter pack that is a
490
+ *parameter-declaration* whose type contains one or more unexpanded packs
491
+ is a pack expansion. Similarly, a template parameter pack that is a
492
+ *type-parameter* with a *template-parameter-list* containing one or more
493
+ unexpanded packs is a pack expansion. A type parameter pack with a
494
+ *type-constraint* that contains an unexpanded parameter pack is a pack
495
+ expansion. A template parameter pack that is a pack expansion shall not
496
+ expand a template parameter pack declared in the same
497
  *template-parameter-list*.
498
 
499
+ [*Example 11*:
500
 
501
  ``` cpp
502
+ template <class... Types> // Types is a template type parameter pack
503
+ class Tuple; // but not a pack expansion
504
+
505
+ template <class T, int... Dims> // Dims is a non-type template parameter pack
506
+ struct multi_array; // but not a pack expansion
507
+
508
+ template <class... T>
509
+ struct value_holder {
510
  template <T... Values> struct apply { }; // Values is a non-type template parameter pack
511
+ }; // and a pack expansion
512
+
513
+ template <class... T, T... Values> // error: Values expands template type parameter
514
+ struct static_array; // pack T within the same template parameter list
515
  ```
516
 
517
  — *end example*]
518
 
519
  ## Names of template specializations <a id="temp.names">[[temp.names]]</a>
520
 
521
+ A template specialization [[temp.spec]] can be referred to by a
522
  *template-id*:
523
 
524
  ``` bnf
525
  simple-template-id:
526
  template-name '<' template-argument-listₒₚₜ '>'
 
549
  constant-expression
550
  type-id
551
  id-expression
552
  ```
553
 
554
+ [*Note 1*: The name lookup rules [[basic.lookup]] are used to associate
555
+ the use of a name with a template declaration; that is, to identify a
556
+ name as a *template-name*. — *end note*]
557
 
558
  For a *template-name* to be explicitly qualified by the template
559
+ arguments, the name must be considered to refer to a template.
560
+
561
+ [*Note 2*: Whether a name actually refers to a template cannot be known
562
+ in some cases until after argument dependent lookup is done
563
+ [[basic.lookup.argdep]]. *end note*]
564
+
565
+ A name is considered to refer to a template if name lookup finds a
566
+ *template-name* or an overload set that contains a function template. A
567
+ name is also considered to refer to a template if it is an
568
+ *unqualified-id* followed by a `<` and name lookup either finds one or
569
+ more functions or finds nothing.
570
+
571
+ When a name is considered to be a *template-name*, and it is followed by
572
+ a `<`, the `<` is always taken as the delimiter of a
573
+ *template-argument-list* and never as the less-than operator. When
574
+ parsing a *template-argument-list*, the first non-nested `>`[^2] is
575
+ taken as the ending delimiter rather than a greater-than operator.
576
+ Similarly, the first non-nested `>{>}` is treated as two consecutive but
577
+ distinct `>` tokens, the first of which is taken as the end of the
578
+ *template-argument-list* and completes the *template-id*.
579
+
580
+ [*Note 3*: The second `>` token produced by this replacement rule may
581
  terminate an enclosing *template-id* construct or it may be part of a
582
+ different construct (e.g., a cast). — *end note*]
583
 
584
  [*Example 1*:
585
 
586
  ``` cpp
587
  template<int i> class X { ... };
 
598
  — *end example*]
599
 
600
  The keyword `template` is said to appear at the top level in a
601
  *qualified-id* if it appears outside of a *template-argument-list* or
602
  *decltype-specifier*. In a *qualified-id* of a *declarator-id* or in a
603
+ *qualified-id* formed by a *class-head-name* [[class.pre]] or
604
+ *enum-head-name* [[dcl.enum]], the keyword `template` shall not appear
605
+ at the top level. In a *qualified-id* used as the name in a
606
+ *typename-specifier* [[temp.res]], *elaborated-type-specifier*
607
+ [[dcl.type.elab]], *using-declaration* [[namespace.udecl]], or
608
+ *class-or-decltype* [[class.derived]], an optional keyword `template`
609
+ appearing at the top level is ignored. In these contexts, a `<` token is
610
+ always assumed to introduce a *template-argument-list*. In all other
611
+ contexts, when naming a template specialization of a member of an
612
+ unknown specialization [[temp.dep.type]], the member template name shall
613
+ be prefixed by the keyword `template`.
614
 
615
  [*Example 2*:
616
 
617
  ``` cpp
618
  struct X {
619
  template<std::size_t> X* alloc();
620
  template<std::size_t> static X* adjust();
621
  };
622
  template<class T> void f(T* p) {
623
+ T* p1 = p->alloc<200>(); // error: < means less than
624
  T* p2 = p->template alloc<200>(); // OK: < starts template argument list
625
+ T::adjust<100>(); // error: < means less than
626
  T::template adjust<100>(); // OK: < starts template argument list
627
  }
628
  ```
629
 
630
  — *end example*]
631
 
632
  A name prefixed by the keyword `template` shall be a *template-id* or
633
  the name shall refer to a class template or an alias template.
634
 
635
+ [*Note 4*: The keyword `template` may not be applied to non-template
636
  members of class templates. — *end note*]
637
 
638
+ [*Note 5*: As is the case with the `typename` prefix, the `template`
639
  prefix is allowed in cases where it is not strictly necessary; i.e.,
640
  when the *nested-name-specifier* or the expression on the left of the
641
  `->` or `.` is not dependent on a *template-parameter*, or the use does
642
  not appear in the scope of a template. — *end note*]
643
 
 
664
  D<B<int> > db;
665
  ```
666
 
667
  — *end example*]
668
 
669
+ A *template-id* is *valid* if
 
670
 
671
+ - there are at most as many arguments as there are parameters or a
672
+ parameter is a template parameter pack [[temp.variadic]],
673
+ - there is an argument for each non-deducible non-pack parameter that
674
+ does not have a default *template-argument*,
675
+ - each *template-argument* matches the corresponding
676
+ *template-parameter* [[temp.arg]],
677
+ - substitution of each template argument into the following template
678
+ parameters (if any) succeeds, and
679
+ - if the *template-id* is non-dependent, the associated constraints are
680
+ satisfied as specified in the next paragraph.
681
+
682
+ A *simple-template-id* shall be valid unless it names a function
683
+ template specialization [[temp.deduct]].
684
+
685
+ [*Example 4*:
686
+
687
+ ``` cpp
688
+ template<class T, T::type n = 0> class X;
689
+ struct S {
690
+ using type = int;
691
+ };
692
+ using T1 = X<S, int, int>; // error: too many arguments
693
+ using T2 = X<>; // error: no default argument for first template parameter
694
+ using T3 = X<1>; // error: value 1 does not match type-parameter
695
+ using T4 = X<int>; // error: substitution failure for second template parameter
696
+ using T5 = X<S>; // OK
697
+ ```
698
+
699
+ — *end example*]
700
+
701
+ When the *template-name* of a *simple-template-id* names a constrained
702
+ non-function template or a constrained template *template-parameter*,
703
+ but not a member template that is a member of an unknown specialization
704
+ [[temp.res]], and all *template-argument*s in the *simple-template-id*
705
+ are non-dependent [[temp.dep.temp]], the associated constraints
706
+ [[temp.constr.decl]] of the constrained template shall be satisfied
707
+ [[temp.constr.constr]].
708
+
709
+ [*Example 5*:
710
+
711
+ ``` cpp
712
+ template<typename T> concept C1 = sizeof(T) != sizeof(int);
713
+
714
+ template<C1 T> struct S1 { };
715
+ template<C1 T> using Ptr = T*;
716
+
717
+ S1<int>* p; // error: constraints not satisfied
718
+ Ptr<int> p; // error: constraints not satisfied
719
+
720
+ template<typename T>
721
+ struct S2 { Ptr<int> x; }; // ill-formed, no diagnostic required
722
+
723
+ template<typename T>
724
+ struct S3 { Ptr<T> x; }; // OK, satisfaction is not required
725
+
726
+ S3<int> x; // error: constraints not satisfied
727
+
728
+ template<template<C1 T> class X>
729
+ struct S4 {
730
+ X<int> x; // ill-formed, no diagnostic required
731
+ };
732
+
733
+ template<typename T> concept C2 = sizeof(T) == 1;
734
+
735
+ template<C2 T> struct S { };
736
+
737
+ template struct S<char[2]>; // error: constraints not satisfied
738
+ template<> struct S<char[2]> { }; // error: constraints not satisfied
739
+ ```
740
+
741
+ — *end example*]
742
+
743
+ A *concept-id* is a *simple-template-id* where the *template-name* is a
744
+ *concept-name*. A concept-id is a prvalue of type `bool`, and does not
745
+ name a template specialization. A concept-id evaluates to `true` if the
746
+ concept’s normalized *constraint-expression* [[temp.constr.decl]] is
747
+ satisfied [[temp.constr.constr]] by the specified template arguments and
748
+ `false` otherwise.
749
+
750
+ [*Note 6*: Since a *constraint-expression* is an unevaluated operand, a
751
+ concept-id appearing in a *constraint-expression* is not evaluated
752
+ except as necessary to determine whether the normalized constraints are
753
+ satisfied. — *end note*]
754
+
755
+ [*Example 6*:
756
+
757
+ ``` cpp
758
+ template<typename T> concept C = true;
759
+ static_assert(C<int>); // OK
760
+ ```
761
+
762
+ — *end example*]
763
 
764
  ## Template arguments <a id="temp.arg">[[temp.arg]]</a>
765
 
766
  There are three forms of *template-argument*, corresponding to the three
767
  forms of *template-parameter*: type, non-type and template. The type and
768
  form of each *template-argument* specified in a *template-id* shall
769
  match the type and form specified for the corresponding parameter
770
  declared by the template in its *template-parameter-list*. When the
771
+ parameter declared by the template is a template parameter pack
772
+ [[temp.variadic]], it will correspond to zero or more
773
  *template-argument*s.
774
 
775
  [*Example 1*:
776
 
777
  ``` cpp
 
856
  template <class U> class B {
857
  private:
858
  struct S { ... };
859
  };
860
 
861
+ A<B> b; // error: A has no access to B::S
862
  ```
863
 
864
  — *end example*]
865
 
866
  When template argument packs or default *template-argument*s are used, a
 
878
  Tuple* u; // syntax error
879
  ```
880
 
881
  — *end example*]
882
 
883
+ An explicit destructor call [[class.dtor]] for an object that has a type
884
+ that is a class template specialization may explicitly specify the
885
  *template-argument*s.
886
 
887
  [*Example 6*:
888
 
889
  ``` cpp
 
900
 
901
  If the use of a *template-argument* gives rise to an ill-formed
902
  construct in the instantiation of a template specialization, the program
903
  is ill-formed.
904
 
905
+ When name lookup for the name in a *template-id* finds an overload set,
906
  both non-template functions in the overload set and function templates
907
  in the overload set for which the *template-argument*s do not match the
908
  *template-parameter*s are ignored. If none of the function templates
909
  have matching *template-parameter*s, the program is ill-formed.
910
 
911
  When a *simple-template-id* does not name a function, a default
912
+ *template-argument* is implicitly instantiated [[temp.inst]] when the
913
  value of that default argument is needed.
914
 
915
  [*Example 7*:
916
 
917
  ``` cpp
 
922
  The default argument for `U` is instantiated to form the type
923
  `S<bool, int>*`.
924
 
925
  — *end example*]
926
 
927
+ A *template-argument* followed by an ellipsis is a pack expansion
928
+ [[temp.variadic]].
929
 
930
  ### Template type arguments <a id="temp.arg.type">[[temp.arg.type]]</a>
931
 
932
  A *template-argument* for a *template-parameter* which is a type shall
933
  be a *type-id*.
 
953
  }
954
  ```
955
 
956
  — *end example*]
957
 
958
+ [*Note 1*: A template type argument may be an incomplete type
959
+ [[basic.types]]. — *end note*]
960
 
961
  ### Template non-type arguments <a id="temp.arg.nontype">[[temp.arg.nontype]]</a>
962
 
963
+ If the type `T` of a *template-parameter* [[temp.param]] contains a
964
+ placeholder type [[dcl.spec.auto]] or a placeholder for a deduced class
965
+ type [[dcl.type.class.deduct]], the type of the parameter is the type
966
+ deduced for the variable `x` in the invented declaration
967
+
968
+ ``` cpp
969
+ T x = template-argument ;
970
+ ```
971
+
972
+ If a deduced parameter type is not permitted for a *template-parameter*
973
+ declaration [[temp.param]], the program is ill-formed.
974
 
975
  A *template-argument* for a non-type *template-parameter* shall be a
976
+ converted constant expression [[expr.const]] of the type of the
977
+ *template-parameter*.
 
 
978
 
979
+ [*Note 1*: If the *template-argument* is an overload set (or the
980
+ address of such, including forming a pointer-to-member), the matching
981
+ function is selected from the set [[over.over]]. — *end note*]
 
 
982
 
983
+ For a non-type *template-parameter* of reference or pointer type, or for
984
+ each non-static data member of reference or pointer type in a non-type
985
+ *template-parameter* of class type or subobject thereof, the reference
986
+ or pointer value shall not refer to or be the address of (respectively):
987
+
988
+ - a temporary object [[class.temporary]],
989
+ - a string literal object [[lex.string]],
990
+ - the result of a `typeid` expression [[expr.typeid]],
991
+ - a predefined `__func__` variable [[dcl.fct.def.general]], or
992
+ - a subobject [[intro.object]] of one of the above.
993
 
994
  [*Example 1*:
995
 
996
  ``` cpp
997
  template<const int* pci> struct X { ... };
 
1013
  template<void (*pf)(int)> struct A { ... };
1014
 
1015
  A<&f> a; // selects f(int)
1016
 
1017
  template<auto n> struct B { ... };
1018
+ B<5> b1; // OK, template parameter type is int
1019
+ B<'a'> b2; // OK, template parameter type is char
1020
+ B<2.5> b3; // OK, template parameter type is double
1021
+ B<void(0)> b4; // error: template parameter type cannot be void
1022
  ```
1023
 
1024
  — *end example*]
1025
 
1026
  [*Note 2*:
1027
 
1028
+ A *string-literal* [[lex.string]] is not an acceptable
1029
+ *template-argument* for a *template-parameter* of non-class type.
1030
 
1031
  [*Example 2*:
1032
 
1033
  ``` cpp
1034
+ template<class T, T p> class X {
1035
  ...
1036
  };
1037
 
1038
+ X<const char*, "Studebaker"> x; // error: string literal object as template-argument
1039
+ X<const char*, "Knope" + 1> x2; // error: subobject of string literal object as template-argument
1040
 
1041
  const char p[] = "Vivisectionist";
1042
+ X<const char*, p> y; // OK
1043
+
1044
+ struct A {
1045
+ constexpr A(const char*) {}
1046
+ };
1047
+
1048
+ X<A, "Pyrophoricity"> z; // OK, string-literal is a constructor argument to A
1049
  ```
1050
 
1051
  — *end example*]
1052
 
1053
  — *end note*]
1054
 
1055
  [*Note 3*:
1056
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1057
  A temporary object is not an acceptable *template-argument* when the
1058
  corresponding *template-parameter* has reference type.
1059
 
1060
+ [*Example 3*:
1061
 
1062
  ``` cpp
1063
  template<const int& CRI> struct B { ... };
1064
 
1065
+ B<1> b1; // error: temporary would be required for template argument
1066
 
1067
  int c = 1;
1068
+ B<c> b2; // OK
1069
+
1070
+ struct X { int n; };
1071
+ struct Y { const int &r; };
1072
+ template<Y y> struct C { ... };
1073
+ C<Y{X{1}.n}> c; // error: subobject of temporary object used to initialize
1074
+ // reference member of template parameter
1075
  ```
1076
 
1077
  — *end example*]
1078
 
1079
  — *end note*]
 
1086
  only primary class templates are considered when matching the template
1087
  template argument with the corresponding parameter; partial
1088
  specializations are not considered even if their parameter lists match
1089
  that of the template template parameter.
1090
 
1091
+ Any partial specializations [[temp.class.spec]] associated with the
1092
  primary class template or primary variable template are considered when
1093
  a specialization based on the template *template-parameter* is
1094
  instantiated. If a specialization is not visible at the point of
1095
  instantiation, and it would have been selected had it been visible, the
1096
  program is ill-formed, no diagnostic required.
 
1113
  ```
1114
 
1115
  — *end example*]
1116
 
1117
  A *template-argument* matches a template *template-parameter* `P` when
1118
+ `P` is at least as specialized as the *template-argument* `A`. In this
1119
+ comparison, if `P` is unconstrained, the constraints on `A` are not
1120
+ considered. If `P` contains a template parameter pack, then `A` also
1121
+ matches `P` if each of `A`’s template parameters matches the
1122
+ corresponding template parameter in the *template-head* of `P`. Two
1123
+ template parameters match if they are of the same kind (type, non-type,
1124
+ template), for non-type *template-parameter*s, their types are
1125
+ equivalent [[temp.over.link]], and for template *template-parameter*s,
1126
+ each of their corresponding *template-parameter*s matches, recursively.
1127
+ When `P`’s *template-head* contains a template parameter pack
1128
+ [[temp.variadic]], the template parameter pack will match zero or more
1129
+ template parameters or template parameter packs in the *template-head*
1130
+ of `A` with the same type and form as the template parameter pack in `P`
1131
+ (ignoring whether those template parameters are template parameter
1132
+ packs).
1133
 
1134
  [*Example 2*:
1135
 
1136
  ``` cpp
1137
  template<class T> class A { ... };
 
1174
  eval<E<int, float>> eE; // error: E does not match TT in partial specialization
1175
  ```
1176
 
1177
  — *end example*]
1178
 
1179
+ [*Example 4*:
1180
+
1181
+ ``` cpp
1182
+ template<typename T> concept C = requires (T t) { t.f(); };
1183
+ template<typename T> concept D = C<T> && requires (T t) { t.g(); };
1184
+
1185
+ template<template<C> class P> struct S { };
1186
+
1187
+ template<C> struct X { };
1188
+ template<D> struct Y { };
1189
+ template<typename T> struct Z { };
1190
+
1191
+ S<X> s1; // OK, X and P have equivalent constraints
1192
+ S<Y> s2; // error: P is not at least as specialized as Y
1193
+ S<Z> s3; // OK, P is at least as specialized as Z
1194
+ ```
1195
+
1196
+ — *end example*]
1197
+
1198
  A template *template-parameter* `P` is at least as specialized as a
1199
  template *template-argument* `A` if, given the following rewrite to two
1200
  function templates, the function template corresponding to `P` is at
1201
  least as specialized as the function template corresponding to `A`
1202
+ according to the partial ordering rules for function templates
1203
+ [[temp.func.order]]. Given an invented class template `X` with the
1204
+ *template-head* of `A` (including default arguments and
1205
+ *requires-clause*, if any):
1206
 
1207
+ - Each of the two function templates has the same template parameters
1208
+ and *requires-clause* (if any), respectively, as `P` or `A`.
1209
  - Each function template has a single function parameter whose type is a
1210
  specialization of `X` with template arguments corresponding to the
1211
  template parameters from the respective function template where, for
1212
+ each template parameter `PP` in the *template-head* of the function
1213
+ template, a corresponding template argument `AA` is formed. If `PP`
1214
+ declares a template parameter pack, then `AA` is the pack expansion
1215
+ `PP...` [[temp.variadic]]; otherwise, `AA` is the *id-expression*
1216
  `PP`.
1217
 
1218
  If the rewrite produces an invalid type, then `P` is not at least as
1219
  specialized as `A`.
1220
 
1221
+ ## Template constraints <a id="temp.constr">[[temp.constr]]</a>
1222
+
1223
+ [*Note 1*: This subclause defines the meaning of constraints on
1224
+ template arguments. The abstract syntax and satisfaction rules are
1225
+ defined in [[temp.constr.constr]]. Constraints are associated with
1226
+ declarations in [[temp.constr.decl]]. Declarations are partially ordered
1227
+ by their associated constraints [[temp.constr.order]]. — *end note*]
1228
+
1229
+ ### Constraints <a id="temp.constr.constr">[[temp.constr.constr]]</a>
1230
+
1231
+ A *constraint* is a sequence of logical operations and operands that
1232
+ specifies requirements on template arguments. The operands of a logical
1233
+ operation are constraints. There are three different kinds of
1234
+ constraints:
1235
+
1236
+ - conjunctions [[temp.constr.op]],
1237
+ - disjunctions [[temp.constr.op]], and
1238
+ - atomic constraints [[temp.constr.atomic]].
1239
+
1240
+ In order for a constrained template to be instantiated [[temp.spec]],
1241
+ its associated constraints [[temp.constr.decl]] shall be satisfied as
1242
+ described in the following subclauses.
1243
+
1244
+ [*Note 1*: Forming the name of a specialization of a class template, a
1245
+ variable template, or an alias template [[temp.names]] requires the
1246
+ satisfaction of its constraints. Overload resolution
1247
+ [[over.match.viable]] requires the satisfaction of constraints on
1248
+ functions and function templates. — *end note*]
1249
+
1250
+ #### Logical operations <a id="temp.constr.op">[[temp.constr.op]]</a>
1251
+
1252
+ There are two binary logical operations on constraints: conjunction and
1253
+ disjunction.
1254
+
1255
+ [*Note 1*: These logical operations have no corresponding C++ syntax.
1256
+ For the purpose of exposition, conjunction is spelled using the symbol ∧
1257
+ and disjunction is spelled using the symbol ∨. The operands of these
1258
+ operations are called the left and right operands. In the constraint
1259
+ A ∧ B, A is the left operand, and B is the right operand. — *end note*]
1260
+
1261
+ A *conjunction* is a constraint taking two operands. To determine if a
1262
+ conjunction is *satisfied*, the satisfaction of the first operand is
1263
+ checked. If that is not satisfied, the conjunction is not satisfied.
1264
+ Otherwise, the conjunction is satisfied if and only if the second
1265
+ operand is satisfied.
1266
+
1267
+ A *disjunction* is a constraint taking two operands. To determine if a
1268
+ disjunction is *satisfied*, the satisfaction of the first operand is
1269
+ checked. If that is satisfied, the disjunction is satisfied. Otherwise,
1270
+ the disjunction is satisfied if and only if the second operand is
1271
+ satisfied.
1272
+
1273
+ [*Example 1*:
1274
+
1275
+ ``` cpp
1276
+ template<typename T>
1277
+ constexpr bool get_value() { return T::value; }
1278
+
1279
+ template<typename T>
1280
+ requires (sizeof(T) > 1) && (get_value<T>())
1281
+ void f(T); // has associated constraint sizeof(T) > 1 ∧ get_value<T>()
1282
+
1283
+ void f(int);
1284
+
1285
+ f('a'); // OK: calls f(int)
1286
+ ```
1287
+
1288
+ In the satisfaction of the associated constraints [[temp.constr.decl]]
1289
+ of `f`, the constraint `sizeof(char) > 1` is not satisfied; the second
1290
+ operand is not checked for satisfaction.
1291
+
1292
+ — *end example*]
1293
+
1294
+ [*Note 2*:
1295
+
1296
+ A logical negation expression [[expr.unary.op]] is an atomic constraint;
1297
+ the negation operator is not treated as a logical operation on
1298
+ constraints. As a result, distinct negation *constraint-expression*s
1299
+ that are equivalent under  [[temp.over.link]] do not subsume one another
1300
+ under  [[temp.constr.order]]. Furthermore, if substitution to determine
1301
+ whether an atomic constraint is satisfied [[temp.constr.atomic]]
1302
+ encounters a substitution failure, the constraint is not satisfied,
1303
+ regardless of the presence of a negation operator.
1304
+
1305
+ [*Example 2*:
1306
+
1307
+ ``` cpp
1308
+ template <class T> concept sad = false;
1309
+
1310
+ template <class T> int f1(T) requires (!sad<T>);
1311
+ template <class T> int f1(T) requires (!sad<T>) && true;
1312
+ int i1 = f1(42); // ambiguous, !sad<T> atomic constraint expressions[temp.constr.atomic]
1313
+ // are not formed from the same expression
1314
+
1315
+ template <class T> concept not_sad = !sad<T>;
1316
+ template <class T> int f2(T) requires not_sad<T>;
1317
+ template <class T> int f2(T) requires not_sad<T> && true;
1318
+ int i2 = f2(42); // OK, !sad<T> atomic constraint expressions both come from not_sad
1319
+
1320
+ template <class T> int f3(T) requires (!sad<typename T::type>);
1321
+ int i3 = f3(42); // error: associated constraints not satisfied due to substitution failure
1322
+
1323
+ template <class T> concept sad_nested_type = sad<typename T::type>;
1324
+ template <class T> int f4(T) requires (!sad_nested_type<T>);
1325
+ int i4 = f4(42); // OK, substitution failure contained within sad_nested_type
1326
+ ```
1327
+
1328
+ Here, `requires (!sad<typename T::type>)` requires that there is a
1329
+ nested `type` that is not `sad`, whereas
1330
+ `requires (!sad_nested_type<T>)` requires that there is no `sad` nested
1331
+ `type`.
1332
+
1333
+ — *end example*]
1334
+
1335
+ — *end note*]
1336
+
1337
+ #### Atomic constraints <a id="temp.constr.atomic">[[temp.constr.atomic]]</a>
1338
+
1339
+ An *atomic constraint* is formed from an expression `E` and a mapping
1340
+ from the template parameters that appear within `E` to template
1341
+ arguments that are formed via substitution during constraint
1342
+ normalization in the declaration of a constrained entity (and,
1343
+ therefore, can involve the unsubstituted template parameters of the
1344
+ constrained entity), called the *parameter mapping*
1345
+ [[temp.constr.decl]].
1346
+
1347
+ [*Note 1*: Atomic constraints are formed by constraint normalization
1348
+ [[temp.constr.normal]]. `E` is never a logical expression
1349
+ [[expr.log.and]] nor a logical expression
1350
+ [[expr.log.or]]. — *end note*]
1351
+
1352
+ Two atomic constraints, e₁ and e₂, are *identical* if they are formed
1353
+ from the same appearance of the same *expression* and if, given a
1354
+ hypothetical template A whose *template-parameter-list* consists of
1355
+ *template-parameter*s corresponding and equivalent [[temp.over.link]] to
1356
+ those mapped by the parameter mappings of the expression, a
1357
+ *template-id* naming A whose *template-argument*s are the targets of the
1358
+ parameter mapping of e₁ is the same [[temp.type]] as a *template-id*
1359
+ naming A whose *template-argument*s are the targets of the parameter
1360
+ mapping of e₂.
1361
+
1362
+ [*Note 2*:
1363
+
1364
+ The comparison of parameter mappings of atomic constraints operates in a
1365
+ manner similar to that of declaration matching with alias template
1366
+ substitution [[temp.alias]].
1367
+
1368
+ [*Example 1*:
1369
+
1370
+ ``` cpp
1371
+ template <unsigned N> constexpr bool Atomic = true;
1372
+ template <unsigned N> concept C = Atomic<N>;
1373
+ template <unsigned N> concept Add1 = C<N + 1>;
1374
+ template <unsigned N> concept AddOne = C<N + 1>;
1375
+ template <unsigned M> void f()
1376
+ requires Add1<2 * M>;
1377
+ template <unsigned M> int f()
1378
+ requires AddOne<2 * M> && true;
1379
+
1380
+ int x = f<0>(); // OK, the atomic constraints from concept C in both fs are Atomic<N>
1381
+ // with mapping similar to `N` ↦ `2 * M + 1`
1382
+
1383
+ template <unsigned N> struct WrapN;
1384
+ template <unsigned N> using Add1Ty = WrapN<N + 1>;
1385
+ template <unsigned N> using AddOneTy = WrapN<N + 1>;
1386
+ template <unsigned M> void g(Add1Ty<2 * M> *);
1387
+ template <unsigned M> void g(AddOneTy<2 * M> *);
1388
+
1389
+ void h() {
1390
+ g<0>(nullptr); // OK, there is only one g
1391
+ }
1392
+ ```
1393
+
1394
+ — *end example*]
1395
+
1396
+ This similarity includes the situation where a program is ill-formed, no
1397
+ diagnostic required, when the meaning of the program depends on whether
1398
+ two constructs are equivalent, and they are functionally equivalent but
1399
+ not equivalent.
1400
+
1401
+ [*Example 2*:
1402
+
1403
+ ``` cpp
1404
+ template <unsigned N> void f2()
1405
+ requires Add1<2 * N>;
1406
+ template <unsigned N> int f2()
1407
+ requires Add1<N * 2> && true;
1408
+ void h2() {
1409
+ f2<0>(); // ill-formed, no diagnostic required:
1410
+ // requires determination of subsumption between atomic constraints that are
1411
+ // functionally equivalent but not equivalent
1412
+ }
1413
+ ```
1414
+
1415
+ — *end example*]
1416
+
1417
+ — *end note*]
1418
+
1419
+ To determine if an atomic constraint is *satisfied*, the parameter
1420
+ mapping and template arguments are first substituted into its
1421
+ expression. If substitution results in an invalid type or expression,
1422
+ the constraint is not satisfied. Otherwise, the lvalue-to-rvalue
1423
+ conversion [[conv.lval]] is performed if necessary, and `E` shall be a
1424
+ constant expression of type `bool`. The constraint is satisfied if and
1425
+ only if evaluation of `E` results in `true`. If, at different points in
1426
+ the program, the satisfaction result is different for identical atomic
1427
+ constraints and template arguments, the program is ill-formed, no
1428
+ diagnostic required.
1429
+
1430
+ [*Example 3*:
1431
+
1432
+ ``` cpp
1433
+ template<typename T> concept C =
1434
+ sizeof(T) == 4 && !true; // requires atomic constraints sizeof(T) == 4 and !true
1435
+
1436
+ template<typename T> struct S {
1437
+ constexpr operator bool() const { return true; }
1438
+ };
1439
+
1440
+ template<typename T> requires (S<T>{})
1441
+ void f(T); // #1
1442
+ void f(int); // #2
1443
+
1444
+ void g() {
1445
+ f(0); // error: expression S<int>{} does not have type bool
1446
+ } // while checking satisfaction of deduced arguments of #1;
1447
+ // call is ill-formed even though #2 is a better match
1448
+ ```
1449
+
1450
+ — *end example*]
1451
+
1452
+ ### Constrained declarations <a id="temp.constr.decl">[[temp.constr.decl]]</a>
1453
+
1454
+ A template declaration [[temp.pre]] or templated function declaration
1455
+ [[dcl.fct]] can be constrained by the use of a *requires-clause*. This
1456
+ allows the specification of constraints for that declaration as an
1457
+ expression:
1458
+
1459
+ ``` bnf
1460
+ constraint-expression:
1461
+ logical-or-expression
1462
+ ```
1463
+
1464
+ Constraints can also be associated with a declaration through the use of
1465
+ *type-constraint*s in a *template-parameter-list* or
1466
+ parameter-type-list. Each of these forms introduces additional
1467
+ *constraint-expression*s that are used to constrain the declaration.
1468
+
1469
+ A declaration’s *associated constraints* are defined as follows:
1470
+
1471
+ - If there are no introduced *constraint-expression*s, the declaration
1472
+ has no associated constraints.
1473
+ - Otherwise, if there is a single introduced *constraint-expression*,
1474
+ the associated constraints are the normal form [[temp.constr.normal]]
1475
+ of that expression.
1476
+ - Otherwise, the associated constraints are the normal form of a logical
1477
+ expression [[expr.log.and]] whose operands are in the following order:
1478
+ - the *constraint-expression* introduced by each *type-constraint*
1479
+ [[temp.param]] in the declaration’s *template-parameter-list*, in
1480
+ order of appearance, and
1481
+ - the *constraint-expression* introduced by a *requires-clause*
1482
+ following a *template-parameter-list* [[temp.pre]], and
1483
+ - the *constraint-expression* introduced by each *type-constraint* in
1484
+ the parameter-type-list of a function declaration, and
1485
+ - the *constraint-expression* introduced by a trailing
1486
+ *requires-clause* [[dcl.decl]] of a function declaration
1487
+ [[dcl.fct]].
1488
+
1489
+ The formation of the associated constraints establishes the order in
1490
+ which constraints are instantiated when checking for satisfaction
1491
+ [[temp.constr.constr]].
1492
+
1493
+ [*Example 1*:
1494
+
1495
+ ``` cpp
1496
+ template<typename T> concept C = true;
1497
+
1498
+ template<C T> void f1(T);
1499
+ template<typename T> requires C<T> void f2(T);
1500
+ template<typename T> void f3(T) requires C<T>;
1501
+ ```
1502
+
1503
+ The functions `f1`, `f2`, and `f3` have the associated constraint
1504
+ `C<T>`.
1505
+
1506
+ ``` cpp
1507
+ template<typename T> concept C1 = true;
1508
+ template<typename T> concept C2 = sizeof(T) > 0;
1509
+
1510
+ template<C1 T> void f4(T) requires C2<T>;
1511
+ template<typename T> requires C1<T> && C2<T> void f5(T);
1512
+ ```
1513
+
1514
+ The associated constraints of `f4` and `f5` are `C1<T> ∧ C2<T>`.
1515
+
1516
+ ``` cpp
1517
+ template<C1 T> requires C2<T> void f6();
1518
+ template<C2 T> requires C1<T> void f7();
1519
+ ```
1520
+
1521
+ The associated constraints of `f6` are `C1<T> ∧ C2<T>`, and those of
1522
+ `f7` are `C2<T> ∧ C1<T>`.
1523
+
1524
+ — *end example*]
1525
+
1526
+ When determining whether a given introduced *constraint-expression* C₁
1527
+ of a declaration in an instantiated specialization of a templated class
1528
+ is equivalent [[temp.over.link]] to the corresponding
1529
+ *constraint-expression* C₂ of a declaration outside the class body, C₁
1530
+ is instantiated. If the instantiation results in an invalid expression,
1531
+ the *constraint-expression*s are not equivalent.
1532
+
1533
+ [*Note 1*: This can happen when determining which member template is
1534
+ specialized by an explicit specialization declaration. — *end note*]
1535
+
1536
+ [*Example 2*:
1537
+
1538
+ ``` cpp
1539
+ template <class T> concept C = true;
1540
+ template <class T> struct A {
1541
+ template <class U> U f(U) requires C<typename T::type>; // #1
1542
+ template <class U> U f(U) requires C<T>; // #2
1543
+ };
1544
+
1545
+ template <> template <class U>
1546
+ U A<int>::f(U u) requires C<int> { return u; } // OK, specializes #2
1547
+ ```
1548
+
1549
+ Substituting `int` for `T` in `C<typename T::type>` produces an invalid
1550
+ expression, so the specialization does not match \#1. Substituting `int`
1551
+ for `T` in `C<T>` produces `C<int>`, which is equivalent to the
1552
+ *constraint-expression* for the specialization, so it does match \#2.
1553
+
1554
+ — *end example*]
1555
+
1556
+ ### Constraint normalization <a id="temp.constr.normal">[[temp.constr.normal]]</a>
1557
+
1558
+ The *normal form* of an *expression* `E` is a constraint
1559
+ [[temp.constr.constr]] that is defined as follows:
1560
+
1561
+ - The normal form of an expression `( E )` is the normal form of `E`.
1562
+ - The normal form of an expression `E1 || E2` is the disjunction
1563
+ [[temp.constr.op]] of the normal forms of `E1` and `E2`.
1564
+ - The normal form of an expression `E1 && E2` is the conjunction of the
1565
+ normal forms of `E1` and `E2`.
1566
+ - The normal form of a concept-id `C<A₁, A₂, ..., Aₙ>` is the normal
1567
+ form of the *constraint-expression* of `C`, after substituting
1568
+ `A₁, A₂, ..., Aₙ` for `C`'s respective template parameters in the
1569
+ parameter mappings in each atomic constraint. If any such substitution
1570
+ results in an invalid type or expression, the program is ill-formed;
1571
+ no diagnostic is required.
1572
+ \[*Example 1*:
1573
+ ``` cpp
1574
+ template<typename T> concept A = T::value || true;
1575
+ template<typename U> concept B = A<U*>;
1576
+ template<typename V> concept C = B<V&>;
1577
+ ```
1578
+
1579
+ Normalization of `B`'s *constraint-expression* is valid and results in
1580
+ `T::value` (with the mapping `T` ↦ `U*`) ∨ `true` (with an empty
1581
+ mapping), despite the expression `T::value` being ill-formed for a
1582
+ pointer type `T`. Normalization of `C`'s *constraint-expression*
1583
+ results in the program being ill-formed, because it would form the
1584
+ invalid type `V&*` in the parameter mapping.
1585
+ — *end example*]
1586
+ - The normal form of any other expression `E` is the atomic constraint
1587
+ whose expression is `E` and whose parameter mapping is the identity
1588
+ mapping.
1589
+
1590
+ The process of obtaining the normal form of a *constraint-expression* is
1591
+ called *normalization*.
1592
+
1593
+ [*Note 1*: Normalization of *constraint-expression*s is performed when
1594
+ determining the associated constraints [[temp.constr.constr]] of a
1595
+ declaration and when evaluating the value of an *id-expression* that
1596
+ names a concept specialization [[expr.prim.id]]. — *end note*]
1597
+
1598
+ [*Example 2*:
1599
+
1600
+ ``` cpp
1601
+ template<typename T> concept C1 = sizeof(T) == 1;
1602
+ template<typename T> concept C2 = C1<T> && 1 == 2;
1603
+ template<typename T> concept C3 = requires { typename T::type; };
1604
+ template<typename T> concept C4 = requires (T x) { ++x; }
1605
+
1606
+ template<C2 U> void f1(U); // #1
1607
+ template<C3 U> void f2(U); // #2
1608
+ template<C4 U> void f3(U); // #3
1609
+ ```
1610
+
1611
+ The associated constraints of \#1 are `sizeof(T) == 1` (with mapping
1612
+ `T` ↦ `U`) ∧ `1 == 2`.
1613
+ The associated constraints of \#2 are `requires { typename T::type; }`
1614
+ (with mapping `T` ↦ `U`).
1615
+ The associated constraints of \#3 are `requires (T x) { ++x; }` (with
1616
+ mapping `T` ↦ `U`).
1617
+
1618
+ — *end example*]
1619
+
1620
+ ### Partial ordering by constraints <a id="temp.constr.order">[[temp.constr.order]]</a>
1621
+
1622
+ A constraint P *subsumes* a constraint Q if and only if, for every
1623
+ disjunctive clause Pᵢ in the disjunctive normal form[^4] of P, Pᵢ
1624
+ subsumes every conjunctive clause Qⱼ in the conjunctive normal form[^5]
1625
+ of Q, where
1626
+
1627
+ - a disjunctive clause Pᵢ subsumes a conjunctive clause Qⱼ if and only
1628
+ if there exists an atomic constraint Pᵢₐ in Pᵢ for which there exists
1629
+ an atomic constraint $Q_{jb}$ in Qⱼ such that Pᵢₐ subsumes $Q_{jb}$,
1630
+ and
1631
+ - an atomic constraint A subsumes another atomic constraint B if and
1632
+ only if A and B are identical using the rules described in
1633
+ [[temp.constr.atomic]].
1634
+
1635
+ [*Example 1*: Let A and B be atomic constraints [[temp.constr.atomic]].
1636
+ The constraint A ∧ B subsumes A, but A does not subsume A ∧ B. The
1637
+ constraint A subsumes A ∨ B, but A ∨ B does not subsume A. Also note
1638
+ that every constraint subsumes itself. — *end example*]
1639
+
1640
+ [*Note 1*:
1641
+
1642
+ The subsumption relation defines a partial ordering on constraints. This
1643
+ partial ordering is used to determine
1644
+
1645
+ - the best viable candidate of non-template functions
1646
+ [[over.match.best]],
1647
+ - the address of a non-template function [[over.over]],
1648
+ - the matching of template template arguments [[temp.arg.template]],
1649
+ - the partial ordering of class template specializations
1650
+ [[temp.class.order]], and
1651
+ - the partial ordering of function templates [[temp.func.order]].
1652
+
1653
+ — *end note*]
1654
+
1655
+ A declaration `D1` is *at least as constrained* as a declaration `D2` if
1656
+
1657
+ - `D1` and `D2` are both constrained declarations and `D1`’s associated
1658
+ constraints subsume those of `D2`; or
1659
+ - `D2` has no associated constraints.
1660
+
1661
+ A declaration `D1` is *more constrained* than another declaration `D2`
1662
+ when `D1` is at least as constrained as `D2`, and `D2` is not at least
1663
+ as constrained as `D1`.
1664
+
1665
+ [*Example 2*:
1666
+
1667
+ ``` cpp
1668
+ template<typename T> concept C1 = requires(T t) { --t; };
1669
+ template<typename T> concept C2 = C1<T> && requires(T t) { *t; };
1670
+
1671
+ template<C1 T> void f(T); // #1
1672
+ template<C2 T> void f(T); // #2
1673
+ template<typename T> void g(T); // #3
1674
+ template<C1 T> void g(T); // #4
1675
+
1676
+ f(0); // selects #1
1677
+ f((int*)0); // selects #2
1678
+ g(true); // selects #3 because C1<bool> is not satisfied
1679
+ g(0); // selects #4
1680
+ ```
1681
+
1682
+ — *end example*]
1683
+
1684
  ## Type equivalence <a id="temp.type">[[temp.type]]</a>
1685
 
1686
+ Two *template-id*s are the same if
1687
 
1688
  - their *template-name*s, *operator-function-id*s, or
1689
+ *literal-operator-id*s refer to the same template, and
1690
+ - their corresponding type *template-argument*s are the same type, and
1691
+ - their corresponding non-type *template-argument*s are
1692
+ template-argument-equivalent (see below) after conversion to the type
1693
+ of the *template-parameter*, and
 
 
 
 
 
 
 
1694
  - their corresponding template *template-argument*s refer to the same
1695
  template.
1696
 
1697
+ Two *template-id*s that are the same refer to the same class, function,
1698
+ or variable.
1699
+
1700
+ Two values are *template-argument-equivalent* if they are of the same
1701
+ type and
1702
+
1703
+ - they are of integral type and their values are the same, or
1704
+ - they are of floating-point type and their values are identical, or
1705
+ - they are of type `std::nullptr_t`, or
1706
+ - they are of enumeration type and their values are the same, [^6] or
1707
+ - they are of pointer type and they have the same pointer value, or
1708
+ - they are of pointer-to-member type and they refer to the same class
1709
+ member or are both the null member pointer value, or
1710
+ - they are of reference type and they refer to the same object or
1711
+ function, or
1712
+ - they are of array type and their corresponding elements are
1713
+ template-argument-equivalent, [^7] or
1714
+ - they are of union type and either they both have no active member or
1715
+ they have the same active member and their active members are
1716
+ template-argument-equivalent, or
1717
+ - they are of class type and their corresponding direct subobjects and
1718
+ reference members are template-argument-equivalent.
1719
+
1720
  [*Example 1*:
1721
 
1722
  ``` cpp
1723
  template<class E, int size> class buffer { ... };
1724
  buffer<char,2*512> x;
 
1748
 
1749
  declares `y` and `z` to be of the same type.
1750
 
1751
  — *end example*]
1752
 
1753
+ If an expression e is type-dependent [[temp.dep.expr]], `decltype(e)`
1754
  denotes a unique dependent type. Two such *decltype-specifier*s refer to
1755
+ the same type only if their *expression*s are equivalent
1756
+ [[temp.over.link]].
1757
 
1758
  [*Note 1*: However, such a type may be aliased, e.g., by a
1759
  *typedef-name*. — *end note*]
1760
 
1761
  ## Template declarations <a id="temp.decls">[[temp.decls]]</a>
 
1772
  ```
1773
 
1774
  — *end example*]
1775
 
1776
  [*Note 1*: However, this syntax is allowed in class template partial
1777
+ specializations [[temp.class.spec]]. — *end note*]
1778
 
1779
+ For purposes of name lookup and instantiation, default arguments,
1780
+ *type-constraint*s, *requires-clause*s [[temp.pre]], and
1781
+ *noexcept-specifier*s of function templates and of member functions of
1782
+ class templates are considered definitions; each default argument,
1783
+ *type-constraint*, *requires-clause*, or *noexcept-specifier* is a
1784
+ separate definition which is unrelated to the templated function
1785
+ definition or to any other default arguments *type-constraint*s,
1786
+ *requires-clause*s, or *noexcept-specifier*s. For the purpose of
1787
+ instantiation, the substatements of a constexpr if statement [[stmt.if]]
1788
+ are considered definitions.
1789
 
1790
  Because an *alias-declaration* cannot declare a *template-id*, it is not
1791
  possible to partially or explicitly specialize an alias template.
1792
 
1793
  ### Class templates <a id="temp.class">[[temp.class]]</a>
 
1820
  — *end example*]
1821
 
1822
  When a member function, a member class, a member enumeration, a static
1823
  data member or a member template of a class template is defined outside
1824
  of the class template definition, the member definition is defined as a
1825
+ template definition in which the *template-head* is equivalent to that
1826
+ of the class template [[temp.over.link]]. The names of the template
1827
+ parameters used in the definition of the member may be different from
1828
+ the template parameter names used in the class template definition. The
1829
+ template argument list following the class template name in the member
1830
+ definition shall name the parameters in the same order as the one used
1831
+ in the template parameter list of the member. Each template parameter
1832
+ pack shall be expanded with an ellipsis in the template argument list.
1833
 
1834
  [*Example 2*:
1835
 
1836
  ``` cpp
1837
  template<class T1, class T2> struct A {
 
1851
 
1852
  template<class ... Types> void B<Types ...>::f3() { } // OK
1853
  template<class ... Types> void B<Types>::f4() { } // error
1854
  ```
1855
 
1856
+ ``` cpp
1857
+ template<typename T> concept C = true;
1858
+ template<typename T> concept D = true;
1859
+
1860
+ template<C T> struct S {
1861
+ void f();
1862
+ void g();
1863
+ void h();
1864
+ template<D U> struct Inner;
1865
+ };
1866
+
1867
+ template<C A> void S<A>::f() { } // OK: template-head{s} match
1868
+ template<typename T> void S<T>::g() { } // error: no matching declaration for S<T>
1869
+
1870
+ template<typename T> requires C<T> // ill-formed, no diagnostic required: template-head{s} are
1871
+ void S<T>::h() { } // functionally equivalent but not equivalent
1872
+
1873
+ template<C X> template<D Y>
1874
+ struct S<X>::Inner { }; // OK
1875
+ ```
1876
+
1877
  — *end example*]
1878
 
1879
  In a redeclaration, partial specialization, explicit specialization or
1880
  explicit instantiation of a class template, the *class-key* shall agree
1881
+ in kind with the original class template declaration [[dcl.type.elab]].
 
1882
 
1883
  #### Member functions of class templates <a id="temp.mem.func">[[temp.mem.func]]</a>
1884
 
1885
  A member function of a class template may be defined outside of the
1886
  class template definition in which it is declared.
 
1896
  T& operator[](int);
1897
  T& elem(int i) { return v[i]; }
1898
  };
1899
  ```
1900
 
1901
+ declares three member functions of a class template. The subscript
1902
+ function might be defined like this:
1903
 
1904
  ``` cpp
1905
  template<class T> T& Array<T>::operator[](int i) {
1906
  if (i<0 || sz<=i) error("Array: range error");
1907
  return v[i];
1908
  }
1909
  ```
1910
 
1911
+ A constrained member function can be defined out of line:
1912
+
1913
+ ``` cpp
1914
+ template<typename T> concept C = requires {
1915
+ typename T::type;
1916
+ };
1917
+
1918
+ template<typename T> struct S {
1919
+ void f() requires C<T>;
1920
+ void g() requires C<T>;
1921
+ };
1922
+
1923
+ template<typename T>
1924
+ void S<T>::f() requires C<T> { } // OK
1925
+ template<typename T>
1926
+ void S<T>::g() { } // error: no matching function in S<T>
1927
+ ```
1928
+
1929
  — *end example*]
1930
 
1931
  The *template-argument*s for a member function of a class template are
1932
  determined by the *template-argument*s of the type of the object for
1933
  which the member function is called.
1934
 
1935
  [*Example 2*:
1936
 
1937
+ The *template-argument* for `Array<T>::operator[]` will be determined by
1938
+ the `Array` to which the subscripting operation is applied.
1939
 
1940
  ``` cpp
1941
  Array<int> v1(20);
1942
  Array<dcomplex> v2(30);
1943
 
1944
+ v1[3] = 7; // Array<int>::operator[]
1945
+ v2[3] = dcomplex(7,8); // Array<dcomplex>::operator[]
1946
  ```
1947
 
1948
  — *end example*]
1949
 
1950
+ #### Deduction guides <a id="temp.deduct.guide">[[temp.deduct.guide]]</a>
1951
+
1952
+ Deduction guides are used when a *template-name* appears as a type
1953
+ specifier for a deduced class type [[dcl.type.class.deduct]]. Deduction
1954
+ guides are not found by name lookup. Instead, when performing class
1955
+ template argument deduction [[over.match.class.deduct]], any deduction
1956
+ guides declared for the class template are considered.
1957
+
1958
+ ``` bnf
1959
+ deduction-guide:
1960
+ explicit-specifierₒₚₜ template-name '(' parameter-declaration-clause ')' '->' simple-template-id ';'
1961
+ ```
1962
+
1963
+ [*Example 1*:
1964
+
1965
+ ``` cpp
1966
+ template<class T, class D = int>
1967
+ struct S {
1968
+ T data;
1969
+ };
1970
+ template<class U>
1971
+ S(U) -> S<typename U::type>;
1972
+
1973
+ struct A {
1974
+ using type = short;
1975
+ operator type();
1976
+ };
1977
+ S x{A()}; // x is of type S<short, int>
1978
+ ```
1979
+
1980
+ — *end example*]
1981
+
1982
+ The same restrictions apply to the *parameter-declaration-clause* of a
1983
+ deduction guide as in a function declaration [[dcl.fct]]. The
1984
+ *simple-template-id* shall name a class template specialization. The
1985
+ *template-name* shall be the same *identifier* as the *template-name* of
1986
+ the *simple-template-id*. A *deduction-guide* shall be declared in the
1987
+ same scope as the corresponding class template and, for a member class
1988
+ template, with the same access. Two deduction guide declarations in the
1989
+ same translation unit for the same class template shall not have
1990
+ equivalent *parameter-declaration-clause*s.
1991
+
1992
  #### Member classes of class templates <a id="temp.mem.class">[[temp.mem.class]]</a>
1993
 
1994
  A member class of a class template may be defined outside the class
1995
  template definition in which it is declared.
1996
 
1997
  [*Note 1*:
1998
 
1999
  The member class must be defined before its first use that requires an
2000
+ instantiation [[temp.inst]]. For example,
2001
 
2002
  ``` cpp
2003
  template<class T> struct A {
2004
  class B;
2005
  };
 
2072
 
2073
  A template can be declared within a class or class template; such a
2074
  template is called a member template. A member template can be defined
2075
  within or outside its class definition or class template definition. A
2076
  member template of a class template that is defined outside of its class
2077
+ template definition shall be specified with a *template-head* equivalent
2078
+ to that of the class template followed by a *template-head* equivalent
2079
+ to that of the member template [[temp.over.link]].
2080
 
2081
  [*Example 1*:
2082
 
2083
  ``` cpp
2084
  template<class T> struct string {
 
2090
  }
2091
  ```
2092
 
2093
  — *end example*]
2094
 
2095
+ [*Example 2*:
2096
+
2097
+ ``` cpp
2098
+ template<typename T> concept C1 = true;
2099
+ template<typename T> concept C2 = sizeof(T) <= 4;
2100
+
2101
+ template<C1 T> struct S {
2102
+ template<C2 U> void f(U);
2103
+ template<C2 U> void g(U);
2104
+ };
2105
+
2106
+ template<C1 T> template<C2 U>
2107
+ void S<T>::f(U) { } // OK
2108
+ template<C1 T> template<typename U>
2109
+ void S<T>::g(U) { } // error: no matching function in S<T>
2110
+ ```
2111
+
2112
+ — *end example*]
2113
+
2114
  A local class of non-closure type shall not have member templates.
2115
+ Access control rules [[class.access]] apply to member template names. A
2116
+ destructor shall not be a member template. A non-template member
2117
+ function [[dcl.fct]] with a given name and type and a member function
2118
+ template of the same name, which could be used to generate a
2119
  specialization of the same type, can both be declared in a class. When
2120
  both exist, a use of that name and type refers to the non-template
2121
  member unless an explicit template argument list is supplied.
2122
 
2123
+ [*Example 3*:
2124
 
2125
  ``` cpp
2126
  template <class T> struct A {
2127
  void f(int);
2128
  template <class T2> void f(T2);
 
2141
 
2142
  — *end example*]
2143
 
2144
  A member function template shall not be virtual.
2145
 
2146
+ [*Example 4*:
2147
 
2148
  ``` cpp
2149
  template <class T> struct AA {
2150
  template <class C> virtual void g(C); // error
2151
  virtual void f(); // OK
 
2155
  — *end example*]
2156
 
2157
  A specialization of a member function template does not override a
2158
  virtual function from a base class.
2159
 
2160
+ [*Example 5*:
2161
 
2162
  ``` cpp
2163
  class B {
2164
  virtual void f(int);
2165
  };
 
2174
 
2175
  A specialization of a conversion function template is referenced in the
2176
  same way as a non-template conversion function that converts to the same
2177
  type.
2178
 
2179
+ [*Example 6*:
2180
 
2181
  ``` cpp
2182
  struct A {
2183
  template <class T> operator T*();
2184
  };
 
2193
  }
2194
  ```
2195
 
2196
  — *end example*]
2197
 
2198
+ [*Note 1*: There is no syntax to form a *template-id* [[temp.names]] by
2199
+ providing an explicit template argument list [[temp.arg.explicit]] for a
2200
+ conversion function template [[class.conv.fct]]. *end note*]
 
 
2201
 
2202
  A specialization of a conversion function template is not found by name
2203
  lookup. Instead, any conversion function templates visible in the
2204
  context of the use are considered. For each such operator, if argument
2205
+ deduction succeeds [[temp.deduct.conv]], the resulting specialization is
2206
+ used as if found by name lookup.
2207
 
2208
  A *using-declaration* in a derived class cannot refer to a
2209
  specialization of a conversion function template in a base class.
2210
 
2211
+ Overload resolution [[over.ics.rank]] and partial ordering
2212
+ [[temp.func.order]] are used to select the best conversion function
2213
  among multiple specializations of conversion function templates and/or
2214
  non-template conversion functions.
2215
 
2216
  ### Variadic templates <a id="temp.variadic">[[temp.variadic]]</a>
2217
 
 
2237
  [*Example 2*:
2238
 
2239
  ``` cpp
2240
  template<class ... Types> void f(Types ... args);
2241
 
2242
+ f(); // args contains no arguments
2243
+ f(1); // args contains one argument: int
2244
+ f(2, 1.0); // args contains two arguments: int and double
2245
  ```
2246
 
2247
  — *end example*]
2248
 
2249
+ An **init-capture* pack* is a lambda capture that introduces an
2250
+ *init-capture* for each of the elements in the pack expansion of its
2251
+ *initializer*.
2252
+
2253
+ [*Example 3*:
2254
+
2255
+ ``` cpp
2256
+ template <typename... Args>
2257
+ void foo(Args... args) {
2258
+ [...xs=args]{
2259
+ bar(xs...); // xs is an init-capture pack
2260
+ };
2261
+ }
2262
+
2263
+ foo(); // xs contains zero init-captures
2264
+ foo(1); // xs contains one init-capture
2265
+ ```
2266
+
2267
+ — *end example*]
2268
+
2269
+ A *pack* is a template parameter pack, a function parameter pack, or an
2270
+ *init-capture* pack. The number of elements of a template parameter pack
2271
+ or a function parameter pack is the number of arguments provided for the
2272
+ parameter pack. The number of elements of an *init-capture* pack is the
2273
+ number of elements in the pack expansion of its *initializer*.
2274
 
2275
  A *pack expansion* consists of a *pattern* and an ellipsis, the
2276
  instantiation of which produces zero or more instantiations of the
2277
  pattern in a list (described below). The form of the pattern depends on
2278
  the context in which the expansion occurs. Pack expansions can occur in
2279
  the following contexts:
2280
 
2281
+ - In a function parameter pack [[dcl.fct]]; the pattern is the
2282
  *parameter-declaration* without the ellipsis.
2283
+ - In a *using-declaration* [[namespace.udecl]]; the pattern is a
2284
  *using-declarator*.
2285
+ - In a template parameter pack that is a pack expansion [[temp.param]]:
 
2286
  - if the template parameter pack is a *parameter-declaration*; the
2287
  pattern is the *parameter-declaration* without the ellipsis;
2288
+ - if the template parameter pack is a *type-parameter*; the pattern is
2289
+ the corresponding *type-parameter* without the ellipsis.
2290
+ - In an *initializer-list* [[dcl.init]]; the pattern is an
 
2291
  *initializer-clause*.
2292
+ - In a *base-specifier-list* [[class.derived]]; the pattern is a
2293
+ *base-specifier*.
2294
+ - In a *mem-initializer-list* [[class.base.init]] for a
2295
  *mem-initializer* whose *mem-initializer-id* denotes a base class; the
2296
  pattern is the *mem-initializer*.
2297
+ - In a *template-argument-list* [[temp.arg]]; the pattern is a
2298
  *template-argument*.
2299
+ - In an *attribute-list* [[dcl.attr.grammar]]; the pattern is an
2300
  *attribute*.
2301
+ - In an *alignment-specifier* [[dcl.align]]; the pattern is the
2302
  *alignment-specifier* without the ellipsis.
2303
+ - In a *capture-list* [[expr.prim.lambda.capture]]; the pattern is the
2304
+ *capture* without the ellipsis.
2305
+ - In a `sizeof...` expression [[expr.sizeof]]; the pattern is an
2306
  *identifier*.
2307
+ - In a *fold-expression* [[expr.prim.fold]]; the pattern is the
2308
+ *cast-expression* that contains an unexpanded pack.
2309
 
2310
+ [*Example 4*:
2311
 
2312
  ``` cpp
2313
  template<class ... Types> void f(Types ... rest);
2314
  template<class ... Types> void g(Types ... rest) {
2315
  f(&rest ...); // ``&rest ...'' is a pack expansion; ``&rest'' is its pattern
2316
  }
2317
  ```
2318
 
2319
  — *end example*]
2320
 
2321
+ For the purpose of determining whether a pack satisfies a rule regarding
2322
+ entities other than packs, the pack is considered to be the entity that
2323
+ would result from an instantiation of the pattern in which it appears.
 
2324
 
2325
+ A pack whose name appears within the pattern of a pack expansion is
2326
+ expanded by that pack expansion. An appearance of the name of a pack is
2327
+ only expanded by the innermost enclosing pack expansion. The pattern of
2328
+ a pack expansion shall name one or more packs that are not expanded by a
2329
+ nested pack expansion; such packs are called *unexpanded packs* in the
2330
+ pattern. All of the packs expanded by a pack expansion shall have the
2331
+ same number of arguments specified. An appearance of a name of a pack
2332
+ that is not expanded is ill-formed.
 
2333
 
2334
+ [*Example 5*:
2335
 
2336
  ``` cpp
2337
  template<typename...> struct Tuple {};
2338
  template<typename T1, typename T2> struct Pair {};
2339
 
 
2349
  // error: different number of arguments specified for Args1 and Args2
2350
 
2351
  template<class ... Args>
2352
  void g(Args ... args) { // OK: Args is expanded by the function parameter pack args
2353
  f(const_cast<const Args*>(&args)...); // OK: ``Args'' and ``args'' are expanded
2354
+ f(5 ...); // error: pattern does not contain any packs
2355
+ f(args); // error: pack ``args'' is not expanded
2356
  f(h(args ...) + args ...); // OK: first ``args'' expanded within h,
2357
  // second ``args'' expanded within f
2358
  }
2359
  ```
2360
 
2361
  — *end example*]
2362
 
2363
  The instantiation of a pack expansion that is neither a `sizeof...`
2364
+ expression nor a *fold-expression* produces a list of elements E₁, E₂,
2365
+ , $\mathtt{E}_N$, where N is the number of elements in the pack
2366
+ expansion parameters. Each Eᵢ is generated by instantiating the pattern
2367
+ and replacing each pack expansion parameter with its iᵗʰ element. Such
2368
+ an element, in the context of the instantiation, is interpreted as
2369
+ follows:
2370
 
2371
  - if the pack is a template parameter pack, the element is a template
2372
+ parameter [[temp.param]] of the corresponding kind (type or non-type)
2373
+ designating the iᵗʰ corresponding type or value template argument;
 
2374
  - if the pack is a function parameter pack, the element is an
2375
+ *id-expression* designating the iᵗʰ function parameter that resulted
2376
+ from instantiation of the function parameter pack declaration;
2377
+ otherwise
2378
+ - if the pack is an *init-capture* pack, the element is an
2379
+ *id-expression* designating the variable introduced by the iᵗʰ
2380
+ *init-capture* that resulted from instantiation of the *init-capture*
2381
+ pack.
2382
 
2383
+ All of the Eᵢ become items in the enclosing list.
2384
 
2385
  [*Note 1*: The variety of list varies with the context:
2386
  *expression-list*, *base-specifier-list*, *template-argument-list*,
2387
  etc. — *end note*]
2388
 
 
2390
  list. Such an instantiation does not alter the syntactic interpretation
2391
  of the enclosing construct, even in cases where omitting the list
2392
  entirely would otherwise be ill-formed or would result in an ambiguity
2393
  in the grammar.
2394
 
2395
+ [*Example 6*:
2396
 
2397
  ``` cpp
2398
  template<class... T> struct X : T... { };
2399
  template<class... T> void f(T... values) {
2400
  X<T...> x(values...);
 
2404
  // x is a variable of type X<> that is value-initialized
2405
  ```
2406
 
2407
  — *end example*]
2408
 
2409
+ The instantiation of a `sizeof...` expression [[expr.sizeof]] produces
2410
+ an integral constant containing the number of elements in the pack it
2411
+ expands.
2412
 
2413
  The instantiation of a *fold-expression* produces:
2414
 
2415
  - `((`E₁ *op* E₂`)` *op* ⋯`)` *op* $\mathtt{E}_N$ for a unary left fold,
2416
  - E₁ *op* `(`⋯ *op* `(`$\mathtt{E}_{N-1}$ *op* $\mathtt{E}_N$`))` for a
 
2421
  E`)))` for a binary right fold.
2422
 
2423
  In each case, *op* is the *fold-operator*, N is the number of elements
2424
  in the pack expansion parameters, and each Eᵢ is generated by
2425
  instantiating the pattern and replacing each pack expansion parameter
2426
+ with its iᵗʰ element. For a binary fold-expression, E is generated by
2427
  instantiating the *cast-expression* that did not contain an unexpanded
2428
+ pack.
2429
 
2430
+ [*Example 7*:
2431
 
2432
  ``` cpp
2433
  template<typename ...Args>
2434
  bool all(Args ...args) { return (... && args); }
2435
 
 
2440
  `((true && true) && true) && false`, which evaluates to `false`.
2441
 
2442
  — *end example*]
2443
 
2444
  If N is zero for a unary fold-expression, the value of the expression is
2445
+ shown in [[temp.fold.empty]]; if the operator is not listed in
2446
+ [[temp.fold.empty]], the instantiation is ill-formed.
2447
 
2448
+ **Table: Value of folding empty sequences** <a id="temp.fold.empty">[temp.fold.empty]</a>
2449
 
2450
+ | Operator | Value when pack is empty |
2451
+ | -------- | ------------------------ |
2452
  | `&&` | `true` |
2453
  | `||` | `false` |
2454
  | `,` | `void()` |
2455
 
2456
 
 
2468
  non-template function is found in the specified class or namespace,
2469
  the friend declaration refers to that function, otherwise,
2470
  - if the name of the friend is a *qualified-id* and a matching function
2471
  template is found in the specified class or namespace, the friend
2472
  declaration refers to the deduced specialization of that function
2473
+ template [[temp.deduct.decl]], otherwise,
2474
  - the name shall be an *unqualified-id* that declares (or redeclares) a
2475
  non-template function.
2476
 
2477
  [*Example 1*:
2478
 
 
2523
  ```
2524
 
2525
  — *end example*]
2526
 
2527
  A template friend declaration specifies that all specializations of that
2528
+ template, whether they are implicitly instantiated [[temp.inst]],
2529
+ partially specialized [[temp.class.spec]] or explicitly specialized
2530
+ [[temp.expl.spec]], are friends of the class containing the template
2531
  friend declaration.
2532
 
2533
  [*Example 3*:
2534
 
2535
  ``` cpp
 
2542
  template<class T> struct A<T*> { X::Y ab; }; // OK
2543
  ```
2544
 
2545
  — *end example*]
2546
 
2547
+ A template friend declaration may declare a member of a dependent type
2548
+ to be a friend. The friend declaration shall declare a function or
2549
+ specify a type with an *elaborated-type-specifier*, in either case with
2550
+ a *nested-name-specifier* ending with a *simple-template-id*, *C*, whose
2551
+ *template-name* names a class template. The template parameters of the
2552
+ template friend declaration shall be deducible from *C* (
2553
+ [[temp.deduct.type]]). In this case, a member of a specialization *S* of
2554
+ the class template is a friend of the class granting friendship if
2555
+ deduction of the template parameters of *C* from *S* succeeds, and
2556
+ substituting the deduced template arguments into the friend declaration
2557
+ produces a declaration that would be a valid redeclaration of the member
2558
+ of the specialization.
2559
 
2560
  [*Example 4*:
2561
 
2562
  ``` cpp
2563
  template<class T> struct A {
2564
  struct B { };
2565
  void f();
2566
  struct D {
2567
  void g();
2568
  };
2569
+ T h();
2570
+ template<T U> T i();
2571
  };
2572
  template<> struct A<int> {
2573
  struct B { };
2574
  int f();
2575
  struct D {
2576
  void g();
2577
  };
2578
+ template<int U> int i();
2579
+ };
2580
+ template<> struct A<float*> {
2581
+ int *h();
2582
  };
2583
 
2584
  class C {
2585
  template<class T> friend struct A<T>::B; // grants friendship to A<int>::B even though
2586
  // it is not a specialization of A<T>::B
2587
  template<class T> friend void A<T>::f(); // does not grant friendship to A<int>::f()
2588
  // because its return type does not match
2589
+ template<class T> friend void A<T>::D::g(); // error: A<T>::D does not end with a simple-template-id
2590
+ template<class T> friend int *A<T*>::h(); // grants friendship to A<int*>::h() and A<float*>::h()
2591
+ template<class T> template<T U> // grants friendship to instantiations of A<T>::i() and
2592
+ friend T A<T>::i(); // to A<int>::i(), and thereby to all specializations
2593
+ }; // of those function templates
2594
  ```
2595
 
2596
  — *end example*]
2597
 
2598
  [*Note 1*: A friend declaration may first declare a member of an
2599
+ enclosing namespace scope [[temp.inject]]. — *end note*]
2600
 
2601
  A friend template shall not be declared in a local class.
2602
 
2603
  Friend declarations shall not declare partial specializations.
2604
 
 
2613
 
2614
  — *end example*]
2615
 
2616
  When a friend declaration refers to a specialization of a function
2617
  template, the function parameter declarations shall not include default
2618
+ arguments, nor shall the `inline`, `constexpr`, or `consteval`
2619
+ specifiers be used in such a declaration.
2620
+
2621
+ A non-template friend declaration with a *requires-clause* shall be a
2622
+ definition. A friend function template with a constraint that depends on
2623
+ a template parameter from an enclosing template shall be a definition.
2624
+ Such a constrained friend function or function template declaration does
2625
+ not declare the same function or function template as a declaration in
2626
+ any other scope.
2627
 
2628
  ### Class template partial specializations <a id="temp.class.spec">[[temp.class.spec]]</a>
2629
 
2630
  A *primary class template* declaration is one in which the class
2631
  template name is an identifier. A template declaration in which the
2632
  class template name is a *simple-template-id* is a *partial
2633
  specialization* of the class template named in the *simple-template-id*.
2634
  A partial specialization of a class template provides an alternative
2635
  definition of the template that is used instead of the primary
2636
  definition when the arguments in a specialization match those given in
2637
+ the partial specialization [[temp.class.spec.match]]. The primary
2638
  template shall be declared before any specializations of that template.
2639
  A partial specialization shall be declared before the first use of a
2640
  class template specialization that would make use of the partial
2641
  specialization as the result of an implicit or explicit instantiation in
2642
  every translation unit in which such a use occurs; no diagnostic is
2643
  required.
2644
 
2645
  Each class template partial specialization is a distinct template and
2646
  definitions shall be provided for the members of a template partial
2647
+ specialization [[temp.class.spec.mfunc]].
2648
 
2649
  [*Example 1*:
2650
 
2651
  ``` cpp
2652
  template<class T1, class T2, int I> class A { };
 
2660
  template. The second and subsequent declarations declare partial
2661
  specializations of the primary template.
2662
 
2663
  — *end example*]
2664
 
2665
+ A class template partial specialization may be constrained [[temp.pre]].
2666
+
2667
+ [*Example 2*:
2668
+
2669
+ ``` cpp
2670
+ template<typename T> concept C = true;
2671
+
2672
+ template<typename T> struct X { };
2673
+ template<typename T> struct X<T*> { }; // #1
2674
+ template<C T> struct X<T> { }; // #2
2675
+ ```
2676
+
2677
+ Both partial specializations are more specialized than the primary
2678
+ template. \#1 is more specialized because the deduction of its template
2679
+ arguments from the template argument list of the class template
2680
+ specialization succeeds, while the reverse does not. \#2 is more
2681
+ specialized because the template arguments are equivalent, but the
2682
+ partial specialization is more constrained [[temp.constr.order]].
2683
+
2684
+ — *end example*]
2685
+
2686
  The template parameters are specified in the angle bracket enclosed list
2687
  that immediately follows the keyword `template`. For partial
2688
  specializations, the template argument list is explicitly written
2689
  immediately following the class template name. For primary templates,
2690
  this list is implicitly described by the template parameter list.
2691
  Specifically, the order of the template arguments is the sequence in
2692
  which they appear in the template parameter list.
2693
 
2694
+ [*Example 3*: The template argument list for the primary template in
2695
  the example above is `<T1,` `T2,` `I>`. — *end example*]
2696
 
2697
  [*Note 1*:
2698
 
2699
+ The template argument list cannot be specified in the primary template
2700
+ declaration. For example,
2701
 
2702
  ``` cpp
2703
  template<class T1, class T2, int I>
2704
  class A<T1, T2, I> { }; // error
2705
  ```
 
2708
 
2709
  A class template partial specialization may be declared in any scope in
2710
  which the corresponding primary template may be defined (
2711
  [[namespace.memdef]], [[class.mem]], [[temp.mem]]).
2712
 
2713
+ [*Example 4*:
2714
 
2715
  ``` cpp
2716
  template<class T> struct A {
2717
  struct C {
2718
  template<class T2> struct B { };
 
2734
  previously-declared partial specializations of the primary template are
2735
  also considered. One consequence is that a *using-declaration* which
2736
  refers to a class template does not restrict the set of partial
2737
  specializations which may be found through the *using-declaration*.
2738
 
2739
+ [*Example 5*:
2740
 
2741
  ``` cpp
2742
  namespace N {
2743
  template<class T1, class T2> class A { }; // primary template
2744
  }
 
2762
  following restrictions apply:
2763
 
2764
  - The type of a template parameter corresponding to a specialized
2765
  non-type argument shall not be dependent on a parameter of the
2766
  specialization.
2767
+ \[*Example 6*:
2768
  ``` cpp
2769
  template <class T, T t> struct C {};
2770
  template <class T> struct C<T, 1>; // error
2771
 
2772
  template< int X, int (*array_ptr)[X] > class A {};
2773
  int array[5];
2774
  template< int X > class A<X,&array> { }; // error
2775
  ```
2776
 
2777
  — *end example*]
2778
+ - The specialization shall be more specialized than the primary template
2779
+ [[temp.class.order]].
2780
  - The template parameter list of a specialization shall not contain
2781
+ default template argument values.[^8]
2782
+ - An argument shall not contain an unexpanded pack. If an argument is a
2783
+ pack expansion [[temp.variadic]], it shall be the last argument in the
2784
+ template argument list.
2785
+
2786
+ The usual access checking rules do not apply to non-dependent names used
2787
+ to specify template arguments of the *simple-template-id* of the partial
2788
+ specialization.
2789
+
2790
+ [*Note 2*: The template arguments may be private types or objects that
2791
+ would normally not be accessible. Dependent names cannot be checked when
2792
+ declaring the partial specialization, but will be checked when
2793
+ substituting into the partial specialization. — *end note*]
2794
 
2795
  #### Matching of class template partial specializations <a id="temp.class.spec.match">[[temp.class.spec.match]]</a>
2796
 
2797
  When a class template is used in a context that requires an
2798
  instantiation of the class, it is necessary to determine whether the
 
2802
  argument lists of the partial specializations.
2803
 
2804
  - If exactly one matching specialization is found, the instantiation is
2805
  generated from that specialization.
2806
  - If more than one matching specialization is found, the partial order
2807
+ rules [[temp.class.order]] are used to determine whether one of the
2808
  specializations is more specialized than the others. If none of the
2809
  specializations is more specialized than all of the other matching
2810
  specializations, then the use of the class template is ambiguous and
2811
  the program is ill-formed.
2812
  - If no matches are found, the instantiation is generated from the
2813
  primary template.
2814
 
2815
  A partial specialization matches a given actual template argument list
2816
  if the template arguments of the partial specialization can be deduced
2817
+ from the actual template argument list [[temp.deduct]], and the deduced
2818
+ template arguments satisfy the associated constraints of the partial
2819
+ specialization, if any [[temp.constr.decl]].
2820
 
2821
  [*Example 1*:
2822
 
2823
  ``` cpp
2824
  template<class T1, class T2, int I> class A { }; // #1
 
2834
  A<int*, int*, 2> a5; // ambiguous: matches #3 and #5
2835
  ```
2836
 
2837
  — *end example*]
2838
 
2839
+ [*Example 2*:
2840
+
2841
+ ``` cpp
2842
+ template<typename T> concept C = requires (T t) { t.f(); };
2843
+
2844
+ template<typename T> struct S { }; // #1
2845
+ template<C T> struct S<T> { }; // #2
2846
+
2847
+ struct Arg { void f(); };
2848
+
2849
+ S<int> s1; // uses #1; the constraints of #2 are not satisfied
2850
+ S<Arg> s2; // uses #2; both constraints are satisfied but #2 is more specialized
2851
+ ```
2852
+
2853
+ — *end example*]
2854
+
2855
  If the template arguments of a partial specialization cannot be deduced
2856
  because of the structure of its *template-parameter-list* and the
2857
  *template-id*, the program is ill-formed.
2858
 
2859
+ [*Example 3*:
2860
 
2861
  ``` cpp
2862
  template <int I, int J> struct A {};
2863
  template <int I> struct A<I+5, I*2> {}; // error
2864
 
 
2878
  #### Partial ordering of class template specializations <a id="temp.class.order">[[temp.class.order]]</a>
2879
 
2880
  For two class template partial specializations, the first is *more
2881
  specialized* than the second if, given the following rewrite to two
2882
  function templates, the first function template is more specialized than
2883
+ the second according to the ordering rules for function templates
2884
+ [[temp.func.order]]:
2885
 
2886
+ - Each of the two function templates has the same template parameters
2887
+ and associated constraints [[temp.constr.decl]] as the corresponding
2888
+ partial specialization.
2889
  - Each function template has a single function parameter whose type is a
2890
  class template specialization where the template arguments are the
2891
  corresponding template parameters from the function template for each
2892
  template argument in the *template-argument-list* of the
2893
  *simple-template-id* of the partial specialization.
 
2917
  the partial specialization \#1 and the partial specialization \#4 is
2918
  more specialized than the partial specialization \#3.
2919
 
2920
  — *end example*]
2921
 
2922
+ [*Example 2*:
2923
+
2924
+ ``` cpp
2925
+ template<typename T> concept C = requires (T t) { t.f(); };
2926
+ template<typename T> concept D = C<T> && requires (T t) { t.f(); };
2927
+
2928
+ template<typename T> class S { };
2929
+ template<C T> class S<T> { }; // #1
2930
+ template<D T> class S<T> { }; // #2
2931
+
2932
+ template<C T> void f(S<T>); // A
2933
+ template<D T> void f(S<T>); // B
2934
+ ```
2935
+
2936
+ The partial specialization \#2 is more specialized than \#1 because `B`
2937
+ is more specialized than `A`.
2938
+
2939
+ — *end example*]
2940
+
2941
  #### Members of class template specializations <a id="temp.class.spec.mfunc">[[temp.class.spec.mfunc]]</a>
2942
 
2943
  The template parameter list of a member of a class template partial
2944
  specialization shall match the template parameter list of the class
2945
  template partial specialization. The template argument list of a member
2946
  of a class template partial specialization shall match the template
2947
  argument list of the class template partial specialization. A class
2948
+ template partial specialization is a distinct template. The members of
2949
+ the class template partial specialization are unrelated to the members
2950
+ of the primary template. Class template partial specialization members
2951
+ that are used in a way that requires a definition shall be defined; the
2952
  definitions of members of the primary template are never used as
2953
  definitions for members of a class template partial specialization. An
2954
  explicit specialization of a member of a class template partial
2955
  specialization is declared in the same way as an explicit specialization
2956
  of the primary template.
 
2983
  A<char,0> a0;
2984
  A<char,2> a2;
2985
  a0.f(); // OK, uses definition of primary template's member
2986
  a2.g(); // OK, uses definition of partial specialization's member
2987
  a2.h(); // OK, uses definition of explicit specialization's member
2988
+ a2.f(); // error: no definition of f for A<T,2>; the primary template is not used here
2989
  }
2990
  ```
2991
 
2992
  — *end example*]
2993
 
 
3037
  ```
3038
 
3039
  — *end example*]
3040
 
3041
  A function template can be overloaded with other function templates and
3042
+ with non-template functions [[dcl.fct]]. A non-template function is not
3043
+ related to a function template (i.e., it is never considered to be a
3044
  specialization), even if it has the same name and type as a potentially
3045
+ generated function template specialization.[^9]
3046
 
3047
  #### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
3048
 
3049
  It is possible to overload function templates so that two different
3050
  function template specializations have the same type.
 
3070
  ```
3071
 
3072
  — *end example*]
3073
 
3074
  Such specializations are distinct functions and do not violate the
3075
+ one-definition rule [[basic.def.odr]].
3076
 
3077
+ The signature of a function template is defined in [[intro.defs]]. The
3078
+ names of the template parameters are significant only for establishing
3079
+ the relationship between the template parameters and the rest of the
3080
+ signature.
3081
 
3082
  [*Note 1*:
3083
 
3084
  Two distinct function templates may have identical function return types
3085
  and function parameter lists, even if overload resolution alone cannot
 
3117
  type parameter. For example, a template type parameter can be used in
3118
  the `sizeof` operator. — *end note*]
3119
 
3120
  Two expressions involving template parameters are considered
3121
  *equivalent* if two function definitions containing the expressions
3122
+ would satisfy the one-definition rule [[basic.def.odr]], except that the
3123
+ tokens used to name the template parameters may differ as long as a
3124
  token used to name a template parameter in one expression is replaced by
3125
  another token that names the same template parameter in the other
3126
+ expression. Two unevaluated operands that do not involve template
3127
+ parameters are considered equivalent if two function definitions
3128
+ containing the expressions would satisfy the one-definition rule, except
3129
+ that the tokens used to name types and declarations may differ as long
3130
+ as they name the same entities, and the tokens used to form concept-ids
3131
+ may differ as long as the two *template-id*s are the same [[temp.type]].
3132
+
3133
+ [*Note 3*: For instance, `A<42>` and `A<40+2>` name the same
3134
+ type. — *end note*]
3135
+
3136
+ Two *lambda-expression*s are never considered equivalent.
3137
+
3138
+ [*Note 4*: The intent is to avoid *lambda-expression*s appearing in the
3139
+ signature of a function template with external linkage. — *end note*]
3140
+
3141
+ For determining whether two dependent names [[temp.dep]] are equivalent,
3142
+ only the name itself is considered, not the result of name lookup in the
3143
+ context of the template. If multiple declarations of the same function
3144
+ template differ in the result of this name lookup, the result for the
3145
+ first declaration is used.
3146
 
3147
  [*Example 3*:
3148
 
3149
  ``` cpp
3150
  template <int I, int J> void f(A<I+J>); // #1
3151
  template <int K, int L> void f(A<K+L>); // same as #1
3152
 
3153
  template <class T> decltype(g(T())) h();
3154
  int g(int);
3155
+ template <class T> decltype(g(T())) h() // redeclaration of h() uses the earlier lookup
3156
+ { return g(T()); } // …{} although the lookup here does find g(int)
3157
  int i = h<int>(); // template argument substitution fails; g(int)
3158
  // was not in scope at the first declaration of h()
3159
+
3160
+ // ill-formed, no diagnostic required: the two expressions are functionally equivalent but not equivalent
3161
+ template <int N> void foo(const char (*s)[([]{}, N)]);
3162
+ template <int N> void foo(const char (*s)[([]{}, N)]);
3163
+
3164
+ // two different declarations because the non-dependent portions are not considered equivalent
3165
+ template <class T> void spam(decltype([]{}) (*s)[sizeof(T)]);
3166
+ template <class T> void spam(decltype([]{}) (*s)[sizeof(T)]);
3167
  ```
3168
 
3169
  — *end example*]
3170
 
3171
+ Two potentially-evaluated expressions involving template parameters that
3172
+ are not equivalent are *functionally equivalent* if, for any given set
3173
+ of template arguments, the evaluation of the expression results in the
3174
+ same value. Two unevaluated operands that are not equivalent are
3175
+ functionally equivalent if, for any given set of template arguments, the
3176
+ expressions perform the same operations in the same order with the same
3177
+ entities.
3178
+
3179
+ [*Note 5*: For instance, one could have redundant
3180
+ parentheses. — *end note*]
3181
+
3182
+ Two *template-head*s are *equivalent* if their
3183
+ *template-parameter-list*s have the same length, corresponding
3184
+ *template-parameter*s are equivalent and are both declared with
3185
+ *type-constraint*s that are equivalent if either *template-parameter* is
3186
+ declared with a *type-constraint*, and if either *template-head* has a
3187
+ *requires-clause*, they both have *requires-clause*s and the
3188
+ corresponding *constraint-expression*s are equivalent. Two
3189
+ *template-parameter*s are *equivalent* under the following conditions:
3190
+
3191
+ - they declare template parameters of the same kind,
3192
+ - if either declares a template parameter pack, they both do,
3193
+ - if they declare non-type template parameters, they have equivalent
3194
+ types ignoring the use of *type-constraint*s for placeholder types,
3195
+ and
3196
+ - if they declare template template parameters, their template
3197
+ parameters are equivalent.
3198
+
3199
+ When determining whether types or *type-constraint*s are equivalent, the
3200
+ rules above are used to compare expressions involving template
3201
+ parameters. Two *template-head*s are *functionally equivalent* if they
3202
+ accept and are satisfied by [[temp.constr.constr]] the same set of
3203
+ template argument lists.
3204
 
3205
  Two function templates are *equivalent* if they are declared in the same
3206
+ scope, have the same name, have equivalent *template-head*s, and have
3207
+ return types, parameter lists, and trailing *requires-clause*s (if any)
3208
+ that are equivalent using the rules described above to compare
3209
+ expressions involving template parameters. Two function templates are
3210
+ *functionally equivalent* if they are declared in the same scope, have
3211
+ the same name, accept and are satisfied by the same set of template
3212
+ argument lists, and have return types and parameter lists that are
3213
+ functionally equivalent using the rules described above to compare
3214
+ expressions involving template parameters. If the validity or meaning of
3215
+ the program depends on whether two constructs are equivalent, and they
3216
+ are functionally equivalent but not equivalent, the program is
3217
+ ill-formed, no diagnostic required.
3218
 
3219
+ [*Note 6*:
3220
 
3221
  This rule guarantees that equivalent declarations will be linked with
3222
  one another, while not requiring implementations to use heroic efforts
3223
  to guarantee that functionally equivalent declarations will be treated
3224
  as distinct. For example, the last two declarations are functionally
3225
  equivalent and would cause a program to be ill-formed:
3226
 
3227
  ``` cpp
3228
+ // guaranteed to be the same
3229
  template <int I> void f(A<I>, A<I+10>);
3230
  template <int I> void f(A<I>, A<I+10>);
3231
 
3232
+ // guaranteed to be different
3233
  template <int I> void f(A<I>, A<I+10>);
3234
  template <int I> void f(A<I>, A<I+11>);
3235
 
3236
+ // ill-formed, no diagnostic required
3237
  template <int I> void f(A<I>, A<I+10>);
3238
  template <int I> void f(A<I>, A<I+1+2+3+4>);
3239
  ```
3240
 
3241
  — *end note*]
3242
 
3243
  #### Partial ordering of function templates <a id="temp.func.order">[[temp.func.order]]</a>
3244
 
3245
  If a function template is overloaded, the use of a function template
3246
+ specialization might be ambiguous because template argument deduction
3247
+ [[temp.deduct]] may associate the function template specialization with
3248
  more than one function template declaration. *Partial ordering* of
3249
  overloaded function template declarations is used in the following
3250
  contexts to select the function template to which a function template
3251
  specialization refers:
3252
 
3253
  - during overload resolution for a call to a function template
3254
+ specialization [[over.match.best]];
3255
  - when the address of a function template specialization is taken;
3256
  - when a placement operator delete that is a function template
3257
  specialization is selected to match a placement operator new (
3258
  [[basic.stc.dynamic.deallocation]], [[expr.new]]);
3259
+ - when a friend function declaration [[temp.friend]], an explicit
3260
+ instantiation [[temp.explicit]] or an explicit specialization
3261
+ [[temp.expl.spec]] refers to a function template specialization.
3262
 
3263
  Partial ordering selects which of two function templates is more
3264
  specialized than the other by transforming each template in turn (see
3265
  next paragraph) and performing template argument deduction using the
3266
  function type. The deduction process determines whether one of the
3267
  templates is more specialized than the other. If so, the more
3268
  specialized template is the one chosen by the partial ordering process.
3269
+ If both deductions succeed, the partial ordering selects the more
3270
+ constrained template (if one exists) as determined below.
3271
 
3272
  To produce the transformed template, for each type, non-type, or
3273
+ template template parameter (including template parameter packs
3274
+ [[temp.variadic]] thereof) synthesize a unique type, value, or class
3275
  template respectively and substitute it for each occurrence of that
3276
  parameter in the function type of the template.
3277
 
3278
  [*Note 1*: The type replacing the placeholder in the type of the value
3279
  synthesized for a non-type template parameter is also a unique
3280
  synthesized type. — *end note*]
3281
 
3282
+ Each function template M that is a member function is considered to have
3283
+ a new first parameter of type X(M), described below, inserted in its
3284
+ function parameter list. If exactly one of the function templates was
3285
+ considered by overload resolution via a rewritten candidate
3286
+ [[over.match.oper]] with a reversed order of parameters, then the order
3287
+ of the function parameters in its transformed template is reversed. For
3288
+ a function template M with cv-qualifiers cv that is a member of a class
3289
+ A:
3290
+
3291
+ - The type X(M) is “rvalue reference to cv A” if the optional
3292
+ *ref-qualifier* of M is `&&` or if M has no *ref-qualifier* and the
3293
+ positionally-corresponding parameter of the other transformed template
3294
+ has rvalue reference type; if this determination depends recursively
3295
+ upon whether X(M) is an rvalue reference type, it is not considered to
3296
+ have rvalue reference type.
3297
+ - Otherwise, X(M) is “lvalue reference to cv A”.
3298
 
3299
  [*Note 2*: This allows a non-static member to be ordered with respect
3300
  to a non-member function and for the results to be equivalent to the
3301
  ordering of two equivalent non-members. — *end note*]
3302
 
 
3314
  // template<class R> int operator*(B<A>&, R&);\quad\quad\quad// #1a
3315
 
3316
  int main() {
3317
  A a;
3318
  B<A> b;
3319
+ b * a; // calls #1
3320
  }
3321
  ```
3322
 
3323
  — *end example*]
3324
 
 
3355
 
3356
  — *end example*]
3357
 
3358
  [*Note 3*:
3359
 
3360
+ Since, in a call context, such type deduction considers only parameters
3361
+ for which there are explicit call arguments, some parameters are ignored
3362
  (namely, function parameter packs, parameters with default arguments,
3363
  and ellipsis parameters).
3364
 
3365
  [*Example 3*:
3366
 
 
3407
  template<class T > void f(T); // #2
3408
  template<class T, class... U> void g(T*, U...); // #3
3409
  template<class T > void g(T); // #4
3410
 
3411
  void h(int i) {
3412
+ f(&i); // OK: calls #2
3413
  g(&i); // OK: calls #3
3414
  }
3415
  ```
3416
 
3417
  — *end example*]
3418
 
3419
  — *end note*]
3420
 
3421
+ If deduction against the other template succeeds for both transformed
3422
+ templates, constraints can be considered as follows:
3423
+
3424
+ - If their *template-parameter-list*s (possibly including
3425
+ *template-parameter*s invented for an abbreviated function template
3426
+ [[dcl.fct]]) or function parameter lists differ in length, neither
3427
+ template is more specialized than the other.
3428
+ - Otherwise:
3429
+ - If exactly one of the templates was considered by overload
3430
+ resolution via a rewritten candidate with reversed order of
3431
+ parameters:
3432
+ - If, for either template, some of the template parameters are not
3433
+ deducible from their function parameters, neither template is more
3434
+ specialized than the other.
3435
+ - If there is either no reordering or more than one reordering of
3436
+ the associated *template-parameter-list* such that
3437
+ - the corresponding *template-parameter*s of the
3438
+ *template-parameter-list*s are equivalent and
3439
+ - the function parameters that positionally correspond between the
3440
+ two templates are of the same type,
3441
+
3442
+ neither template is more specialized than the other.
3443
+ - Otherwise, if the corresponding *template-parameter*s of the
3444
+ *template-parameter-list*s are not equivalent [[temp.over.link]] or
3445
+ if the function parameters that positionally correspond between the
3446
+ two templates are not of the same type, neither template is more
3447
+ specialized than the other.
3448
+ - Otherwise, if the context in which the partial ordering is done is
3449
+ that of a call to a conversion function and the return types of the
3450
+ templates are not the same, then neither template is more specialized
3451
+ than the other.
3452
+ - Otherwise, if one template is more constrained than the other
3453
+ [[temp.constr.order]], the more constrained template is more
3454
+ specialized than the other.
3455
+ - Otherwise, neither template is more specialized than the other.
3456
+
3457
+ [*Example 6*:
3458
+
3459
+ ``` cpp
3460
+ template <typename> constexpr bool True = true;
3461
+ template <typename T> concept C = True<T>;
3462
+
3463
+ void f(C auto &, auto &) = delete;
3464
+ template <C Q> void f(Q &, C auto &);
3465
+
3466
+ void g(struct A *ap, struct B *bp) {
3467
+ f(*ap, *bp); // OK: Can use different methods to produce template parameters
3468
+ }
3469
+
3470
+ template <typename T, typename U> struct X {};
3471
+
3472
+ template <typename T, C U, typename V> bool operator==(X<T, U>, V) = delete;
3473
+ template <C T, C U, C V> bool operator==(T, X<U, V>);
3474
+
3475
+ void h() {
3476
+ X<void *, int>{} == 0; // OK: Correspondence of [T, U, V] and [U, V, T]
3477
+ }
3478
+ ```
3479
+
3480
+ — *end example*]
3481
+
3482
  ### Alias templates <a id="temp.alias">[[temp.alias]]</a>
3483
 
3484
  A *template-declaration* in which the *declaration* is an
3485
+ *alias-declaration* [[dcl.pre]] declares the *identifier* to be an
3486
+ *alias template*. An alias template is a name for a family of types. The
3487
+ name of the alias template is a *template-name*.
3488
 
3489
  When a *template-id* refers to the specialization of an alias template,
3490
  it is equivalent to the associated type obtained by substitution of its
3491
+ *template-argument*s for the *template-parameter*s in the
3492
+ *defining-type-id* of the alias template.
3493
 
3494
  [*Note 1*: An alias template name is never deduced. — *end note*]
3495
 
3496
  [*Example 1*:
3497
 
 
3526
  [*Example 2*:
3527
 
3528
  ``` cpp
3529
  template<typename...> using void_t = void;
3530
  template<typename T> void_t<typename T::foo> f();
3531
+ f<int>(); // error: int does not have a nested type foo
3532
  ```
3533
 
3534
  — *end example*]
3535
 
3536
+ The *defining-type-id* in an alias template declaration shall not refer
3537
+ to the alias template being declared. The type produced by an alias
3538
+ template specialization shall not directly or indirectly make use of
3539
+ that specialization.
3540
 
3541
  [*Example 3*:
3542
 
3543
  ``` cpp
3544
  template <class T> struct A;
 
3549
  B<short> b; // error: instantiation of B<short> uses own type via A<short>::U
3550
  ```
3551
 
3552
  — *end example*]
3553
 
3554
+ The type of a *lambda-expression* appearing in an alias template
3555
+ declaration is different between instantiations of that template, even
3556
+ when the *lambda-expression* is not dependent.
3557
+
3558
+ [*Example 4*:
3559
+
3560
+ ``` cpp
3561
+ template <class T>
3562
+ using A = decltype([] { }); // A<int> and A<char> refer to different closure types
3563
+ ```
3564
+
3565
+ — *end example*]
3566
+
3567
+ ### Concept definitions <a id="temp.concept">[[temp.concept]]</a>
3568
+
3569
+ A *concept* is a template that defines constraints on its template
3570
+ arguments.
3571
+
3572
+ ``` bnf
3573
+ concept-definition:
3574
+ concept concept-name '=' constraint-expression ';'
3575
+ ```
3576
+
3577
+ ``` bnf
3578
+ concept-name:
3579
+ identifier
3580
+ ```
3581
+
3582
+ A *concept-definition* declares a concept. Its *identifier* becomes a
3583
+ *concept-name* referring to that concept within its scope.
3584
+
3585
+ [*Example 1*:
3586
+
3587
+ ``` cpp
3588
+ template<typename T>
3589
+ concept C = requires(T x) {
3590
+ { x == x } -> std::convertible_to<bool>;
3591
+ };
3592
+
3593
+ template<typename T>
3594
+ requires C<T> // C constrains f1(T) in constraint-expression
3595
+ T f1(T x) { return x; }
3596
+
3597
+ template<C T> // C, as a type-constraint, constrains f2(T)
3598
+ T f2(T x) { return x; }
3599
+ ```
3600
+
3601
+ — *end example*]
3602
+
3603
+ A *concept-definition* shall appear at namespace scope
3604
+ [[basic.scope.namespace]].
3605
+
3606
+ A concept shall not have associated constraints [[temp.constr.decl]].
3607
+
3608
+ A concept is not instantiated [[temp.spec]].
3609
+
3610
+ [*Note 1*: A concept-id [[temp.names]] is evaluated as an expression. A
3611
+ concept cannot be explicitly instantiated [[temp.explicit]], explicitly
3612
+ specialized [[temp.expl.spec]], or partially specialized. — *end note*]
3613
+
3614
+ The *constraint-expression* of a *concept-definition* is an unevaluated
3615
+ operand [[expr.context]].
3616
+
3617
+ The first declared template parameter of a concept definition is its
3618
+ *prototype parameter*. A *type concept* is a concept whose prototype
3619
+ parameter is a type *template-parameter*.
3620
+
3621
  ## Name resolution <a id="temp.res">[[temp.res]]</a>
3622
 
3623
  Three kinds of names can be used within a template definition:
3624
 
3625
  - The name of the template itself, and names declared within the
3626
  template itself.
3627
+ - Names dependent on a *template-parameter* [[temp.dep]].
3628
  - Names from scopes which are visible within the template definition.
3629
 
3630
  A name used in a template declaration or definition and that is
3631
  dependent on a *template-parameter* is assumed not to name a type unless
3632
  the applicable name lookup finds a type name or the name is qualified by
 
3648
  Y* a3; // declare pointer to Y<T>
3649
  Z* a4; // declare pointer to Z
3650
  typedef typename T::A TA;
3651
  TA* a5; // declare pointer to T's A
3652
  typename T::A* a6; // declare pointer to T's A
3653
+ T::A* a7; // error: no visible declaration of a7
3654
+ // T::A is not a type name; multiplication of T::A by a7
3655
+ B* a8; // error: no visible declarations of B and a8
3656
+ // B is not a type name; multiplication of B by a8
3657
  }
3658
  };
3659
  ```
3660
 
3661
  — *end example*]
3662
 
 
 
 
 
 
 
 
3663
  ``` bnf
3664
  typename-specifier:
3665
+ typename nested-name-specifier identifier
3666
+ typename nested-name-specifier 'templateₒₚₜ ' simple-template-id
3667
  ```
3668
 
3669
+ A *typename-specifier* denotes the type or class template denoted by the
3670
+ *simple-type-specifier* [[dcl.type.simple]] formed by omitting the
3671
+ keyword `typename`. The usual qualified name lookup
3672
+ [[basic.lookup.qual]] is used to find the *qualified-id* even in the
3673
+ presence of `typename`.
3674
 
3675
  [*Example 2*:
3676
 
3677
  ``` cpp
3678
  struct A {
 
3693
  }
3694
  ```
3695
 
3696
  — *end example*]
3697
 
3698
+ A qualified name used as the name in a *class-or-decltype*
3699
+ [[class.derived]] or an *elaborated-type-specifier* is implicitly
3700
  assumed to name a type, without the use of the `typename` keyword. In a
3701
  *nested-name-specifier* that immediately contains a
3702
  *nested-name-specifier* that depends on a template parameter, the
3703
  *identifier* or *simple-template-id* is implicitly assumed to name a
3704
  type, without the use of the `typename` keyword.
3705
 
3706
  [*Note 1*: The `typename` keyword is not permitted by the syntax of
3707
  these constructs. — *end note*]
3708
 
3709
+ A *qualified-id* is assumed to name a type if
3710
+
3711
+ - it is a qualified name in a type-id-only context (see below), or
3712
+ - it is a *decl-specifier* of the *decl-specifier-seq* of a
3713
+ - *simple-declaration* or a *function-definition* in namespace scope,
3714
+ - *member-declaration*,
3715
+ - *parameter-declaration* in a *member-declaration* [^10], unless that
3716
+ *parameter-declaration* appears in a default argument,
3717
+ - *parameter-declaration* in a *declarator* of a function or function
3718
+ template declaration whose *declarator-id* is qualified, unless that
3719
+ *parameter-declaration* appears in a default argument,
3720
+ - *parameter-declaration* in a *lambda-declarator* or
3721
+ *requirement-parameter-list*, unless that *parameter-declaration*
3722
+ appears in a default argument, or
3723
+ - *parameter-declaration* of a (non-type) *template-parameter*.
3724
+
3725
+ A qualified name is said to be in a *type-id-only context* if it appears
3726
+ in a *type-id*, *new-type-id*, or *defining-type-id* and the smallest
3727
+ enclosing *type-id*, *new-type-id*, or *defining-type-id* is a
3728
+ *new-type-id*, *defining-type-id*, *trailing-return-type*, default
3729
+ argument of a *type-parameter* of a template, or *type-id* of a
3730
+ `static_cast`, `const_cast`, `reinterpret_cast`, or `dynamic_cast`.
3731
 
3732
  [*Example 3*:
3733
 
3734
+ ``` cpp
3735
+ template<class T> T::R f(); // OK, return type of a function declaration at global scope
3736
+ template<class T> void f(T::R); // ill-formed, no diagnostic required: attempt to declare
3737
+ // a void variable template
3738
+ template<class T> struct S {
3739
+ using Ptr = PtrTraits<T>::Ptr; // OK, in a defining-type-id
3740
+ T::R f(T::P p) { // OK, class scope
3741
+ return static_cast<T::R>(p); // OK, type-id of a static_cast
3742
+ }
3743
+ auto g() -> S<T*>::Ptr; // OK, trailing-return-type
3744
+ };
3745
+ template<typename T> void f() {
3746
+ void (*pf)(T::X); // variable pf of type void* initialized with T::X
3747
+ void g(T::X); // error: T::X at block scope does not denote a type
3748
+ // (attempt to declare a void variable)
3749
+ }
3750
+ ```
3751
+
3752
+ — *end example*]
3753
+
3754
+ A *qualified-id* that refers to a member of an unknown specialization,
3755
+ that is not prefixed by `typename`, and that is not otherwise assumed to
3756
+ name a type (see above) denotes a non-type.
3757
+
3758
+ [*Example 4*:
3759
+
3760
  ``` cpp
3761
  template <class T> void f(int i) {
3762
+ T::x * i; // expression, not the declaration of a variable i
3763
  }
3764
 
3765
  struct Foo {
3766
  typedef int x;
3767
  };
 
3778
 
3779
  — *end example*]
3780
 
3781
  Within the definition of a class template or within the definition of a
3782
  member of a class template following the *declarator-id*, the keyword
3783
+ `typename` is not required when referring to a member of the current
3784
+ instantiation [[temp.dep.type]].
 
3785
 
3786
+ [*Example 5*:
 
 
 
 
 
 
 
3787
 
3788
  ``` cpp
3789
  template<class T> struct A {
3790
  typedef int B;
3791
  B b; // OK, no typename required
3792
  };
3793
  ```
3794
 
3795
  — *end example*]
3796
 
3797
+ The validity of a template may be checked prior to any instantiation.
3798
+
3799
+ [*Note 2*: Knowing which names are type names allows the syntax of
3800
+ every template to be checked in this way. — *end note*]
3801
+
3802
+ The program is ill-formed, no diagnostic required, if:
3803
 
3804
  - no valid specialization can be generated for a template or a
3805
+ substatement of a constexpr if statement [[stmt.if]] within a template
3806
+ and the template is not instantiated, or
3807
+ - no substitution of template arguments into a *type-constraint* or
3808
+ *requires-clause* would result in a valid expression, or
3809
  - every valid specialization of a variadic template requires an empty
3810
  template parameter pack, or
3811
  - a hypothetical instantiation of a template immediately following its
3812
  definition would be ill-formed due to a construct that does not depend
3813
  on a template parameter, or
 
3825
  *using-declaration* was a pack expansion and the corresponding pack
3826
  is empty, or
3827
  - an instantiation uses a default argument or default template
3828
  argument that had not been defined at the point at which the
3829
  template was defined, or
3830
+ - constant expression evaluation [[expr.const]] within the template
3831
  instantiation uses
3832
+ - the value of a const object of integral or unscoped enumeration
3833
  type or
3834
  - the value of a `constexpr` object or
3835
  - the value of a reference or
3836
  - the definition of a constexpr function,
3837
 
 
3847
 
3848
  Otherwise, no diagnostic shall be issued for a template for which a
3849
  valid specialization can be generated.
3850
 
3851
  [*Note 4*: If a template is instantiated, errors will be diagnosed
3852
+ according to the other rules in this document. Exactly when these errors
3853
+ are diagnosed is a quality of implementation issue. — *end note*]
 
3854
 
3855
+ [*Example 6*:
3856
 
3857
  ``` cpp
3858
  int j;
3859
  template<class T> class X {
3860
  void f(T t, int i, char* p) {
 
3878
 
3879
  When looking for the declaration of a name used in a template
3880
  definition, the usual lookup rules ([[basic.lookup.unqual]],
3881
  [[basic.lookup.argdep]]) are used for non-dependent names. The lookup of
3882
  names dependent on the template parameters is postponed until the actual
3883
+ template argument is known [[temp.dep]].
3884
 
3885
+ [*Example 7*:
3886
 
3887
  ``` cpp
3888
  #include <iostream>
3889
  using namespace std;
3890
 
 
3899
  cout << p[i] << '\n';
3900
  }
3901
  };
3902
  ```
3903
 
3904
+ In the example, `i` is the local variable `i` declared in `printall`,
3905
  `cnt` is the member `cnt` declared in `Set`, and `cout` is the standard
3906
  output stream declared in `iostream`. However, not every declaration can
3907
  be found this way; the resolution of some names must be postponed until
3908
  the actual *template-argument*s are known. For example, even though the
3909
  name `operator<<` is known within the definition of `printall()` and a
3910
  declaration of it can be found in `<iostream>`, the actual declaration
3911
  of `operator<<` needed to print `p[i]` cannot be known until it is known
3912
+ what type `T` is [[temp.dep]].
3913
 
3914
  — *end example*]
3915
 
3916
  If a name does not depend on a *template-parameter* (as defined in 
3917
  [[temp.dep]]), a declaration (or set of declarations) for that name
3918
  shall be in scope at the point where the name appears in the template
3919
  definition; the name is bound to the declaration (or declarations) found
3920
  at that point and this binding is not affected by declarations that are
3921
  visible at the point of instantiation.
3922
 
3923
+ [*Example 8*:
3924
 
3925
  ``` cpp
3926
  void f(char);
3927
 
3928
  template<class T> void g(T t) {
 
3945
  — *end example*]
3946
 
3947
  [*Note 5*: For purposes of name lookup, default arguments and
3948
  *noexcept-specifier*s of function templates and default arguments and
3949
  *noexcept-specifier*s of member functions of class templates are
3950
+ considered definitions [[temp.decls]]. — *end note*]
3951
 
3952
  ### Locally declared names <a id="temp.local">[[temp.local]]</a>
3953
 
3954
  Like normal (non-template) classes, class templates have an
3955
+ injected-class-name [[class.pre]]. The injected-class-name can be used
3956
+ as a *template-name* or a *type-name*. When it is used with a
3957
  *template-argument-list*, as a *template-argument* for a template
3958
  *template-parameter*, or as the final identifier in the
3959
  *elaborated-type-specifier* of a friend class template declaration, it
3960
+ is a *template-name* that refers to the class template itself.
3961
+ Otherwise, it is a *type-name* equivalent to the *template-name*
3962
+ followed by the *template-parameter*s of the class template enclosed in
3963
+ `<>`.
3964
 
3965
  Within the scope of a class template specialization or partial
3966
  specialization, when the injected-class-name is used as a *type-name*,
3967
  it is equivalent to the *template-name* followed by the
3968
  *template-argument*s of the class template specialization or partial
 
3984
  ```
3985
 
3986
  — *end example*]
3987
 
3988
  The injected-class-name of a class template or class template
3989
+ specialization can be used as either a *template-name* or a *type-name*
3990
  wherever it is in scope.
3991
 
3992
  [*Example 2*:
3993
 
3994
  ``` cpp
 
3999
  template <class T> struct Derived: public Base<T> {
4000
  typename Derived::Base* p; // meaning Derived::Base<T>
4001
  };
4002
 
4003
  template<class T, template<class> class U = T::template Base> struct Third { };
4004
+ Third<Derived<int> > t; // OK: default argument uses injected-class-name as a template
4005
  ```
4006
 
4007
  — *end example*]
4008
 
4009
+ A lookup that finds an injected-class-name [[class.member.lookup]] can
4010
+ result in an ambiguity in certain cases (for example, if it is found in
4011
+ more than one base class). If all of the injected-class-names that are
4012
+ found refer to specializations of the same class template, and if the
4013
+ name is used as a *template-name*, the reference refers to the class
4014
  template itself and not a specialization thereof, and is not ambiguous.
4015
 
4016
  [*Example 3*:
4017
 
4018
  ``` cpp
 
4041
  };
4042
  ```
4043
 
4044
  — *end example*]
4045
 
4046
+ The name of a *template-parameter* shall not be redeclared within its
4047
+ scope (including nested scopes). A *template-parameter* shall not have
4048
+ the same name as the template name.
4049
 
4050
  [*Example 5*:
4051
 
4052
  ``` cpp
4053
  template<class T, int i> class Y {
 
4110
 
4111
  — *end example*]
4112
 
4113
  In the definition of a class template or in the definition of a member
4114
  of such a template that appears outside of the template definition, for
4115
+ each non-dependent base class [[temp.dep.type]], if the name of the base
4116
+ class or the name of a member of the base class is the same as the name
4117
+ of a *template-parameter*, the base class name or member name hides the
4118
+ *template-parameter* name [[basic.scope.hiding]].
4119
 
4120
  [*Example 8*:
4121
 
4122
  ``` cpp
4123
  struct A {
 
4139
  Inside a template, some constructs have semantics which may differ from
4140
  one instantiation to another. Such a construct *depends* on the template
4141
  parameters. In particular, types and expressions may depend on the type
4142
  and/or value of template parameters (as determined by the template
4143
  arguments) and this determines the context for name lookup for certain
4144
+ names. An expression may be *type-dependent* (that is, its type may
4145
  depend on a template parameter) or *value-dependent* (that is, its value
4146
+ when evaluated as a constant expression [[expr.const]] may depend on a
4147
+ template parameter) as described in this subclause.
4148
+
4149
+ In an expression of the form:
4150
+
4151
+ ``` bnf
4152
+ postfix-expression '(' expression-listₒₚₜ ')'
4153
+ ```
4154
 
4155
  where the *postfix-expression* is an *unqualified-id*, the
4156
  *unqualified-id* denotes a *dependent name* if
4157
 
4158
+ - any of the expressions in the *expression-list* is a pack expansion
4159
+ [[temp.variadic]],
4160
  - any of the expressions or *braced-init-list*s in the *expression-list*
4161
+ is type-dependent [[temp.dep.expr]], or
4162
  - the *unqualified-id* is a *template-id* in which any of the template
4163
  arguments depends on a template parameter.
4164
 
4165
  If an operand of an operator is a type-dependent expression, the
4166
+ operator also denotes a dependent name.
4167
+
4168
+ [*Note 1*: Such names are unbound and are looked up at the point of the
4169
+ template instantiation [[temp.point]] in both the context of the
4170
+ template definition and the context of the point of instantiation
4171
+ [[temp.dep.candidate]]. — *end note*]
4172
 
4173
  [*Example 1*:
4174
 
4175
  ``` cpp
4176
  template<class T> struct X : B<T> {
 
4180
  pb->j++;
4181
  }
4182
  };
4183
  ```
4184
 
4185
+ The base class name `B<T>`, the type name `T::A`, the names `B<T>::i`
4186
  and `pb->j` explicitly depend on the *template-parameter*.
4187
 
4188
  — *end example*]
4189
 
4190
  In the definition of a class or class template, the scope of a dependent
4191
+ base class [[temp.dep.type]] is not examined during unqualified name
4192
  lookup either at the point of definition of the class template or member
4193
  or during an instantiation of the class template or member.
4194
 
4195
  [*Example 2*:
4196
 
 
4240
 
4241
  A name refers to the *current instantiation* if it is
4242
 
4243
  - in the definition of a class template, a nested class of a class
4244
  template, a member of a class template, or a member of a nested class
4245
+ of a class template, the injected-class-name [[class.pre]] of the
4246
+ class template or nested class,
4247
  - in the definition of a primary class template or a member of a primary
4248
  class template, the name of the class template followed by the
4249
  template argument list of the primary template (as described below)
4250
  enclosed in `<>` (or an equivalent template alias specialization),
4251
  - in the definition of a nested class of a class template, the name of
4252
  the nested class referenced as a member of the current instantiation,
4253
  or
4254
  - in the definition of a partial specialization or a member of a partial
4255
  specialization, the name of the class template followed by the
4256
  template argument list of the partial specialization enclosed in `<>`
4257
+ (or an equivalent template alias specialization). If the nᵗʰ template
4258
+ parameter is a template parameter pack, the nᵗʰ template argument is a
4259
+ pack expansion [[temp.variadic]] whose pattern is the name of the
4260
+ template parameter pack.
4261
 
4262
  The template argument list of a primary template is a template argument
4263
+ list in which the nᵗʰ template argument has the value of the nᵗʰ
4264
+ template parameter of the class template. If the nᵗʰ template parameter
4265
+ is a template parameter pack [[temp.variadic]], the nᵗʰ template
4266
+ argument is a pack expansion [[temp.variadic]] whose pattern is the name
4267
+ of the template parameter pack.
4268
 
4269
+ A template argument that is equivalent to a template parameter can be
4270
+ used in place of that template parameter in a reference to the current
4271
+ instantiation. For a template *type-parameter*, a template argument is
4272
+ equivalent to a template parameter if it denotes the same type. For a
4273
+ non-type template parameter, a template argument is equivalent to a
4274
+ template parameter if it is an *identifier* that names a variable that
4275
+ is equivalent to the template parameter. A variable is equivalent to a
4276
+ template parameter if
4277
+
4278
+ - it has the same type as the template parameter (ignoring
4279
+ cv-qualification) and
4280
+ - its initializer consists of a single *identifier* that names the
4281
+ template parameter or, recursively, such a variable.
4282
+
4283
+ [*Note 1*: Using a parenthesized variable name breaks the
4284
+ equivalence. — *end note*]
4285
 
4286
  [*Example 1*:
4287
 
4288
  ``` cpp
4289
  template <class T> class A {
 
4308
  B<T2, T1, I>* b2; // not the current instantiation
4309
  typedef T1 my_T1;
4310
  static const int my_I = I;
4311
  static const int my_I2 = I+0;
4312
  static const int my_I3 = my_I;
4313
+ static const long my_I4 = I;
4314
+ static const int my_I5 = (I);
4315
  B<my_T1, T2, my_I>* b3; // refers to the current instantiation
4316
  B<my_T1, T2, my_I2>* b4; // not the current instantiation
4317
  B<my_T1, T2, my_I3>* b5; // refers to the current instantiation
4318
+ B<my_T1, T2, my_I4>* b6; // not the current instantiation
4319
+ B<my_T1, T2, my_I5>* b7; // not the current instantiation
4320
  };
4321
  ```
4322
 
4323
  — *end example*]
4324
 
4325
  A *dependent base class* is a base class that is a dependent type and is
4326
  not the current instantiation.
4327
 
4328
+ [*Note 2*:
4329
 
4330
  A base class can be the current instantiation in the case of a nested
4331
  class naming an enclosing class as a base.
4332
 
4333
  [*Example 2*:
 
4352
 
4353
  A name is a *member of the current instantiation* if it is
4354
 
4355
  - An unqualified name that, when looked up, refers to at least one
4356
  member of a class that is the current instantiation or a non-dependent
4357
+ base class thereof. \[*Note 3*: This can only occur when looking up a
4358
  name in a scope enclosed by the definition of a class
4359
  template. — *end note*]
4360
  - A *qualified-id* in which the *nested-name-specifier* refers to the
4361
  current instantiation and that, when looked up, refers to at least one
4362
  member of a class that is the current instantiation or a non-dependent
4363
+ base class thereof. \[*Note 4*: If no such member is found, and the
4364
  current instantiation has any dependent base classes, then the
4365
  *qualified-id* is a member of an unknown specialization; see
4366
  below. — *end note*]
4367
  - An *id-expression* denoting the member in a class member access
4368
+ expression [[expr.ref]] for which the type of the object expression is
4369
+ the current instantiation, and the *id-expression*, when looked up
4370
+ [[basic.lookup.classref]], refers to at least one member of a class
4371
+ that is the current instantiation or a non-dependent base class
4372
+ thereof. \[*Note 5*: If no such member is found, and the current
4373
  instantiation has any dependent base classes, then the *id-expression*
4374
  is a member of an unknown specialization; see below. — *end note*]
4375
 
4376
  [*Example 3*:
4377
 
 
4403
  current instantiation, the current instantiation has at least one
4404
  dependent base class, and name lookup of the *qualified-id* does not
4405
  find any member of a class that is the current instantiation or a
4406
  non-dependent base class thereof.
4407
  - An *id-expression* denoting the member in a class member access
4408
+ expression [[expr.ref]] in which either
4409
  - the type of the object expression is the current instantiation, the
4410
  current instantiation has at least one dependent base class, and
4411
  name lookup of the *id-expression* does not find a member of a class
4412
  that is the current instantiation or a non-dependent base class
4413
  thereof; or
4414
+ - the type of the object expression is not the current instantiation
4415
+ and the object expression is type-dependent.
4416
 
4417
  If a *qualified-id* in which the *nested-name-specifier* refers to the
4418
  current instantiation is not a member of the current instantiation or a
4419
  member of an unknown specialization, the program is ill-formed even if
4420
  the template containing the *qualified-id* is not instantiated; no
 
4481
  - a cv-qualified type where the cv-unqualified type is dependent,
4482
  - a compound type constructed from any dependent type,
4483
  - an array type whose element type is dependent or whose bound (if any)
4484
  is value-dependent,
4485
  - a function type whose exception specification is value-dependent,
4486
+ - denoted by a *simple-template-id* in which either the template name is
4487
+ a template parameter or any of the template arguments is a dependent
4488
+ type or an expression that is type-dependent or value-dependent or is
4489
+ a pack expansion \[*Note 6*: This includes an injected-class-name
4490
+ [[class.pre]] of a class template used without a
4491
  *template-argument-list*. — *end note*] , or
4492
  - denoted by `decltype(`*expression*`)`, where *expression* is
4493
+ type-dependent [[temp.dep.expr]].
4494
 
4495
+ [*Note 7*: Because typedefs do not introduce new types, but instead
4496
  simply refer to other types, a name that refers to a typedef that is a
4497
  member of the current instantiation is dependent only if the type
4498
  referred to is dependent. — *end note*]
4499
 
4500
  #### Type-dependent expressions <a id="temp.dep.expr">[[temp.dep.expr]]</a>
 
4503
  subexpression is type-dependent.
4504
 
4505
  `this`
4506
 
4507
  is type-dependent if the class type of the enclosing member function is
4508
+ dependent [[temp.dep.type]].
4509
 
4510
+ An *id-expression* is type-dependent if it is not a concept-id and it
4511
+ contains
4512
 
4513
  - an *identifier* associated by name lookup with one or more
4514
  declarations declared with a dependent type,
4515
  - an *identifier* associated by name lookup with a non-type
4516
  *template-parameter* declared with a type that contains a placeholder
4517
+ type [[dcl.spec.auto]],
4518
+ - an *identifier* associated by name lookup with a variable declared
4519
+ with a type that contains a placeholder type [[dcl.spec.auto]] where
4520
+ the initializer is type-dependent,
4521
  - an *identifier* associated by name lookup with one or more
4522
  declarations of member functions of the current instantiation declared
4523
  with a return type that contains a placeholder type,
4524
  - an *identifier* associated by name lookup with a structured binding
4525
+ declaration [[dcl.struct.bind]] whose *brace-or-equal-initializer* is
4526
+ type-dependent,
4527
+ - the *identifier* `__func__` [[dcl.fct.def.general]], where any
4528
  enclosing function is a template, a member of a class template, or a
4529
  generic lambda,
4530
  - a *template-id* that is dependent,
4531
  - a *conversion-function-id* that specifies a dependent type, or
4532
  - a *nested-name-specifier* or a *qualified-id* that names a member of
4533
  an unknown specialization;
4534
 
4535
  or if it names a dependent member of the current instantiation that is a
4536
+ static data member of type “array of unknown bound of `T`” for some `T`
4537
+ [[temp.static]]. Expressions of the following forms are type-dependent
4538
+ only if the type specified by the *type-id*, *simple-type-specifier* or
4539
+ *new-type-id* is dependent, even if any subexpression is type-dependent:
4540
+
4541
+ ``` bnf
4542
+ simple-type-specifier '(' expression-listₒₚₜ ')'
4543
+ '::'ₒₚₜ new new-placementₒₚₜ new-type-id new-initializerₒₚₜ
4544
+ '::'ₒₚₜ new new-placementₒₚₜ '(' type-id ')' new-initializerₒₚₜ
4545
+ dynamic_cast '<' type-id '>' '(' expression ')'
4546
+ static_cast '<' type-id '>' '(' expression ')'
4547
+ const_cast '<' type-id '>' '(' expression ')'
4548
+ reinterpret_cast '<' type-id '>' '(' expression ')'
4549
+ '(' type-id ')' cast-expression
4550
+ ```
4551
 
4552
  Expressions of the following forms are never type-dependent (because the
4553
  type of the expression cannot be dependent):
4554
 
4555
+ ``` bnf
4556
+ literal
4557
+ sizeof unary-expression
4558
+ sizeof '(' type-id ')'
4559
+ sizeof '...' '(' identifier ')'
4560
+ alignof '(' type-id ')'
4561
+ typeid '(' expression ')'
4562
+ typeid '(' type-id ')'
4563
+ '::'ₒₚₜ delete cast-expression
4564
+ '::'ₒₚₜ delete '[' ']' cast-expression
4565
+ throw assignment-expressionₒₚₜ
4566
+ noexcept '(' expression ')'
4567
+ ```
4568
+
4569
  [*Note 1*: For the standard library macro `offsetof`, see 
4570
  [[support.types]]. — *end note*]
4571
 
4572
+ A class member access expression [[expr.ref]] is type-dependent if the
4573
+ expression refers to a member of the current instantiation and the type
4574
+ of the referenced member is dependent, or the class member access
4575
  expression refers to a member of an unknown specialization.
4576
 
4577
  [*Note 2*: In an expression of the form `x.y` or `xp->y` the type of
4578
  the expression is usually the type of the member `y` of the class of `x`
4579
  (or the class pointed to by `xp`). However, if `x` or `xp` refers to a
 
4593
  constant expression is required is value-dependent if any subexpression
4594
  is value-dependent.
4595
 
4596
  An *id-expression* is value-dependent if:
4597
 
4598
+ - it is a concept-id and any of its arguments are dependent,
4599
  - it is type-dependent,
4600
  - it is the name of a non-type template parameter,
4601
  - it names a static data member that is a dependent member of the
4602
  current instantiation and is not initialized in a *member-declarator*,
4603
  - it names a static member function that is a dependent member of the
4604
  current instantiation, or
4605
+ - it names a potentially-constant variable [[expr.const]] that is
4606
+ initialized with an expression that is value-dependent.
4607
 
4608
  Expressions of the following form are value-dependent if the
4609
  *unary-expression* or *expression* is type-dependent or the *type-id* is
4610
  dependent:
4611
 
4612
+ ``` bnf
4613
+ sizeof unary-expression
4614
+ sizeof '(' type-id ')'
4615
+ typeid '(' expression ')'
4616
+ typeid '(' type-id ')'
4617
+ alignof '(' type-id ')'
4618
+ noexcept '(' expression ')'
4619
+ ```
4620
+
4621
  [*Note 1*: For the standard library macro `offsetof`, see 
4622
  [[support.types]]. — *end note*]
4623
 
4624
  Expressions of the following form are value-dependent if either the
4625
  *type-id* or *simple-type-specifier* is dependent or the *expression* or
4626
  *cast-expression* is value-dependent:
4627
 
4628
+ ``` bnf
4629
+ simple-type-specifier '(' expression-listₒₚₜ ')'
4630
+ static_cast '<' type-id '>' '(' expression ')'
4631
+ const_cast '<' type-id '>' '(' expression ')'
4632
+ reinterpret_cast '<' type-id '>' '(' expression ')'
4633
+ '(' type-id ')' cast-expression
4634
+ ```
4635
+
4636
  Expressions of the following form are value-dependent:
4637
 
4638
+ ``` bnf
4639
+ sizeof '...' '(' identifier ')'
4640
+ fold-expression
4641
+ ```
4642
+
4643
  An expression of the form `&`*qualified-id* where the *qualified-id*
4644
  names a dependent member of the current instantiation is
4645
  value-dependent. An expression of the form `&`*cast-expression* is also
4646
  value-dependent if evaluating *cast-expression* as a core constant
4647
+ expression [[expr.const]] succeeds and the result of the evaluation
4648
  refers to a templated entity that is an object with static or thread
4649
  storage duration or a member function.
4650
 
4651
  #### Dependent template arguments <a id="temp.dep.temp">[[temp.dep.temp]]</a>
4652
 
 
4690
 
4691
  — *end example*]
4692
 
4693
  ### Dependent name resolution <a id="temp.dep.res">[[temp.dep.res]]</a>
4694
 
 
 
 
 
 
 
 
 
 
4695
  #### Point of instantiation <a id="temp.point">[[temp.point]]</a>
4696
 
4697
  For a function template specialization, a member function template
4698
  specialization, or a specialization for a member function or static data
4699
  member of a class template, if the specialization is implicitly
 
4739
 
4740
  An explicit instantiation definition is an instantiation point for the
4741
  specialization or specializations specified by the explicit
4742
  instantiation.
4743
 
 
 
 
 
 
4744
  A specialization for a function template, a member function template, or
4745
  of a member function or static data member of a class template may have
4746
  multiple points of instantiations within a translation unit, and in
4747
+ addition to the points of instantiation described above,
4748
+
4749
+ - for any such specialization that has a point of instantiation within
4750
+ the *declaration-seq* of the *translation-unit*, prior to the
4751
+ *private-module-fragment* (if any), the point after the
4752
+ *declaration-seq* of the *translation-unit* is also considered a point
4753
+ of instantiation, and
4754
+ - for any such specialization that has a point of instantiation within
4755
+ the *private-module-fragment*, the end of the translation unit is also
4756
+ considered a point of instantiation.
4757
+
4758
+ A specialization for a class template has at most one point of
4759
+ instantiation within a translation unit. A specialization for any
4760
+ template may have points of instantiation in multiple translation units.
4761
+ If two different points of instantiation give a template specialization
4762
+ different meanings according to the one-definition rule
4763
+ [[basic.def.odr]], the program is ill-formed, no diagnostic required.
4764
 
4765
  #### Candidate functions <a id="temp.dep.candidate">[[temp.dep.candidate]]</a>
4766
 
4767
  For a function call where the *postfix-expression* is a dependent name,
4768
+ the candidate functions are found using the usual lookup rules from the
4769
+ template definition context ([[basic.lookup.unqual]],
4770
+ [[basic.lookup.argdep]]).
4771
 
4772
+ [*Note 1*: For the part of the lookup using associated namespaces
4773
+ [[basic.lookup.argdep]], function declarations found in the template
4774
+ instantiation context are found by this lookup, as described in
4775
+ [[basic.lookup.argdep]]. *end note*]
 
 
 
4776
 
4777
  If the call would be ill-formed or would find a better match had the
4778
  lookup within the associated namespaces considered all the function
4779
  declarations with external linkage introduced in those namespaces in all
4780
  translation units, not just considering those declarations found in the
4781
  template definition and template instantiation contexts, then the
4782
  program has undefined behavior.
4783
 
4784
+ [*Example 1*:
4785
+
4786
+ Source file \`"X.h"\`
4787
+
4788
+ ``` cpp
4789
+ namespace Q {
4790
+ struct X { };
4791
+ }
4792
+ ```
4793
+
4794
+ Source file \`"G.h"\`
4795
+
4796
+ ``` cpp
4797
+ namespace Q {
4798
+ void g_impl(X, X);
4799
+ }
4800
+ ```
4801
+
4802
+ Module interface unit of \`M1\`
4803
+
4804
+ ``` cpp
4805
+ module;
4806
+ #include "X.h"
4807
+ #include "G.h"
4808
+ export module M1;
4809
+ export template<typename T>
4810
+ void g(T t) {
4811
+ g_impl(t, Q::X{ }); // ADL in definition context finds Q::g_impl, g_impl not discarded
4812
+ }
4813
+ ```
4814
+
4815
+ Module interface unit of \`M2\`
4816
+
4817
+ ``` cpp
4818
+ module;
4819
+ #include "X.h"
4820
+ export module M2;
4821
+ import M1;
4822
+ void h(Q::X x) {
4823
+ g(x); // OK
4824
+ }
4825
+ ```
4826
+
4827
+ — *end example*]
4828
+
4829
+ [*Example 2*:
4830
+
4831
+ Module interface unit of \`Std\`
4832
+
4833
+ ``` cpp
4834
+ export module Std;
4835
+ export template<typename Iter>
4836
+ void indirect_swap(Iter lhs, Iter rhs)
4837
+ {
4838
+ swap(*lhs, *rhs); // swap not found by unqualified lookup, can be found only via ADL
4839
+ }
4840
+ ```
4841
+
4842
+ Module interface unit of \`M\`
4843
+
4844
+ ``` cpp
4845
+ export module M;
4846
+ import Std;
4847
+
4848
+ struct S { /* ...*/ };
4849
+ void swap(S&, S&); // #1
4850
+
4851
+ void f(S* p, S* q)
4852
+ {
4853
+ indirect_swap(p, q); // finds #1 via ADL in instantiation context
4854
+ }
4855
+ ```
4856
+
4857
+ — *end example*]
4858
+
4859
+ [*Example 3*:
4860
+
4861
+ Source file \`"X.h"\`
4862
+
4863
+ ``` cpp
4864
+ struct X { /* ... */ };
4865
+ X operator+(X, X);
4866
+ ```
4867
+
4868
+ Module interface unit of \`F\`
4869
+
4870
+ ``` cpp
4871
+ export module F;
4872
+ export template<typename T>
4873
+ void f(T t) {
4874
+ t + t;
4875
+ }
4876
+ ```
4877
+
4878
+ Module interface unit of \`M\`
4879
+
4880
+ ``` cpp
4881
+ module;
4882
+ #include "X.h"
4883
+ export module M;
4884
+ import F;
4885
+ void g(X x) {
4886
+ f(x); // OK: instantiates f from F,
4887
+ // operator+ is visible in instantiation context
4888
+ }
4889
+ ```
4890
+
4891
+ — *end example*]
4892
+
4893
+ [*Example 4*:
4894
+
4895
+ Module interface unit of \`A\`
4896
+
4897
+ ``` cpp
4898
+ export module A;
4899
+ export template<typename T>
4900
+ void f(T t) {
4901
+ cat(t, t); // #1
4902
+ dog(t, t); // #2
4903
+ }
4904
+ ```
4905
+
4906
+ Module interface unit of \`B\`
4907
+
4908
+ ``` cpp
4909
+ export module B;
4910
+ import A;
4911
+ export template<typename T, typename U>
4912
+ void g(T t, U u) {
4913
+ f(t);
4914
+ }
4915
+ ```
4916
+
4917
+ Source file \`"foo.h"\`, not an importable header
4918
+
4919
+ ``` cpp
4920
+ struct foo {
4921
+ friend int cat(foo, foo);
4922
+ };
4923
+ int dog(foo, foo);
4924
+ ```
4925
+
4926
+ Module interface unit of \`C1\`
4927
+
4928
+ ``` cpp
4929
+ module;
4930
+ #include "foo.h" // dog not referenced, discarded
4931
+ export module C1;
4932
+ import B;
4933
+ export template<typename T>
4934
+ void h(T t) {
4935
+ g(foo{ }, t);
4936
+ }
4937
+ ```
4938
+
4939
+ Translation unit
4940
+
4941
+ ``` cpp
4942
+ import C1;
4943
+ void i() {
4944
+ h(0); // error: dog not found at #2
4945
+ }
4946
+ ```
4947
+
4948
+ Importable header \`"bar.h"\`
4949
+
4950
+ ``` cpp
4951
+ struct bar {
4952
+ friend int cat(bar, bar);
4953
+ };
4954
+ int dog(bar, bar);
4955
+ ```
4956
+
4957
+ Module interface unit of \`C2\`
4958
+
4959
+ ``` cpp
4960
+ module;
4961
+ #include "bar.h" // imports header unit "bar.h"
4962
+ export module C2;
4963
+ import B;
4964
+ export template<typename T>
4965
+ void j(T t) {
4966
+ g(bar{ }, t);
4967
+ }
4968
+ ```
4969
+
4970
+ Translation unit
4971
+
4972
+ ``` cpp
4973
+ import C2;
4974
+ void k() {
4975
+ j(0); // OK, dog found in instantiation context:
4976
+ // visible at end of module interface unit of C2
4977
+ }
4978
+ ```
4979
+
4980
+ — *end example*]
4981
+
4982
  ### Friend names declared within a class template <a id="temp.inject">[[temp.inject]]</a>
4983
 
4984
  Friend classes or functions can be declared within a class template.
4985
  When a template is instantiated, the names of its friends are treated as
4986
  if the specialization had been explicitly declared at its point of
4987
  instantiation.
4988
 
4989
  As with non-template classes, the names of namespace-scope friend
4990
  functions of a class template specialization are not visible during an
4991
+ ordinary lookup unless explicitly declared at namespace scope
4992
+ [[class.friend]]. Such names may be found under the rules for associated
4993
+ classes [[basic.lookup.argdep]].[^11]
4994
 
4995
  [*Example 1*:
4996
 
4997
  ``` cpp
4998
  template<typename T> struct number {
 
5002
 
5003
  void g() {
5004
  number<double> a(3), b(4);
5005
  a = gcd(a,b); // finds gcd because number<double> is an associated class,
5006
  // making gcd visible in its namespace (global scope)
5007
+ b = gcd(3,4); // error: gcd is not visible
5008
  }
5009
  ```
5010
 
5011
  — *end example*]
5012
 
5013
  ## Template instantiation and specialization <a id="temp.spec">[[temp.spec]]</a>
5014
 
5015
+ The act of instantiating a function, a variable, a class, a member of a
5016
+ class template, or a member template is referred to as *template
5017
  instantiation*.
5018
 
5019
  A function instantiated from a function template is called an
5020
  instantiated function. A class instantiated from a class template is
5021
  called an instantiated class. A member function, a member class, a
 
5023
  instantiated from the member definition of the class template is called,
5024
  respectively, an instantiated member function, member class, member
5025
  enumeration, or static data member. A member function instantiated from
5026
  a member function template is called an instantiated member function. A
5027
  member class instantiated from a member class template is called an
5028
+ instantiated member class. A variable instantiated from a variable
5029
+ template is called an instantiated variable. A static data member
5030
+ instantiated from a static data member template is called an
5031
+ instantiated static data member.
5032
 
5033
  An explicit specialization may be declared for a function template, a
5034
+ variable template, a class template, a member of a class template, or a
5035
+ member template. An explicit specialization declaration is introduced by
5036
+ `template<>`. In an explicit specialization declaration for a variable
5037
+ template, a class template, a member of a class template or a class
5038
+ member template, the name of the variable or class that is explicitly
5039
+ specialized shall be a *simple-template-id*. In the explicit
5040
  specialization declaration for a function template or a member function
5041
  template, the name of the function or member function explicitly
5042
  specialized may be a *template-id*.
5043
 
5044
  [*Example 1*:
 
5063
  ```
5064
 
5065
  — *end example*]
5066
 
5067
  An instantiated template specialization can be either implicitly
5068
+ instantiated [[temp.inst]] for a given argument list or be explicitly
5069
+ instantiated [[temp.explicit]]. A *specialization* is a class, variable,
5070
+ function, or class member that is either instantiated [[temp.inst]] from
5071
+ a templated entity or is an explicit specialization [[temp.expl.spec]]
5072
+ of a templated entity.
5073
 
5074
  For a given template and a given set of *template-argument*s,
5075
 
5076
  - an explicit instantiation definition shall appear at most once in a
5077
  program,
5078
+ - an explicit specialization shall be defined at most once in a program,
5079
+ as specified in [[basic.def.odr]], and
5080
  - both an explicit instantiation and a declaration of an explicit
5081
  specialization shall not appear in a program unless the explicit
5082
  instantiation follows a declaration of the explicit specialization.
5083
 
5084
  An implementation is not required to diagnose a violation of this rule.
5085
 
5086
+ The usual access checking rules do not apply to names in a declaration
5087
+ of an explicit instantiation or explicit specialization, with the
5088
+ exception of names appearing in a function body, default argument,
5089
+ base-clause, member-specification, enumerator-list, or static data
5090
+ member or variable template initializer.
5091
+
5092
+ [*Note 1*: In particular, the template arguments and names used in the
5093
+ function declarator (including parameter types, return types and
5094
+ exception specifications) may be private types or objects that would
5095
+ normally not be accessible. — *end note*]
5096
+
5097
  Each class template specialization instantiated from a template has its
5098
  own copy of any static members.
5099
 
5100
  [*Example 2*:
5101
 
 
5114
  `s` of type `char*`.
5115
 
5116
  — *end example*]
5117
 
5118
  If a function declaration acquired its function type through a dependent
5119
+ type [[temp.dep.type]] without using the syntactic form of a function
5120
  declarator, the program is ill-formed.
5121
 
5122
  [*Example 3*:
5123
 
5124
  ``` cpp
5125
  template<class T> struct A {
5126
  static T t;
5127
  };
5128
  typedef int function();
5129
+ A<function> a; // error: would declare A<function>::t as a static member function
5130
  ```
5131
 
5132
  — *end example*]
5133
 
5134
  ### Implicit instantiation <a id="temp.inst">[[temp.inst]]</a>
5135
 
5136
+ A template specialization E is a *declared specialization* if there is a
5137
+ reachable explicit instantiation definition [[temp.explicit]] or
5138
+ explicit specialization declaration [[temp.expl.spec]] for E, or if
5139
+ there is a reachable explicit instantiation declaration for E and E is
5140
+ not
5141
+
5142
+ - an inline function,
5143
+ - declared with a type deduced from its initializer or return value
5144
+ [[dcl.spec.auto]],
5145
+ - a potentially-constant variable [[expr.const]], or
5146
+ - a specialization of a templated class.
5147
+
5148
+ [*Note 1*: An implicit instantiation in an importing translation unit
5149
+ cannot use names with internal linkage from an imported translation unit
5150
+ [[basic.link]]. — *end note*]
5151
+
5152
+ Unless a class template specialization is a declared specialization, the
5153
+ class template specialization is implicitly instantiated when the
5154
+ specialization is referenced in a context that requires a
5155
+ completely-defined object type or when the completeness of the class
5156
+ type affects the semantics of the program.
5157
+
5158
+ [*Note 2*: In particular, if the semantics of an expression depend on
5159
  the member or base class lists of a class template specialization, the
5160
  class template specialization is implicitly generated. For instance,
5161
  deleting a pointer to class type depends on whether or not the class
5162
  declares a destructor, and a conversion between pointers to class type
5163
  depends on the inheritance relationship between the two classes
 
5180
  ```
5181
 
5182
  — *end example*]
5183
 
5184
  If a class template has been declared, but not defined, at the point of
5185
+ instantiation [[temp.point]], the instantiation yields an incomplete
5186
+ class type [[basic.types]].
5187
 
5188
  [*Example 2*:
5189
 
5190
  ``` cpp
5191
  template<class T> class X;
5192
  X<char> ch; // error: incomplete type X<char>
5193
  ```
5194
 
5195
  — *end example*]
5196
 
5197
+ [*Note 3*: Within a template declaration, a local class [[class.local]]
5198
+ or enumeration and the members of a local class are never considered to
5199
+ be entities that can be separately instantiated (this includes their
5200
+ default arguments, *noexcept-specifier*s, and non-static data member
5201
+ initializers, if any, but not their *type-constraint*s or
5202
+ *requires-clause*s). As a result, the dependent names are looked up, the
5203
+ semantic constraints are checked, and any templates used are
5204
+ instantiated as part of the instantiation of the entity within which the
5205
+ local class or enumeration is declared. — *end note*]
5206
 
5207
+ The implicit instantiation of a class template specialization causes
5208
+
5209
+ - the implicit instantiation of the declarations, but not of the
5210
+ definitions, of the non-deleted class member functions, member
5211
+ classes, scoped member enumerations, static data members, member
5212
+ templates, and friends; and
5213
+ - the implicit instantiation of the definitions of deleted member
5214
+ functions, unscoped member enumerations, and member anonymous unions.
5215
+
5216
+ The implicit instantiation of a class template specialization does not
5217
+ cause the implicit instantiation of default arguments or
5218
+ *noexcept-specifier*s of the class member functions.
5219
+
5220
+ [*Example 3*:
5221
+
5222
+ ``` cpp
5223
+ template<class T>
5224
+ struct C {
5225
+ void f() { T x; }
5226
+ void g() = delete;
5227
+ };
5228
+ C<void> c; // OK, definition of C<void>::f is not instantiated at this point
5229
+ template<> void C<int>::g() { } // error: redefinition of C<int>::g
5230
+ ```
5231
+
5232
+ — *end example*]
5233
+
5234
+ However, for the purpose of determining whether an instantiated
5235
+ redeclaration is valid according to  [[basic.def.odr]] and
5236
+ [[class.mem]], a declaration that corresponds to a definition in the
5237
  template is considered to be a definition.
5238
 
5239
+ [*Example 4*:
5240
 
5241
  ``` cpp
5242
  template<class T, class U>
5243
  struct Outer {
5244
  template<class X, class Y> struct Inner;
 
5259
  ``` cpp
5260
  template<typename T> struct Friendly {
5261
  template<typename U> friend int f(U) { return sizeof(T); }
5262
  };
5263
  Friendly<char> fc;
5264
+ Friendly<float> ff; // error: produces second definition of f(U)
5265
  ```
5266
 
5267
  — *end example*]
5268
 
5269
+ Unless a member of a class template or a member template is a declared
5270
+ specialization, the specialization of the member is implicitly
5271
+ instantiated when the specialization is referenced in a context that
5272
+ requires the member definition to exist or if the existence of the
5273
+ definition of the member affects the semantics of the program; in
5274
  particular, the initialization (and any associated side effects) of a
5275
  static data member does not occur unless the static data member is
5276
  itself used in a way that requires the definition of the static data
5277
  member to exist.
5278
 
5279
+ Unless a function template specialization is a declared specialization,
5280
+ the function template specialization is implicitly instantiated when the
5281
+ specialization is referenced in a context that requires a function
5282
+ definition to exist or if the existence of the definition affects the
5283
+ semantics of the program. A function whose declaration was instantiated
5284
+ from a friend function definition is implicitly instantiated when it is
5285
+ referenced in a context that requires a function definition to exist or
5286
+ if the existence of the definition affects the semantics of the program.
5287
+ Unless a call is to a function template explicit specialization or to a
5288
+ member function of an explicitly specialized class template, a default
5289
+ argument for a function template or a member function of a class
5290
+ template is implicitly instantiated when the function is called in a
5291
+ context that requires the value of the default argument.
5292
 
5293
+ [*Note 4*: An inline function that is the subject of an explicit
5294
+ instantiation declaration is not a declared specialization; the intent
5295
+ is that it still be implicitly instantiated when odr-used
5296
+ [[basic.def.odr]] so that the body can be considered for inlining, but
5297
+ that no out-of-line copy of it be generated in the translation
5298
+ unit. — *end note*]
5299
+
5300
+ [*Example 5*:
5301
 
5302
  ``` cpp
5303
  template<class T> struct Z {
5304
  void f();
5305
  void g();
 
5319
  Nothing in this example requires `class` `Z<double>`, `Z<int>::g()`, or
5320
  `Z<char>::f()` to be implicitly instantiated.
5321
 
5322
  — *end example*]
5323
 
5324
+ Unless a variable template specialization is a declared specialization,
5325
+ the variable template specialization is implicitly instantiated when it
5326
+ is referenced in a context that requires a variable definition to exist
5327
+ or if the existence of the definition affects the semantics of the
5328
+ program. A default template argument for a variable template is
5329
+ implicitly instantiated when the variable template is referenced in a
5330
+ context that requires the value of the default argument.
5331
+
5332
+ The existence of a definition of a variable or function is considered to
5333
+ affect the semantics of the program if the variable or function is
5334
+ needed for constant evaluation by an expression [[expr.const]], even if
5335
+ constant evaluation of the expression is not required or if constant
5336
+ expression evaluation does not use the definition.
5337
+
5338
+ [*Example 6*:
5339
+
5340
+ ``` cpp
5341
+ template<typename T> constexpr int f() { return T::value; }
5342
+ template<bool B, typename T> void g(decltype(B ? f<T>() : 0));
5343
+ template<bool B, typename T> void g(...);
5344
+ template<bool B, typename T> void h(decltype(int{B ? f<T>() : 0}));
5345
+ template<bool B, typename T> void h(...);
5346
+ void x() {
5347
+ g<false, int>(0); // OK, B ? f<T>() :\ 0 is not potentially constant evaluated
5348
+ h<false, int>(0); // error, instantiates f<int> even though B evaluates to false and
5349
+ // list-initialization of int from int cannot be narrowing
5350
+ }
5351
+ ```
5352
+
5353
+ — *end example*]
5354
+
5355
+ If the function selected by overload resolution [[over.match]] can be
5356
  determined without instantiating a class template definition, it is
5357
  unspecified whether that instantiation actually takes place.
5358
 
5359
+ [*Example 7*:
5360
 
5361
  ``` cpp
5362
  template <class T> struct S {
5363
  operator int();
5364
  };
 
5375
 
5376
  — *end example*]
5377
 
5378
  If a function template or a member function template specialization is
5379
  used in a way that involves overload resolution, a declaration of the
5380
+ specialization is implicitly instantiated [[temp.over]].
5381
 
5382
  An implementation shall not implicitly instantiate a function template,
5383
  a variable template, a member template, a non-virtual member function, a
5384
  member class, a static data member of a class template, or a
5385
+ substatement of a constexpr if statement [[stmt.if]], unless such
5386
+ instantiation is required.
5387
+
5388
+ [*Note 5*: The instantiation of a generic lambda does not require
5389
+ instantiation of substatements of a constexpr if statement within its
5390
+ *compound-statement* unless the call operator template is
5391
+ instantiated. *end note*]
5392
+
5393
+ It is unspecified whether or not an implementation implicitly
5394
+ instantiates a virtual member function of a class template if the
5395
+ virtual member function would not otherwise be instantiated. The use of
5396
+ a template specialization in a default argument shall not cause the
5397
+ template to be implicitly instantiated except that a class template may
5398
+ be instantiated where its complete type is needed to determine the
5399
+ correctness of the default argument. The use of a default argument in a
5400
+ function call causes specializations in the default argument to be
5401
+ implicitly instantiated.
5402
 
5403
  Implicitly instantiated class, function, and variable template
5404
  specializations are placed in the namespace where the template is
5405
  defined. Implicitly instantiated specializations for members of a class
5406
  template are placed in the namespace where the enclosing class template
5407
  is defined. Implicitly instantiated member templates are placed in the
5408
  namespace where the enclosing class or class template is defined.
5409
 
5410
+ [*Example 8*:
5411
 
5412
  ``` cpp
5413
  namespace N {
5414
  template<class T> class List {
5415
  public:
 
5426
  void g(Map<const char*,int>& m) {
5427
  int i = m.get("Nicholas");
5428
  }
5429
  ```
5430
 
5431
+ A call of `lt.get()` from `Map<const char*,int>::get()` would place
5432
  `List<int>::get()` in the namespace `N` rather than in the global
5433
  namespace.
5434
 
5435
  — *end example*]
5436
 
 
5439
  constraints are checked, and the instantiation of any template used in
5440
  the default argument is done as if the default argument had been an
5441
  initializer used in a function template specialization with the same
5442
  scope, the same template parameters and the same access as that of the
5443
  function template `f` used at that point, except that the scope in which
5444
+ a closure type is declared [[expr.prim.lambda.closure]] – and therefore
5445
+ its associated namespaces – remain as determined from the context of the
5446
+ definition for the default argument. This analysis is called *default
5447
+ argument instantiation*. The instantiated default argument is then used
5448
+ as the argument of `f`.
5449
 
5450
  Each default argument is instantiated independently.
5451
 
5452
+ [*Example 9*:
5453
 
5454
  ``` cpp
5455
  template<class T> void f(T x, T y = ydef(T()), T z = zdef(T()));
5456
 
5457
  class A { };
 
5459
  A zdef(A);
5460
 
5461
  void g(A a, A b, A c) {
5462
  f(a, b, c); // no default argument instantiation
5463
  f(a, b); // default argument z = zdef(T()) instantiated
5464
+ f(a); // error: ydef is not declared
5465
  }
5466
  ```
5467
 
5468
  — *end example*]
5469
 
5470
  The *noexcept-specifier* of a function template specialization is not
5471
  instantiated along with the function declaration; it is instantiated
5472
+ when needed [[except.spec]]. If such an *noexcept-specifier* is needed
5473
+ but has not yet been instantiated, the dependent names are looked up,
5474
+ the semantics constraints are checked, and the instantiation of any
5475
  template used in the *noexcept-specifier* is done as if it were being
5476
  done as part of instantiating the declaration of the specialization at
5477
  that point.
5478
 
5479
+ [*Note 6*: [[temp.point]] defines the point of instantiation of a
5480
  template specialization. — *end note*]
5481
 
5482
  There is an *implementation-defined* quantity that specifies the limit
5483
+ on the total depth of recursive instantiations [[implimits]], which
5484
  could involve more than one template. The result of an infinite
5485
  recursion in instantiation is undefined.
5486
 
5487
+ [*Example 10*:
5488
 
5489
  ``` cpp
5490
  template<class T> class X {
5491
  X<T>* p; // OK
5492
  X<T*> a; // implicit generation of X<T> requires
 
5495
  };
5496
  ```
5497
 
5498
  — *end example*]
5499
 
5500
+ The *type-constraint*s and *requires-clause* of a template
5501
+ specialization or member function are not instantiated along with the
5502
+ specialization or function itself, even for a member function of a local
5503
+ class; substitution into the atomic constraints formed from them is
5504
+ instead performed as specified in [[temp.constr.decl]] and
5505
+ [[temp.constr.atomic]] when determining whether the constraints are
5506
+ satisfied or as specified in [[temp.constr.decl]] when comparing
5507
+ declarations.
5508
+
5509
+ [*Note 7*: The satisfaction of constraints is determined during
5510
+ template argument deduction [[temp.deduct]] and overload resolution
5511
+ [[over.match]]. — *end note*]
5512
+
5513
+ [*Example 11*:
5514
+
5515
+ ``` cpp
5516
+ template<typename T> concept C = sizeof(T) > 2;
5517
+ template<typename T> concept D = C<T> && sizeof(T) > 4;
5518
+
5519
+ template<typename T> struct S {
5520
+ S() requires C<T> { } // #1
5521
+ S() requires D<T> { } // #2
5522
+ };
5523
+
5524
+ S<char> s1; // error: no matching constructor
5525
+ S<char[8]> s2; // OK, calls #2
5526
+ ```
5527
+
5528
+ When `S<char>` is instantiated, both constructors are part of the
5529
+ specialization. Their constraints are not satisfied, and they suppress
5530
+ the implicit declaration of a default constructor for `S<char>`
5531
+ [[class.default.ctor]], so there is no viable constructor for `s1`.
5532
+
5533
+ — *end example*]
5534
+
5535
+ [*Example 12*:
5536
+
5537
+ ``` cpp
5538
+ template<typename T> struct S1 {
5539
+ template<typename U>
5540
+ requires false
5541
+ struct Inner1; // ill-formed, no diagnostic required
5542
+ };
5543
+
5544
+ template<typename T> struct S2 {
5545
+ template<typename U>
5546
+ requires (sizeof(T[-(int)sizeof(T)]) > 1)
5547
+ struct Inner2; // ill-formed, no diagnostic required
5548
+ };
5549
+ ```
5550
+
5551
+ The class `S1<T>::Inner1` is ill-formed, no diagnostic required, because
5552
+ it has no valid specializations. `S2` is ill-formed, no diagnostic
5553
+ required, since no substitution into the constraints of its `Inner2`
5554
+ template would result in a valid expression.
5555
+
5556
+ — *end example*]
5557
+
5558
  ### Explicit instantiation <a id="temp.explicit">[[temp.explicit]]</a>
5559
 
5560
  A class, function, variable, or member template specialization can be
5561
  explicitly instantiated from its template. A member function, member
5562
  class or static data member of a class template can be explicitly
5563
  instantiated from the member definition associated with its class
5564
+ template.
 
 
5565
 
5566
  The syntax for explicit instantiation is:
5567
 
5568
  ``` bnf
5569
  explicit-instantiation:
5570
+ externₒₚₜ template declaration
5571
  ```
5572
 
5573
  There are two forms of explicit instantiation: an explicit instantiation
5574
  definition and an explicit instantiation declaration. An explicit
5575
  instantiation declaration begins with the `extern` keyword.
5576
 
5577
+ An explicit instantiation shall not use a *storage-class-specifier*
5578
+ [[dcl.stc]] other than `thread_local`. An explicit instantiation of a
5579
+ function template, member function of a class template, or variable
5580
+ template shall not use the `inline`, `constexpr`, or `consteval`
5581
+ specifiers. No *attribute-specifier-seq* [[dcl.attr.grammar]] shall
5582
+ appertain to an explicit instantiation.
5583
+
5584
  If the explicit instantiation is for a class or member class, the
5585
  *elaborated-type-specifier* in the *declaration* shall include a
5586
+ *simple-template-id*; otherwise, the *declaration* shall be a
5587
+ *simple-declaration* whose *init-declarator-list* comprises a single
5588
+ *init-declarator* that does not have an *initializer*. If the explicit
5589
+ instantiation is for a function or member function, the *unqualified-id*
5590
+ in the *declarator* shall be either a *template-id* or, where all
5591
+ template arguments can be deduced, a *template-name* or
5592
+ *operator-function-id*.
5593
 
5594
  [*Note 1*: The declaration may declare a *qualified-id*, in which case
5595
  the *unqualified-id* of the *qualified-id* must be a
5596
  *template-id*. — *end note*]
5597
 
5598
  If the explicit instantiation is for a member function, a member class
5599
  or a static data member of a class template specialization, the name of
5600
  the class template specialization in the *qualified-id* for the member
5601
  name shall be a *simple-template-id*. If the explicit instantiation is
5602
+ for a variable template specialization, the *unqualified-id* in the
5603
+ *declarator* shall be a *simple-template-id*. An explicit instantiation
5604
+ shall appear in an enclosing namespace of its template. If the name
5605
+ declared in the explicit instantiation is an unqualified name, the
5606
+ explicit instantiation shall appear in the namespace where its template
5607
+ is declared or, if that namespace is inline [[namespace.def]], any
5608
+ namespace from its enclosing namespace set.
5609
 
5610
  [*Note 2*: Regarding qualified names in declarators, see 
5611
  [[dcl.meaning]]. — *end note*]
5612
 
5613
  [*Example 1*:
 
5635
  class of a class template, or a member class template of a class or
5636
  class template shall precede an explicit instantiation of that entity
5637
  unless the explicit instantiation is preceded by an explicit
5638
  specialization of the entity with the same template arguments. If the
5639
  *declaration* of the explicit instantiation names an implicitly-declared
5640
+ special member function [[special]], the program is ill-formed.
5641
+
5642
+ The *declaration* in an *explicit-instantiation* and the *declaration*
5643
+ produced by the corresponding substitution into the templated function,
5644
+ variable, or class are two declarations of the same entity.
5645
+
5646
+ [*Note 3*:
5647
+
5648
+ These declarations are required to have matching types as specified in 
5649
+ [[basic.link]], except as specified in  [[except.spec]].
5650
+
5651
+ [*Example 2*:
5652
+
5653
+ ``` cpp
5654
+ template<typename T> T var = {};
5655
+ template float var<float>; // OK, instantiated variable has type float
5656
+ template int var<int[16]>[]; // OK, absence of major array bound is permitted
5657
+ template int *var<int>; // error: instantiated variable has type int
5658
+
5659
+ template<typename T> auto av = T();
5660
+ template int av<int>; // OK, variable with type int can be redeclared with type auto
5661
+
5662
+ template<typename T> auto f() {}
5663
+ template void f<int>(); // error: function with deduced return type
5664
+ // redeclared with non-deduced return type[dcl.spec.auto]
5665
+ ```
5666
+
5667
+ — *end example*]
5668
+
5669
+ — *end note*]
5670
+
5671
+ Despite its syntactic form, the *declaration* in an
5672
+ *explicit-instantiation* for a variable is not itself a definition and
5673
+ does not conflict with the definition instantiated by an explicit
5674
+ instantiation definition for that variable.
5675
 
5676
  For a given set of template arguments, if an explicit instantiation of a
5677
  template appears after a declaration of an explicit specialization for
5678
  that template, the explicit instantiation has no effect. Otherwise, for
5679
+ an explicit instantiation definition, the definition of a function
5680
  template, a variable template, a member function template, or a member
5681
  function or static data member of a class template shall be present in
5682
  every translation unit in which it is explicitly instantiated.
5683
 
5684
  An explicit instantiation of a class, function template, or variable
 
5686
  is defined. An explicit instantiation for a member of a class template
5687
  is placed in the namespace where the enclosing class template is
5688
  defined. An explicit instantiation for a member template is placed in
5689
  the namespace where the enclosing class or class template is defined.
5690
 
5691
+ [*Example 3*:
5692
 
5693
  ``` cpp
5694
  namespace N {
5695
  template<class T> class Y { void mf() { } };
5696
  }
 
5707
  — *end example*]
5708
 
5709
  A trailing *template-argument* can be left unspecified in an explicit
5710
  instantiation of a function template specialization or of a member
5711
  function template specialization provided it can be deduced from the
5712
+ type of a function parameter [[temp.deduct]].
5713
 
5714
+ [*Example 4*:
5715
 
5716
  ``` cpp
5717
  template<class T> class Array { ... };
5718
  template<class T> void sort(Array<T>& v) { ... }
5719
 
 
5721
  template void sort<>(Array<int>&);
5722
  ```
5723
 
5724
  — *end example*]
5725
 
5726
+ [*Note 4*: An explicit instantiation of a constrained template is
5727
+ required to satisfy that template’s associated constraints
5728
+ [[temp.constr.decl]]. The satisfaction of constraints is determined when
5729
+ forming the template name of an explicit instantiation in which all
5730
+ template arguments are specified [[temp.names]], or, for explicit
5731
+ instantiations of function templates, during template argument deduction
5732
+ [[temp.deduct.decl]] when one or more trailing template arguments are
5733
+ left unspecified. — *end note*]
5734
+
5735
  An explicit instantiation that names a class template specialization is
5736
  also an explicit instantiation of the same kind (declaration or
5737
  definition) of each of its members (not including members inherited from
5738
  base classes and members that are templates) that has not been
5739
  previously explicitly specialized in the translation unit containing the
5740
+ explicit instantiation, provided that the associated constraints, if
5741
+ any, of that member are satisfied by the template arguments of the
5742
+ explicit instantiation ([[temp.constr.decl]], [[temp.constr.constr]]),
5743
+ except as described below.
5744
 
5745
+ [*Note 5*: In addition, it will typically be an explicit instantiation
5746
  of certain implementation-dependent data about the class. — *end note*]
5747
 
5748
  An explicit instantiation definition that names a class template
5749
  specialization explicitly instantiates the class template specialization
5750
  and is an explicit instantiation definition of only those members that
5751
  have been defined at the point of instantiation.
5752
 
5753
+ An explicit instantiation of a prospective destructor [[class.dtor]]
5754
+ shall name the selected destructor of the class.
 
 
 
 
 
 
 
 
 
 
5755
 
5756
  If an entity is the subject of both an explicit instantiation
5757
  declaration and an explicit instantiation definition in the same
5758
  translation unit, the definition shall follow the declaration. An entity
5759
  that is the subject of an explicit instantiation declaration and that is
5760
+ also used in a way that would otherwise cause an implicit instantiation
5761
+ [[temp.inst]] in the translation unit shall be the subject of an
5762
+ explicit instantiation definition somewhere in the program; otherwise
5763
+ the program is ill-formed, no diagnostic required.
5764
 
5765
+ [*Note 6*: This rule does apply to inline functions even though an
5766
  explicit instantiation declaration of such an entity has no other
5767
  normative effect. This is needed to ensure that if the address of an
5768
  inline function is taken in a translation unit in which the
5769
  implementation chose to suppress the out-of-line body, another
5770
  translation unit will supply the body. — *end note*]
5771
 
5772
  An explicit instantiation declaration shall not name a specialization of
5773
  a template with internal linkage.
5774
 
 
 
 
 
 
 
 
 
 
5775
  An explicit instantiation does not constitute a use of a default
5776
  argument, so default argument instantiation is not done.
5777
 
5778
+ [*Example 5*:
5779
 
5780
  ``` cpp
5781
  char* p = 0;
5782
  template<class T> T g(T x = &p) { return x; }
5783
  template int g<int>(int); // OK even though &p isn't an int.
 
5801
 
5802
  can be declared by a declaration introduced by `template<>`; that is:
5803
 
5804
  ``` bnf
5805
  explicit-specialization:
5806
+ template '<' '>' declaration
5807
  ```
5808
 
5809
  [*Example 1*:
5810
 
5811
  ``` cpp
 
5826
  `Array<char*>`; other `Array` types will be sorted by functions
5827
  generated from the template.
5828
 
5829
  — *end example*]
5830
 
5831
+ An explicit specialization shall not use a *storage-class-specifier*
5832
+ [[dcl.stc]] other than `thread_local`.
5833
+
5834
  An explicit specialization may be declared in any scope in which the
5835
  corresponding primary template may be defined ([[namespace.memdef]],
5836
  [[class.mem]], [[temp.mem]]).
5837
 
5838
  A declaration of a function template, class template, or variable
 
5863
  data member template of a class template may be explicitly specialized
5864
  for a class specialization that is implicitly instantiated; in this
5865
  case, the definition of the class template shall precede the explicit
5866
  specialization for the member of the class template. If such an explicit
5867
  specialization for the member of a class template names an
5868
+ implicitly-declared special member function [[special]], the program is
5869
+ ill-formed.
5870
 
5871
  A member of an explicitly specialized class is not implicitly
5872
  instantiated from the member declaration of the class template; instead,
5873
  the member of the class template specialization shall itself be
5874
  explicitly defined if its definition is required. In this case, the
 
5962
  };
5963
  template<> enum A<int>::E : int { eint }; // OK
5964
  template<> enum class A<int>::S : int { sint }; // OK
5965
  template<class T> enum A<T>::E : T { eT };
5966
  template<class T> enum class A<T>::S : T { sT };
5967
+ template<> enum A<char>::E : char { echar }; // error: A<char>::E was instantiated
5968
  // when A<char> was instantiated
5969
  template<> enum class A<char>::S : char { schar }; // OK
5970
  ```
5971
 
5972
  — *end example*]
 
6010
 
6011
  — *end example*]
6012
 
6013
  A *simple-template-id* that names a class template explicit
6014
  specialization that has been declared but not defined can be used
6015
+ exactly like the names of other incompletely-defined classes
6016
+ [[basic.types]].
6017
 
6018
  [*Example 6*:
6019
 
6020
  ``` cpp
6021
  template<class T> class X; // X is a class template
 
6042
  template<> void sort(Array<int>&);
6043
  ```
6044
 
6045
  — *end example*]
6046
 
6047
+ [*Note 2*: An explicit specialization of a constrained template is
6048
+ required to satisfy that template’s associated constraints
6049
+ [[temp.constr.decl]]. The satisfaction of constraints is determined when
6050
+ forming the template name of an explicit specialization in which all
6051
+ template arguments are specified [[temp.names]], or, for explicit
6052
+ specializations of function templates, during template argument
6053
+ deduction [[temp.deduct.decl]] when one or more trailing template
6054
+ arguments are left unspecified. — *end note*]
6055
+
6056
  A function with the same name as a template and a type that exactly
6057
  matches that of a template specialization is not an explicit
6058
+ specialization [[temp.fct]].
6059
 
6060
+ Whether an explicit specialization of a function or variable template is
6061
+ inline, constexpr, or an immediate function is determined by the
6062
+ explicit specialization and is independent of those properties of the
6063
+ template.
6064
 
6065
  [*Example 8*:
6066
 
6067
  ``` cpp
6068
  template<class T> void f(T) { ... }
 
6077
  An explicit specialization of a static data member of a template or an
6078
  explicit specialization of a static data member template is a definition
6079
  if the declaration includes an initializer; otherwise, it is a
6080
  declaration.
6081
 
6082
+ [*Note 3*:
6083
 
6084
  The definition of a static data member of a template that requires
6085
  default-initialization must use a *braced-init-list*:
6086
 
6087
  ``` cpp
 
6151
  In an explicit specialization declaration for a member of a class
6152
  template or a member template that appears in namespace scope, the
6153
  member template and some of its enclosing class templates may remain
6154
  unspecialized, except that the declaration shall not explicitly
6155
  specialize a class member template if its enclosing class templates are
6156
+ not explicitly specialized as well. In such an explicit specialization
6157
  declaration, the keyword `template` followed by a
6158
  *template-parameter-list* shall be provided instead of the `template<>`
6159
  preceding the explicit specialization declaration of the member. The
6160
  types of the *template-parameter*s in the *template-parameter-list*
6161
  shall be the same as those specified in the primary template definition.
 
6174
  template <class T> void mf1(T);
6175
  };
6176
  template <> template <> template<class T>
6177
  void A<int>::B<double>::mf1(T t) { }
6178
  template <class Y> template <>
6179
+ void A<Y>::B<double>::mf2() { } // error: B<double> is specialized but
6180
  // its enclosing class template A is not
6181
  ```
6182
 
6183
  — *end example*]
6184
 
 
6194
 
6195
  - the explicit specialization of a function template;
6196
  - the explicit specialization of a member function template;
6197
  - the explicit specialization of a member function of a class template
6198
  where the class template specialization to which the member function
6199
+ specialization belongs is implicitly instantiated. \[*Note 4*: Default
6200
  function arguments may be specified in the declaration or definition
6201
  of a member function of a class template specialization that is
6202
  explicitly specialized. — *end note*]
6203
 
6204
  ## Function template specializations <a id="temp.fct.spec">[[temp.fct.spec]]</a>
 
6233
  — *end example*]
6234
 
6235
  ### Explicit template argument specification <a id="temp.arg.explicit">[[temp.arg.explicit]]</a>
6236
 
6237
  Template arguments can be specified when referring to a function
6238
+ template specialization that is not a specialization of a constructor
6239
+ template by qualifying the function template name with the list of
6240
+ *template-argument*s in the same way as *template-argument*s are
6241
+ specified in uses of a class template specialization.
6242
 
6243
  [*Example 1*:
6244
 
6245
  ``` cpp
6246
  template<class T> void sort(Array<T>& v);
 
6261
  }
6262
  ```
6263
 
6264
  — *end example*]
6265
 
6266
+ Template arguments shall not be specified when referring to a
6267
+ specialization of a constructor template ([[class.ctor]],
6268
+ [[class.qual]]).
6269
+
6270
  A template argument list may be specified when referring to a
6271
  specialization of a function template
6272
 
6273
  - when a function is called,
6274
  - when the address of a function is taken, when a function initializes a
6275
  reference to function, or when a pointer to member function is formed,
6276
  - in an explicit specialization,
6277
  - in an explicit instantiation, or
6278
  - in a friend declaration.
6279
 
6280
+ Trailing template arguments that can be deduced [[temp.deduct]] or
6281
  obtained from default *template-argument*s may be omitted from the list
6282
+ of explicit *template-argument*s. A trailing template parameter pack
6283
+ [[temp.variadic]] not otherwise deduced will be deduced as an empty
6284
  sequence of template arguments. If all of the template arguments can be
6285
  deduced, they may all be omitted; in this case, the empty template
6286
  argument list `<>` itself may also be omitted. In contexts where
6287
  deduction is done and fails, or in contexts where deduction is not done,
6288
  if a template argument list is specified and it, along with any default
 
6294
 
6295
  ``` cpp
6296
  template<class X, class Y> X f(Y);
6297
  template<class X, class Y, class ... Z> X g(Y);
6298
  void h() {
6299
+ int i = f<int>(5.6); // Y deduced as double
6300
+ int j = f(5.6); // error: X cannot be deduced
6301
+ f<void>(f<int, bool>); // Y for outer f deduced as int (*)(bool)
6302
+ f<void>(f<int>); // error: f<int> does not denote a single function template specialization
6303
+ int k = g<int>(5.6); // Y deduced as double; Z deduced as an empty sequence
6304
+ f<void>(g<int, bool>); // Y for outer f deduced as int (*)(bool),
6305
+ // Z deduced as an empty sequence
6306
  }
6307
  ```
6308
 
6309
  — *end example*]
6310
 
6311
  [*Note 1*:
6312
 
6313
  An empty template argument list can be used to indicate that a given use
6314
  refers to a specialization of a function template even when a
6315
+ non-template function [[dcl.fct]] is visible that would otherwise be
6316
  used. For example:
6317
 
6318
  ``` cpp
6319
  template <class T> int f(T); // #1
6320
  int f(int); // #2
 
6335
  ``` cpp
6336
  template<class X, class Y, class Z> X f(Y,Z);
6337
  template<class ... Args> void f2();
6338
  void g() {
6339
  f<int,const char*,double>("aa",3.0);
6340
+ f<int,const char*>("aa",3.0); // Z deduced as double
6341
+ f<int>("aa",3.0); // Y deduced as const char*; Z deduced as double
6342
  f("aa",3.0); // error: X cannot be deduced
6343
  f2<char, short, int, long>(); // OK
6344
  }
6345
  ```
6346
 
6347
  — *end example*]
6348
 
6349
+ Implicit conversions [[conv]] will be performed on a function argument
6350
+ to convert it to the type of the corresponding function parameter if the
6351
+ parameter type contains no *template-parameter*s that participate in
6352
+ template argument deduction.
6353
 
6354
  [*Note 2*:
6355
 
6356
  Template parameters do not participate in template argument deduction if
6357
  they are explicitly specified. For example,
 
6369
  ```
6370
 
6371
  — *end note*]
6372
 
6373
  [*Note 3*: Because the explicit template argument list follows the
6374
+ function template name, and because constructor templates [[class.ctor]]
6375
+ are named without using a function name [[class.qual]], there is no way
6376
+ to provide an explicit template argument list for these function
6377
+ templates. — *end note*]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6378
 
6379
  Template argument deduction can extend the sequence of template
6380
  arguments corresponding to a template parameter pack, even when the
6381
  sequence contains explicitly specified template arguments.
6382
 
6383
+ [*Example 4*:
6384
 
6385
  ``` cpp
6386
  template<class ... Types> void f(Types ... values);
6387
 
6388
  void g() {
6389
+ f<int*, float*>(0, 0, 0); // Types deduced as the sequence int*, float*, int
6390
  }
6391
  ```
6392
 
6393
  — *end example*]
6394
 
 
6417
  }
6418
  ```
6419
 
6420
  — *end example*]
6421
 
6422
+ When an explicit template argument list is specified, if the given
6423
+ *template-id* is not valid [[temp.names]], type deduction fails.
6424
+ Otherwise, the specified template argument values are substituted for
6425
+ the corresponding template parameters as specified below.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6426
 
6427
  After this substitution is performed, the function parameter type
6428
  adjustments described in  [[dcl.fct]] are performed.
6429
 
6430
  [*Example 2*: A parameter type of “`void (const int, int[5])`” becomes
 
6493
  When all template arguments have been deduced or obtained from default
6494
  template arguments, all uses of template parameters in the template
6495
  parameter list of the template and the function type are replaced with
6496
  the corresponding deduced or default argument values. If the
6497
  substitution results in an invalid type, as described above, type
6498
+ deduction fails. If the function template has associated constraints
6499
+ [[temp.constr.decl]], those constraints are checked for satisfaction
6500
+ [[temp.constr.constr]]. If the constraints are not satisfied, type
6501
  deduction fails.
6502
 
6503
  At certain points in the template argument deduction process it is
6504
  necessary to take a function type that makes use of template parameters
6505
  and replace those template parameters with the corresponding template
 
6514
  expressions include not only constant expressions such as those that
6515
  appear in array bounds or as nontype template arguments but also general
6516
  expressions (i.e., non-constant expressions) inside `sizeof`,
6517
  `decltype`, and other contexts that allow non-constant expressions. The
6518
  substitution proceeds in lexical order and stops when a condition that
6519
+ causes deduction to fail is encountered. If substitution into different
6520
+ declarations of the same function template would cause template
6521
+ instantiations to occur in a different order or not at all, the program
6522
+ is ill-formed; no diagnostic required.
6523
 
6524
  [*Note 3*: The equivalent substitution in exception specifications is
6525
  done only when the *noexcept-specifier* is instantiated, at which point
6526
  a program is ill-formed if the substitution results in an invalid type
6527
  or expression. — *end note*]
 
6532
  template <class T> struct A { using X = typename T::X; };
6533
  template <class T> typename T::X f(typename A<T>::X);
6534
  template <class T> void f(...) { }
6535
  template <class T> auto g(typename A<T>::X) -> typename T::X;
6536
  template <class T> void g(...) { }
6537
+ template <class T> typename T::X h(typename A<T>::X);
6538
+ template <class T> auto h(typename A<T>::X) -> typename T::X; // redeclaration
6539
+ template <class T> void h(...) { }
6540
 
6541
+ void x() {
6542
  f<int>(0); // OK, substituting return type causes deduction to fail
6543
  g<int>(0); // error, substituting parameter type instantiates A<int>
6544
+ h<int>(0); // ill-formed, no diagnostic required
6545
  }
6546
  ```
6547
 
6548
  — *end example*]
6549
 
 
6555
  [*Note 4*: If no diagnostic is required, the program is still
6556
  ill-formed. Access checking is done as part of the substitution
6557
  process. — *end note*]
6558
 
6559
  Only invalid types and expressions in the immediate context of the
6560
+ function type, its template parameter types, and its
6561
+ *explicit-specifier* can result in a deduction failure.
6562
 
6563
  [*Note 5*: The substitution into types and expressions can result in
6564
  effects such as the instantiation of class template specializations
6565
  and/or function template specializations, the generation of
6566
  implicitly-defined functions, etc. Such effects are not in the
6567
  “immediate context” and can result in the program being
6568
  ill-formed. — *end note*]
6569
 
6570
+ A *lambda-expression* appearing in a function type or a template
6571
+ parameter is not considered part of the immediate context for the
6572
+ purposes of template argument deduction.
6573
+
6574
+ [*Note 6*:
6575
+
6576
+ The intent is to avoid requiring implementations to deal with
6577
+ substitution failure involving arbitrary statements.
6578
+
6579
  [*Example 6*:
6580
 
6581
+ ``` cpp
6582
+ template <class T>
6583
+ auto f(T) -> decltype([]() { T::invalid; } ());
6584
+ void f(...);
6585
+ f(0); // error: invalid expression not part of the immediate context
6586
+
6587
+ template <class T, std::size_t = sizeof([]() { T::invalid; })>
6588
+ void g(T);
6589
+ void g(...);
6590
+ g(0); // error: invalid expression not part of the immediate context
6591
+
6592
+ template <class T>
6593
+ auto h(T) -> decltype([x = T::invalid]() { });
6594
+ void h(...);
6595
+ h(0); // error: invalid expression not part of the immediate context
6596
+
6597
+ template <class T>
6598
+ auto i(T) -> decltype([]() -> typename T::invalid { });
6599
+ void i(...);
6600
+ i(0); // error: invalid expression not part of the immediate context
6601
+
6602
+ template <class T>
6603
+ auto j(T t) -> decltype([](auto x) -> decltype(x.invalid) { } (t)); // #1
6604
+ void j(...); // #2
6605
+ j(0); // deduction fails on #1, calls #2
6606
+ ```
6607
+
6608
+ — *end example*]
6609
+
6610
+ — *end note*]
6611
+
6612
+ [*Example 7*:
6613
+
6614
  ``` cpp
6615
  struct X { };
6616
  struct Y {
6617
  Y(X){}
6618
  };
 
6624
  X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
6625
  ```
6626
 
6627
  — *end example*]
6628
 
6629
+ [*Note 7*:
6630
 
6631
  Type deduction may fail for the following reasons:
6632
 
6633
+ - Attempting to instantiate a pack expansion containing multiple packs
6634
+ of differing lengths.
6635
  - Attempting to create an array with an element type that is `void`, a
6636
+ function type, or a reference type, or attempting to create an array
6637
+ with a size that is zero or negative.
6638
+ \[*Example 8*:
6639
  ``` cpp
6640
  template <class T> int f(T[5]);
6641
  int I = f<int>(0);
6642
  int j = f<void>(0); // invalid array
6643
  ```
6644
 
6645
  — *end example*]
6646
  - Attempting to use a type that is not a class or enumeration type in a
6647
  qualified name.
6648
+ \[*Example 9*:
6649
  ``` cpp
6650
  template <class T> int f(typename T::B*);
6651
  int i = f<int>(0);
6652
  ```
6653
 
 
6658
  - the specified member is not a type where a type is required, or
6659
  - the specified member is not a template where a template is required,
6660
  or
6661
  - the specified member is not a non-type where a non-type is required.
6662
 
6663
+ \[*Example 10*:
6664
  ``` cpp
6665
  template <int I> struct X { };
6666
  template <template <class T> class> struct Z { };
6667
  template <class T> void f(typename T::Y*){}
6668
  template <class T> void g(X<T::N>*){}
 
6688
  — *end example*]
6689
  - Attempting to create a pointer to reference type.
6690
  - Attempting to create a reference to `void`.
6691
  - Attempting to create “pointer to member of `T`” when `T` is not a
6692
  class type.
6693
+ \[*Example 11*:
6694
  ``` cpp
6695
  template <class T> int f(int T::*);
6696
  int i = f<int>(0);
6697
  ```
6698
 
6699
  — *end example*]
6700
  - Attempting to give an invalid type to a non-type template parameter.
6701
+ \[*Example 12*:
6702
  ``` cpp
6703
  template <class T, T> struct S {};
6704
  template <class T> int f(S<T, T()>*);
6705
  struct X {};
6706
  int i0 = f<X>(0);
 
6708
 
6709
  — *end example*]
6710
  - Attempting to perform an invalid conversion in either a template
6711
  argument expression, or an expression used in the function
6712
  declaration.
6713
+ \[*Example 13*:
6714
  ``` cpp
6715
  template <class T, T*> int f(int);
6716
  int i2 = f<int,1>(0); // can't conv 1 to int*
6717
  ```
6718
 
6719
  — *end example*]
6720
  - Attempting to create a function type in which a parameter has a type
6721
  of `void`, or in which the return type is a function type or array
6722
  type.
 
 
6723
 
6724
  — *end note*]
6725
 
6726
+ [*Example 14*:
6727
 
6728
  In the following example, assuming a `signed char` cannot represent the
6729
+ value 1000, a narrowing conversion [[dcl.init.list]] would be required
6730
+ to convert the *template-argument* of type `int` to `signed char`,
6731
+ therefore substitution fails for the second template
6732
+ [[temp.arg.nontype]].
6733
 
6734
  ``` cpp
6735
  template <int> int f(int);
6736
  template <signed char> int f(int);
6737
  int i1 = f<1000>(0); // OK
 
6745
  Template argument deduction is done by comparing each function template
6746
  parameter type (call it `P`) that contains *template-parameter*s that
6747
  participate in template argument deduction with the type of the
6748
  corresponding argument of the call (call it `A`) as described below. If
6749
  removing references and cv-qualifiers from `P` gives
6750
+ `std::initializer_list<P^{\prime}>` or `P`'`[N]` for some `P`' and `N`
6751
+ and the argument is a non-empty initializer list [[dcl.init.list]], then
6752
+ deduction is performed instead for each element of the initializer list
6753
+ independently, taking `P`' as separate function template parameter types
6754
+ `P`'_i and the iᵗʰ initializer element as the corresponding argument. In
6755
+ the `P`'`[N]` case, if `N` is a non-type template parameter, `N` is
6756
+ deduced from the length of the initializer list. Otherwise, an
6757
+ initializer list argument causes the parameter to be considered a
6758
+ non-deduced context [[temp.deduct.type]].
6759
 
6760
  [*Example 1*:
6761
 
6762
  ``` cpp
6763
  template<class T> void f(std::initializer_list<T>);
6764
+ f({1,2,3}); // T deduced as int
6765
+ f({1,"asdf"}); // error: T deduced as both int and const char*
6766
 
6767
  template<class T> void g(T);
6768
  g({1,2,3}); // error: no argument deduced for T
6769
 
6770
  template<class T, int N> void h(T const(&)[N]);
6771
+ h({1,2,3}); // T deduced as int; N deduced as 3
6772
 
6773
  template<class T> void j(T const(&)[3]);
6774
+ j({42}); // T deduced as int; array bound not considered
6775
 
6776
  struct Aggr { int i; int j; };
6777
  template<int N> void k(Aggr const(&)[N]);
6778
  k({1,2,3}); // error: deduction fails, no conversion from int to Aggr
6779
+ k({{1},{2},{3}}); // OK, N deduced as 3
6780
 
6781
  template<int M, int N> void m(int const(&)[M][N]);
6782
+ m({{1,2},{3,4}}); // M and N both deduced as 2
6783
 
6784
  template<class T, int N> void n(T const(&)[N], T);
6785
  n({{1},{2},{3}},Aggr()); // OK, T is Aggr, N is 3
6786
+
6787
+ template<typename T, int N> void o(T (* const (&)[N])(T)) { }
6788
+ int f1(int);
6789
+ int f4(int);
6790
+ char f4(char);
6791
+ o({ &f1, &f4 }); // OK, T deduced as int from first element, nothing
6792
+ // deduced from second element, N deduced as 2
6793
+ o({ &f1, static_cast<char(*)(char)>(&f4) }); // error: conflicting deductions for T
6794
  ```
6795
 
6796
  — *end example*]
6797
 
6798
  For a function parameter pack that occurs at the end of the
6799
  *parameter-declaration-list*, deduction is performed for each remaining
6800
  argument of the call, taking the type `P` of the *declarator-id* of the
6801
  function parameter pack as the corresponding function template parameter
6802
  type. Each deduction deduces template arguments for subsequent positions
6803
  in the template parameter packs expanded by the function parameter pack.
6804
+ When a function parameter pack appears in a non-deduced context
6805
+ [[temp.deduct.type]], the type of that pack is never deduced.
6806
 
6807
  [*Example 2*:
6808
 
6809
  ``` cpp
6810
  template<class ... Types> void f(Types& ...);
6811
  template<class T1, class ... Types> void g(T1, Types ...);
6812
  template<class T1, class ... Types> void g1(Types ..., T1);
6813
 
6814
  void h(int x, float& y) {
6815
  const int z = x;
6816
+ f(x, y, z); // Types deduced as int, float, const int
6817
+ g(x, y, z); // T1 deduced as int; Types deduced as float, int
6818
  g1(x, y, z); // error: Types is not deduced
6819
  g1<int, int, int>(x, y, z); // OK, no deduction occurs
6820
  }
6821
  ```
6822
 
6823
  — *end example*]
6824
 
6825
  If `P` is not a reference type:
6826
 
6827
  - If `A` is an array type, the pointer type produced by the
6828
+ array-to-pointer standard conversion [[conv.array]] is used in place
6829
+ of `A` for type deduction; otherwise,
6830
  - If `A` is a function type, the pointer type produced by the
6831
+ function-to-pointer standard conversion [[conv.func]] is used in place
6832
+ of `A` for type deduction; otherwise,
6833
  - If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s
6834
  type are ignored for type deduction.
6835
 
6836
  If `P` is a cv-qualified type, the top-level cv-qualifiers of `P`’s type
6837
  are ignored for type deduction. If `P` is a reference type, the type
 
6850
 
6851
  — *end example*]
6852
 
6853
  A *forwarding reference* is an rvalue reference to a cv-unqualified
6854
  template parameter that does not represent a template parameter of a
6855
+ class template (during class template argument deduction
6856
+ [[over.match.class.deduct]]). If `P` is a forwarding reference and the
6857
  argument is an lvalue, the type “lvalue reference to `A`” is used in
6858
  place of `A` for type deduction.
6859
 
6860
  [*Example 4*:
6861
 
 
6891
  that allow a difference:
6892
 
6893
  - If the original `P` is a reference type, the deduced `A` (i.e., the
6894
  type referred to by the reference) can be more cv-qualified than the
6895
  transformed `A`.
6896
+ - The transformed `A` can be another pointer or pointer-to-member type
6897
  that can be converted to the deduced `A` via a function pointer
6898
+ conversion [[conv.fctptr]] and/or qualification conversion
6899
+ [[conv.qual]].
6900
  - If `P` is a class and `P` has the form *simple-template-id*, then the
6901
+ transformed `A` can be a derived class `D` of the deduced `A`.
6902
+ Likewise, if `P` is a pointer to a class of the form
6903
+ *simple-template-id*, the transformed `A` can be a pointer to a
6904
+ derived class `D` pointed to by the deduced `A`. However, if there is
6905
+ a class `C` that is a (direct or indirect) base class of `D` and
6906
+ derived (directly or indirectly) from a class `B` and that would be a
6907
+ valid deduced `A`, the deduced `A` cannot be `B` or pointer to `B`,
6908
+ respectively.
6909
+ \[*Example 5*:
6910
+ ``` cpp
6911
+ template <typename... T> struct X;
6912
+ template <> struct X<> {};
6913
+ template <typename T, typename... Ts>
6914
+ struct X<T, Ts...> : X<Ts...> {};
6915
+ struct D : X<int> {};
6916
+
6917
+ template <typename... T>
6918
+ int f(const X<T...>&);
6919
+ int x = f(D()); // calls f<int>, not f<>
6920
+ // B is X<>, C is X<int>
6921
+ ```
6922
+
6923
+ — *end example*]
6924
 
6925
  These alternatives are considered only if type deduction would otherwise
6926
  fail. If they yield more than one possible deduced `A`, the type
6927
  deduction fails.
6928
 
 
6930
  parameters of a function template, or is used only in a non-deduced
6931
  context, its corresponding *template-argument* cannot be deduced from a
6932
  function call and the *template-argument* must be explicitly
6933
  specified. — *end note*]
6934
 
6935
+ When `P` is a function type, function pointer type, or
6936
+ pointer-to-member-function type:
6937
 
6938
  - If the argument is an overload set containing one or more function
6939
  templates, the parameter is treated as a non-deduced context.
6940
  - If the argument is an overload set (not containing function
6941
  templates), trial argument deduction is attempted using each of the
6942
  members of the set. If deduction succeeds for only one of the overload
6943
  set members, that member is used as the argument value for the
6944
  deduction. If deduction succeeds for more than one member of the
6945
  overload set the parameter is treated as a non-deduced context.
6946
 
6947
+ [*Example 6*:
6948
 
6949
  ``` cpp
6950
  // Only one function of an overload set matches the call so the function parameter is a deduced context.
6951
  template <class T> int f(T (*p)(T));
6952
  int g(int);
 
6954
  int i = f(g); // calls f(int (*)(int))
6955
  ```
6956
 
6957
  — *end example*]
6958
 
6959
+ [*Example 7*:
6960
 
6961
  ``` cpp
6962
  // Ambiguous deduction causes the second function parameter to be a non-deduced context.
6963
  template <class T> int f(T, T (*p)(T));
6964
  int g(int);
 
6966
  int i = f(1, g); // calls f(int, int (*)(int))
6967
  ```
6968
 
6969
  — *end example*]
6970
 
6971
+ [*Example 8*:
6972
 
6973
  ``` cpp
6974
  // The overload set contains a template, causing the second function parameter to be a non-deduced context.
6975
  template <class T> int f(T, T (*p)(T));
6976
  char g(char);
 
6993
  *template-parameter*s participate in template argument deduction, and
6994
  parameters that became non-dependent due to substitution of
6995
  explicitly-specified template arguments, will be checked during overload
6996
  resolution. — *end note*]
6997
 
6998
+ [*Example 9*:
6999
 
7000
  ``` cpp
7001
  template <class T> struct Z {
7002
  typedef typename T::x xx;
7003
  };
 
7012
  — *end example*]
7013
 
7014
  #### Deducing template arguments taking the address of a function template <a id="temp.deduct.funcaddr">[[temp.deduct.funcaddr]]</a>
7015
 
7016
  Template arguments can be deduced from the type specified when taking
7017
+ the address of an overloaded function [[over.over]]. If there is a
7018
+ target, the function template’s function type and the target type are
7019
+ used as the types of `P` and `A`, and the deduction is done as described
7020
+ in  [[temp.deduct.type]]. Otherwise, deduction is performed with empty
7021
+ sets of types P and A.
7022
 
7023
+ A placeholder type [[dcl.spec.auto]] in the return type of a function
7024
  template is a non-deduced context. If template argument deduction
7025
  succeeds for such a function, the return type is determined from
7026
  instantiation of the function body.
7027
 
7028
  #### Deducing conversion function template arguments <a id="temp.deduct.conv">[[temp.deduct.conv]]</a>
 
7033
  [[dcl.init]], [[over.match.conv]], and [[over.match.ref]] for the
7034
  determination of that type) as described in  [[temp.deduct.type]].
7035
 
7036
  If `P` is a reference type, the type referred to by `P` is used in place
7037
  of `P` for type deduction and for any further references to or
7038
+ transformations of `P` in the remainder of this subclause.
7039
 
7040
  If `A` is not a reference type:
7041
 
7042
  - If `P` is an array type, the pointer type produced by the
7043
+ array-to-pointer standard conversion [[conv.array]] is used in place
7044
+ of `P` for type deduction; otherwise,
7045
  - If `P` is a function type, the pointer type produced by the
7046
+ function-to-pointer standard conversion [[conv.func]] is used in place
7047
+ of `P` for type deduction; otherwise,
7048
  - If `P` is a cv-qualified type, the top-level cv-qualifiers of `P`’s
7049
  type are ignored for type deduction.
7050
 
7051
  If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s type
7052
  are ignored for type deduction. If `A` is a reference type, the type
 
7055
  In general, the deduction process attempts to find template argument
7056
  values that will make the deduced `A` identical to `A`. However, there
7057
  are four cases that allow a difference:
7058
 
7059
  - If the original `A` is a reference type, `A` can be more cv-qualified
7060
+ than the deduced `A` (i.e., the type referred to by the reference).
7061
  - If the original `A` is a function pointer type, `A` can be “pointer to
7062
+ function” even if the deduced `A` is “pointer to `noexcept` function”.
7063
+ - If the original `A` is a pointer-to-member-function type, `A` can be
7064
  “pointer to member of type function” even if the deduced `A` is
7065
+ “pointer to member of type `noexcept` function”.
7066
+ - The deduced `A` can be another pointer or pointer-to-member type that
7067
  can be converted to `A` via a qualification conversion.
7068
 
7069
  These alternatives are considered only if type deduction would otherwise
7070
  fail. If they yield more than one possible deduced `A`, the type
7071
  deduction fails.
7072
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7073
  #### Deducing template arguments during partial ordering <a id="temp.deduct.partial">[[temp.deduct.partial]]</a>
7074
 
7075
  Template argument deduction is done by comparing certain types
7076
  associated with the two function templates being compared.
7077
 
 
7092
 
7093
  The types used to determine the ordering depend on the context in which
7094
  the partial ordering is done:
7095
 
7096
  - In the context of a function call, the types used are those function
7097
+ parameter types for which the function call has arguments.[^12]
7098
  - In the context of a call to a conversion function, the return types of
7099
  the conversion function templates are used.
7100
+ - In other contexts [[temp.func.order]] the function template’s function
7101
+ type is used.
7102
 
7103
  Each type nominated above from the parameter template and the
7104
  corresponding type from the argument template are used as the types of
7105
+ `P` and `A`.
 
 
7106
 
7107
  Before the partial ordering is done, certain transformations are
7108
  performed on the types used for partial ordering:
7109
 
7110
  - If `P` is a reference type, `P` is replaced by the type referred to.
 
7148
  // than the variadic templates #1 and #2
7149
  ```
7150
 
7151
  — *end example*]
7152
 
7153
+ If, for a given type, the types are identical after the transformations
7154
+ above and both `P` and `A` were reference types (before being replaced
7155
+ with the type referred to above):
 
7156
 
7157
  - if the type from the argument template was an lvalue reference and the
7158
  type from the parameter template was not, the parameter type is not
7159
  considered to be at least as specialized as the argument type;
7160
  otherwise,
 
7169
  specialized than* `G` if `F` is at least as specialized as `G` and `G`
7170
  is not at least as specialized as `F`.
7171
 
7172
  If, after considering the above, function template `F` is at least as
7173
  specialized as function template `G` and vice-versa, and if `G` has a
7174
+ trailing function parameter pack for which `F` does not have a
7175
+ corresponding parameter, and if `F` does not have a trailing function
7176
+ parameter pack, then `F` is more specialized than `G`.
7177
 
7178
+ In most cases, deduction fails if not all template parameters have
7179
+ values, but for partial ordering purposes a template parameter may
7180
+ remain without a value provided it is not used in the types being used
7181
+ for partial ordering.
7182
 
7183
  [*Note 2*: A template parameter used in a non-deduced context is
7184
  considered used. — *end note*]
7185
 
7186
  [*Example 2*:
 
7239
  A given type `P` can be composed from a number of other types,
7240
  templates, and non-type values:
7241
 
7242
  - A function type includes the types of each of the function parameters
7243
  and the return type.
7244
+ - A pointer-to-member type includes the type of the class object pointed
7245
  to and the type of the member pointed to.
7246
  - A type that is a specialization of a class template (e.g., `A<int>`)
7247
  includes the types, templates, and non-type values referenced by the
7248
  template argument list of the specialization.
7249
  - An array type includes the array element type and the value of the
7250
  array bound.
7251
 
7252
  In most cases, the types, templates, and non-type values that are used
7253
  to compose `P` participate in template argument deduction. That is, they
7254
+ may be used to determine the value of a template argument, and template
7255
+ argument deduction fails if the value so determined is not consistent
7256
+ with the values determined elsewhere. In certain contexts, however, the
7257
+ value does not participate in type deduction, but instead uses the
7258
+ values of template arguments that were either deduced elsewhere or
7259
+ explicitly specified. If a template parameter is used only in
7260
+ non-deduced contexts and is not explicitly specified, template argument
7261
+ deduction fails.
7262
 
7263
+ [*Note 1*: Under [[temp.deduct.call]], if `P` contains no
7264
+ *template-parameter*s that appear in deduced contexts, no deduction is
7265
+ done, so `P` and `A` need not have the same form. — *end note*]
 
7266
 
7267
  The non-deduced contexts are:
7268
 
7269
  - The *nested-name-specifier* of a type that was specified using a
7270
  *qualified-id*.
 
7272
  - A non-type template argument or an array bound in which a
7273
  subexpression references a template parameter.
7274
  - A template parameter used in the parameter type of a function
7275
  parameter that has a default argument that is being used in the call
7276
  for which argument deduction is being done.
7277
+ - A function parameter for which the associated argument is an overload
7278
+ set [[over.over]], and one or more of the following apply:
 
 
7279
  - more than one function matches the function parameter type
7280
  (resulting in an ambiguous deduction), or
7281
  - no function matches the function parameter type, or
7282
+ - the overload set supplied as an argument contains one or more
7283
  function templates.
7284
  - A function parameter for which the associated argument is an
7285
+ initializer list [[dcl.init.list]] but the parameter does not have a
7286
+ type for which deduction from an initializer list is specified
7287
+ [[temp.deduct.call]].
7288
  \[*Example 1*:
7289
  ``` cpp
7290
  template<class T> void g(T);
7291
  g({1,2,3}); // error: no argument deduced for T
7292
  ```
 
7376
  template non-type argument `i` can be deduced if `P` and `A` have one of
7377
  the following forms:
7378
 
7379
  ``` cpp
7380
  T
7381
+ cv T
7382
  T*
7383
  T&
7384
  T&&
7385
  T[integer-constant]
7386
  template-name<T> (where template-name refers to a class template)
 
7402
  TT<T>
7403
  TT<i>
7404
  TT<>
7405
  ```
7406
 
7407
+ where `(T)` represents a parameter-type-list [[dcl.fct]] where at least
7408
+ one parameter type contains a `T`, and `()` represents a
7409
  parameter-type-list where no parameter type contains a `T`. Similarly,
7410
  `<T>` represents template argument lists where at least one argument
7411
  contains a `T`, `<i>` represents template argument lists where at least
7412
  one argument contains an `i` and `<>` represents template argument lists
7413
  where no argument contains a `T` or an `i`.
 
7419
  is not the last template argument, the entire template argument list is
7420
  a non-deduced context. If `Pᵢ` is a pack expansion, then the pattern of
7421
  `Pᵢ` is compared with each remaining argument in the template argument
7422
  list of `A`. Each comparison deduces template arguments for subsequent
7423
  positions in the template parameter packs expanded by `Pᵢ`. During
7424
+ partial ordering [[temp.deduct.partial]], if `Aᵢ` was originally a pack
7425
+ expansion:
7426
 
7427
  - if `P` does not contain a template argument corresponding to `Aᵢ` then
7428
  `Aᵢ` is ignored;
7429
  - otherwise, if `Pᵢ` is not a pack expansion, template argument
7430
  deduction fails.
 
7444
  ```
7445
 
7446
  — *end example*]
7447
 
7448
  Similarly, if `P` has a form that contains `(T)`, then each parameter
7449
+ type `Pᵢ` of the respective parameter-type-list [[dcl.fct]] of `P` is
7450
  compared with the corresponding parameter type `Aᵢ` of the corresponding
7451
  parameter-type-list of `A`. If `P` and `A` are function types that
7452
+ originated from deduction when taking the address of a function template
7453
+ [[temp.deduct.funcaddr]] or when deducing template arguments from a
7454
+ function declaration [[temp.deduct.decl]] and `Pᵢ` and `Aᵢ` are
7455
+ parameters of the top-level parameter-type-list of `P` and `A`,
7456
+ respectively, `Pᵢ` is adjusted if it is a forwarding reference
7457
+ [[temp.deduct.call]] and `Aᵢ` is an lvalue reference, in which case the
7458
  type of `Pᵢ` is changed to be the template parameter type (i.e., `T&&`
7459
  is changed to simply `T`).
7460
 
7461
  [*Note 2*: As a result, when `Pᵢ` is `T&&` and `Aᵢ` is `X&`, the
7462
  adjusted `Pᵢ` will be `T`, causing `T` to be deduced as
 
7479
  If the *parameter-declaration* corresponding to `Pᵢ` is a function
7480
  parameter pack, then the type of its *declarator-id* is compared with
7481
  each remaining parameter type in the parameter-type-list of `A`. Each
7482
  comparison deduces template arguments for subsequent positions in the
7483
  template parameter packs expanded by the function parameter pack. During
7484
+ partial ordering [[temp.deduct.partial]], if `Aᵢ` was originally a
7485
  function parameter pack:
7486
 
7487
  - if `P` does not contain a function parameter type corresponding to
7488
  `Aᵢ` then `Aᵢ` is ignored;
7489
  - otherwise, if `Pᵢ` is not a function parameter pack, template argument
 
7541
  template<typename T, T n> struct C<A<n>> {
7542
  using Q = T;
7543
  };
7544
 
7545
  using R = long;
7546
+ using R = C<A<2>>::Q; // OK; T was deduced as long from the
7547
  // template argument value in the type A<2>
7548
  ```
7549
 
7550
  — *end example*]
7551
 
 
7558
  template<typename T, T n> struct S<int[n]> {
7559
  using Q = T;
7560
  };
7561
 
7562
  using V = decltype(sizeof 0);
7563
+ using V = S<int[42]>::Q; // OK; T was deduced as std::size_t from the type int[42]
7564
  ```
7565
 
7566
  — *end example*]
7567
 
7568
  [*Example 10*:
 
7587
  template<int i> void f2(int a[i][20]);
7588
  template<int i> void f3(int (&a)[i][20]);
7589
 
7590
  void g() {
7591
  int v[10][20];
7592
+ f1(v); // OK: i deduced as 20
7593
  f1<20>(v); // OK
7594
  f2(v); // error: cannot deduce template-argument i
7595
  f2<10>(v); // OK
7596
+ f3(v); // OK: i deduced as 10
7597
  }
7598
  ```
7599
 
7600
  — *end note*]
7601
 
 
7637
  typename B<i>::Y y); // i is not deduced here
7638
  A<int> a;
7639
  B<77> b;
7640
 
7641
  int x = deduce<77>(a.xm, 62, b.ym);
7642
+ // T deduced as int; a.xm must be convertible to A<int>::X
7643
+ // i is explicitly specified to be 77; b.ym must be convertible to B<77>::Y
7644
  ```
7645
 
7646
  — *end note*]
7647
 
7648
  If `P` has a form that contains `<i>`, and if the type of `i` differs
7649
  from the type of the corresponding template parameter of the template
7650
  named by the enclosing *simple-template-id*, deduction fails. If `P` has
7651
  a form that contains `[i]`, and if the type of `i` is not an integral
7652
+ type, deduction fails.[^13]
7653
 
7654
  [*Example 12*:
7655
 
7656
  ``` cpp
7657
  template<int i> class A { ... };
 
7671
  ```
7672
 
7673
  — *end example*]
7674
 
7675
  A *template-argument* can be deduced from a function, pointer to
7676
+ function, or pointer-to-member-function type.
7677
 
7678
  [*Example 13*:
7679
 
7680
  ``` cpp
7681
  template<class T> void f(void(*)(T,int));
 
7724
  f(ab); // calls f(A<B>)
7725
  ```
7726
 
7727
  — *end example*]
7728
 
7729
+ [*Note 6*: Template argument deduction involving parameter packs
7730
+ [[temp.variadic]] can deduce zero or more arguments for each parameter
7731
  pack. — *end note*]
7732
 
7733
  [*Example 16*:
7734
 
7735
  ``` cpp
 
7755
  #### Deducing template arguments from a function declaration <a id="temp.deduct.decl">[[temp.deduct.decl]]</a>
7756
 
7757
  In a declaration whose *declarator-id* refers to a specialization of a
7758
  function template, template argument deduction is performed to identify
7759
  the specialization to which the declaration refers. Specifically, this
7760
+ is done for explicit instantiations [[temp.explicit]], explicit
7761
+ specializations [[temp.expl.spec]], and certain friend declarations
7762
+ [[temp.friend]]. This is also done to determine whether a deallocation
7763
  function template specialization matches a placement `operator new` (
7764
+ [[basic.stc.dynamic.deallocation]], [[expr.new]]). In all these cases,
7765
  `P` is the type of the function template being considered as a potential
7766
  match and `A` is either the function type from the declaration or the
7767
  type of the deallocation function that would match the placement
7768
  `operator new` as described in  [[expr.new]]. The deduction is done as
7769
  described in  [[temp.deduct.type]].
7770
 
7771
  If, for the set of function templates so considered, there is either no
7772
+ match or more than one match after partial ordering has been considered
7773
+ [[temp.func.order]], deduction fails and, in the declaration cases, the
7774
+ program is ill-formed.
7775
 
7776
  ### Overload resolution <a id="temp.over">[[temp.over]]</a>
7777
 
7778
+ When a call to the name of a function or function template is written
7779
+ (explicitly, or implicitly using the operator notation), template
7780
+ argument deduction [[temp.deduct]] and checking of any explicit template
7781
+ arguments [[temp.arg]] are performed for each function template to find
7782
+ the template argument values (if any) that can be used with that
7783
+ function template to instantiate a function template specialization that
7784
+ can be invoked with the call arguments. For each function template, if
7785
+ the argument deduction and checking succeeds, the *template-argument*s
7786
+ (deduced and/or explicit) are used to synthesize the declaration of a
7787
+ single function template specialization which is added to the candidate
 
 
7788
  functions set to be used in overload resolution. If, for a given
7789
  function template, argument deduction fails or the synthesized function
7790
  template specialization would be ill-formed, no such function is added
7791
  to the set of candidate functions for that template. The complete set of
7792
  candidate functions includes all the synthesized declarations and all of
7793
  the non-template overloaded functions of the same name. The synthesized
7794
  declarations are treated like any other functions in the remainder of
7795
  overload resolution, except as explicitly noted in 
7796
+ [[over.match.best]].[^14]
7797
 
7798
  [*Example 1*:
7799
 
7800
  ``` cpp
7801
  template<class T> T max(T a, T b) { return a>b?a:b; }
 
7877
  unless a specialization for `f<const char*>`, either implicitly or
7878
  explicitly generated, is present in some translation unit.
7879
 
7880
  — *end example*]
7881
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7882
  <!-- Link reference definitions -->
7883
  [basic.def]: basic.md#basic.def
7884
  [basic.def.odr]: basic.md#basic.def.odr
7885
  [basic.link]: basic.md#basic.link
7886
  [basic.lookup]: basic.md#basic.lookup
 
7888
  [basic.lookup.classref]: basic.md#basic.lookup.classref
7889
  [basic.lookup.qual]: basic.md#basic.lookup.qual
7890
  [basic.lookup.unqual]: basic.md#basic.lookup.unqual
7891
  [basic.scope]: basic.md#basic.scope
7892
  [basic.scope.hiding]: basic.md#basic.scope.hiding
7893
+ [basic.scope.namespace]: basic.md#basic.scope.namespace
7894
  [basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
7895
  [basic.types]: basic.md#basic.types
 
 
7896
  [class.access]: class.md#class.access
7897
+ [class.base.init]: class.md#class.base.init
7898
+ [class.conv.fct]: class.md#class.conv.fct
7899
+ [class.ctor]: class.md#class.ctor
7900
+ [class.default.ctor]: class.md#class.default.ctor
7901
  [class.derived]: class.md#class.derived
7902
+ [class.dtor]: class.md#class.dtor
7903
  [class.friend]: class.md#class.friend
7904
  [class.local]: class.md#class.local
7905
  [class.mem]: class.md#class.mem
7906
  [class.member.lookup]: class.md#class.member.lookup
7907
+ [class.pre]: class.md#class.pre
7908
  [class.qual]: basic.md#class.qual
7909
+ [class.temporary]: basic.md#class.temporary
7910
+ [conv]: expr.md#conv
7911
+ [conv.array]: expr.md#conv.array
7912
+ [conv.fctptr]: expr.md#conv.fctptr
7913
+ [conv.func]: expr.md#conv.func
7914
+ [conv.lval]: expr.md#conv.lval
7915
+ [conv.qual]: expr.md#conv.qual
7916
  [dcl.align]: dcl.md#dcl.align
7917
  [dcl.attr.grammar]: dcl.md#dcl.attr.grammar
7918
+ [dcl.decl]: dcl.md#dcl.decl
7919
  [dcl.enum]: dcl.md#dcl.enum
7920
  [dcl.fct]: dcl.md#dcl.fct
7921
  [dcl.fct.def.general]: dcl.md#dcl.fct.def.general
7922
  [dcl.fct.default]: dcl.md#dcl.fct.default
7923
  [dcl.init]: dcl.md#dcl.init
7924
  [dcl.init.list]: dcl.md#dcl.init.list
7925
  [dcl.meaning]: dcl.md#dcl.meaning
7926
+ [dcl.pre]: dcl.md#dcl.pre
7927
  [dcl.spec.auto]: dcl.md#dcl.spec.auto
7928
+ [dcl.stc]: dcl.md#dcl.stc
7929
  [dcl.struct.bind]: dcl.md#dcl.struct.bind
 
7930
  [dcl.type.class.deduct]: dcl.md#dcl.type.class.deduct
7931
  [dcl.type.elab]: dcl.md#dcl.type.elab
7932
+ [dcl.type.simple]: dcl.md#dcl.type.simple
7933
  [except.spec]: except.md#except.spec
7934
  [expr.const]: expr.md#expr.const
7935
+ [expr.context]: expr.md#expr.context
7936
+ [expr.log.and]: expr.md#expr.log.and
7937
+ [expr.log.or]: expr.md#expr.log.or
7938
  [expr.new]: expr.md#expr.new
7939
  [expr.prim.fold]: expr.md#expr.prim.fold
7940
+ [expr.prim.id]: expr.md#expr.prim.id
7941
+ [expr.prim.id.unqual]: expr.md#expr.prim.id.unqual
7942
+ [expr.prim.lambda.capture]: expr.md#expr.prim.lambda.capture
7943
  [expr.prim.lambda.closure]: expr.md#expr.prim.lambda.closure
7944
  [expr.ref]: expr.md#expr.ref
7945
  [expr.sizeof]: expr.md#expr.sizeof
7946
  [expr.typeid]: expr.md#expr.typeid
7947
+ [expr.unary.op]: expr.md#expr.unary.op
7948
  [implimits]: limits.md#implimits
7949
  [intro.defs]: intro.md#intro.defs
7950
+ [intro.object]: basic.md#intro.object
7951
  [lex.string]: lex.md#lex.string
7952
  [namespace.def]: dcl.md#namespace.def
7953
  [namespace.memdef]: dcl.md#namespace.memdef
7954
  [namespace.udecl]: dcl.md#namespace.udecl
7955
  [over.ics.rank]: over.md#over.ics.rank
7956
  [over.match]: over.md#over.match
7957
  [over.match.best]: over.md#over.match.best
7958
  [over.match.class.deduct]: over.md#over.match.class.deduct
7959
  [over.match.conv]: over.md#over.match.conv
7960
+ [over.match.oper]: over.md#over.match.oper
7961
  [over.match.ref]: over.md#over.match.ref
7962
+ [over.match.viable]: over.md#over.match.viable
7963
  [over.over]: over.md#over.over
7964
+ [special]: class.md#special
7965
  [stmt.if]: stmt.md#stmt.if
7966
+ [support.types]: support.md#support.types
 
7967
  [temp]: #temp
7968
  [temp.alias]: #temp.alias
7969
  [temp.arg]: #temp.arg
7970
  [temp.arg.explicit]: #temp.arg.explicit
7971
  [temp.arg.nontype]: #temp.arg.nontype
 
7974
  [temp.class]: #temp.class
7975
  [temp.class.order]: #temp.class.order
7976
  [temp.class.spec]: #temp.class.spec
7977
  [temp.class.spec.match]: #temp.class.spec.match
7978
  [temp.class.spec.mfunc]: #temp.class.spec.mfunc
7979
+ [temp.concept]: #temp.concept
7980
+ [temp.constr]: #temp.constr
7981
+ [temp.constr.atomic]: #temp.constr.atomic
7982
+ [temp.constr.constr]: #temp.constr.constr
7983
+ [temp.constr.decl]: #temp.constr.decl
7984
+ [temp.constr.normal]: #temp.constr.normal
7985
+ [temp.constr.op]: #temp.constr.op
7986
+ [temp.constr.order]: #temp.constr.order
7987
  [temp.decls]: #temp.decls
7988
  [temp.deduct]: #temp.deduct
7989
  [temp.deduct.call]: #temp.deduct.call
7990
  [temp.deduct.conv]: #temp.deduct.conv
7991
  [temp.deduct.decl]: #temp.deduct.decl
 
8002
  [temp.dep.type]: #temp.dep.type
8003
  [temp.expl.spec]: #temp.expl.spec
8004
  [temp.explicit]: #temp.explicit
8005
  [temp.fct]: #temp.fct
8006
  [temp.fct.spec]: #temp.fct.spec
8007
+ [temp.fold.empty]: #temp.fold.empty
8008
  [temp.friend]: #temp.friend
8009
  [temp.func.order]: #temp.func.order
8010
  [temp.inject]: #temp.inject
8011
  [temp.inst]: #temp.inst
8012
  [temp.local]: #temp.local
 
8018
  [temp.nondep]: #temp.nondep
8019
  [temp.over]: #temp.over
8020
  [temp.over.link]: #temp.over.link
8021
  [temp.param]: #temp.param
8022
  [temp.point]: #temp.point
8023
+ [temp.pre]: #temp.pre
8024
  [temp.res]: #temp.res
8025
  [temp.spec]: #temp.spec
8026
  [temp.static]: #temp.static
8027
  [temp.type]: #temp.type
8028
  [temp.variadic]: #temp.variadic
 
8039
 
8040
  [^3]: There is no such ambiguity in a default *template-argument*
8041
  because the form of the *template-parameter* determines the
8042
  allowable forms of the *template-argument*.
8043
 
8044
+ [^4]: A constraint is in disjunctive normal form when it is a
8045
+ disjunction of clauses where each clause is a conjunction of atomic
8046
+ constraints.
8047
 
8048
+ \[*Example 5*: For atomic constraints A, B, and C, the disjunctive
8049
+ normal form of the constraint A ∧ (B ∨ C) is (A ∧ B) ∨ (A ∧ C). Its
8050
+ disjunctive clauses are (A ∧ B) and (A ∧ C). — *end example*]
8051
+
8052
+ [^5]: A constraint is in conjunctive normal form when it is a
8053
+ conjunction of clauses where each clause is a disjunction of atomic
8054
+ constraints.
8055
+
8056
+ \[*Example 6*: For atomic constraints A, B, and C, the constraint
8057
+ A ∧ (B ∨ C) is in conjunctive normal form. Its conjunctive clauses
8058
+ are A and (B ∨ C). — *end example*]
8059
+
8060
+ [^6]: The identity of enumerators is not preserved.
8061
+
8062
+ [^7]: An array as a *template-parameter* decays to a pointer.
8063
+
8064
+ [^8]: There is no way in which they could be used.
8065
+
8066
+ [^9]: That is, declarations of non-template functions do not merely
8067
  guide overload resolution of function template specializations with
8068
+ the same name. If such a non-template function is odr-used
8069
+ [[basic.def.odr]] in a program, it must be defined; it will not be
8070
  implicitly instantiated using the function template definition.
8071
 
8072
+ [^10]: This includes friend function declarations.
8073
+
8074
+ [^11]: Friend declarations do not introduce new names into any scope,
8075
  either when the template is declared or when it is instantiated.
8076
 
8077
+ [^12]: Default arguments are not considered to be arguments in this
8078
  context; they only become arguments after a function has been
8079
  selected.
8080
 
8081
+ [^13]: Although the *template-argument* corresponding to a
8082
  *template-parameter* of type `bool` may be deduced from an array
8083
  bound, the resulting value will always be `true` because the array
8084
  bound will be nonzero.
8085
 
8086
+ [^14]: The parameters of function template specializations contain no
8087
  template parameter types. The set of conversions allowed on deduced
8088
  arguments is limited, because the argument deduction process
8089
  produces function templates with parameters that either match the
8090
  call arguments exactly or differ only in ways that can be bridged by
8091
  the allowed limited conversions. Non-deduced arguments allow the