From Jason Turner

[temp.deduct]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp34o46s4y/{from.md → to.md} +55 -29
tmp/tmp34o46s4y/{from.md → to.md} RENAMED
@@ -73,12 +73,15 @@ int main() {
73
  `f<int>(1)` and `f<const int>(1)` call distinct functions even though
74
  both of the functions called have the same function type.
75
 
76
  The resulting substituted and adjusted function type is used as the type
77
  of the function template for template argument deduction. If a template
78
- argument has not been deduced, its default template argument, if any, is
79
- used.
 
 
 
80
 
81
  ``` cpp
82
  template <class T, class U = double>
83
  void f(T t = 0, U u = 0);
84
 
@@ -111,24 +114,40 @@ The substitution occurs in all types and expressions that are used in
111
  the function type and in template parameter declarations. The
112
  expressions include not only constant expressions such as those that
113
  appear in array bounds or as nontype template arguments but also general
114
  expressions (i.e., non-constant expressions) inside `sizeof`,
115
  `decltype`, and other contexts that allow non-constant expressions. The
116
- equivalent substitution in exception specifications is done only when
117
- the function is instantiated, at which point a program is ill-formed if
118
- the substitution results in an invalid type or expression.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
  If a substitution results in an invalid type or expression, type
121
  deduction fails. An invalid type or expression is one that would be
122
- ill-formed if written using the substituted arguments. Access checking
123
- is done as part of the substitution process. Only invalid types and
124
- expressions in the immediate context of the function type and its
125
- template parameter types can result in a deduction failure. The
126
- evaluation of the substituted types and expressions can result in side
127
- effects such as the instantiation of class template specializations
128
- and/or function template specializations, the generation of
129
- implicitly-defined functions, etc. Such side effects are not in the
 
130
  “immediate context” and can result in the program being ill-formed.
131
 
132
  ``` cpp
133
  struct X { };
134
  struct Y {
@@ -258,22 +277,26 @@ g({1,2,3}); // error: no argument deduced for T
258
  For a function parameter pack that occurs at the end of the
259
  *parameter-declaration-list*, the type `A` of each remaining argument of
260
  the call is compared with the type `P` of the *declarator-id* of the
261
  function parameter pack. Each comparison deduces template arguments for
262
  subsequent positions in the template parameter packs expanded by the
263
- function parameter pack. For a function parameter pack that does not
264
- occur at the end of the *parameter-declaration-list*, the type of the
265
- parameter pack is a non-deduced context.
266
 
267
  ``` cpp
268
  template<class ... Types> void f(Types& ...);
269
  template<class T1, class ... Types> void g(T1, Types ...);
 
270
 
271
  void h(int x, float& y) {
272
  const int z = x;
273
  f(x, y, z); // Types is deduced to int, float, const int
274
  g(x, y, z); // T1 is deduced to int; Types is deduced to float, int
 
 
 
275
  }
276
  ```
277
 
278
  If `P` is not a reference type:
279
 
@@ -379,17 +402,22 @@ Template arguments can be deduced from the type specified when taking
379
  the address of an overloaded function ([[over.over]]). The function
380
  template’s function type and the specified type are used as the types of
381
  `P` and `A`, and the deduction is done as described in 
382
  [[temp.deduct.type]].
383
 
 
 
 
 
 
384
  #### Deducing conversion function template arguments <a id="temp.deduct.conv">[[temp.deduct.conv]]</a>
385
 
386
  Template argument deduction is done by comparing the return type of the
387
- conversion function template (call it `P`; see  [[dcl.init]],
388
- [[over.match.conv]], and [[over.match.ref]] for the determination of
389
- that type) with the type that is required as the result of the
390
- conversion (call it `A`) as described in  [[temp.deduct.type]].
391
 
392
  If `P` is a reference type, the type referred to by `P` is used in place
393
  of `P` for type deduction and for any further references to or
394
  transformations of `P` in the remainder of this section.
395
 
@@ -459,11 +487,11 @@ argument template and template-1 as the parameter template.
459
  The types used to determine the ordering depend on the context in which
460
  the partial ordering is done:
461
 
462
  - In the context of a function call, the types used are those function
463
  parameter types for which the function call has arguments.[^7]
464
- - In the context of a call to a conversion operator, the return types of
465
  the conversion function templates are used.
466
  - In other contexts ([[temp.func.order]]) the function template’s
467
  function type is used.
468
 
469
  Each type nominated above from the parameter template and the
@@ -607,10 +635,11 @@ specified, template argument deduction fails.
607
 
608
  The non-deduced contexts are:
609
 
610
  - The *nested-name-specifier* of a type that was specified using a
611
  *qualified-id*.
 
612
  - A non-type template argument or an array bound in which a
613
  subexpression references a template parameter.
614
  - A template parameter used in the parameter type of a function
615
  parameter that has a default argument that is being used in the call
616
  for which argument deduction is being done.
@@ -630,11 +659,11 @@ The non-deduced contexts are:
630
  ``` cpp
631
  template<class T> void g(T);
632
  g({1,2,3}); // error: no argument deduced for T
633
  ```
634
  - A function parameter pack that does not occur at the end of the
635
- *parameter-declaration-clause*.
636
 
637
  When a type name is specified in a way that includes a non-deduced
638
  context, all of the types that comprise that type name are also
639
  non-deduced. However, a compound type can include both deduced and
640
  non-deduced types. If a type is specified as `A<T>::B<T2>`, both `T` and
@@ -902,17 +931,14 @@ int x = deduce<77>(a.xm, 62, b.ym);
902
  // A<int>::X
903
  // i is explicitly specified to be 77, b.ym must be convertible
904
  // to B<77>::Y
905
  ```
906
 
907
- If, in the declaration of a function template with a non-type
908
- *template-parameter,* the non-type *template-parameter* is used in an
909
- expression in the function parameter-list and, if the corresponding
910
- *template-argument* is deduced, the *template-argument* type shall match
911
- the type of the *template-parameter* exactly, except that a
912
- *template-argument* deduced from an array bound may be of any integral
913
- type.[^8]
914
 
915
  ``` cpp
916
  template<int i> class A { /* ... */ };
917
  template<short s> void f(A<s>);
918
  void k1() {
 
73
  `f<int>(1)` and `f<const int>(1)` call distinct functions even though
74
  both of the functions called have the same function type.
75
 
76
  The resulting substituted and adjusted function type is used as the type
77
  of the function template for template argument deduction. If a template
78
+ argument has not been deduced and its corresponding template parameter
79
+ has a default argument, the template argument is determined by
80
+ substituting the template arguments determined for preceding template
81
+ parameters into the default argument. If the substitution results in an
82
+ invalid type, as described above, type deduction fails.
83
 
84
  ``` cpp
85
  template <class T, class U = double>
86
  void f(T t = 0, U u = 0);
87
 
 
114
  the function type and in template parameter declarations. The
115
  expressions include not only constant expressions such as those that
116
  appear in array bounds or as nontype template arguments but also general
117
  expressions (i.e., non-constant expressions) inside `sizeof`,
118
  `decltype`, and other contexts that allow non-constant expressions. The
119
+ substitution proceeds in lexical order and stops when a condition that
120
+ causes deduction to fail is encountered. The equivalent substitution in
121
+ exception specifications is done only when the *exception-specification*
122
+ is instantiated, at which point a program is ill-formed if the
123
+ substitution results in an invalid type or expression.
124
+
125
+ ``` cpp
126
+ template <class T> struct A { using X = typename T::X; };
127
+ template <class T> typename T::X f(typename A<T>::X);
128
+ template <class T> void f(...) { }
129
+ template <class T> auto g(typename A<T>::X) -> typename T::X;
130
+ template <class T> void g(...) { }
131
+
132
+ void h() {
133
+ f<int>(0); // OK, substituting return type causes deduction to fail
134
+ g<int>(0); // error, substituting parameter type instantiates A<int>
135
+ }
136
+ ```
137
 
138
  If a substitution results in an invalid type or expression, type
139
  deduction fails. An invalid type or expression is one that would be
140
+ ill-formed, with a diagnostic required, if written using the substituted
141
+ arguments. If no diagnostic is required, the program is still
142
+ ill-formed. Access checking is done as part of the substitution process.
143
+ Only invalid types and expressions in the immediate context of the
144
+ function type and its template parameter types can result in a deduction
145
+ failure. The evaluation of the substituted types and expressions can
146
+ result in side effects such as the instantiation of class template
147
+ specializations and/or function template specializations, the generation
148
+ of implicitly-defined functions, etc. Such side effects are not in the
149
  “immediate context” and can result in the program being ill-formed.
150
 
151
  ``` cpp
152
  struct X { };
153
  struct Y {
 
277
  For a function parameter pack that occurs at the end of the
278
  *parameter-declaration-list*, the type `A` of each remaining argument of
279
  the call is compared with the type `P` of the *declarator-id* of the
280
  function parameter pack. Each comparison deduces template arguments for
281
  subsequent positions in the template parameter packs expanded by the
282
+ function parameter pack. When a function parameter pack appears in a
283
+ non-deduced context ([[temp.deduct.type]]), the type of that parameter
284
+ pack is never deduced.
285
 
286
  ``` cpp
287
  template<class ... Types> void f(Types& ...);
288
  template<class T1, class ... Types> void g(T1, Types ...);
289
+ template<class T1, class ... Types> void g1(Types ..., T1);
290
 
291
  void h(int x, float& y) {
292
  const int z = x;
293
  f(x, y, z); // Types is deduced to int, float, const int
294
  g(x, y, z); // T1 is deduced to int; Types is deduced to float, int
295
+ g1(x, y, z); // error: Types is not deduced
296
+ g1<int, int, int>(x, y, z); // OK, no deduction occurs
297
+
298
  }
299
  ```
300
 
301
  If `P` is not a reference type:
302
 
 
402
  the address of an overloaded function ([[over.over]]). The function
403
  template’s function type and the specified type are used as the types of
404
  `P` and `A`, and the deduction is done as described in 
405
  [[temp.deduct.type]].
406
 
407
+ A placeholder type ([[dcl.spec.auto]]) in the return type of a function
408
+ template is a non-deduced context. If template argument deduction
409
+ succeeds for such a function, the return type is determined from
410
+ instantiation of the function body.
411
+
412
  #### Deducing conversion function template arguments <a id="temp.deduct.conv">[[temp.deduct.conv]]</a>
413
 
414
  Template argument deduction is done by comparing the return type of the
415
+ conversion function template (call it `P`) with the type that is
416
+ required as the result of the conversion (call it `A`; see 
417
+ [[dcl.init]], [[over.match.conv]], and [[over.match.ref]] for the
418
+ determination of that type) as described in  [[temp.deduct.type]].
419
 
420
  If `P` is a reference type, the type referred to by `P` is used in place
421
  of `P` for type deduction and for any further references to or
422
  transformations of `P` in the remainder of this section.
423
 
 
487
  The types used to determine the ordering depend on the context in which
488
  the partial ordering is done:
489
 
490
  - In the context of a function call, the types used are those function
491
  parameter types for which the function call has arguments.[^7]
492
+ - In the context of a call to a conversion function, the return types of
493
  the conversion function templates are used.
494
  - In other contexts ([[temp.func.order]]) the function template’s
495
  function type is used.
496
 
497
  Each type nominated above from the parameter template and the
 
635
 
636
  The non-deduced contexts are:
637
 
638
  - The *nested-name-specifier* of a type that was specified using a
639
  *qualified-id*.
640
+ - The *expression* of a *decltype-specifier*.
641
  - A non-type template argument or an array bound in which a
642
  subexpression references a template parameter.
643
  - A template parameter used in the parameter type of a function
644
  parameter that has a default argument that is being used in the call
645
  for which argument deduction is being done.
 
659
  ``` cpp
660
  template<class T> void g(T);
661
  g({1,2,3}); // error: no argument deduced for T
662
  ```
663
  - A function parameter pack that does not occur at the end of the
664
+ *parameter-declaration-list*.
665
 
666
  When a type name is specified in a way that includes a non-deduced
667
  context, all of the types that comprise that type name are also
668
  non-deduced. However, a compound type can include both deduced and
669
  non-deduced types. If a type is specified as `A<T>::B<T2>`, both `T` and
 
931
  // A<int>::X
932
  // i is explicitly specified to be 77, b.ym must be convertible
933
  // to B<77>::Y
934
  ```
935
 
936
+ If `P` has a form that contains `<i>`, and if the type of the
937
+ corresponding value of `A` differs from the type of `i`, deduction
938
+ fails. If `P` has a form that contains `[i]`, and if the type of `i` is
939
+ not an integral type, deduction fails.[^8]
 
 
 
940
 
941
  ``` cpp
942
  template<int i> class A { /* ... */ };
943
  template<short s> void f(A<s>);
944
  void k1() {