From Jason Turner

[temp.fct.spec]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpj8h6n6et/{from.md → to.md} +59 -32
tmp/tmpj8h6n6et/{from.md → to.md} RENAMED
@@ -89,13 +89,13 @@ void h() {
89
  // int (*)(bool), Z is deduced to an empty sequence
90
  }
91
  ```
92
 
93
  An empty template argument list can be used to indicate that a given use
94
- refers to a specialization of a function template even when a normal
95
- (i.e., non-template) function is visible that would otherwise be used.
96
- For example:
97
 
98
  ``` cpp
99
  template <class T> int f(T); // #1
100
  int f(int); // #2
101
  int k = f(1); // uses #2
@@ -264,12 +264,15 @@ int main() {
264
  `f<int>(1)` and `f<const int>(1)` call distinct functions even though
265
  both of the functions called have the same function type.
266
 
267
  The resulting substituted and adjusted function type is used as the type
268
  of the function template for template argument deduction. If a template
269
- argument has not been deduced, its default template argument, if any, is
270
- used.
 
 
 
271
 
272
  ``` cpp
273
  template <class T, class U = double>
274
  void f(T t = 0, U u = 0);
275
 
@@ -302,24 +305,40 @@ The substitution occurs in all types and expressions that are used in
302
  the function type and in template parameter declarations. The
303
  expressions include not only constant expressions such as those that
304
  appear in array bounds or as nontype template arguments but also general
305
  expressions (i.e., non-constant expressions) inside `sizeof`,
306
  `decltype`, and other contexts that allow non-constant expressions. The
307
- equivalent substitution in exception specifications is done only when
308
- the function is instantiated, at which point a program is ill-formed if
309
- the substitution results in an invalid type or expression.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
310
 
311
  If a substitution results in an invalid type or expression, type
312
  deduction fails. An invalid type or expression is one that would be
313
- ill-formed if written using the substituted arguments. Access checking
314
- is done as part of the substitution process. Only invalid types and
315
- expressions in the immediate context of the function type and its
316
- template parameter types can result in a deduction failure. The
317
- evaluation of the substituted types and expressions can result in side
318
- effects such as the instantiation of class template specializations
319
- and/or function template specializations, the generation of
320
- implicitly-defined functions, etc. Such side effects are not in the
 
321
  “immediate context” and can result in the program being ill-formed.
322
 
323
  ``` cpp
324
  struct X { };
325
  struct Y {
@@ -449,22 +468,26 @@ g({1,2,3}); // error: no argument deduced for T
449
  For a function parameter pack that occurs at the end of the
450
  *parameter-declaration-list*, the type `A` of each remaining argument of
451
  the call is compared with the type `P` of the *declarator-id* of the
452
  function parameter pack. Each comparison deduces template arguments for
453
  subsequent positions in the template parameter packs expanded by the
454
- function parameter pack. For a function parameter pack that does not
455
- occur at the end of the *parameter-declaration-list*, the type of the
456
- parameter pack is a non-deduced context.
457
 
458
  ``` cpp
459
  template<class ... Types> void f(Types& ...);
460
  template<class T1, class ... Types> void g(T1, Types ...);
 
461
 
462
  void h(int x, float& y) {
463
  const int z = x;
464
  f(x, y, z); // Types is deduced to int, float, const int
465
  g(x, y, z); // T1 is deduced to int; Types is deduced to float, int
 
 
 
466
  }
467
  ```
468
 
469
  If `P` is not a reference type:
470
 
@@ -570,17 +593,22 @@ Template arguments can be deduced from the type specified when taking
570
  the address of an overloaded function ([[over.over]]). The function
571
  template’s function type and the specified type are used as the types of
572
  `P` and `A`, and the deduction is done as described in 
573
  [[temp.deduct.type]].
574
 
 
 
 
 
 
575
  #### Deducing conversion function template arguments <a id="temp.deduct.conv">[[temp.deduct.conv]]</a>
576
 
577
  Template argument deduction is done by comparing the return type of the
578
- conversion function template (call it `P`; see  [[dcl.init]],
579
- [[over.match.conv]], and [[over.match.ref]] for the determination of
580
- that type) with the type that is required as the result of the
581
- conversion (call it `A`) as described in  [[temp.deduct.type]].
582
 
583
  If `P` is a reference type, the type referred to by `P` is used in place
584
  of `P` for type deduction and for any further references to or
585
  transformations of `P` in the remainder of this section.
586
 
@@ -650,11 +678,11 @@ argument template and template-1 as the parameter template.
650
  The types used to determine the ordering depend on the context in which
651
  the partial ordering is done:
652
 
653
  - In the context of a function call, the types used are those function
654
  parameter types for which the function call has arguments.[^7]
655
- - In the context of a call to a conversion operator, the return types of
656
  the conversion function templates are used.
657
  - In other contexts ([[temp.func.order]]) the function template’s
658
  function type is used.
659
 
660
  Each type nominated above from the parameter template and the
@@ -798,10 +826,11 @@ specified, template argument deduction fails.
798
 
799
  The non-deduced contexts are:
800
 
801
  - The *nested-name-specifier* of a type that was specified using a
802
  *qualified-id*.
 
803
  - A non-type template argument or an array bound in which a
804
  subexpression references a template parameter.
805
  - A template parameter used in the parameter type of a function
806
  parameter that has a default argument that is being used in the call
807
  for which argument deduction is being done.
@@ -821,11 +850,11 @@ The non-deduced contexts are:
821
  ``` cpp
822
  template<class T> void g(T);
823
  g({1,2,3}); // error: no argument deduced for T
824
  ```
825
  - A function parameter pack that does not occur at the end of the
826
- *parameter-declaration-clause*.
827
 
828
  When a type name is specified in a way that includes a non-deduced
829
  context, all of the types that comprise that type name are also
830
  non-deduced. However, a compound type can include both deduced and
831
  non-deduced types. If a type is specified as `A<T>::B<T2>`, both `T` and
@@ -1093,17 +1122,14 @@ int x = deduce<77>(a.xm, 62, b.ym);
1093
  // A<int>::X
1094
  // i is explicitly specified to be 77, b.ym must be convertible
1095
  // to B<77>::Y
1096
  ```
1097
 
1098
- If, in the declaration of a function template with a non-type
1099
- *template-parameter,* the non-type *template-parameter* is used in an
1100
- expression in the function parameter-list and, if the corresponding
1101
- *template-argument* is deduced, the *template-argument* type shall match
1102
- the type of the *template-parameter* exactly, except that a
1103
- *template-argument* deduced from an array bound may be of any integral
1104
- type.[^8]
1105
 
1106
  ``` cpp
1107
  template<int i> class A { /* ... */ };
1108
  template<short s> void f(A<s>);
1109
  void k1() {
@@ -1331,10 +1357,11 @@ explicitly generated, is present in some translation unit.
1331
  [dcl.fct]: dcl.md#dcl.fct
1332
  [dcl.fct.default]: dcl.md#dcl.fct.default
1333
  [dcl.init]: dcl.md#dcl.init
1334
  [dcl.init.list]: dcl.md#dcl.init.list
1335
  [dcl.meaning]: dcl.md#dcl.meaning
 
1336
  [dcl.type.elab]: dcl.md#dcl.type.elab
1337
  [except.spec]: except.md#except.spec
1338
  [expr.const]: expr.md#expr.const
1339
  [expr.new]: expr.md#expr.new
1340
  [expr.prim.lambda]: expr.md#expr.prim.lambda
 
89
  // int (*)(bool), Z is deduced to an empty sequence
90
  }
91
  ```
92
 
93
  An empty template argument list can be used to indicate that a given use
94
+ refers to a specialization of a function template even when a
95
+ non-template function ([[dcl.fct]]) is visible that would otherwise be
96
+ used. For example:
97
 
98
  ``` cpp
99
  template <class T> int f(T); // #1
100
  int f(int); // #2
101
  int k = f(1); // uses #2
 
264
  `f<int>(1)` and `f<const int>(1)` call distinct functions even though
265
  both of the functions called have the same function type.
266
 
267
  The resulting substituted and adjusted function type is used as the type
268
  of the function template for template argument deduction. If a template
269
+ argument has not been deduced and its corresponding template parameter
270
+ has a default argument, the template argument is determined by
271
+ substituting the template arguments determined for preceding template
272
+ parameters into the default argument. If the substitution results in an
273
+ invalid type, as described above, type deduction fails.
274
 
275
  ``` cpp
276
  template <class T, class U = double>
277
  void f(T t = 0, U u = 0);
278
 
 
305
  the function type and in template parameter declarations. The
306
  expressions include not only constant expressions such as those that
307
  appear in array bounds or as nontype template arguments but also general
308
  expressions (i.e., non-constant expressions) inside `sizeof`,
309
  `decltype`, and other contexts that allow non-constant expressions. The
310
+ substitution proceeds in lexical order and stops when a condition that
311
+ causes deduction to fail is encountered. The equivalent substitution in
312
+ exception specifications is done only when the *exception-specification*
313
+ is instantiated, at which point a program is ill-formed if the
314
+ substitution results in an invalid type or expression.
315
+
316
+ ``` cpp
317
+ template <class T> struct A { using X = typename T::X; };
318
+ template <class T> typename T::X f(typename A<T>::X);
319
+ template <class T> void f(...) { }
320
+ template <class T> auto g(typename A<T>::X) -> typename T::X;
321
+ template <class T> void g(...) { }
322
+
323
+ void h() {
324
+ f<int>(0); // OK, substituting return type causes deduction to fail
325
+ g<int>(0); // error, substituting parameter type instantiates A<int>
326
+ }
327
+ ```
328
 
329
  If a substitution results in an invalid type or expression, type
330
  deduction fails. An invalid type or expression is one that would be
331
+ ill-formed, with a diagnostic required, if written using the substituted
332
+ arguments. If no diagnostic is required, the program is still
333
+ ill-formed. Access checking is done as part of the substitution process.
334
+ Only invalid types and expressions in the immediate context of the
335
+ function type and its template parameter types can result in a deduction
336
+ failure. The evaluation of the substituted types and expressions can
337
+ result in side effects such as the instantiation of class template
338
+ specializations and/or function template specializations, the generation
339
+ of implicitly-defined functions, etc. Such side effects are not in the
340
  “immediate context” and can result in the program being ill-formed.
341
 
342
  ``` cpp
343
  struct X { };
344
  struct Y {
 
468
  For a function parameter pack that occurs at the end of the
469
  *parameter-declaration-list*, the type `A` of each remaining argument of
470
  the call is compared with the type `P` of the *declarator-id* of the
471
  function parameter pack. Each comparison deduces template arguments for
472
  subsequent positions in the template parameter packs expanded by the
473
+ function parameter pack. When a function parameter pack appears in a
474
+ non-deduced context ([[temp.deduct.type]]), the type of that parameter
475
+ pack is never deduced.
476
 
477
  ``` cpp
478
  template<class ... Types> void f(Types& ...);
479
  template<class T1, class ... Types> void g(T1, Types ...);
480
+ template<class T1, class ... Types> void g1(Types ..., T1);
481
 
482
  void h(int x, float& y) {
483
  const int z = x;
484
  f(x, y, z); // Types is deduced to int, float, const int
485
  g(x, y, z); // T1 is deduced to int; Types is deduced to float, int
486
+ g1(x, y, z); // error: Types is not deduced
487
+ g1<int, int, int>(x, y, z); // OK, no deduction occurs
488
+
489
  }
490
  ```
491
 
492
  If `P` is not a reference type:
493
 
 
593
  the address of an overloaded function ([[over.over]]). The function
594
  template’s function type and the specified type are used as the types of
595
  `P` and `A`, and the deduction is done as described in 
596
  [[temp.deduct.type]].
597
 
598
+ A placeholder type ([[dcl.spec.auto]]) in the return type of a function
599
+ template is a non-deduced context. If template argument deduction
600
+ succeeds for such a function, the return type is determined from
601
+ instantiation of the function body.
602
+
603
  #### Deducing conversion function template arguments <a id="temp.deduct.conv">[[temp.deduct.conv]]</a>
604
 
605
  Template argument deduction is done by comparing the return type of the
606
+ conversion function template (call it `P`) with the type that is
607
+ required as the result of the conversion (call it `A`; see 
608
+ [[dcl.init]], [[over.match.conv]], and [[over.match.ref]] for the
609
+ determination of that type) as described in  [[temp.deduct.type]].
610
 
611
  If `P` is a reference type, the type referred to by `P` is used in place
612
  of `P` for type deduction and for any further references to or
613
  transformations of `P` in the remainder of this section.
614
 
 
678
  The types used to determine the ordering depend on the context in which
679
  the partial ordering is done:
680
 
681
  - In the context of a function call, the types used are those function
682
  parameter types for which the function call has arguments.[^7]
683
+ - In the context of a call to a conversion function, the return types of
684
  the conversion function templates are used.
685
  - In other contexts ([[temp.func.order]]) the function template’s
686
  function type is used.
687
 
688
  Each type nominated above from the parameter template and the
 
826
 
827
  The non-deduced contexts are:
828
 
829
  - The *nested-name-specifier* of a type that was specified using a
830
  *qualified-id*.
831
+ - The *expression* of a *decltype-specifier*.
832
  - A non-type template argument or an array bound in which a
833
  subexpression references a template parameter.
834
  - A template parameter used in the parameter type of a function
835
  parameter that has a default argument that is being used in the call
836
  for which argument deduction is being done.
 
850
  ``` cpp
851
  template<class T> void g(T);
852
  g({1,2,3}); // error: no argument deduced for T
853
  ```
854
  - A function parameter pack that does not occur at the end of the
855
+ *parameter-declaration-list*.
856
 
857
  When a type name is specified in a way that includes a non-deduced
858
  context, all of the types that comprise that type name are also
859
  non-deduced. However, a compound type can include both deduced and
860
  non-deduced types. If a type is specified as `A<T>::B<T2>`, both `T` and
 
1122
  // A<int>::X
1123
  // i is explicitly specified to be 77, b.ym must be convertible
1124
  // to B<77>::Y
1125
  ```
1126
 
1127
+ If `P` has a form that contains `<i>`, and if the type of the
1128
+ corresponding value of `A` differs from the type of `i`, deduction
1129
+ fails. If `P` has a form that contains `[i]`, and if the type of `i` is
1130
+ not an integral type, deduction fails.[^8]
 
 
 
1131
 
1132
  ``` cpp
1133
  template<int i> class A { /* ... */ };
1134
  template<short s> void f(A<s>);
1135
  void k1() {
 
1357
  [dcl.fct]: dcl.md#dcl.fct
1358
  [dcl.fct.default]: dcl.md#dcl.fct.default
1359
  [dcl.init]: dcl.md#dcl.init
1360
  [dcl.init.list]: dcl.md#dcl.init.list
1361
  [dcl.meaning]: dcl.md#dcl.meaning
1362
+ [dcl.spec.auto]: dcl.md#dcl.spec.auto
1363
  [dcl.type.elab]: dcl.md#dcl.type.elab
1364
  [except.spec]: except.md#except.spec
1365
  [expr.const]: expr.md#expr.const
1366
  [expr.new]: expr.md#expr.new
1367
  [expr.prim.lambda]: expr.md#expr.prim.lambda