From Jason Turner

[temp.decls]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp3_0ziqb9/{from.md → to.md} +621 -218
tmp/tmp3_0ziqb9/{from.md → to.md} RENAMED
@@ -12,20 +12,22 @@ template<class T1, int I> void sort<T1, I>(T1 data[I]); // error
12
  ```
13
 
14
  — *end example*]
15
 
16
  [*Note 1*: However, this syntax is allowed in class template partial
17
- specializations ([[temp.class.spec]]). — *end note*]
18
 
19
- For purposes of name lookup and instantiation, default arguments and
20
- *noexcept-specifier*s of function templates and default arguments and
21
- *noexcept-specifier*s of member functions of class templates are
22
- considered definitions; each default argument or *noexcept-specifier* is
23
- a separate definition which is unrelated to the function template
24
- definition or to any other default arguments or *noexcept-specifier*s.
25
- For the purpose of instantiation, the substatements of a constexpr if
26
- statement ([[stmt.if]]) are considered definitions.
 
 
27
 
28
  Because an *alias-declaration* cannot declare a *template-id*, it is not
29
  possible to partially or explicitly specialize an alias template.
30
 
31
  ### Class templates <a id="temp.class">[[temp.class]]</a>
@@ -58,18 +60,18 @@ other words, `Array` is a parameterized type with `T` as its parameter.
58
  — *end example*]
59
 
60
  When a member function, a member class, a member enumeration, a static
61
  data member or a member template of a class template is defined outside
62
  of the class template definition, the member definition is defined as a
63
- template definition in which the *template-parameter*s are those of the
64
- class template. The names of the template parameters used in the
65
- definition of the member may be different from the template parameter
66
- names used in the class template definition. The template argument list
67
- following the class template name in the member definition shall name
68
- the parameters in the same order as the one used in the template
69
- parameter list of the member. Each template parameter pack shall be
70
- expanded with an ellipsis in the template argument list.
71
 
72
  [*Example 2*:
73
 
74
  ``` cpp
75
  template<class T1, class T2> struct A {
@@ -89,16 +91,36 @@ template<class ... Types> struct B {
89
 
90
  template<class ... Types> void B<Types ...>::f3() { } // OK
91
  template<class ... Types> void B<Types>::f4() { } // error
92
  ```
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  — *end example*]
95
 
96
  In a redeclaration, partial specialization, explicit specialization or
97
  explicit instantiation of a class template, the *class-key* shall agree
98
- in kind with the original class template declaration (
99
- [[dcl.type.elab]]).
100
 
101
  #### Member functions of class templates <a id="temp.mem.func">[[temp.mem.func]]</a>
102
 
103
  A member function of a class template may be defined outside of the
104
  class template definition in which it is declared.
@@ -114,50 +136,110 @@ public:
114
  T& operator[](int);
115
  T& elem(int i) { return v[i]; }
116
  };
117
  ```
118
 
119
- declares three function templates. The subscript function might be
120
- defined like this:
121
 
122
  ``` cpp
123
  template<class T> T& Array<T>::operator[](int i) {
124
  if (i<0 || sz<=i) error("Array: range error");
125
  return v[i];
126
  }
127
  ```
128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  — *end example*]
130
 
131
  The *template-argument*s for a member function of a class template are
132
  determined by the *template-argument*s of the type of the object for
133
  which the member function is called.
134
 
135
  [*Example 2*:
136
 
137
- The *template-argument* for `Array<T>::operator[]()` will be determined
138
- by the `Array` to which the subscripting operation is applied.
139
 
140
  ``` cpp
141
  Array<int> v1(20);
142
  Array<dcomplex> v2(30);
143
 
144
- v1[3] = 7; // Array<int>::operator[]()
145
- v2[3] = dcomplex(7,8); // Array<dcomplex>::operator[]()
146
  ```
147
 
148
  — *end example*]
149
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  #### Member classes of class templates <a id="temp.mem.class">[[temp.mem.class]]</a>
151
 
152
  A member class of a class template may be defined outside the class
153
  template definition in which it is declared.
154
 
155
  [*Note 1*:
156
 
157
  The member class must be defined before its first use that requires an
158
- instantiation ([[temp.inst]]). For example,
159
 
160
  ``` cpp
161
  template<class T> struct A {
162
  class B;
163
  };
@@ -230,13 +312,13 @@ A<int>::E e = A<int>::e1;
230
 
231
  A template can be declared within a class or class template; such a
232
  template is called a member template. A member template can be defined
233
  within or outside its class definition or class template definition. A
234
  member template of a class template that is defined outside of its class
235
- template definition shall be specified with the *template-parameter*s of
236
- the class template followed by the *template-parameter*s of the member
237
- template.
238
 
239
  [*Example 1*:
240
 
241
  ``` cpp
242
  template<class T> struct string {
@@ -248,20 +330,39 @@ template<class T> template<class T2> int string<T>::compare(const T2& s) {
248
  }
249
  ```
250
 
251
  — *end example*]
252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
  A local class of non-closure type shall not have member templates.
254
- Access control rules (Clause  [[class.access]]) apply to member template
255
- names. A destructor shall not be a member template. A non-template
256
- member function ([[dcl.fct]]) with a given name and type and a member
257
- function template of the same name, which could be used to generate a
258
  specialization of the same type, can both be declared in a class. When
259
  both exist, a use of that name and type refers to the non-template
260
  member unless an explicit template argument list is supplied.
261
 
262
- [*Example 2*:
263
 
264
  ``` cpp
265
  template <class T> struct A {
266
  void f(int);
267
  template <class T2> void f(T2);
@@ -280,11 +381,11 @@ int main() {
280
 
281
  — *end example*]
282
 
283
  A member function template shall not be virtual.
284
 
285
- [*Example 3*:
286
 
287
  ``` cpp
288
  template <class T> struct AA {
289
  template <class C> virtual void g(C); // error
290
  virtual void f(); // OK
@@ -294,11 +395,11 @@ template <class T> struct AA {
294
  — *end example*]
295
 
296
  A specialization of a member function template does not override a
297
  virtual function from a base class.
298
 
299
- [*Example 4*:
300
 
301
  ``` cpp
302
  class B {
303
  virtual void f(int);
304
  };
@@ -313,11 +414,11 @@ class D : public B {
313
 
314
  A specialization of a conversion function template is referenced in the
315
  same way as a non-template conversion function that converts to the same
316
  type.
317
 
318
- [*Example 5*:
319
 
320
  ``` cpp
321
  struct A {
322
  template <class T> operator T*();
323
  };
@@ -332,27 +433,25 @@ int main() {
332
  }
333
  ```
334
 
335
  — *end example*]
336
 
337
- [*Note 1*: Because the explicit template argument list follows the
338
- function template name, and because conversion member function templates
339
- and constructor member function templates are called without using a
340
- function name, there is no way to provide an explicit template argument
341
- list for these function templates. — *end note*]
342
 
343
  A specialization of a conversion function template is not found by name
344
  lookup. Instead, any conversion function templates visible in the
345
  context of the use are considered. For each such operator, if argument
346
- deduction succeeds ([[temp.deduct.conv]]), the resulting specialization
347
- is used as if found by name lookup.
348
 
349
  A *using-declaration* in a derived class cannot refer to a
350
  specialization of a conversion function template in a base class.
351
 
352
- Overload resolution ([[over.ics.rank]]) and partial ordering (
353
- [[temp.func.order]]) are used to select the best conversion function
354
  among multiple specializations of conversion function templates and/or
355
  non-template conversion functions.
356
 
357
  ### Variadic templates <a id="temp.variadic">[[temp.variadic]]</a>
358
 
@@ -378,84 +477,103 @@ more function arguments.
378
  [*Example 2*:
379
 
380
  ``` cpp
381
  template<class ... Types> void f(Types ... args);
382
 
383
- f(); // OK: args contains no arguments
384
- f(1); // OK: args contains one argument: int
385
- f(2, 1.0); // OK: args contains two arguments: int and double
386
  ```
387
 
388
  — *end example*]
389
 
390
- A *parameter pack* is either a template parameter pack or a function
391
- parameter pack.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
392
 
393
  A *pack expansion* consists of a *pattern* and an ellipsis, the
394
  instantiation of which produces zero or more instantiations of the
395
  pattern in a list (described below). The form of the pattern depends on
396
  the context in which the expansion occurs. Pack expansions can occur in
397
  the following contexts:
398
 
399
- - In a function parameter pack ([[dcl.fct]]); the pattern is the
400
  *parameter-declaration* without the ellipsis.
401
- - In a *using-declaration* ([[namespace.udecl]]); the pattern is a
402
  *using-declarator*.
403
- - In a template parameter pack that is a pack expansion (
404
- [[temp.param]]):
405
  - if the template parameter pack is a *parameter-declaration*; the
406
  pattern is the *parameter-declaration* without the ellipsis;
407
- - if the template parameter pack is a *type-parameter* with a
408
- *template-parameter-list*; the pattern is the corresponding
409
- *type-parameter* without the ellipsis.
410
- - In an *initializer-list* ([[dcl.init]]); the pattern is an
411
  *initializer-clause*.
412
- - In a *base-specifier-list* (Clause  [[class.derived]]); the pattern is
413
- a *base-specifier*.
414
- - In a *mem-initializer-list* ([[class.base.init]]) for a
415
  *mem-initializer* whose *mem-initializer-id* denotes a base class; the
416
  pattern is the *mem-initializer*.
417
- - In a *template-argument-list* ([[temp.arg]]); the pattern is a
418
  *template-argument*.
419
- - In an *attribute-list* ([[dcl.attr.grammar]]); the pattern is an
420
  *attribute*.
421
- - In an *alignment-specifier* ([[dcl.align]]); the pattern is the
422
  *alignment-specifier* without the ellipsis.
423
- - In a *capture-list* ([[expr.prim.lambda]]); the pattern is a
424
- *capture*.
425
- - In a `sizeof...` expression ([[expr.sizeof]]); the pattern is an
426
  *identifier*.
427
- - In a *fold-expression* ([[expr.prim.fold]]); the pattern is the
428
- *cast-expression* that contains an unexpanded parameter pack.
429
 
430
- [*Example 3*:
431
 
432
  ``` cpp
433
  template<class ... Types> void f(Types ... rest);
434
  template<class ... Types> void g(Types ... rest) {
435
  f(&rest ...); // ``&rest ...'' is a pack expansion; ``&rest'' is its pattern
436
  }
437
  ```
438
 
439
  — *end example*]
440
 
441
- For the purpose of determining whether a parameter pack satisfies a rule
442
- regarding entities other than parameter packs, the parameter pack is
443
- considered to be the entity that would result from an instantiation of
444
- the pattern in which it appears.
445
 
446
- A parameter pack whose name appears within the pattern of a pack
447
- expansion is expanded by that pack expansion. An appearance of the name
448
- of a parameter pack is only expanded by the innermost enclosing pack
449
- expansion. The pattern of a pack expansion shall name one or more
450
- parameter packs that are not expanded by a nested pack expansion; such
451
- parameter packs are called *unexpanded parameter packs* in the pattern.
452
- All of the parameter packs expanded by a pack expansion shall have the
453
- same number of arguments specified. An appearance of a name of a
454
- parameter pack that is not expanded is ill-formed.
455
 
456
- [*Example 4*:
457
 
458
  ``` cpp
459
  template<typename...> struct Tuple {};
460
  template<typename T1, typename T2> struct Pair {};
461
 
@@ -471,36 +589,40 @@ typedef zip<short>::with<unsigned short, unsigned>::type T2;
471
  // error: different number of arguments specified for Args1 and Args2
472
 
473
  template<class ... Args>
474
  void g(Args ... args) { // OK: Args is expanded by the function parameter pack args
475
  f(const_cast<const Args*>(&args)...); // OK: ``Args'' and ``args'' are expanded
476
- f(5 ...); // error: pattern does not contain any parameter packs
477
- f(args); // error: parameter pack ``args'' is not expanded
478
  f(h(args ...) + args ...); // OK: first ``args'' expanded within h,
479
  // second ``args'' expanded within f
480
  }
481
  ```
482
 
483
  — *end example*]
484
 
485
  The instantiation of a pack expansion that is neither a `sizeof...`
486
- expression nor a *fold-expression* produces a list
487
- $\mathtt{E}_1, \mathtt{E}_2, \dotsc, \mathtt{E}_N$, where N is the
488
- number of elements in the pack expansion parameters. Each Eᵢ is
489
- generated by instantiating the pattern and replacing each pack expansion
490
- parameter with its ith element. Such an element, in the context of the
491
- instantiation, is interpreted as follows:
492
 
493
  - if the pack is a template parameter pack, the element is a template
494
- parameter ([[temp.param]]) of the corresponding kind (type or
495
- non-type) designating the type or value from the template argument;
496
- otherwise,
497
  - if the pack is a function parameter pack, the element is an
498
- *id-expression* designating the function parameter that resulted from
499
- the instantiation of the pattern where the pack is declared.
 
 
 
 
 
500
 
501
- All of the Eᵢ become elements in the enclosing list.
502
 
503
  [*Note 1*: The variety of list varies with the context:
504
  *expression-list*, *base-specifier-list*, *template-argument-list*,
505
  etc. — *end note*]
506
 
@@ -508,11 +630,11 @@ When N is zero, the instantiation of the expansion produces an empty
508
  list. Such an instantiation does not alter the syntactic interpretation
509
  of the enclosing construct, even in cases where omitting the list
510
  entirely would otherwise be ill-formed or would result in an ambiguity
511
  in the grammar.
512
 
513
- [*Example 5*:
514
 
515
  ``` cpp
516
  template<class... T> struct X : T... { };
517
  template<class... T> void f(T... values) {
518
  X<T...> x(values...);
@@ -522,13 +644,13 @@ template void f<>(); // OK: X<> has no base classes
522
  // x is a variable of type X<> that is value-initialized
523
  ```
524
 
525
  — *end example*]
526
 
527
- The instantiation of a `sizeof...` expression ([[expr.sizeof]])
528
- produces an integral constant containing the number of elements in the
529
- parameter pack it expands.
530
 
531
  The instantiation of a *fold-expression* produces:
532
 
533
  - `((`E₁ *op* E₂`)` *op* ⋯`)` *op* $\mathtt{E}_N$ for a unary left fold,
534
  - E₁ *op* `(`⋯ *op* `(`$\mathtt{E}_{N-1}$ *op* $\mathtt{E}_N$`))` for a
@@ -539,15 +661,15 @@ The instantiation of a *fold-expression* produces:
539
  E`)))` for a binary right fold.
540
 
541
  In each case, *op* is the *fold-operator*, N is the number of elements
542
  in the pack expansion parameters, and each Eᵢ is generated by
543
  instantiating the pattern and replacing each pack expansion parameter
544
- with its ith element. For a binary fold-expression, E is generated by
545
  instantiating the *cast-expression* that did not contain an unexpanded
546
- parameter pack.
547
 
548
- [*Example 6*:
549
 
550
  ``` cpp
551
  template<typename ...Args>
552
  bool all(Args ...args) { return (... && args); }
553
 
@@ -558,17 +680,17 @@ Within the instantiation of `all`, the returned expression expands to
558
  `((true && true) && true) && false`, which evaluates to `false`.
559
 
560
  — *end example*]
561
 
562
  If N is zero for a unary fold-expression, the value of the expression is
563
- shown in Table  [[tab:fold.empty]]; if the operator is not listed in
564
- Table  [[tab:fold.empty]], the instantiation is ill-formed.
565
 
566
- **Table: Value of folding empty sequences** <a id="tab:fold.empty">[tab:fold.empty]</a>
567
 
568
- | Operator | Value when parameter pack is empty |
569
- | -------- | ---------------------------------- |
570
  | `&&` | `true` |
571
  | `||` | `false` |
572
  | `,` | `void()` |
573
 
574
 
@@ -586,11 +708,11 @@ declaration that is not a template declaration:
586
  non-template function is found in the specified class or namespace,
587
  the friend declaration refers to that function, otherwise,
588
  - if the name of the friend is a *qualified-id* and a matching function
589
  template is found in the specified class or namespace, the friend
590
  declaration refers to the deduced specialization of that function
591
- template ([[temp.deduct.decl]]), otherwise,
592
  - the name shall be an *unqualified-id* that declares (or redeclares) a
593
  non-template function.
594
 
595
  [*Example 1*:
596
 
@@ -641,13 +763,13 @@ class A {
641
  ```
642
 
643
  — *end example*]
644
 
645
  A template friend declaration specifies that all specializations of that
646
- template, whether they are implicitly instantiated ([[temp.inst]]),
647
- partially specialized ([[temp.class.spec]]) or explicitly specialized (
648
- [[temp.expl.spec]]), are friends of the class containing the template
649
  friend declaration.
650
 
651
  [*Example 3*:
652
 
653
  ``` cpp
@@ -660,52 +782,63 @@ template<class T> struct A { X::Y ab; }; // OK
660
  template<class T> struct A<T*> { X::Y ab; }; // OK
661
  ```
662
 
663
  — *end example*]
664
 
665
- A member of a class template may be declared to be a friend of a
666
- non-template class. In this case, the corresponding member of every
667
- specialization of the primary class template and class template partial
668
- specializations thereof is a friend of the class granting friendship.
669
- For explicit specializations and specializations of partial
670
- specializations, the corresponding member is the member (if any) that
671
- has the same name, kind (type, function, class template, or function
672
- template), template parameters, and signature as the member of the class
673
- template instantiation that would otherwise have been generated.
 
 
 
674
 
675
  [*Example 4*:
676
 
677
  ``` cpp
678
  template<class T> struct A {
679
  struct B { };
680
  void f();
681
  struct D {
682
  void g();
683
  };
 
 
684
  };
685
  template<> struct A<int> {
686
  struct B { };
687
  int f();
688
  struct D {
689
  void g();
690
  };
 
 
 
 
691
  };
692
 
693
  class C {
694
  template<class T> friend struct A<T>::B; // grants friendship to A<int>::B even though
695
  // it is not a specialization of A<T>::B
696
  template<class T> friend void A<T>::f(); // does not grant friendship to A<int>::f()
697
  // because its return type does not match
698
- template<class T> friend void A<T>::D::g(); // does not grant friendship to A<int>::D::g()
699
- // because A<int>::D is not a specialization of A<T>::D
700
- };
 
 
701
  ```
702
 
703
  — *end example*]
704
 
705
  [*Note 1*: A friend declaration may first declare a member of an
706
- enclosing namespace scope ([[temp.inject]]). — *end note*]
707
 
708
  A friend template shall not be declared in a local class.
709
 
710
  Friend declarations shall not declare partial specializations.
711
 
@@ -720,32 +853,40 @@ class X {
720
 
721
  — *end example*]
722
 
723
  When a friend declaration refers to a specialization of a function
724
  template, the function parameter declarations shall not include default
725
- arguments, nor shall the inline specifier be used in such a declaration.
 
 
 
 
 
 
 
 
726
 
727
  ### Class template partial specializations <a id="temp.class.spec">[[temp.class.spec]]</a>
728
 
729
  A *primary class template* declaration is one in which the class
730
  template name is an identifier. A template declaration in which the
731
  class template name is a *simple-template-id* is a *partial
732
  specialization* of the class template named in the *simple-template-id*.
733
  A partial specialization of a class template provides an alternative
734
  definition of the template that is used instead of the primary
735
  definition when the arguments in a specialization match those given in
736
- the partial specialization ([[temp.class.spec.match]]). The primary
737
  template shall be declared before any specializations of that template.
738
  A partial specialization shall be declared before the first use of a
739
  class template specialization that would make use of the partial
740
  specialization as the result of an implicit or explicit instantiation in
741
  every translation unit in which such a use occurs; no diagnostic is
742
  required.
743
 
744
  Each class template partial specialization is a distinct template and
745
  definitions shall be provided for the members of a template partial
746
- specialization ([[temp.class.spec.mfunc]]).
747
 
748
  [*Example 1*:
749
 
750
  ``` cpp
751
  template<class T1, class T2, int I> class A { };
@@ -759,25 +900,46 @@ The first declaration declares the primary (unspecialized) class
759
  template. The second and subsequent declarations declare partial
760
  specializations of the primary template.
761
 
762
  — *end example*]
763
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
764
  The template parameters are specified in the angle bracket enclosed list
765
  that immediately follows the keyword `template`. For partial
766
  specializations, the template argument list is explicitly written
767
  immediately following the class template name. For primary templates,
768
  this list is implicitly described by the template parameter list.
769
  Specifically, the order of the template arguments is the sequence in
770
  which they appear in the template parameter list.
771
 
772
- [*Example 2*: The template argument list for the primary template in
773
  the example above is `<T1,` `T2,` `I>`. — *end example*]
774
 
775
  [*Note 1*:
776
 
777
- The template argument list shall not be specified in the primary
778
- template declaration. For example,
779
 
780
  ``` cpp
781
  template<class T1, class T2, int I>
782
  class A<T1, T2, I> { }; // error
783
  ```
@@ -786,11 +948,11 @@ class A<T1, T2, I> { }; // error
786
 
787
  A class template partial specialization may be declared in any scope in
788
  which the corresponding primary template may be defined (
789
  [[namespace.memdef]], [[class.mem]], [[temp.mem]]).
790
 
791
- [*Example 3*:
792
 
793
  ``` cpp
794
  template<class T> struct A {
795
  struct C {
796
  template<class T2> struct B { };
@@ -812,11 +974,11 @@ lookup. Rather, when the primary template name is used, any
812
  previously-declared partial specializations of the primary template are
813
  also considered. One consequence is that a *using-declaration* which
814
  refers to a class template does not restrict the set of partial
815
  specializations which may be found through the *using-declaration*.
816
 
817
- [*Example 4*:
818
 
819
  ``` cpp
820
  namespace N {
821
  template<class T1, class T2> class A { }; // primary template
822
  }
@@ -840,28 +1002,37 @@ Within the argument list of a class template partial specialization, the
840
  following restrictions apply:
841
 
842
  - The type of a template parameter corresponding to a specialized
843
  non-type argument shall not be dependent on a parameter of the
844
  specialization.
845
- \[*Example 5*:
846
  ``` cpp
847
  template <class T, T t> struct C {};
848
  template <class T> struct C<T, 1>; // error
849
 
850
  template< int X, int (*array_ptr)[X] > class A {};
851
  int array[5];
852
  template< int X > class A<X,&array> { }; // error
853
  ```
854
 
855
  — *end example*]
856
- - The specialization shall be more specialized than the primary
857
- template ([[temp.class.order]]).
858
  - The template parameter list of a specialization shall not contain
859
- default template argument values.[^4]
860
- - An argument shall not contain an unexpanded parameter pack. If an
861
- argument is a pack expansion ([[temp.variadic]]), it shall be the
862
- last argument in the template argument list.
 
 
 
 
 
 
 
 
 
863
 
864
  #### Matching of class template partial specializations <a id="temp.class.spec.match">[[temp.class.spec.match]]</a>
865
 
866
  When a class template is used in a context that requires an
867
  instantiation of the class, it is necessary to determine whether the
@@ -871,21 +1042,23 @@ arguments of the class template specialization with the template
871
  argument lists of the partial specializations.
872
 
873
  - If exactly one matching specialization is found, the instantiation is
874
  generated from that specialization.
875
  - If more than one matching specialization is found, the partial order
876
- rules ([[temp.class.order]]) are used to determine whether one of the
877
  specializations is more specialized than the others. If none of the
878
  specializations is more specialized than all of the other matching
879
  specializations, then the use of the class template is ambiguous and
880
  the program is ill-formed.
881
  - If no matches are found, the instantiation is generated from the
882
  primary template.
883
 
884
  A partial specialization matches a given actual template argument list
885
  if the template arguments of the partial specialization can be deduced
886
- from the actual template argument list ([[temp.deduct]]).
 
 
887
 
888
  [*Example 1*:
889
 
890
  ``` cpp
891
  template<class T1, class T2, int I> class A { }; // #1
@@ -901,15 +1074,31 @@ A<int, char*, 1> a4; // uses #5, T1 is int, T2 is char, I is 1
901
  A<int*, int*, 2> a5; // ambiguous: matches #3 and #5
902
  ```
903
 
904
  — *end example*]
905
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
906
  If the template arguments of a partial specialization cannot be deduced
907
  because of the structure of its *template-parameter-list* and the
908
  *template-id*, the program is ill-formed.
909
 
910
- [*Example 2*:
911
 
912
  ``` cpp
913
  template <int I, int J> struct A {};
914
  template <int I> struct A<I+5, I*2> {}; // error
915
 
@@ -929,15 +1118,16 @@ are deduced from the arguments of the primary template.
929
  #### Partial ordering of class template specializations <a id="temp.class.order">[[temp.class.order]]</a>
930
 
931
  For two class template partial specializations, the first is *more
932
  specialized* than the second if, given the following rewrite to two
933
  function templates, the first function template is more specialized than
934
- the second according to the ordering rules for function templates (
935
- [[temp.func.order]]):
936
 
937
- - Each of the two function templates has the same template parameters as
938
- the corresponding partial specialization.
 
939
  - Each function template has a single function parameter whose type is a
940
  class template specialization where the template arguments are the
941
  corresponding template parameters from the function template for each
942
  template argument in the *template-argument-list* of the
943
  *simple-template-id* of the partial specialization.
@@ -967,21 +1157,40 @@ function template *D* is more specialized than the function template
967
  the partial specialization \#1 and the partial specialization \#4 is
968
  more specialized than the partial specialization \#3.
969
 
970
  — *end example*]
971
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
972
  #### Members of class template specializations <a id="temp.class.spec.mfunc">[[temp.class.spec.mfunc]]</a>
973
 
974
  The template parameter list of a member of a class template partial
975
  specialization shall match the template parameter list of the class
976
  template partial specialization. The template argument list of a member
977
  of a class template partial specialization shall match the template
978
  argument list of the class template partial specialization. A class
979
- template specialization is a distinct template. The members of the class
980
- template partial specialization are unrelated to the members of the
981
- primary template. Class template partial specialization members that are
982
- used in a way that requires a definition shall be defined; the
983
  definitions of members of the primary template are never used as
984
  definitions for members of a class template partial specialization. An
985
  explicit specialization of a member of a class template partial
986
  specialization is declared in the same way as an explicit specialization
987
  of the primary template.
@@ -1014,11 +1223,11 @@ int main() {
1014
  A<char,0> a0;
1015
  A<char,2> a2;
1016
  a0.f(); // OK, uses definition of primary template's member
1017
  a2.g(); // OK, uses definition of partial specialization's member
1018
  a2.h(); // OK, uses definition of explicit specialization's member
1019
- a2.f(); // ill-formed, no definition of f for A<T,2>; the primary template is not used here
1020
  }
1021
  ```
1022
 
1023
  — *end example*]
1024
 
@@ -1068,14 +1277,14 @@ template<class T> void sort(Array<T>&);
1068
  ```
1069
 
1070
  — *end example*]
1071
 
1072
  A function template can be overloaded with other function templates and
1073
- with non-template functions ([[dcl.fct]]). A non-template function is
1074
- not related to a function template (i.e., it is never considered to be a
1075
  specialization), even if it has the same name and type as a potentially
1076
- generated function template specialization.[^5]
1077
 
1078
  #### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
1079
 
1080
  It is possible to overload function templates so that two different
1081
  function template specializations have the same type.
@@ -1101,16 +1310,16 @@ void h(int* p) {
1101
  ```
1102
 
1103
  — *end example*]
1104
 
1105
  Such specializations are distinct functions and do not violate the
1106
- one-definition rule ([[basic.def.odr]]).
1107
 
1108
- The signature of a function template is defined in Clause 
1109
- [[intro.defs]]. The names of the template parameters are significant
1110
- only for establishing the relationship between the template parameters
1111
- and the rest of the signature.
1112
 
1113
  [*Note 1*:
1114
 
1115
  Two distinct function templates may have identical function return types
1116
  and function parameter lists, even if overload resolution alone cannot
@@ -1148,120 +1357,186 @@ template parameters, but it is possible for an expression to reference a
1148
  type parameter. For example, a template type parameter can be used in
1149
  the `sizeof` operator. — *end note*]
1150
 
1151
  Two expressions involving template parameters are considered
1152
  *equivalent* if two function definitions containing the expressions
1153
- would satisfy the one-definition rule ([[basic.def.odr]]), except that
1154
- the tokens used to name the template parameters may differ as long as a
1155
  token used to name a template parameter in one expression is replaced by
1156
  another token that names the same template parameter in the other
1157
- expression. For determining whether two dependent names ([[temp.dep]])
1158
- are equivalent, only the name itself is considered, not the result of
1159
- name lookup in the context of the template. If multiple declarations of
1160
- the same function template differ in the result of this name lookup, the
1161
- result for the first declaration is used.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1162
 
1163
  [*Example 3*:
1164
 
1165
  ``` cpp
1166
  template <int I, int J> void f(A<I+J>); // #1
1167
  template <int K, int L> void f(A<K+L>); // same as #1
1168
 
1169
  template <class T> decltype(g(T())) h();
1170
  int g(int);
1171
- template <class T> decltype(g(T())) h() // redeclaration of h() uses the earlier lookup
1172
- { return g(T()); } // ...although the lookup here does find g(int)
1173
  int i = h<int>(); // template argument substitution fails; g(int)
1174
  // was not in scope at the first declaration of h()
 
 
 
 
 
 
 
 
1175
  ```
1176
 
1177
  — *end example*]
1178
 
1179
- Two expressions involving template parameters that are not equivalent
1180
- are *functionally equivalent* if, for any given set of template
1181
- arguments, the evaluation of the expression results in the same value.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1182
 
1183
  Two function templates are *equivalent* if they are declared in the same
1184
- scope, have the same name, have identical template parameter lists, and
1185
- have return types and parameter lists that are equivalent using the
1186
- rules described above to compare expressions involving template
1187
- parameters. Two function templates are *functionally equivalent* if they
1188
- are equivalent except that one or more expressions that involve template
1189
- parameters in the return types and parameter lists are functionally
1190
- equivalent using the rules described above to compare expressions
1191
- involving template parameters. If a program contains declarations of
1192
- function templates that are functionally equivalent but not equivalent,
1193
- the program is ill-formed, no diagnostic required.
 
 
1194
 
1195
- [*Note 3*:
1196
 
1197
  This rule guarantees that equivalent declarations will be linked with
1198
  one another, while not requiring implementations to use heroic efforts
1199
  to guarantee that functionally equivalent declarations will be treated
1200
  as distinct. For example, the last two declarations are functionally
1201
  equivalent and would cause a program to be ill-formed:
1202
 
1203
  ``` cpp
1204
- // Guaranteed to be the same
1205
  template <int I> void f(A<I>, A<I+10>);
1206
  template <int I> void f(A<I>, A<I+10>);
1207
 
1208
- // Guaranteed to be different
1209
  template <int I> void f(A<I>, A<I+10>);
1210
  template <int I> void f(A<I>, A<I+11>);
1211
 
1212
- // Ill-formed, no diagnostic required
1213
  template <int I> void f(A<I>, A<I+10>);
1214
  template <int I> void f(A<I>, A<I+1+2+3+4>);
1215
  ```
1216
 
1217
  — *end note*]
1218
 
1219
  #### Partial ordering of function templates <a id="temp.func.order">[[temp.func.order]]</a>
1220
 
1221
  If a function template is overloaded, the use of a function template
1222
- specialization might be ambiguous because template argument deduction (
1223
- [[temp.deduct]]) may associate the function template specialization with
1224
  more than one function template declaration. *Partial ordering* of
1225
  overloaded function template declarations is used in the following
1226
  contexts to select the function template to which a function template
1227
  specialization refers:
1228
 
1229
  - during overload resolution for a call to a function template
1230
- specialization ([[over.match.best]]);
1231
  - when the address of a function template specialization is taken;
1232
  - when a placement operator delete that is a function template
1233
  specialization is selected to match a placement operator new (
1234
  [[basic.stc.dynamic.deallocation]], [[expr.new]]);
1235
- - when a friend function declaration ([[temp.friend]]), an explicit
1236
- instantiation ([[temp.explicit]]) or an explicit specialization (
1237
- [[temp.expl.spec]]) refers to a function template specialization.
1238
 
1239
  Partial ordering selects which of two function templates is more
1240
  specialized than the other by transforming each template in turn (see
1241
  next paragraph) and performing template argument deduction using the
1242
  function type. The deduction process determines whether one of the
1243
  templates is more specialized than the other. If so, the more
1244
  specialized template is the one chosen by the partial ordering process.
 
 
1245
 
1246
  To produce the transformed template, for each type, non-type, or
1247
- template template parameter (including template parameter packs (
1248
- [[temp.variadic]]) thereof) synthesize a unique type, value, or class
1249
  template respectively and substitute it for each occurrence of that
1250
  parameter in the function type of the template.
1251
 
1252
  [*Note 1*: The type replacing the placeholder in the type of the value
1253
  synthesized for a non-type template parameter is also a unique
1254
  synthesized type. — *end note*]
1255
 
1256
- If only one of the function templates *M* is a non-static member of some
1257
- class *A*, *M* is considered to have a new first parameter inserted in
1258
- its function parameter list. Given cv as the cv-qualifiers of *M* (if
1259
- any), the new parameter is of type “rvalue reference to cv *A*” if the
1260
- optional *ref-qualifier* of *M* is `&&` or if *M* has no *ref-qualifier*
1261
- and the first parameter of the other template has rvalue reference type.
1262
- Otherwise, the new parameter is of type “lvalue reference to cv *A*”.
 
 
 
 
 
 
 
 
 
1263
 
1264
  [*Note 2*: This allows a non-static member to be ordered with respect
1265
  to a non-member function and for the results to be equivalent to the
1266
  ordering of two equivalent non-members. — *end note*]
1267
 
@@ -1279,11 +1554,11 @@ template<class T, class R> int operator*(T&, R&); // #2
1279
  // template<class R> int operator*(B<A>&, R&);\quad\quad\quad// #1a
1280
 
1281
  int main() {
1282
  A a;
1283
  B<A> b;
1284
- b * a; // calls #1a
1285
  }
1286
  ```
1287
 
1288
  — *end example*]
1289
 
@@ -1320,12 +1595,12 @@ void m() {
1320
 
1321
  — *end example*]
1322
 
1323
  [*Note 3*:
1324
 
1325
- Since partial ordering in a call context considers only parameters for
1326
- which there are explicit call arguments, some parameters are ignored
1327
  (namely, function parameter packs, parameters with default arguments,
1328
  and ellipsis parameters).
1329
 
1330
  [*Example 3*:
1331
 
@@ -1372,30 +1647,91 @@ template<class T, class... U> void f(T, U...); // #1
1372
  template<class T > void f(T); // #2
1373
  template<class T, class... U> void g(T*, U...); // #3
1374
  template<class T > void g(T); // #4
1375
 
1376
  void h(int i) {
1377
- f(&i); // error: ambiguous
1378
  g(&i); // OK: calls #3
1379
  }
1380
  ```
1381
 
1382
  — *end example*]
1383
 
1384
  — *end note*]
1385
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1386
  ### Alias templates <a id="temp.alias">[[temp.alias]]</a>
1387
 
1388
  A *template-declaration* in which the *declaration* is an
1389
- *alias-declaration* (Clause  [[dcl.dcl]]) declares the *identifier* to
1390
- be an *alias template*. An alias template is a name for a family of
1391
- types. The name of the alias template is a *template-name*.
1392
 
1393
  When a *template-id* refers to the specialization of an alias template,
1394
  it is equivalent to the associated type obtained by substitution of its
1395
- *template-argument*s for the *template-parameter*s in the *type-id* of
1396
- the alias template.
1397
 
1398
  [*Note 1*: An alias template name is never deduced. — *end note*]
1399
 
1400
  [*Example 1*:
1401
 
@@ -1430,19 +1766,19 @@ substitution still applies to the *template-id*.
1430
  [*Example 2*:
1431
 
1432
  ``` cpp
1433
  template<typename...> using void_t = void;
1434
  template<typename T> void_t<typename T::foo> f();
1435
- f<int>(); // error, int does not have a nested type foo
1436
  ```
1437
 
1438
  — *end example*]
1439
 
1440
- The *type-id* in an alias template declaration shall not refer to the
1441
- alias template being declared. The type produced by an alias template
1442
- specialization shall not directly or indirectly make use of that
1443
- specialization.
1444
 
1445
  [*Example 3*:
1446
 
1447
  ``` cpp
1448
  template <class T> struct A;
@@ -1453,5 +1789,72 @@ template <class T> struct A {
1453
  B<short> b; // error: instantiation of B<short> uses own type via A<short>::U
1454
  ```
1455
 
1456
  — *end example*]
1457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  ```
13
 
14
  — *end example*]
15
 
16
  [*Note 1*: However, this syntax is allowed in class template partial
17
+ specializations [[temp.class.spec]]. — *end note*]
18
 
19
+ For purposes of name lookup and instantiation, default arguments,
20
+ *type-constraint*s, *requires-clause*s [[temp.pre]], and
21
+ *noexcept-specifier*s of function templates and of member functions of
22
+ class templates are considered definitions; each default argument,
23
+ *type-constraint*, *requires-clause*, or *noexcept-specifier* is a
24
+ separate definition which is unrelated to the templated function
25
+ definition or to any other default arguments *type-constraint*s,
26
+ *requires-clause*s, or *noexcept-specifier*s. For the purpose of
27
+ instantiation, the substatements of a constexpr if statement [[stmt.if]]
28
+ are considered definitions.
29
 
30
  Because an *alias-declaration* cannot declare a *template-id*, it is not
31
  possible to partially or explicitly specialize an alias template.
32
 
33
  ### Class templates <a id="temp.class">[[temp.class]]</a>
 
60
  — *end example*]
61
 
62
  When a member function, a member class, a member enumeration, a static
63
  data member or a member template of a class template is defined outside
64
  of the class template definition, the member definition is defined as a
65
+ template definition in which the *template-head* is equivalent to that
66
+ of the class template [[temp.over.link]]. The names of the template
67
+ parameters used in the definition of the member may be different from
68
+ the template parameter names used in the class template definition. The
69
+ template argument list following the class template name in the member
70
+ definition shall name the parameters in the same order as the one used
71
+ in the template parameter list of the member. Each template parameter
72
+ pack shall be expanded with an ellipsis in the template argument list.
73
 
74
  [*Example 2*:
75
 
76
  ``` cpp
77
  template<class T1, class T2> struct A {
 
91
 
92
  template<class ... Types> void B<Types ...>::f3() { } // OK
93
  template<class ... Types> void B<Types>::f4() { } // error
94
  ```
95
 
96
+ ``` cpp
97
+ template<typename T> concept C = true;
98
+ template<typename T> concept D = true;
99
+
100
+ template<C T> struct S {
101
+ void f();
102
+ void g();
103
+ void h();
104
+ template<D U> struct Inner;
105
+ };
106
+
107
+ template<C A> void S<A>::f() { } // OK: template-head{s} match
108
+ template<typename T> void S<T>::g() { } // error: no matching declaration for S<T>
109
+
110
+ template<typename T> requires C<T> // ill-formed, no diagnostic required: template-head{s} are
111
+ void S<T>::h() { } // functionally equivalent but not equivalent
112
+
113
+ template<C X> template<D Y>
114
+ struct S<X>::Inner { }; // OK
115
+ ```
116
+
117
  — *end example*]
118
 
119
  In a redeclaration, partial specialization, explicit specialization or
120
  explicit instantiation of a class template, the *class-key* shall agree
121
+ in kind with the original class template declaration [[dcl.type.elab]].
 
122
 
123
  #### Member functions of class templates <a id="temp.mem.func">[[temp.mem.func]]</a>
124
 
125
  A member function of a class template may be defined outside of the
126
  class template definition in which it is declared.
 
136
  T& operator[](int);
137
  T& elem(int i) { return v[i]; }
138
  };
139
  ```
140
 
141
+ declares three member functions of a class template. The subscript
142
+ function might be defined like this:
143
 
144
  ``` cpp
145
  template<class T> T& Array<T>::operator[](int i) {
146
  if (i<0 || sz<=i) error("Array: range error");
147
  return v[i];
148
  }
149
  ```
150
 
151
+ A constrained member function can be defined out of line:
152
+
153
+ ``` cpp
154
+ template<typename T> concept C = requires {
155
+ typename T::type;
156
+ };
157
+
158
+ template<typename T> struct S {
159
+ void f() requires C<T>;
160
+ void g() requires C<T>;
161
+ };
162
+
163
+ template<typename T>
164
+ void S<T>::f() requires C<T> { } // OK
165
+ template<typename T>
166
+ void S<T>::g() { } // error: no matching function in S<T>
167
+ ```
168
+
169
  — *end example*]
170
 
171
  The *template-argument*s for a member function of a class template are
172
  determined by the *template-argument*s of the type of the object for
173
  which the member function is called.
174
 
175
  [*Example 2*:
176
 
177
+ The *template-argument* for `Array<T>::operator[]` will be determined by
178
+ the `Array` to which the subscripting operation is applied.
179
 
180
  ``` cpp
181
  Array<int> v1(20);
182
  Array<dcomplex> v2(30);
183
 
184
+ v1[3] = 7; // Array<int>::operator[]
185
+ v2[3] = dcomplex(7,8); // Array<dcomplex>::operator[]
186
  ```
187
 
188
  — *end example*]
189
 
190
+ #### Deduction guides <a id="temp.deduct.guide">[[temp.deduct.guide]]</a>
191
+
192
+ Deduction guides are used when a *template-name* appears as a type
193
+ specifier for a deduced class type [[dcl.type.class.deduct]]. Deduction
194
+ guides are not found by name lookup. Instead, when performing class
195
+ template argument deduction [[over.match.class.deduct]], any deduction
196
+ guides declared for the class template are considered.
197
+
198
+ ``` bnf
199
+ deduction-guide:
200
+ explicit-specifierₒₚₜ template-name '(' parameter-declaration-clause ')' '->' simple-template-id ';'
201
+ ```
202
+
203
+ [*Example 1*:
204
+
205
+ ``` cpp
206
+ template<class T, class D = int>
207
+ struct S {
208
+ T data;
209
+ };
210
+ template<class U>
211
+ S(U) -> S<typename U::type>;
212
+
213
+ struct A {
214
+ using type = short;
215
+ operator type();
216
+ };
217
+ S x{A()}; // x is of type S<short, int>
218
+ ```
219
+
220
+ — *end example*]
221
+
222
+ The same restrictions apply to the *parameter-declaration-clause* of a
223
+ deduction guide as in a function declaration [[dcl.fct]]. The
224
+ *simple-template-id* shall name a class template specialization. The
225
+ *template-name* shall be the same *identifier* as the *template-name* of
226
+ the *simple-template-id*. A *deduction-guide* shall be declared in the
227
+ same scope as the corresponding class template and, for a member class
228
+ template, with the same access. Two deduction guide declarations in the
229
+ same translation unit for the same class template shall not have
230
+ equivalent *parameter-declaration-clause*s.
231
+
232
  #### Member classes of class templates <a id="temp.mem.class">[[temp.mem.class]]</a>
233
 
234
  A member class of a class template may be defined outside the class
235
  template definition in which it is declared.
236
 
237
  [*Note 1*:
238
 
239
  The member class must be defined before its first use that requires an
240
+ instantiation [[temp.inst]]. For example,
241
 
242
  ``` cpp
243
  template<class T> struct A {
244
  class B;
245
  };
 
312
 
313
  A template can be declared within a class or class template; such a
314
  template is called a member template. A member template can be defined
315
  within or outside its class definition or class template definition. A
316
  member template of a class template that is defined outside of its class
317
+ template definition shall be specified with a *template-head* equivalent
318
+ to that of the class template followed by a *template-head* equivalent
319
+ to that of the member template [[temp.over.link]].
320
 
321
  [*Example 1*:
322
 
323
  ``` cpp
324
  template<class T> struct string {
 
330
  }
331
  ```
332
 
333
  — *end example*]
334
 
335
+ [*Example 2*:
336
+
337
+ ``` cpp
338
+ template<typename T> concept C1 = true;
339
+ template<typename T> concept C2 = sizeof(T) <= 4;
340
+
341
+ template<C1 T> struct S {
342
+ template<C2 U> void f(U);
343
+ template<C2 U> void g(U);
344
+ };
345
+
346
+ template<C1 T> template<C2 U>
347
+ void S<T>::f(U) { } // OK
348
+ template<C1 T> template<typename U>
349
+ void S<T>::g(U) { } // error: no matching function in S<T>
350
+ ```
351
+
352
+ — *end example*]
353
+
354
  A local class of non-closure type shall not have member templates.
355
+ Access control rules [[class.access]] apply to member template names. A
356
+ destructor shall not be a member template. A non-template member
357
+ function [[dcl.fct]] with a given name and type and a member function
358
+ template of the same name, which could be used to generate a
359
  specialization of the same type, can both be declared in a class. When
360
  both exist, a use of that name and type refers to the non-template
361
  member unless an explicit template argument list is supplied.
362
 
363
+ [*Example 3*:
364
 
365
  ``` cpp
366
  template <class T> struct A {
367
  void f(int);
368
  template <class T2> void f(T2);
 
381
 
382
  — *end example*]
383
 
384
  A member function template shall not be virtual.
385
 
386
+ [*Example 4*:
387
 
388
  ``` cpp
389
  template <class T> struct AA {
390
  template <class C> virtual void g(C); // error
391
  virtual void f(); // OK
 
395
  — *end example*]
396
 
397
  A specialization of a member function template does not override a
398
  virtual function from a base class.
399
 
400
+ [*Example 5*:
401
 
402
  ``` cpp
403
  class B {
404
  virtual void f(int);
405
  };
 
414
 
415
  A specialization of a conversion function template is referenced in the
416
  same way as a non-template conversion function that converts to the same
417
  type.
418
 
419
+ [*Example 6*:
420
 
421
  ``` cpp
422
  struct A {
423
  template <class T> operator T*();
424
  };
 
433
  }
434
  ```
435
 
436
  — *end example*]
437
 
438
+ [*Note 1*: There is no syntax to form a *template-id* [[temp.names]] by
439
+ providing an explicit template argument list [[temp.arg.explicit]] for a
440
+ conversion function template [[class.conv.fct]]. *end note*]
 
 
441
 
442
  A specialization of a conversion function template is not found by name
443
  lookup. Instead, any conversion function templates visible in the
444
  context of the use are considered. For each such operator, if argument
445
+ deduction succeeds [[temp.deduct.conv]], the resulting specialization is
446
+ used as if found by name lookup.
447
 
448
  A *using-declaration* in a derived class cannot refer to a
449
  specialization of a conversion function template in a base class.
450
 
451
+ Overload resolution [[over.ics.rank]] and partial ordering
452
+ [[temp.func.order]] are used to select the best conversion function
453
  among multiple specializations of conversion function templates and/or
454
  non-template conversion functions.
455
 
456
  ### Variadic templates <a id="temp.variadic">[[temp.variadic]]</a>
457
 
 
477
  [*Example 2*:
478
 
479
  ``` cpp
480
  template<class ... Types> void f(Types ... args);
481
 
482
+ f(); // args contains no arguments
483
+ f(1); // args contains one argument: int
484
+ f(2, 1.0); // args contains two arguments: int and double
485
  ```
486
 
487
  — *end example*]
488
 
489
+ An **init-capture* pack* is a lambda capture that introduces an
490
+ *init-capture* for each of the elements in the pack expansion of its
491
+ *initializer*.
492
+
493
+ [*Example 3*:
494
+
495
+ ``` cpp
496
+ template <typename... Args>
497
+ void foo(Args... args) {
498
+ [...xs=args]{
499
+ bar(xs...); // xs is an init-capture pack
500
+ };
501
+ }
502
+
503
+ foo(); // xs contains zero init-captures
504
+ foo(1); // xs contains one init-capture
505
+ ```
506
+
507
+ — *end example*]
508
+
509
+ A *pack* is a template parameter pack, a function parameter pack, or an
510
+ *init-capture* pack. The number of elements of a template parameter pack
511
+ or a function parameter pack is the number of arguments provided for the
512
+ parameter pack. The number of elements of an *init-capture* pack is the
513
+ number of elements in the pack expansion of its *initializer*.
514
 
515
  A *pack expansion* consists of a *pattern* and an ellipsis, the
516
  instantiation of which produces zero or more instantiations of the
517
  pattern in a list (described below). The form of the pattern depends on
518
  the context in which the expansion occurs. Pack expansions can occur in
519
  the following contexts:
520
 
521
+ - In a function parameter pack [[dcl.fct]]; the pattern is the
522
  *parameter-declaration* without the ellipsis.
523
+ - In a *using-declaration* [[namespace.udecl]]; the pattern is a
524
  *using-declarator*.
525
+ - In a template parameter pack that is a pack expansion [[temp.param]]:
 
526
  - if the template parameter pack is a *parameter-declaration*; the
527
  pattern is the *parameter-declaration* without the ellipsis;
528
+ - if the template parameter pack is a *type-parameter*; the pattern is
529
+ the corresponding *type-parameter* without the ellipsis.
530
+ - In an *initializer-list* [[dcl.init]]; the pattern is an
 
531
  *initializer-clause*.
532
+ - In a *base-specifier-list* [[class.derived]]; the pattern is a
533
+ *base-specifier*.
534
+ - In a *mem-initializer-list* [[class.base.init]] for a
535
  *mem-initializer* whose *mem-initializer-id* denotes a base class; the
536
  pattern is the *mem-initializer*.
537
+ - In a *template-argument-list* [[temp.arg]]; the pattern is a
538
  *template-argument*.
539
+ - In an *attribute-list* [[dcl.attr.grammar]]; the pattern is an
540
  *attribute*.
541
+ - In an *alignment-specifier* [[dcl.align]]; the pattern is the
542
  *alignment-specifier* without the ellipsis.
543
+ - In a *capture-list* [[expr.prim.lambda.capture]]; the pattern is the
544
+ *capture* without the ellipsis.
545
+ - In a `sizeof...` expression [[expr.sizeof]]; the pattern is an
546
  *identifier*.
547
+ - In a *fold-expression* [[expr.prim.fold]]; the pattern is the
548
+ *cast-expression* that contains an unexpanded pack.
549
 
550
+ [*Example 4*:
551
 
552
  ``` cpp
553
  template<class ... Types> void f(Types ... rest);
554
  template<class ... Types> void g(Types ... rest) {
555
  f(&rest ...); // ``&rest ...'' is a pack expansion; ``&rest'' is its pattern
556
  }
557
  ```
558
 
559
  — *end example*]
560
 
561
+ For the purpose of determining whether a pack satisfies a rule regarding
562
+ entities other than packs, the pack is considered to be the entity that
563
+ would result from an instantiation of the pattern in which it appears.
 
564
 
565
+ A pack whose name appears within the pattern of a pack expansion is
566
+ expanded by that pack expansion. An appearance of the name of a pack is
567
+ only expanded by the innermost enclosing pack expansion. The pattern of
568
+ a pack expansion shall name one or more packs that are not expanded by a
569
+ nested pack expansion; such packs are called *unexpanded packs* in the
570
+ pattern. All of the packs expanded by a pack expansion shall have the
571
+ same number of arguments specified. An appearance of a name of a pack
572
+ that is not expanded is ill-formed.
 
573
 
574
+ [*Example 5*:
575
 
576
  ``` cpp
577
  template<typename...> struct Tuple {};
578
  template<typename T1, typename T2> struct Pair {};
579
 
 
589
  // error: different number of arguments specified for Args1 and Args2
590
 
591
  template<class ... Args>
592
  void g(Args ... args) { // OK: Args is expanded by the function parameter pack args
593
  f(const_cast<const Args*>(&args)...); // OK: ``Args'' and ``args'' are expanded
594
+ f(5 ...); // error: pattern does not contain any packs
595
+ f(args); // error: pack ``args'' is not expanded
596
  f(h(args ...) + args ...); // OK: first ``args'' expanded within h,
597
  // second ``args'' expanded within f
598
  }
599
  ```
600
 
601
  — *end example*]
602
 
603
  The instantiation of a pack expansion that is neither a `sizeof...`
604
+ expression nor a *fold-expression* produces a list of elements E₁, E₂,
605
+ , $\mathtt{E}_N$, where N is the number of elements in the pack
606
+ expansion parameters. Each Eᵢ is generated by instantiating the pattern
607
+ and replacing each pack expansion parameter with its iᵗʰ element. Such
608
+ an element, in the context of the instantiation, is interpreted as
609
+ follows:
610
 
611
  - if the pack is a template parameter pack, the element is a template
612
+ parameter [[temp.param]] of the corresponding kind (type or non-type)
613
+ designating the iᵗʰ corresponding type or value template argument;
 
614
  - if the pack is a function parameter pack, the element is an
615
+ *id-expression* designating the iᵗʰ function parameter that resulted
616
+ from instantiation of the function parameter pack declaration;
617
+ otherwise
618
+ - if the pack is an *init-capture* pack, the element is an
619
+ *id-expression* designating the variable introduced by the iᵗʰ
620
+ *init-capture* that resulted from instantiation of the *init-capture*
621
+ pack.
622
 
623
+ All of the Eᵢ become items in the enclosing list.
624
 
625
  [*Note 1*: The variety of list varies with the context:
626
  *expression-list*, *base-specifier-list*, *template-argument-list*,
627
  etc. — *end note*]
628
 
 
630
  list. Such an instantiation does not alter the syntactic interpretation
631
  of the enclosing construct, even in cases where omitting the list
632
  entirely would otherwise be ill-formed or would result in an ambiguity
633
  in the grammar.
634
 
635
+ [*Example 6*:
636
 
637
  ``` cpp
638
  template<class... T> struct X : T... { };
639
  template<class... T> void f(T... values) {
640
  X<T...> x(values...);
 
644
  // x is a variable of type X<> that is value-initialized
645
  ```
646
 
647
  — *end example*]
648
 
649
+ The instantiation of a `sizeof...` expression [[expr.sizeof]] produces
650
+ an integral constant containing the number of elements in the pack it
651
+ expands.
652
 
653
  The instantiation of a *fold-expression* produces:
654
 
655
  - `((`E₁ *op* E₂`)` *op* ⋯`)` *op* $\mathtt{E}_N$ for a unary left fold,
656
  - E₁ *op* `(`⋯ *op* `(`$\mathtt{E}_{N-1}$ *op* $\mathtt{E}_N$`))` for a
 
661
  E`)))` for a binary right fold.
662
 
663
  In each case, *op* is the *fold-operator*, N is the number of elements
664
  in the pack expansion parameters, and each Eᵢ is generated by
665
  instantiating the pattern and replacing each pack expansion parameter
666
+ with its iᵗʰ element. For a binary fold-expression, E is generated by
667
  instantiating the *cast-expression* that did not contain an unexpanded
668
+ pack.
669
 
670
+ [*Example 7*:
671
 
672
  ``` cpp
673
  template<typename ...Args>
674
  bool all(Args ...args) { return (... && args); }
675
 
 
680
  `((true && true) && true) && false`, which evaluates to `false`.
681
 
682
  — *end example*]
683
 
684
  If N is zero for a unary fold-expression, the value of the expression is
685
+ shown in [[temp.fold.empty]]; if the operator is not listed in
686
+ [[temp.fold.empty]], the instantiation is ill-formed.
687
 
688
+ **Table: Value of folding empty sequences** <a id="temp.fold.empty">[temp.fold.empty]</a>
689
 
690
+ | Operator | Value when pack is empty |
691
+ | -------- | ------------------------ |
692
  | `&&` | `true` |
693
  | `||` | `false` |
694
  | `,` | `void()` |
695
 
696
 
 
708
  non-template function is found in the specified class or namespace,
709
  the friend declaration refers to that function, otherwise,
710
  - if the name of the friend is a *qualified-id* and a matching function
711
  template is found in the specified class or namespace, the friend
712
  declaration refers to the deduced specialization of that function
713
+ template [[temp.deduct.decl]], otherwise,
714
  - the name shall be an *unqualified-id* that declares (or redeclares) a
715
  non-template function.
716
 
717
  [*Example 1*:
718
 
 
763
  ```
764
 
765
  — *end example*]
766
 
767
  A template friend declaration specifies that all specializations of that
768
+ template, whether they are implicitly instantiated [[temp.inst]],
769
+ partially specialized [[temp.class.spec]] or explicitly specialized
770
+ [[temp.expl.spec]], are friends of the class containing the template
771
  friend declaration.
772
 
773
  [*Example 3*:
774
 
775
  ``` cpp
 
782
  template<class T> struct A<T*> { X::Y ab; }; // OK
783
  ```
784
 
785
  — *end example*]
786
 
787
+ A template friend declaration may declare a member of a dependent type
788
+ to be a friend. The friend declaration shall declare a function or
789
+ specify a type with an *elaborated-type-specifier*, in either case with
790
+ a *nested-name-specifier* ending with a *simple-template-id*, *C*, whose
791
+ *template-name* names a class template. The template parameters of the
792
+ template friend declaration shall be deducible from *C* (
793
+ [[temp.deduct.type]]). In this case, a member of a specialization *S* of
794
+ the class template is a friend of the class granting friendship if
795
+ deduction of the template parameters of *C* from *S* succeeds, and
796
+ substituting the deduced template arguments into the friend declaration
797
+ produces a declaration that would be a valid redeclaration of the member
798
+ of the specialization.
799
 
800
  [*Example 4*:
801
 
802
  ``` cpp
803
  template<class T> struct A {
804
  struct B { };
805
  void f();
806
  struct D {
807
  void g();
808
  };
809
+ T h();
810
+ template<T U> T i();
811
  };
812
  template<> struct A<int> {
813
  struct B { };
814
  int f();
815
  struct D {
816
  void g();
817
  };
818
+ template<int U> int i();
819
+ };
820
+ template<> struct A<float*> {
821
+ int *h();
822
  };
823
 
824
  class C {
825
  template<class T> friend struct A<T>::B; // grants friendship to A<int>::B even though
826
  // it is not a specialization of A<T>::B
827
  template<class T> friend void A<T>::f(); // does not grant friendship to A<int>::f()
828
  // because its return type does not match
829
+ template<class T> friend void A<T>::D::g(); // error: A<T>::D does not end with a simple-template-id
830
+ template<class T> friend int *A<T*>::h(); // grants friendship to A<int*>::h() and A<float*>::h()
831
+ template<class T> template<T U> // grants friendship to instantiations of A<T>::i() and
832
+ friend T A<T>::i(); // to A<int>::i(), and thereby to all specializations
833
+ }; // of those function templates
834
  ```
835
 
836
  — *end example*]
837
 
838
  [*Note 1*: A friend declaration may first declare a member of an
839
+ enclosing namespace scope [[temp.inject]]. — *end note*]
840
 
841
  A friend template shall not be declared in a local class.
842
 
843
  Friend declarations shall not declare partial specializations.
844
 
 
853
 
854
  — *end example*]
855
 
856
  When a friend declaration refers to a specialization of a function
857
  template, the function parameter declarations shall not include default
858
+ arguments, nor shall the `inline`, `constexpr`, or `consteval`
859
+ specifiers be used in such a declaration.
860
+
861
+ A non-template friend declaration with a *requires-clause* shall be a
862
+ definition. A friend function template with a constraint that depends on
863
+ a template parameter from an enclosing template shall be a definition.
864
+ Such a constrained friend function or function template declaration does
865
+ not declare the same function or function template as a declaration in
866
+ any other scope.
867
 
868
  ### Class template partial specializations <a id="temp.class.spec">[[temp.class.spec]]</a>
869
 
870
  A *primary class template* declaration is one in which the class
871
  template name is an identifier. A template declaration in which the
872
  class template name is a *simple-template-id* is a *partial
873
  specialization* of the class template named in the *simple-template-id*.
874
  A partial specialization of a class template provides an alternative
875
  definition of the template that is used instead of the primary
876
  definition when the arguments in a specialization match those given in
877
+ the partial specialization [[temp.class.spec.match]]. The primary
878
  template shall be declared before any specializations of that template.
879
  A partial specialization shall be declared before the first use of a
880
  class template specialization that would make use of the partial
881
  specialization as the result of an implicit or explicit instantiation in
882
  every translation unit in which such a use occurs; no diagnostic is
883
  required.
884
 
885
  Each class template partial specialization is a distinct template and
886
  definitions shall be provided for the members of a template partial
887
+ specialization [[temp.class.spec.mfunc]].
888
 
889
  [*Example 1*:
890
 
891
  ``` cpp
892
  template<class T1, class T2, int I> class A { };
 
900
  template. The second and subsequent declarations declare partial
901
  specializations of the primary template.
902
 
903
  — *end example*]
904
 
905
+ A class template partial specialization may be constrained [[temp.pre]].
906
+
907
+ [*Example 2*:
908
+
909
+ ``` cpp
910
+ template<typename T> concept C = true;
911
+
912
+ template<typename T> struct X { };
913
+ template<typename T> struct X<T*> { }; // #1
914
+ template<C T> struct X<T> { }; // #2
915
+ ```
916
+
917
+ Both partial specializations are more specialized than the primary
918
+ template. \#1 is more specialized because the deduction of its template
919
+ arguments from the template argument list of the class template
920
+ specialization succeeds, while the reverse does not. \#2 is more
921
+ specialized because the template arguments are equivalent, but the
922
+ partial specialization is more constrained [[temp.constr.order]].
923
+
924
+ — *end example*]
925
+
926
  The template parameters are specified in the angle bracket enclosed list
927
  that immediately follows the keyword `template`. For partial
928
  specializations, the template argument list is explicitly written
929
  immediately following the class template name. For primary templates,
930
  this list is implicitly described by the template parameter list.
931
  Specifically, the order of the template arguments is the sequence in
932
  which they appear in the template parameter list.
933
 
934
+ [*Example 3*: The template argument list for the primary template in
935
  the example above is `<T1,` `T2,` `I>`. — *end example*]
936
 
937
  [*Note 1*:
938
 
939
+ The template argument list cannot be specified in the primary template
940
+ declaration. For example,
941
 
942
  ``` cpp
943
  template<class T1, class T2, int I>
944
  class A<T1, T2, I> { }; // error
945
  ```
 
948
 
949
  A class template partial specialization may be declared in any scope in
950
  which the corresponding primary template may be defined (
951
  [[namespace.memdef]], [[class.mem]], [[temp.mem]]).
952
 
953
+ [*Example 4*:
954
 
955
  ``` cpp
956
  template<class T> struct A {
957
  struct C {
958
  template<class T2> struct B { };
 
974
  previously-declared partial specializations of the primary template are
975
  also considered. One consequence is that a *using-declaration* which
976
  refers to a class template does not restrict the set of partial
977
  specializations which may be found through the *using-declaration*.
978
 
979
+ [*Example 5*:
980
 
981
  ``` cpp
982
  namespace N {
983
  template<class T1, class T2> class A { }; // primary template
984
  }
 
1002
  following restrictions apply:
1003
 
1004
  - The type of a template parameter corresponding to a specialized
1005
  non-type argument shall not be dependent on a parameter of the
1006
  specialization.
1007
+ \[*Example 6*:
1008
  ``` cpp
1009
  template <class T, T t> struct C {};
1010
  template <class T> struct C<T, 1>; // error
1011
 
1012
  template< int X, int (*array_ptr)[X] > class A {};
1013
  int array[5];
1014
  template< int X > class A<X,&array> { }; // error
1015
  ```
1016
 
1017
  — *end example*]
1018
+ - The specialization shall be more specialized than the primary template
1019
+ [[temp.class.order]].
1020
  - The template parameter list of a specialization shall not contain
1021
+ default template argument values.[^8]
1022
+ - An argument shall not contain an unexpanded pack. If an argument is a
1023
+ pack expansion [[temp.variadic]], it shall be the last argument in the
1024
+ template argument list.
1025
+
1026
+ The usual access checking rules do not apply to non-dependent names used
1027
+ to specify template arguments of the *simple-template-id* of the partial
1028
+ specialization.
1029
+
1030
+ [*Note 2*: The template arguments may be private types or objects that
1031
+ would normally not be accessible. Dependent names cannot be checked when
1032
+ declaring the partial specialization, but will be checked when
1033
+ substituting into the partial specialization. — *end note*]
1034
 
1035
  #### Matching of class template partial specializations <a id="temp.class.spec.match">[[temp.class.spec.match]]</a>
1036
 
1037
  When a class template is used in a context that requires an
1038
  instantiation of the class, it is necessary to determine whether the
 
1042
  argument lists of the partial specializations.
1043
 
1044
  - If exactly one matching specialization is found, the instantiation is
1045
  generated from that specialization.
1046
  - If more than one matching specialization is found, the partial order
1047
+ rules [[temp.class.order]] are used to determine whether one of the
1048
  specializations is more specialized than the others. If none of the
1049
  specializations is more specialized than all of the other matching
1050
  specializations, then the use of the class template is ambiguous and
1051
  the program is ill-formed.
1052
  - If no matches are found, the instantiation is generated from the
1053
  primary template.
1054
 
1055
  A partial specialization matches a given actual template argument list
1056
  if the template arguments of the partial specialization can be deduced
1057
+ from the actual template argument list [[temp.deduct]], and the deduced
1058
+ template arguments satisfy the associated constraints of the partial
1059
+ specialization, if any [[temp.constr.decl]].
1060
 
1061
  [*Example 1*:
1062
 
1063
  ``` cpp
1064
  template<class T1, class T2, int I> class A { }; // #1
 
1074
  A<int*, int*, 2> a5; // ambiguous: matches #3 and #5
1075
  ```
1076
 
1077
  — *end example*]
1078
 
1079
+ [*Example 2*:
1080
+
1081
+ ``` cpp
1082
+ template<typename T> concept C = requires (T t) { t.f(); };
1083
+
1084
+ template<typename T> struct S { }; // #1
1085
+ template<C T> struct S<T> { }; // #2
1086
+
1087
+ struct Arg { void f(); };
1088
+
1089
+ S<int> s1; // uses #1; the constraints of #2 are not satisfied
1090
+ S<Arg> s2; // uses #2; both constraints are satisfied but #2 is more specialized
1091
+ ```
1092
+
1093
+ — *end example*]
1094
+
1095
  If the template arguments of a partial specialization cannot be deduced
1096
  because of the structure of its *template-parameter-list* and the
1097
  *template-id*, the program is ill-formed.
1098
 
1099
+ [*Example 3*:
1100
 
1101
  ``` cpp
1102
  template <int I, int J> struct A {};
1103
  template <int I> struct A<I+5, I*2> {}; // error
1104
 
 
1118
  #### Partial ordering of class template specializations <a id="temp.class.order">[[temp.class.order]]</a>
1119
 
1120
  For two class template partial specializations, the first is *more
1121
  specialized* than the second if, given the following rewrite to two
1122
  function templates, the first function template is more specialized than
1123
+ the second according to the ordering rules for function templates
1124
+ [[temp.func.order]]:
1125
 
1126
+ - Each of the two function templates has the same template parameters
1127
+ and associated constraints [[temp.constr.decl]] as the corresponding
1128
+ partial specialization.
1129
  - Each function template has a single function parameter whose type is a
1130
  class template specialization where the template arguments are the
1131
  corresponding template parameters from the function template for each
1132
  template argument in the *template-argument-list* of the
1133
  *simple-template-id* of the partial specialization.
 
1157
  the partial specialization \#1 and the partial specialization \#4 is
1158
  more specialized than the partial specialization \#3.
1159
 
1160
  — *end example*]
1161
 
1162
+ [*Example 2*:
1163
+
1164
+ ``` cpp
1165
+ template<typename T> concept C = requires (T t) { t.f(); };
1166
+ template<typename T> concept D = C<T> && requires (T t) { t.f(); };
1167
+
1168
+ template<typename T> class S { };
1169
+ template<C T> class S<T> { }; // #1
1170
+ template<D T> class S<T> { }; // #2
1171
+
1172
+ template<C T> void f(S<T>); // A
1173
+ template<D T> void f(S<T>); // B
1174
+ ```
1175
+
1176
+ The partial specialization \#2 is more specialized than \#1 because `B`
1177
+ is more specialized than `A`.
1178
+
1179
+ — *end example*]
1180
+
1181
  #### Members of class template specializations <a id="temp.class.spec.mfunc">[[temp.class.spec.mfunc]]</a>
1182
 
1183
  The template parameter list of a member of a class template partial
1184
  specialization shall match the template parameter list of the class
1185
  template partial specialization. The template argument list of a member
1186
  of a class template partial specialization shall match the template
1187
  argument list of the class template partial specialization. A class
1188
+ template partial specialization is a distinct template. The members of
1189
+ the class template partial specialization are unrelated to the members
1190
+ of the primary template. Class template partial specialization members
1191
+ that are used in a way that requires a definition shall be defined; the
1192
  definitions of members of the primary template are never used as
1193
  definitions for members of a class template partial specialization. An
1194
  explicit specialization of a member of a class template partial
1195
  specialization is declared in the same way as an explicit specialization
1196
  of the primary template.
 
1223
  A<char,0> a0;
1224
  A<char,2> a2;
1225
  a0.f(); // OK, uses definition of primary template's member
1226
  a2.g(); // OK, uses definition of partial specialization's member
1227
  a2.h(); // OK, uses definition of explicit specialization's member
1228
+ a2.f(); // error: no definition of f for A<T,2>; the primary template is not used here
1229
  }
1230
  ```
1231
 
1232
  — *end example*]
1233
 
 
1277
  ```
1278
 
1279
  — *end example*]
1280
 
1281
  A function template can be overloaded with other function templates and
1282
+ with non-template functions [[dcl.fct]]. A non-template function is not
1283
+ related to a function template (i.e., it is never considered to be a
1284
  specialization), even if it has the same name and type as a potentially
1285
+ generated function template specialization.[^9]
1286
 
1287
  #### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
1288
 
1289
  It is possible to overload function templates so that two different
1290
  function template specializations have the same type.
 
1310
  ```
1311
 
1312
  — *end example*]
1313
 
1314
  Such specializations are distinct functions and do not violate the
1315
+ one-definition rule [[basic.def.odr]].
1316
 
1317
+ The signature of a function template is defined in [[intro.defs]]. The
1318
+ names of the template parameters are significant only for establishing
1319
+ the relationship between the template parameters and the rest of the
1320
+ signature.
1321
 
1322
  [*Note 1*:
1323
 
1324
  Two distinct function templates may have identical function return types
1325
  and function parameter lists, even if overload resolution alone cannot
 
1357
  type parameter. For example, a template type parameter can be used in
1358
  the `sizeof` operator. — *end note*]
1359
 
1360
  Two expressions involving template parameters are considered
1361
  *equivalent* if two function definitions containing the expressions
1362
+ would satisfy the one-definition rule [[basic.def.odr]], except that the
1363
+ tokens used to name the template parameters may differ as long as a
1364
  token used to name a template parameter in one expression is replaced by
1365
  another token that names the same template parameter in the other
1366
+ expression. Two unevaluated operands that do not involve template
1367
+ parameters are considered equivalent if two function definitions
1368
+ containing the expressions would satisfy the one-definition rule, except
1369
+ that the tokens used to name types and declarations may differ as long
1370
+ as they name the same entities, and the tokens used to form concept-ids
1371
+ may differ as long as the two *template-id*s are the same [[temp.type]].
1372
+
1373
+ [*Note 3*: For instance, `A<42>` and `A<40+2>` name the same
1374
+ type. — *end note*]
1375
+
1376
+ Two *lambda-expression*s are never considered equivalent.
1377
+
1378
+ [*Note 4*: The intent is to avoid *lambda-expression*s appearing in the
1379
+ signature of a function template with external linkage. — *end note*]
1380
+
1381
+ For determining whether two dependent names [[temp.dep]] are equivalent,
1382
+ only the name itself is considered, not the result of name lookup in the
1383
+ context of the template. If multiple declarations of the same function
1384
+ template differ in the result of this name lookup, the result for the
1385
+ first declaration is used.
1386
 
1387
  [*Example 3*:
1388
 
1389
  ``` cpp
1390
  template <int I, int J> void f(A<I+J>); // #1
1391
  template <int K, int L> void f(A<K+L>); // same as #1
1392
 
1393
  template <class T> decltype(g(T())) h();
1394
  int g(int);
1395
+ template <class T> decltype(g(T())) h() // redeclaration of h() uses the earlier lookup
1396
+ { return g(T()); } // …{} although the lookup here does find g(int)
1397
  int i = h<int>(); // template argument substitution fails; g(int)
1398
  // was not in scope at the first declaration of h()
1399
+
1400
+ // ill-formed, no diagnostic required: the two expressions are functionally equivalent but not equivalent
1401
+ template <int N> void foo(const char (*s)[([]{}, N)]);
1402
+ template <int N> void foo(const char (*s)[([]{}, N)]);
1403
+
1404
+ // two different declarations because the non-dependent portions are not considered equivalent
1405
+ template <class T> void spam(decltype([]{}) (*s)[sizeof(T)]);
1406
+ template <class T> void spam(decltype([]{}) (*s)[sizeof(T)]);
1407
  ```
1408
 
1409
  — *end example*]
1410
 
1411
+ Two potentially-evaluated expressions involving template parameters that
1412
+ are not equivalent are *functionally equivalent* if, for any given set
1413
+ of template arguments, the evaluation of the expression results in the
1414
+ same value. Two unevaluated operands that are not equivalent are
1415
+ functionally equivalent if, for any given set of template arguments, the
1416
+ expressions perform the same operations in the same order with the same
1417
+ entities.
1418
+
1419
+ [*Note 5*: For instance, one could have redundant
1420
+ parentheses. — *end note*]
1421
+
1422
+ Two *template-head*s are *equivalent* if their
1423
+ *template-parameter-list*s have the same length, corresponding
1424
+ *template-parameter*s are equivalent and are both declared with
1425
+ *type-constraint*s that are equivalent if either *template-parameter* is
1426
+ declared with a *type-constraint*, and if either *template-head* has a
1427
+ *requires-clause*, they both have *requires-clause*s and the
1428
+ corresponding *constraint-expression*s are equivalent. Two
1429
+ *template-parameter*s are *equivalent* under the following conditions:
1430
+
1431
+ - they declare template parameters of the same kind,
1432
+ - if either declares a template parameter pack, they both do,
1433
+ - if they declare non-type template parameters, they have equivalent
1434
+ types ignoring the use of *type-constraint*s for placeholder types,
1435
+ and
1436
+ - if they declare template template parameters, their template
1437
+ parameters are equivalent.
1438
+
1439
+ When determining whether types or *type-constraint*s are equivalent, the
1440
+ rules above are used to compare expressions involving template
1441
+ parameters. Two *template-head*s are *functionally equivalent* if they
1442
+ accept and are satisfied by [[temp.constr.constr]] the same set of
1443
+ template argument lists.
1444
 
1445
  Two function templates are *equivalent* if they are declared in the same
1446
+ scope, have the same name, have equivalent *template-head*s, and have
1447
+ return types, parameter lists, and trailing *requires-clause*s (if any)
1448
+ that are equivalent using the rules described above to compare
1449
+ expressions involving template parameters. Two function templates are
1450
+ *functionally equivalent* if they are declared in the same scope, have
1451
+ the same name, accept and are satisfied by the same set of template
1452
+ argument lists, and have return types and parameter lists that are
1453
+ functionally equivalent using the rules described above to compare
1454
+ expressions involving template parameters. If the validity or meaning of
1455
+ the program depends on whether two constructs are equivalent, and they
1456
+ are functionally equivalent but not equivalent, the program is
1457
+ ill-formed, no diagnostic required.
1458
 
1459
+ [*Note 6*:
1460
 
1461
  This rule guarantees that equivalent declarations will be linked with
1462
  one another, while not requiring implementations to use heroic efforts
1463
  to guarantee that functionally equivalent declarations will be treated
1464
  as distinct. For example, the last two declarations are functionally
1465
  equivalent and would cause a program to be ill-formed:
1466
 
1467
  ``` cpp
1468
+ // guaranteed to be the same
1469
  template <int I> void f(A<I>, A<I+10>);
1470
  template <int I> void f(A<I>, A<I+10>);
1471
 
1472
+ // guaranteed to be different
1473
  template <int I> void f(A<I>, A<I+10>);
1474
  template <int I> void f(A<I>, A<I+11>);
1475
 
1476
+ // ill-formed, no diagnostic required
1477
  template <int I> void f(A<I>, A<I+10>);
1478
  template <int I> void f(A<I>, A<I+1+2+3+4>);
1479
  ```
1480
 
1481
  — *end note*]
1482
 
1483
  #### Partial ordering of function templates <a id="temp.func.order">[[temp.func.order]]</a>
1484
 
1485
  If a function template is overloaded, the use of a function template
1486
+ specialization might be ambiguous because template argument deduction
1487
+ [[temp.deduct]] may associate the function template specialization with
1488
  more than one function template declaration. *Partial ordering* of
1489
  overloaded function template declarations is used in the following
1490
  contexts to select the function template to which a function template
1491
  specialization refers:
1492
 
1493
  - during overload resolution for a call to a function template
1494
+ specialization [[over.match.best]];
1495
  - when the address of a function template specialization is taken;
1496
  - when a placement operator delete that is a function template
1497
  specialization is selected to match a placement operator new (
1498
  [[basic.stc.dynamic.deallocation]], [[expr.new]]);
1499
+ - when a friend function declaration [[temp.friend]], an explicit
1500
+ instantiation [[temp.explicit]] or an explicit specialization
1501
+ [[temp.expl.spec]] refers to a function template specialization.
1502
 
1503
  Partial ordering selects which of two function templates is more
1504
  specialized than the other by transforming each template in turn (see
1505
  next paragraph) and performing template argument deduction using the
1506
  function type. The deduction process determines whether one of the
1507
  templates is more specialized than the other. If so, the more
1508
  specialized template is the one chosen by the partial ordering process.
1509
+ If both deductions succeed, the partial ordering selects the more
1510
+ constrained template (if one exists) as determined below.
1511
 
1512
  To produce the transformed template, for each type, non-type, or
1513
+ template template parameter (including template parameter packs
1514
+ [[temp.variadic]] thereof) synthesize a unique type, value, or class
1515
  template respectively and substitute it for each occurrence of that
1516
  parameter in the function type of the template.
1517
 
1518
  [*Note 1*: The type replacing the placeholder in the type of the value
1519
  synthesized for a non-type template parameter is also a unique
1520
  synthesized type. — *end note*]
1521
 
1522
+ Each function template M that is a member function is considered to have
1523
+ a new first parameter of type X(M), described below, inserted in its
1524
+ function parameter list. If exactly one of the function templates was
1525
+ considered by overload resolution via a rewritten candidate
1526
+ [[over.match.oper]] with a reversed order of parameters, then the order
1527
+ of the function parameters in its transformed template is reversed. For
1528
+ a function template M with cv-qualifiers cv that is a member of a class
1529
+ A:
1530
+
1531
+ - The type X(M) is “rvalue reference to cv A” if the optional
1532
+ *ref-qualifier* of M is `&&` or if M has no *ref-qualifier* and the
1533
+ positionally-corresponding parameter of the other transformed template
1534
+ has rvalue reference type; if this determination depends recursively
1535
+ upon whether X(M) is an rvalue reference type, it is not considered to
1536
+ have rvalue reference type.
1537
+ - Otherwise, X(M) is “lvalue reference to cv A”.
1538
 
1539
  [*Note 2*: This allows a non-static member to be ordered with respect
1540
  to a non-member function and for the results to be equivalent to the
1541
  ordering of two equivalent non-members. — *end note*]
1542
 
 
1554
  // template<class R> int operator*(B<A>&, R&);\quad\quad\quad// #1a
1555
 
1556
  int main() {
1557
  A a;
1558
  B<A> b;
1559
+ b * a; // calls #1
1560
  }
1561
  ```
1562
 
1563
  — *end example*]
1564
 
 
1595
 
1596
  — *end example*]
1597
 
1598
  [*Note 3*:
1599
 
1600
+ Since, in a call context, such type deduction considers only parameters
1601
+ for which there are explicit call arguments, some parameters are ignored
1602
  (namely, function parameter packs, parameters with default arguments,
1603
  and ellipsis parameters).
1604
 
1605
  [*Example 3*:
1606
 
 
1647
  template<class T > void f(T); // #2
1648
  template<class T, class... U> void g(T*, U...); // #3
1649
  template<class T > void g(T); // #4
1650
 
1651
  void h(int i) {
1652
+ f(&i); // OK: calls #2
1653
  g(&i); // OK: calls #3
1654
  }
1655
  ```
1656
 
1657
  — *end example*]
1658
 
1659
  — *end note*]
1660
 
1661
+ If deduction against the other template succeeds for both transformed
1662
+ templates, constraints can be considered as follows:
1663
+
1664
+ - If their *template-parameter-list*s (possibly including
1665
+ *template-parameter*s invented for an abbreviated function template
1666
+ [[dcl.fct]]) or function parameter lists differ in length, neither
1667
+ template is more specialized than the other.
1668
+ - Otherwise:
1669
+ - If exactly one of the templates was considered by overload
1670
+ resolution via a rewritten candidate with reversed order of
1671
+ parameters:
1672
+ - If, for either template, some of the template parameters are not
1673
+ deducible from their function parameters, neither template is more
1674
+ specialized than the other.
1675
+ - If there is either no reordering or more than one reordering of
1676
+ the associated *template-parameter-list* such that
1677
+ - the corresponding *template-parameter*s of the
1678
+ *template-parameter-list*s are equivalent and
1679
+ - the function parameters that positionally correspond between the
1680
+ two templates are of the same type,
1681
+
1682
+ neither template is more specialized than the other.
1683
+ - Otherwise, if the corresponding *template-parameter*s of the
1684
+ *template-parameter-list*s are not equivalent [[temp.over.link]] or
1685
+ if the function parameters that positionally correspond between the
1686
+ two templates are not of the same type, neither template is more
1687
+ specialized than the other.
1688
+ - Otherwise, if the context in which the partial ordering is done is
1689
+ that of a call to a conversion function and the return types of the
1690
+ templates are not the same, then neither template is more specialized
1691
+ than the other.
1692
+ - Otherwise, if one template is more constrained than the other
1693
+ [[temp.constr.order]], the more constrained template is more
1694
+ specialized than the other.
1695
+ - Otherwise, neither template is more specialized than the other.
1696
+
1697
+ [*Example 6*:
1698
+
1699
+ ``` cpp
1700
+ template <typename> constexpr bool True = true;
1701
+ template <typename T> concept C = True<T>;
1702
+
1703
+ void f(C auto &, auto &) = delete;
1704
+ template <C Q> void f(Q &, C auto &);
1705
+
1706
+ void g(struct A *ap, struct B *bp) {
1707
+ f(*ap, *bp); // OK: Can use different methods to produce template parameters
1708
+ }
1709
+
1710
+ template <typename T, typename U> struct X {};
1711
+
1712
+ template <typename T, C U, typename V> bool operator==(X<T, U>, V) = delete;
1713
+ template <C T, C U, C V> bool operator==(T, X<U, V>);
1714
+
1715
+ void h() {
1716
+ X<void *, int>{} == 0; // OK: Correspondence of [T, U, V] and [U, V, T]
1717
+ }
1718
+ ```
1719
+
1720
+ — *end example*]
1721
+
1722
  ### Alias templates <a id="temp.alias">[[temp.alias]]</a>
1723
 
1724
  A *template-declaration* in which the *declaration* is an
1725
+ *alias-declaration* [[dcl.pre]] declares the *identifier* to be an
1726
+ *alias template*. An alias template is a name for a family of types. The
1727
+ name of the alias template is a *template-name*.
1728
 
1729
  When a *template-id* refers to the specialization of an alias template,
1730
  it is equivalent to the associated type obtained by substitution of its
1731
+ *template-argument*s for the *template-parameter*s in the
1732
+ *defining-type-id* of the alias template.
1733
 
1734
  [*Note 1*: An alias template name is never deduced. — *end note*]
1735
 
1736
  [*Example 1*:
1737
 
 
1766
  [*Example 2*:
1767
 
1768
  ``` cpp
1769
  template<typename...> using void_t = void;
1770
  template<typename T> void_t<typename T::foo> f();
1771
+ f<int>(); // error: int does not have a nested type foo
1772
  ```
1773
 
1774
  — *end example*]
1775
 
1776
+ The *defining-type-id* in an alias template declaration shall not refer
1777
+ to the alias template being declared. The type produced by an alias
1778
+ template specialization shall not directly or indirectly make use of
1779
+ that specialization.
1780
 
1781
  [*Example 3*:
1782
 
1783
  ``` cpp
1784
  template <class T> struct A;
 
1789
  B<short> b; // error: instantiation of B<short> uses own type via A<short>::U
1790
  ```
1791
 
1792
  — *end example*]
1793
 
1794
+ The type of a *lambda-expression* appearing in an alias template
1795
+ declaration is different between instantiations of that template, even
1796
+ when the *lambda-expression* is not dependent.
1797
+
1798
+ [*Example 4*:
1799
+
1800
+ ``` cpp
1801
+ template <class T>
1802
+ using A = decltype([] { }); // A<int> and A<char> refer to different closure types
1803
+ ```
1804
+
1805
+ — *end example*]
1806
+
1807
+ ### Concept definitions <a id="temp.concept">[[temp.concept]]</a>
1808
+
1809
+ A *concept* is a template that defines constraints on its template
1810
+ arguments.
1811
+
1812
+ ``` bnf
1813
+ concept-definition:
1814
+ concept concept-name '=' constraint-expression ';'
1815
+ ```
1816
+
1817
+ ``` bnf
1818
+ concept-name:
1819
+ identifier
1820
+ ```
1821
+
1822
+ A *concept-definition* declares a concept. Its *identifier* becomes a
1823
+ *concept-name* referring to that concept within its scope.
1824
+
1825
+ [*Example 1*:
1826
+
1827
+ ``` cpp
1828
+ template<typename T>
1829
+ concept C = requires(T x) {
1830
+ { x == x } -> std::convertible_to<bool>;
1831
+ };
1832
+
1833
+ template<typename T>
1834
+ requires C<T> // C constrains f1(T) in constraint-expression
1835
+ T f1(T x) { return x; }
1836
+
1837
+ template<C T> // C, as a type-constraint, constrains f2(T)
1838
+ T f2(T x) { return x; }
1839
+ ```
1840
+
1841
+ — *end example*]
1842
+
1843
+ A *concept-definition* shall appear at namespace scope
1844
+ [[basic.scope.namespace]].
1845
+
1846
+ A concept shall not have associated constraints [[temp.constr.decl]].
1847
+
1848
+ A concept is not instantiated [[temp.spec]].
1849
+
1850
+ [*Note 1*: A concept-id [[temp.names]] is evaluated as an expression. A
1851
+ concept cannot be explicitly instantiated [[temp.explicit]], explicitly
1852
+ specialized [[temp.expl.spec]], or partially specialized. — *end note*]
1853
+
1854
+ The *constraint-expression* of a *concept-definition* is an unevaluated
1855
+ operand [[expr.context]].
1856
+
1857
+ The first declared template parameter of a concept definition is its
1858
+ *prototype parameter*. A *type concept* is a concept whose prototype
1859
+ parameter is a type *template-parameter*.
1860
+