- tmp/tmp9t24d4k8/{from.md → to.md} +218 -159
tmp/tmp9t24d4k8/{from.md → to.md}
RENAMED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
### Template argument deduction <a id="temp.deduct">[[temp.deduct]]</a>
|
| 2 |
|
|
|
|
|
|
|
| 3 |
When a function template specialization is referenced, all of the
|
| 4 |
template arguments shall have values. The values can be explicitly
|
| 5 |
specified or, in some cases, be deduced from the use or obtained from
|
| 6 |
default *template-argument*s.
|
| 7 |
|
|
@@ -96,45 +98,90 @@ void g() {
|
|
| 96 |
|
| 97 |
— *end example*]
|
| 98 |
|
| 99 |
When all template arguments have been deduced or obtained from default
|
| 100 |
template arguments, all uses of template parameters in the template
|
| 101 |
-
parameter list of the template
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
[[temp.constr.
|
| 106 |
-
|
| 107 |
-
deduction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
At certain points in the template argument deduction process it is
|
| 110 |
necessary to take a function type that makes use of template parameters
|
| 111 |
and replace those template parameters with the corresponding template
|
| 112 |
arguments. This is done at the beginning of template argument deduction
|
| 113 |
when any explicitly specified template arguments are substituted into
|
| 114 |
the function type, and again at the end of template argument deduction
|
| 115 |
when any template arguments that were deduced or obtained from default
|
| 116 |
arguments are substituted.
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
The substitution occurs in all types and expressions that are used in
|
| 119 |
-
the
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
|
| 130 |
-
[*Note
|
| 131 |
done only when the *noexcept-specifier* is instantiated, at which point
|
| 132 |
a program is ill-formed if the substitution results in an invalid type
|
| 133 |
or expression. — *end note*]
|
| 134 |
|
| 135 |
-
[*Example
|
| 136 |
|
| 137 |
``` cpp
|
| 138 |
template <class T> struct A { using X = typename T::X; };
|
| 139 |
template <class T> typename T::X f(typename A<T>::X);
|
| 140 |
template <class T> void f(...) { }
|
|
@@ -153,38 +200,37 @@ void x() {
|
|
| 153 |
|
| 154 |
— *end example*]
|
| 155 |
|
| 156 |
If a substitution results in an invalid type or expression, type
|
| 157 |
deduction fails. An invalid type or expression is one that would be
|
| 158 |
-
ill-formed, with a diagnostic required, if written
|
| 159 |
-
arguments.
|
| 160 |
|
| 161 |
-
[*Note
|
| 162 |
ill-formed. Access checking is done as part of the substitution
|
| 163 |
process. — *end note*]
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
*explicit-specifier* can result in a deduction failure.
|
| 168 |
|
| 169 |
-
[*Note
|
| 170 |
effects such as the instantiation of class template specializations
|
| 171 |
and/or function template specializations, the generation of
|
| 172 |
implicitly-defined functions, etc. Such effects are not in the
|
| 173 |
“immediate context” and can result in the program being
|
| 174 |
ill-formed. — *end note*]
|
| 175 |
|
| 176 |
A *lambda-expression* appearing in a function type or a template
|
| 177 |
parameter is not considered part of the immediate context for the
|
| 178 |
purposes of template argument deduction.
|
| 179 |
|
| 180 |
-
[*Note
|
| 181 |
|
| 182 |
The intent is to avoid requiring implementations to deal with
|
| 183 |
substitution failure involving arbitrary statements.
|
| 184 |
|
| 185 |
-
[*Example
|
| 186 |
|
| 187 |
``` cpp
|
| 188 |
template <class T>
|
| 189 |
auto f(T) -> decltype([]() { T::invalid; } ());
|
| 190 |
void f(...);
|
|
@@ -213,11 +259,11 @@ j(0); // deduction fails on #1, calls #2
|
|
| 213 |
|
| 214 |
— *end example*]
|
| 215 |
|
| 216 |
— *end note*]
|
| 217 |
|
| 218 |
-
[*Example
|
| 219 |
|
| 220 |
``` cpp
|
| 221 |
struct X { };
|
| 222 |
struct Y {
|
| 223 |
Y(X) {}
|
|
@@ -230,30 +276,30 @@ X x1, x2;
|
|
| 230 |
X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
|
| 231 |
```
|
| 232 |
|
| 233 |
— *end example*]
|
| 234 |
|
| 235 |
-
[*Note
|
| 236 |
|
| 237 |
-
Type deduction
|
| 238 |
|
| 239 |
- Attempting to instantiate a pack expansion containing multiple packs
|
| 240 |
of differing lengths.
|
| 241 |
- Attempting to create an array with an element type that is `void`, a
|
| 242 |
function type, or a reference type, or attempting to create an array
|
| 243 |
with a size that is zero or negative.
|
| 244 |
-
\[*Example
|
| 245 |
``` cpp
|
| 246 |
template <class T> int f(T[5]);
|
| 247 |
int I = f<int>(0);
|
| 248 |
int j = f<void>(0); // invalid array
|
| 249 |
```
|
| 250 |
|
| 251 |
— *end example*]
|
| 252 |
- Attempting to use a type that is not a class or enumeration type in a
|
| 253 |
qualified name.
|
| 254 |
-
\[*Example
|
| 255 |
``` cpp
|
| 256 |
template <class T> int f(typename T::B*);
|
| 257 |
int i = f<int>(0);
|
| 258 |
```
|
| 259 |
|
|
@@ -264,17 +310,17 @@ Type deduction may fail for the following reasons:
|
|
| 264 |
- the specified member is not a type where a type is required, or
|
| 265 |
- the specified member is not a template where a template is required,
|
| 266 |
or
|
| 267 |
- the specified member is not a non-type where a non-type is required.
|
| 268 |
|
| 269 |
-
\[*Example
|
| 270 |
``` cpp
|
| 271 |
template <int I> struct X { };
|
| 272 |
template <template <class T> class> struct Z { };
|
| 273 |
template <class T> void f(typename T::Y*) {}
|
| 274 |
template <class T> void g(X<T::N>*) {}
|
| 275 |
-
template <class T> void h(Z<T::
|
| 276 |
struct A {};
|
| 277 |
struct B { int Y; };
|
| 278 |
struct C {
|
| 279 |
typedef int N;
|
| 280 |
};
|
|
@@ -294,44 +340,46 @@ Type deduction may fail for the following reasons:
|
|
| 294 |
— *end example*]
|
| 295 |
- Attempting to create a pointer to reference type.
|
| 296 |
- Attempting to create a reference to `void`.
|
| 297 |
- Attempting to create “pointer to member of `T`” when `T` is not a
|
| 298 |
class type.
|
| 299 |
-
\[*Example
|
| 300 |
``` cpp
|
| 301 |
template <class T> int f(int T::*);
|
| 302 |
int i = f<int>(0);
|
| 303 |
```
|
| 304 |
|
| 305 |
— *end example*]
|
| 306 |
- Attempting to give an invalid type to a non-type template parameter.
|
| 307 |
-
\[*Example
|
| 308 |
``` cpp
|
| 309 |
template <class T, T> struct S {};
|
| 310 |
-
template <class T> int f(S<T, T
|
| 311 |
-
|
| 312 |
-
|
|
|
|
|
|
|
| 313 |
```
|
| 314 |
|
| 315 |
— *end example*]
|
| 316 |
- Attempting to perform an invalid conversion in either a template
|
| 317 |
argument expression, or an expression used in the function
|
| 318 |
declaration.
|
| 319 |
-
\[*Example
|
| 320 |
``` cpp
|
| 321 |
template <class T, T*> int f(int);
|
| 322 |
-
int i2 = f<int,1>(0); // can't
|
| 323 |
```
|
| 324 |
|
| 325 |
— *end example*]
|
| 326 |
- Attempting to create a function type in which a parameter has a type
|
| 327 |
of `void`, or in which the return type is a function type or array
|
| 328 |
type.
|
| 329 |
|
| 330 |
— *end note*]
|
| 331 |
|
| 332 |
-
[*Example
|
| 333 |
|
| 334 |
In the following example, assuming a `signed char` cannot represent the
|
| 335 |
value 1000, a narrowing conversion [[dcl.init.list]] would be required
|
| 336 |
to convert the *template-argument* of type `int` to `signed char`,
|
| 337 |
therefore substitution fails for the second template
|
|
@@ -517,15 +565,17 @@ that allow a difference:
|
|
| 517 |
template <typename... T> struct X;
|
| 518 |
template <> struct X<> {};
|
| 519 |
template <typename T, typename... Ts>
|
| 520 |
struct X<T, Ts...> : X<Ts...> {};
|
| 521 |
struct D : X<int> {};
|
|
|
|
| 522 |
|
| 523 |
template <typename... T>
|
| 524 |
int f(const X<T...>&);
|
| 525 |
int x = f(D()); // calls f<int>, not f<>
|
| 526 |
// B is X<>, C is X<int>
|
|
|
|
| 527 |
```
|
| 528 |
|
| 529 |
— *end example*]
|
| 530 |
|
| 531 |
These alternatives are considered only if type deduction would otherwise
|
|
@@ -584,62 +634,33 @@ template <class T> T g(T);
|
|
| 584 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 585 |
```
|
| 586 |
|
| 587 |
— *end example*]
|
| 588 |
|
| 589 |
-
If deduction succeeds for all parameters that contain
|
| 590 |
-
*template-parameter*s that participate in template argument deduction,
|
| 591 |
-
and all template arguments are explicitly specified, deduced, or
|
| 592 |
-
obtained from default template arguments, remaining parameters are then
|
| 593 |
-
compared with the corresponding arguments. For each remaining parameter
|
| 594 |
-
`P` with a type that was non-dependent before substitution of any
|
| 595 |
-
explicitly-specified template arguments, if the corresponding argument
|
| 596 |
-
`A` cannot be implicitly converted to `P`, deduction fails.
|
| 597 |
-
|
| 598 |
-
[*Note 2*: Parameters with dependent types in which no
|
| 599 |
-
*template-parameter*s participate in template argument deduction, and
|
| 600 |
-
parameters that became non-dependent due to substitution of
|
| 601 |
-
explicitly-specified template arguments, will be checked during overload
|
| 602 |
-
resolution. — *end note*]
|
| 603 |
-
|
| 604 |
-
[*Example 9*:
|
| 605 |
-
|
| 606 |
-
``` cpp
|
| 607 |
-
template <class T> struct Z {
|
| 608 |
-
typedef typename T::x xx;
|
| 609 |
-
};
|
| 610 |
-
template <class T> typename Z<T>::xx f(void *, T); // #1
|
| 611 |
-
template <class T> void f(int, T); // #2
|
| 612 |
-
struct A {} a;
|
| 613 |
-
int main() {
|
| 614 |
-
f(1, a); // OK, deduction fails for #1 because there is no conversion from int to void*
|
| 615 |
-
}
|
| 616 |
-
```
|
| 617 |
-
|
| 618 |
-
— *end example*]
|
| 619 |
-
|
| 620 |
#### Deducing template arguments taking the address of a function template <a id="temp.deduct.funcaddr">[[temp.deduct.funcaddr]]</a>
|
| 621 |
|
| 622 |
Template arguments can be deduced from the type specified when taking
|
| 623 |
-
the address of an
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
|
| 629 |
A placeholder type [[dcl.spec.auto]] in the return type of a function
|
| 630 |
template is a non-deduced context. If template argument deduction
|
| 631 |
succeeds for such a function, the return type is determined from
|
| 632 |
instantiation of the function body.
|
| 633 |
|
| 634 |
#### Deducing conversion function template arguments <a id="temp.deduct.conv">[[temp.deduct.conv]]</a>
|
| 635 |
|
| 636 |
Template argument deduction is done by comparing the return type of the
|
| 637 |
-
conversion function template (call it `P`) with the type
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
|
|
|
|
|
|
| 641 |
|
| 642 |
If `P` is a reference type, the type referred to by `P` is used in place
|
| 643 |
of `P` for type deduction and for any further references to or
|
| 644 |
transformations of `P` in the remainder of this subclause.
|
| 645 |
|
|
@@ -657,26 +678,24 @@ If `A` is not a reference type:
|
|
| 657 |
If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s type
|
| 658 |
are ignored for type deduction. If `A` is a reference type, the type
|
| 659 |
referred to by `A` is used for type deduction.
|
| 660 |
|
| 661 |
In general, the deduction process attempts to find template argument
|
| 662 |
-
values that will make the deduced `A` identical to `A`. However,
|
| 663 |
-
|
| 664 |
|
| 665 |
-
- If the original `A` is a reference type,
|
| 666 |
-
|
| 667 |
-
- If the original `A` is a function pointer
|
| 668 |
-
function
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
- The deduced `A` can be another pointer or pointer-to-member type that
|
| 673 |
-
can be converted to `A` via a qualification conversion.
|
| 674 |
|
| 675 |
-
These
|
| 676 |
-
fail. If
|
| 677 |
-
deduction fails.
|
| 678 |
|
| 679 |
#### Deducing template arguments during partial ordering <a id="temp.deduct.partial">[[temp.deduct.partial]]</a>
|
| 680 |
|
| 681 |
Template argument deduction is done by comparing certain types
|
| 682 |
associated with the two function templates being compared.
|
|
@@ -698,11 +717,11 @@ as the parameter template.
|
|
| 698 |
|
| 699 |
The types used to determine the ordering depend on the context in which
|
| 700 |
the partial ordering is done:
|
| 701 |
|
| 702 |
- In the context of a function call, the types used are those function
|
| 703 |
-
parameter types for which the function call has arguments.[^
|
| 704 |
- In the context of a call to a conversion function, the return types of
|
| 705 |
the conversion function templates are used.
|
| 706 |
- In other contexts [[temp.func.order]] the function template’s function
|
| 707 |
type is used.
|
| 708 |
|
|
@@ -843,12 +862,12 @@ deduction fails. The type of a type parameter is only deduced from an
|
|
| 843 |
array bound if it is not otherwise deduced.
|
| 844 |
|
| 845 |
A given type `P` can be composed from a number of other types,
|
| 846 |
templates, and non-type values:
|
| 847 |
|
| 848 |
-
- A function type includes the types of each of the function parameters
|
| 849 |
-
|
| 850 |
- A pointer-to-member type includes the type of the class object pointed
|
| 851 |
to and the type of the member pointed to.
|
| 852 |
- A type that is a specialization of a class template (e.g., `A<int>`)
|
| 853 |
includes the types, templates, and non-type values referenced by the
|
| 854 |
template argument list of the specialization.
|
|
@@ -921,14 +940,14 @@ inconsistent template argument deductions:
|
|
| 921 |
``` cpp
|
| 922 |
template<class T> void f(T x, T y) { ... }
|
| 923 |
struct A { ... };
|
| 924 |
struct B : A { ... };
|
| 925 |
void g(A a, B b) {
|
| 926 |
-
f(a,b); // error: T
|
| 927 |
-
f(b,a); // error: T
|
| 928 |
-
f(a,a); // OK
|
| 929 |
-
f(b,b); // OK
|
| 930 |
}
|
| 931 |
```
|
| 932 |
|
| 933 |
Here is an example where two template arguments are deduced from a
|
| 934 |
single function parameter/argument pair. This can lead to conflicts that
|
|
@@ -940,13 +959,32 @@ template <class T, class U> void f( T (*)( T, U, U ) );
|
|
| 940 |
int g1( int, float, float);
|
| 941 |
char g2( int, float, float);
|
| 942 |
int g3( int, char, float);
|
| 943 |
|
| 944 |
void r() {
|
| 945 |
-
f(g1); // OK
|
| 946 |
-
f(g2); // error: T
|
| 947 |
-
f(g3); // error: U
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 948 |
}
|
| 949 |
```
|
| 950 |
|
| 951 |
Here is an example where a qualification conversion applies between the
|
| 952 |
argument type on the function call and the deduced template argument
|
|
@@ -976,49 +1014,50 @@ void t() {
|
|
| 976 |
}
|
| 977 |
```
|
| 978 |
|
| 979 |
— *end example*]
|
| 980 |
|
| 981 |
-
A template type argument `T`, a template template argument `TT` or a
|
| 982 |
template non-type argument `i` can be deduced if `P` and `A` have one of
|
| 983 |
the following forms:
|
| 984 |
|
| 985 |
``` cpp
|
| 986 |
-
T
|
| 987 |
-
cv T
|
| 988 |
T*
|
| 989 |
T&
|
| 990 |
T&&
|
| 991 |
-
T[
|
| 992 |
-
|
| 993 |
-
|
| 994 |
-
T
|
| 995 |
-
|
| 996 |
-
|
| 997 |
-
|
| 998 |
-
T T::*
|
| 999 |
-
T (type::*)()
|
| 1000 |
-
type (T::*)()
|
| 1001 |
-
type (type::*)(T)
|
| 1002 |
-
type (T::*)(T)
|
| 1003 |
-
T (type::*)(T)
|
| 1004 |
-
T (T::*)()
|
| 1005 |
-
T (T::*)(T)
|
| 1006 |
-
type[i]
|
| 1007 |
-
template-name<i> (where template-name refers to a class template)
|
| 1008 |
-
TT<T>
|
| 1009 |
-
TT<i>
|
| 1010 |
-
TT<>
|
| 1011 |
```
|
| 1012 |
|
| 1013 |
-
where
|
| 1014 |
-
|
| 1015 |
-
|
| 1016 |
-
|
| 1017 |
-
|
| 1018 |
-
|
| 1019 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1020 |
|
| 1021 |
If `P` has a form that contains `<T>` or `<i>`, then each argument Pᵢ of
|
| 1022 |
the respective template argument list of `P` is compared with the
|
| 1023 |
corresponding argument Aᵢ of the corresponding template argument list of
|
| 1024 |
`A`. If the template argument list of `P` contains a pack expansion that
|
|
@@ -1062,11 +1101,11 @@ parameters of the top-level parameter-type-list of `P` and `A`,
|
|
| 1062 |
respectively, `Pᵢ` is adjusted if it is a forwarding reference
|
| 1063 |
[[temp.deduct.call]] and `Aᵢ` is an lvalue reference, in which case the
|
| 1064 |
type of `Pᵢ` is changed to be the template parameter type (i.e., `T&&`
|
| 1065 |
is changed to simply `T`).
|
| 1066 |
|
| 1067 |
-
[*Note
|
| 1068 |
adjusted `Pᵢ` will be `T`, causing `T` to be deduced as
|
| 1069 |
`X&`. — *end note*]
|
| 1070 |
|
| 1071 |
[*Example 5*:
|
| 1072 |
|
|
@@ -1169,23 +1208,40 @@ using V = decltype(sizeof 0);
|
|
| 1169 |
using V = S<int[42]>::Q; // OK; T was deduced as std::size_t from the type int[42]
|
| 1170 |
```
|
| 1171 |
|
| 1172 |
— *end example*]
|
| 1173 |
|
|
|
|
|
|
|
|
|
|
| 1174 |
[*Example 10*:
|
| 1175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1176 |
``` cpp
|
| 1177 |
template<class T, T i> void f(int (&a)[i]);
|
| 1178 |
int v[10];
|
| 1179 |
void g() {
|
| 1180 |
-
f(v); // OK
|
| 1181 |
}
|
| 1182 |
```
|
| 1183 |
|
| 1184 |
— *end example*]
|
| 1185 |
|
| 1186 |
-
[*Note
|
| 1187 |
|
| 1188 |
Except for reference and pointer types, a major array bound is not part
|
| 1189 |
of a function parameter type and cannot be deduced from an argument:
|
| 1190 |
|
| 1191 |
``` cpp
|
|
@@ -1193,28 +1249,28 @@ template<int i> void f1(int a[10][i]);
|
|
| 1193 |
template<int i> void f2(int a[i][20]);
|
| 1194 |
template<int i> void f3(int (&a)[i][20]);
|
| 1195 |
|
| 1196 |
void g() {
|
| 1197 |
int v[10][20];
|
| 1198 |
-
f1(v); // OK
|
| 1199 |
f1<20>(v); // OK
|
| 1200 |
f2(v); // error: cannot deduce template-argument i
|
| 1201 |
f2<10>(v); // OK
|
| 1202 |
-
f3(v); // OK
|
| 1203 |
}
|
| 1204 |
```
|
| 1205 |
|
| 1206 |
— *end note*]
|
| 1207 |
|
| 1208 |
-
[*Note
|
| 1209 |
|
| 1210 |
If, in the declaration of a function template with a non-type template
|
| 1211 |
parameter, the non-type template parameter is used in a subexpression in
|
| 1212 |
the function parameter list, the expression is a non-deduced context as
|
| 1213 |
specified above.
|
| 1214 |
|
| 1215 |
-
[*Example
|
| 1216 |
|
| 1217 |
``` cpp
|
| 1218 |
template <int i> class A { ... };
|
| 1219 |
template <int i> void g(A<i+1>);
|
| 1220 |
template <int i> void f(A<i>, A<i+1>);
|
|
@@ -1229,11 +1285,11 @@ void k() {
|
|
| 1229 |
|
| 1230 |
— *end example*]
|
| 1231 |
|
| 1232 |
— *end note*]
|
| 1233 |
|
| 1234 |
-
[*Note
|
| 1235 |
|
| 1236 |
Template parameters do not participate in template argument deduction if
|
| 1237 |
they are used only in non-deduced contexts. For example,
|
| 1238 |
|
| 1239 |
``` cpp
|
|
@@ -1253,13 +1309,16 @@ int x = deduce<77>(a.xm, 62, b.ym);
|
|
| 1253 |
|
| 1254 |
If `P` has a form that contains `<i>`, and if the type of `i` differs
|
| 1255 |
from the type of the corresponding template parameter of the template
|
| 1256 |
named by the enclosing *simple-template-id*, deduction fails. If `P` has
|
| 1257 |
a form that contains `[i]`, and if the type of `i` is not an integral
|
| 1258 |
-
type, deduction fails.[^
|
| 1259 |
|
| 1260 |
-
|
|
|
|
|
|
|
|
|
|
| 1261 |
|
| 1262 |
``` cpp
|
| 1263 |
template<int i> class A { ... };
|
| 1264 |
template<short s> void f(A<s>);
|
| 1265 |
void k1() {
|
|
@@ -1270,20 +1329,20 @@ void k1() {
|
|
| 1270 |
|
| 1271 |
template<const short cs> class B { };
|
| 1272 |
template<short s> void g(B<s>);
|
| 1273 |
void k2() {
|
| 1274 |
B<1> b;
|
| 1275 |
-
g(b); // OK
|
| 1276 |
}
|
| 1277 |
```
|
| 1278 |
|
| 1279 |
— *end example*]
|
| 1280 |
|
| 1281 |
A *template-argument* can be deduced from a function, pointer to
|
| 1282 |
function, or pointer-to-member-function type.
|
| 1283 |
|
| 1284 |
-
[*Example
|
| 1285 |
|
| 1286 |
``` cpp
|
| 1287 |
template<class T> void f(void(*)(T,int));
|
| 1288 |
template<class T> void foo(T,int);
|
| 1289 |
void g(int,int);
|
|
@@ -1291,38 +1350,38 @@ void g(char,int);
|
|
| 1291 |
|
| 1292 |
void h(int,int,int);
|
| 1293 |
void h(char,int);
|
| 1294 |
int m() {
|
| 1295 |
f(&g); // error: ambiguous
|
| 1296 |
-
f(&h); // OK
|
| 1297 |
f(&foo); // error: type deduction fails because foo is a template
|
| 1298 |
}
|
| 1299 |
```
|
| 1300 |
|
| 1301 |
— *end example*]
|
| 1302 |
|
| 1303 |
A template *type-parameter* cannot be deduced from the type of a
|
| 1304 |
function default argument.
|
| 1305 |
|
| 1306 |
-
[*Example
|
| 1307 |
|
| 1308 |
``` cpp
|
| 1309 |
template <class T> void f(T = 5, T = 7);
|
| 1310 |
void g() {
|
| 1311 |
-
f(1); // OK
|
| 1312 |
f(); // error: cannot deduce T
|
| 1313 |
-
f<int>(); // OK
|
| 1314 |
}
|
| 1315 |
```
|
| 1316 |
|
| 1317 |
— *end example*]
|
| 1318 |
|
| 1319 |
The *template-argument* corresponding to a template *template-parameter*
|
| 1320 |
is deduced from the type of the *template-argument* of a class template
|
| 1321 |
specialization used in the argument list of a function call.
|
| 1322 |
|
| 1323 |
-
[*Example
|
| 1324 |
|
| 1325 |
``` cpp
|
| 1326 |
template <template <class T> class X> struct A { };
|
| 1327 |
template <template <class T> class X> void f(A<X>) { }
|
| 1328 |
template<class T> struct B { };
|
|
@@ -1330,15 +1389,15 @@ A<B> ab;
|
|
| 1330 |
f(ab); // calls f(A<B>)
|
| 1331 |
```
|
| 1332 |
|
| 1333 |
— *end example*]
|
| 1334 |
|
| 1335 |
-
[*Note
|
| 1336 |
[[temp.variadic]] can deduce zero or more arguments for each parameter
|
| 1337 |
pack. — *end note*]
|
| 1338 |
|
| 1339 |
-
[*Example
|
| 1340 |
|
| 1341 |
``` cpp
|
| 1342 |
template<class> struct X { };
|
| 1343 |
template<class R, class ... ArgTypes> struct X<R(int, ArgTypes ...)> { };
|
| 1344 |
template<class ... Types> struct Y { };
|
|
@@ -1348,11 +1407,11 @@ template<class ... Types> int f(void (*)(Types ...));
|
|
| 1348 |
void g(int, float);
|
| 1349 |
|
| 1350 |
X<int> x1; // uses primary template
|
| 1351 |
X<int(int, float, double)> x2; // uses partial specialization; ArgTypes contains float, double
|
| 1352 |
X<int(float, int)> x3; // uses primary template
|
| 1353 |
-
Y<> y1; //
|
| 1354 |
Y<int&, float&, double&> y2; // uses partial specialization; T is int&, Types contains float, double
|
| 1355 |
Y<int, float, double> y3; // uses primary template; Types contains int, float, double
|
| 1356 |
int fv = f(g); // OK; Types contains int, float
|
| 1357 |
```
|
| 1358 |
|
|
@@ -1364,12 +1423,12 @@ In a declaration whose *declarator-id* refers to a specialization of a
|
|
| 1364 |
function template, template argument deduction is performed to identify
|
| 1365 |
the specialization to which the declaration refers. Specifically, this
|
| 1366 |
is done for explicit instantiations [[temp.explicit]], explicit
|
| 1367 |
specializations [[temp.expl.spec]], and certain friend declarations
|
| 1368 |
[[temp.friend]]. This is also done to determine whether a deallocation
|
| 1369 |
-
function template specialization matches a placement `operator new`
|
| 1370 |
-
[[basic.stc.dynamic.deallocation]], [[expr.new]]
|
| 1371 |
`P` is the type of the function template being considered as a potential
|
| 1372 |
match and `A` is either the function type from the declaration or the
|
| 1373 |
type of the deallocation function that would match the placement
|
| 1374 |
`operator new` as described in [[expr.new]]. The deduction is done as
|
| 1375 |
described in [[temp.deduct.type]].
|
|
|
|
| 1 |
### Template argument deduction <a id="temp.deduct">[[temp.deduct]]</a>
|
| 2 |
|
| 3 |
+
#### General <a id="temp.deduct.general">[[temp.deduct.general]]</a>
|
| 4 |
+
|
| 5 |
When a function template specialization is referenced, all of the
|
| 6 |
template arguments shall have values. The values can be explicitly
|
| 7 |
specified or, in some cases, be deduced from the use or obtained from
|
| 8 |
default *template-argument*s.
|
| 9 |
|
|
|
|
| 98 |
|
| 99 |
— *end example*]
|
| 100 |
|
| 101 |
When all template arguments have been deduced or obtained from default
|
| 102 |
template arguments, all uses of template parameters in the template
|
| 103 |
+
parameter list of the template are replaced with the corresponding
|
| 104 |
+
deduced or default argument values. If the substitution results in an
|
| 105 |
+
invalid type, as described above, type deduction fails. If the function
|
| 106 |
+
template has associated constraints [[temp.constr.decl]], those
|
| 107 |
+
constraints are checked for satisfaction [[temp.constr.constr]]. If the
|
| 108 |
+
constraints are not satisfied, type deduction fails. In the context of a
|
| 109 |
+
function call, if type deduction has not yet failed, then for those
|
| 110 |
+
function parameters for which the function call has arguments, each
|
| 111 |
+
function parameter with a type that was non-dependent before
|
| 112 |
+
substitution of any explicitly-specified template arguments is checked
|
| 113 |
+
against its corresponding argument; if the corresponding argument cannot
|
| 114 |
+
be implicitly converted to the parameter type, type deduction fails.
|
| 115 |
+
|
| 116 |
+
[*Note 3*: Overload resolution will check the other parameters,
|
| 117 |
+
including parameters with dependent types in which no template
|
| 118 |
+
parameters participate in template argument deduction and parameters
|
| 119 |
+
that became non-dependent due to substitution of explicitly-specified
|
| 120 |
+
template arguments. — *end note*]
|
| 121 |
+
|
| 122 |
+
If type deduction has not yet failed, then all uses of template
|
| 123 |
+
parameters in the function type are replaced with the corresponding
|
| 124 |
+
deduced or default argument values. If the substitution results in an
|
| 125 |
+
invalid type, as described above, type deduction fails.
|
| 126 |
+
|
| 127 |
+
[*Example 5*:
|
| 128 |
+
|
| 129 |
+
``` cpp
|
| 130 |
+
template <class T> struct Z {
|
| 131 |
+
typedef typename T::x xx;
|
| 132 |
+
};
|
| 133 |
+
template <class T> concept C = requires { typename T::A; };
|
| 134 |
+
template <C T> typename Z<T>::xx f(void *, T); // #1
|
| 135 |
+
template <class T> void f(int, T); // #2
|
| 136 |
+
struct A {} a;
|
| 137 |
+
struct ZZ {
|
| 138 |
+
template <class T, class = typename Z<T>::xx> operator T *();
|
| 139 |
+
operator int();
|
| 140 |
+
};
|
| 141 |
+
int main() {
|
| 142 |
+
ZZ zz;
|
| 143 |
+
f(1, a); // OK, deduction fails for #1 because there is no conversion from int to void*
|
| 144 |
+
f(zz, 42); // OK, deduction fails for #1 because C<int> is not satisfied
|
| 145 |
+
}
|
| 146 |
+
```
|
| 147 |
+
|
| 148 |
+
— *end example*]
|
| 149 |
|
| 150 |
At certain points in the template argument deduction process it is
|
| 151 |
necessary to take a function type that makes use of template parameters
|
| 152 |
and replace those template parameters with the corresponding template
|
| 153 |
arguments. This is done at the beginning of template argument deduction
|
| 154 |
when any explicitly specified template arguments are substituted into
|
| 155 |
the function type, and again at the end of template argument deduction
|
| 156 |
when any template arguments that were deduced or obtained from default
|
| 157 |
arguments are substituted.
|
| 158 |
|
| 159 |
+
The *deduction substitution loci* are
|
| 160 |
+
|
| 161 |
+
- the function type outside of the *noexcept-specifier*,
|
| 162 |
+
- the *explicit-specifier*, and
|
| 163 |
+
- the template parameter declarations.
|
| 164 |
+
|
| 165 |
The substitution occurs in all types and expressions that are used in
|
| 166 |
+
the deduction substitution loci. The expressions include not only
|
| 167 |
+
constant expressions such as those that appear in array bounds or as
|
| 168 |
+
nontype template arguments but also general expressions (i.e.,
|
| 169 |
+
non-constant expressions) inside `sizeof`, `decltype`, and other
|
| 170 |
+
contexts that allow non-constant expressions. The substitution proceeds
|
| 171 |
+
in lexical order and stops when a condition that causes deduction to
|
| 172 |
+
fail is encountered. If substitution into different declarations of the
|
| 173 |
+
same function template would cause template instantiations to occur in a
|
| 174 |
+
different order or not at all, the program is ill-formed; no diagnostic
|
| 175 |
+
required.
|
| 176 |
|
| 177 |
+
[*Note 4*: The equivalent substitution in exception specifications is
|
| 178 |
done only when the *noexcept-specifier* is instantiated, at which point
|
| 179 |
a program is ill-formed if the substitution results in an invalid type
|
| 180 |
or expression. — *end note*]
|
| 181 |
|
| 182 |
+
[*Example 6*:
|
| 183 |
|
| 184 |
``` cpp
|
| 185 |
template <class T> struct A { using X = typename T::X; };
|
| 186 |
template <class T> typename T::X f(typename A<T>::X);
|
| 187 |
template <class T> void f(...) { }
|
|
|
|
| 200 |
|
| 201 |
— *end example*]
|
| 202 |
|
| 203 |
If a substitution results in an invalid type or expression, type
|
| 204 |
deduction fails. An invalid type or expression is one that would be
|
| 205 |
+
ill-formed, with a diagnostic required, if written in the same context
|
| 206 |
+
using the substituted arguments.
|
| 207 |
|
| 208 |
+
[*Note 5*: If no diagnostic is required, the program is still
|
| 209 |
ill-formed. Access checking is done as part of the substitution
|
| 210 |
process. — *end note*]
|
| 211 |
|
| 212 |
+
Invalid types and expressions can result in a deduction failure only in
|
| 213 |
+
the immediate context of the deduction substitution loci.
|
|
|
|
| 214 |
|
| 215 |
+
[*Note 6*: The substitution into types and expressions can result in
|
| 216 |
effects such as the instantiation of class template specializations
|
| 217 |
and/or function template specializations, the generation of
|
| 218 |
implicitly-defined functions, etc. Such effects are not in the
|
| 219 |
“immediate context” and can result in the program being
|
| 220 |
ill-formed. — *end note*]
|
| 221 |
|
| 222 |
A *lambda-expression* appearing in a function type or a template
|
| 223 |
parameter is not considered part of the immediate context for the
|
| 224 |
purposes of template argument deduction.
|
| 225 |
|
| 226 |
+
[*Note 7*:
|
| 227 |
|
| 228 |
The intent is to avoid requiring implementations to deal with
|
| 229 |
substitution failure involving arbitrary statements.
|
| 230 |
|
| 231 |
+
[*Example 7*:
|
| 232 |
|
| 233 |
``` cpp
|
| 234 |
template <class T>
|
| 235 |
auto f(T) -> decltype([]() { T::invalid; } ());
|
| 236 |
void f(...);
|
|
|
|
| 259 |
|
| 260 |
— *end example*]
|
| 261 |
|
| 262 |
— *end note*]
|
| 263 |
|
| 264 |
+
[*Example 8*:
|
| 265 |
|
| 266 |
``` cpp
|
| 267 |
struct X { };
|
| 268 |
struct Y {
|
| 269 |
Y(X) {}
|
|
|
|
| 276 |
X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
|
| 277 |
```
|
| 278 |
|
| 279 |
— *end example*]
|
| 280 |
|
| 281 |
+
[*Note 8*:
|
| 282 |
|
| 283 |
+
Type deduction can fail for the following reasons:
|
| 284 |
|
| 285 |
- Attempting to instantiate a pack expansion containing multiple packs
|
| 286 |
of differing lengths.
|
| 287 |
- Attempting to create an array with an element type that is `void`, a
|
| 288 |
function type, or a reference type, or attempting to create an array
|
| 289 |
with a size that is zero or negative.
|
| 290 |
+
\[*Example 9*:
|
| 291 |
``` cpp
|
| 292 |
template <class T> int f(T[5]);
|
| 293 |
int I = f<int>(0);
|
| 294 |
int j = f<void>(0); // invalid array
|
| 295 |
```
|
| 296 |
|
| 297 |
— *end example*]
|
| 298 |
- Attempting to use a type that is not a class or enumeration type in a
|
| 299 |
qualified name.
|
| 300 |
+
\[*Example 10*:
|
| 301 |
``` cpp
|
| 302 |
template <class T> int f(typename T::B*);
|
| 303 |
int i = f<int>(0);
|
| 304 |
```
|
| 305 |
|
|
|
|
| 310 |
- the specified member is not a type where a type is required, or
|
| 311 |
- the specified member is not a template where a template is required,
|
| 312 |
or
|
| 313 |
- the specified member is not a non-type where a non-type is required.
|
| 314 |
|
| 315 |
+
\[*Example 11*:
|
| 316 |
``` cpp
|
| 317 |
template <int I> struct X { };
|
| 318 |
template <template <class T> class> struct Z { };
|
| 319 |
template <class T> void f(typename T::Y*) {}
|
| 320 |
template <class T> void g(X<T::N>*) {}
|
| 321 |
+
template <class T> void h(Z<T::TT>*) {}
|
| 322 |
struct A {};
|
| 323 |
struct B { int Y; };
|
| 324 |
struct C {
|
| 325 |
typedef int N;
|
| 326 |
};
|
|
|
|
| 340 |
— *end example*]
|
| 341 |
- Attempting to create a pointer to reference type.
|
| 342 |
- Attempting to create a reference to `void`.
|
| 343 |
- Attempting to create “pointer to member of `T`” when `T` is not a
|
| 344 |
class type.
|
| 345 |
+
\[*Example 12*:
|
| 346 |
``` cpp
|
| 347 |
template <class T> int f(int T::*);
|
| 348 |
int i = f<int>(0);
|
| 349 |
```
|
| 350 |
|
| 351 |
— *end example*]
|
| 352 |
- Attempting to give an invalid type to a non-type template parameter.
|
| 353 |
+
\[*Example 13*:
|
| 354 |
``` cpp
|
| 355 |
template <class T, T> struct S {};
|
| 356 |
+
template <class T> int f(S<T, T{}>*); // #1
|
| 357 |
+
class X {
|
| 358 |
+
int m;
|
| 359 |
+
};
|
| 360 |
+
int i0 = f<X>(0); // #1 uses a value of non-structural type X as a non-type template argument
|
| 361 |
```
|
| 362 |
|
| 363 |
— *end example*]
|
| 364 |
- Attempting to perform an invalid conversion in either a template
|
| 365 |
argument expression, or an expression used in the function
|
| 366 |
declaration.
|
| 367 |
+
\[*Example 14*:
|
| 368 |
``` cpp
|
| 369 |
template <class T, T*> int f(int);
|
| 370 |
+
int i2 = f<int,1>(0); // can't convert 1 to int*
|
| 371 |
```
|
| 372 |
|
| 373 |
— *end example*]
|
| 374 |
- Attempting to create a function type in which a parameter has a type
|
| 375 |
of `void`, or in which the return type is a function type or array
|
| 376 |
type.
|
| 377 |
|
| 378 |
— *end note*]
|
| 379 |
|
| 380 |
+
[*Example 15*:
|
| 381 |
|
| 382 |
In the following example, assuming a `signed char` cannot represent the
|
| 383 |
value 1000, a narrowing conversion [[dcl.init.list]] would be required
|
| 384 |
to convert the *template-argument* of type `int` to `signed char`,
|
| 385 |
therefore substitution fails for the second template
|
|
|
|
| 565 |
template <typename... T> struct X;
|
| 566 |
template <> struct X<> {};
|
| 567 |
template <typename T, typename... Ts>
|
| 568 |
struct X<T, Ts...> : X<Ts...> {};
|
| 569 |
struct D : X<int> {};
|
| 570 |
+
struct E : X<>, X<int> {};
|
| 571 |
|
| 572 |
template <typename... T>
|
| 573 |
int f(const X<T...>&);
|
| 574 |
int x = f(D()); // calls f<int>, not f<>
|
| 575 |
// B is X<>, C is X<int>
|
| 576 |
+
int z = f(E()); // calls f<int>, not f<>
|
| 577 |
```
|
| 578 |
|
| 579 |
— *end example*]
|
| 580 |
|
| 581 |
These alternatives are considered only if type deduction would otherwise
|
|
|
|
| 634 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 635 |
```
|
| 636 |
|
| 637 |
— *end example*]
|
| 638 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 639 |
#### Deducing template arguments taking the address of a function template <a id="temp.deduct.funcaddr">[[temp.deduct.funcaddr]]</a>
|
| 640 |
|
| 641 |
Template arguments can be deduced from the type specified when taking
|
| 642 |
+
the address of an overload set [[over.over]]. If there is a target, the
|
| 643 |
+
function template’s function type and the target type are used as the
|
| 644 |
+
types of `P` and `A`, and the deduction is done as described in
|
| 645 |
+
[[temp.deduct.type]]. Otherwise, deduction is performed with empty sets
|
| 646 |
+
of types `P` and `A`.
|
| 647 |
|
| 648 |
A placeholder type [[dcl.spec.auto]] in the return type of a function
|
| 649 |
template is a non-deduced context. If template argument deduction
|
| 650 |
succeeds for such a function, the return type is determined from
|
| 651 |
instantiation of the function body.
|
| 652 |
|
| 653 |
#### Deducing conversion function template arguments <a id="temp.deduct.conv">[[temp.deduct.conv]]</a>
|
| 654 |
|
| 655 |
Template argument deduction is done by comparing the return type of the
|
| 656 |
+
conversion function template (call it `P`) with the type specified by
|
| 657 |
+
the *conversion-type-id* of the *conversion-function-id* being looked up
|
| 658 |
+
(call it `A`) as described in [[temp.deduct.type]]. If the
|
| 659 |
+
*conversion-function-id* is constructed during overload resolution
|
| 660 |
+
[[over.match.funcs]], the rules in the remainder of this subclause
|
| 661 |
+
apply.
|
| 662 |
|
| 663 |
If `P` is a reference type, the type referred to by `P` is used in place
|
| 664 |
of `P` for type deduction and for any further references to or
|
| 665 |
transformations of `P` in the remainder of this subclause.
|
| 666 |
|
|
|
|
| 678 |
If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s type
|
| 679 |
are ignored for type deduction. If `A` is a reference type, the type
|
| 680 |
referred to by `A` is used for type deduction.
|
| 681 |
|
| 682 |
In general, the deduction process attempts to find template argument
|
| 683 |
+
values that will make the deduced `A` identical to `A`. However, certain
|
| 684 |
+
attributes of `A` may be ignored:
|
| 685 |
|
| 686 |
+
- If the original `A` is a reference type, any cv-qualifiers of `A`
|
| 687 |
+
(i.e., the type referred to by the reference).
|
| 688 |
+
- If the original `A` is a function pointer or
|
| 689 |
+
pointer-to-member-function type with a potentially-throwing exception
|
| 690 |
+
specification [[except.spec]], the exception specification.
|
| 691 |
+
- Any cv-qualifiers in `A` that can be restored by a qualification
|
| 692 |
+
conversion.
|
|
|
|
|
|
|
| 693 |
|
| 694 |
+
These attributes are ignored only if type deduction would otherwise
|
| 695 |
+
fail. If ignoring them allows more than one possible deduced `A`, the
|
| 696 |
+
type deduction fails.
|
| 697 |
|
| 698 |
#### Deducing template arguments during partial ordering <a id="temp.deduct.partial">[[temp.deduct.partial]]</a>
|
| 699 |
|
| 700 |
Template argument deduction is done by comparing certain types
|
| 701 |
associated with the two function templates being compared.
|
|
|
|
| 717 |
|
| 718 |
The types used to determine the ordering depend on the context in which
|
| 719 |
the partial ordering is done:
|
| 720 |
|
| 721 |
- In the context of a function call, the types used are those function
|
| 722 |
+
parameter types for which the function call has arguments.[^13]
|
| 723 |
- In the context of a call to a conversion function, the return types of
|
| 724 |
the conversion function templates are used.
|
| 725 |
- In other contexts [[temp.func.order]] the function template’s function
|
| 726 |
type is used.
|
| 727 |
|
|
|
|
| 862 |
array bound if it is not otherwise deduced.
|
| 863 |
|
| 864 |
A given type `P` can be composed from a number of other types,
|
| 865 |
templates, and non-type values:
|
| 866 |
|
| 867 |
+
- A function type includes the types of each of the function parameters,
|
| 868 |
+
the return type, and its exception specification.
|
| 869 |
- A pointer-to-member type includes the type of the class object pointed
|
| 870 |
to and the type of the member pointed to.
|
| 871 |
- A type that is a specialization of a class template (e.g., `A<int>`)
|
| 872 |
includes the types, templates, and non-type values referenced by the
|
| 873 |
template argument list of the specialization.
|
|
|
|
| 940 |
``` cpp
|
| 941 |
template<class T> void f(T x, T y) { ... }
|
| 942 |
struct A { ... };
|
| 943 |
struct B : A { ... };
|
| 944 |
void g(A a, B b) {
|
| 945 |
+
f(a,b); // error: T deduced as both A and B
|
| 946 |
+
f(b,a); // error: T deduced as both A and B
|
| 947 |
+
f(a,a); // OK, T is A
|
| 948 |
+
f(b,b); // OK, T is B
|
| 949 |
}
|
| 950 |
```
|
| 951 |
|
| 952 |
Here is an example where two template arguments are deduced from a
|
| 953 |
single function parameter/argument pair. This can lead to conflicts that
|
|
|
|
| 959 |
int g1( int, float, float);
|
| 960 |
char g2( int, float, float);
|
| 961 |
int g3( int, char, float);
|
| 962 |
|
| 963 |
void r() {
|
| 964 |
+
f(g1); // OK, T is int and U is float
|
| 965 |
+
f(g2); // error: T deduced as both char and int
|
| 966 |
+
f(g3); // error: U deduced as both char and float
|
| 967 |
+
}
|
| 968 |
+
```
|
| 969 |
+
|
| 970 |
+
Here is an example where the exception specification of a function type
|
| 971 |
+
is deduced:
|
| 972 |
+
|
| 973 |
+
``` cpp
|
| 974 |
+
template<bool E> void f1(void (*)() noexcept(E));
|
| 975 |
+
template<bool> struct A { };
|
| 976 |
+
template<bool B> void f2(void (*)(A<B>) noexcept(B));
|
| 977 |
+
|
| 978 |
+
void g1();
|
| 979 |
+
void g2() noexcept;
|
| 980 |
+
void g3(A<true>);
|
| 981 |
+
|
| 982 |
+
void h() {
|
| 983 |
+
f1(g1); // OK, E is false
|
| 984 |
+
f1(g2); // OK, E is true
|
| 985 |
+
f2(g3); // error: B deduced as both true and false
|
| 986 |
}
|
| 987 |
```
|
| 988 |
|
| 989 |
Here is an example where a qualification conversion applies between the
|
| 990 |
argument type on the function call and the deduced template argument
|
|
|
|
| 1014 |
}
|
| 1015 |
```
|
| 1016 |
|
| 1017 |
— *end example*]
|
| 1018 |
|
| 1019 |
+
A template type argument `T`, a template template argument `TT`, or a
|
| 1020 |
template non-type argument `i` can be deduced if `P` and `A` have one of
|
| 1021 |
the following forms:
|
| 1022 |
|
| 1023 |
``` cpp
|
| 1024 |
+
\opt{cv} T
|
|
|
|
| 1025 |
T*
|
| 1026 |
T&
|
| 1027 |
T&&
|
| 1028 |
+
\opt{T}[\opt{i}]
|
| 1029 |
+
\opt{T}(\opt{T}) noexcept(\opt{i})
|
| 1030 |
+
\opt{T} \opt{T}::*
|
| 1031 |
+
\opt{TT}<T>
|
| 1032 |
+
\opt{TT}<i>
|
| 1033 |
+
\opt{TT}<TT>
|
| 1034 |
+
\opt{TT}<>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1035 |
```
|
| 1036 |
|
| 1037 |
+
where
|
| 1038 |
+
|
| 1039 |
+
- `\opt{T}` represents a type or parameter-type-list that either
|
| 1040 |
+
satisfies these rules recursively, is a non-deduced context in `P` or
|
| 1041 |
+
`A`, or is the same non-dependent type in `P` and `A`,
|
| 1042 |
+
- `\opt{TT}` represents either a class template or a template template
|
| 1043 |
+
parameter,
|
| 1044 |
+
- `\opt{i}` represents an expression that either is an `i`, is
|
| 1045 |
+
value-dependent in `P` or `A`, or has the same constant value in `P`
|
| 1046 |
+
and `A`, and
|
| 1047 |
+
- `noexcept(\opt{i})` represents an exception specification
|
| 1048 |
+
[[except.spec]] in which the (possibly-implicit, see [[dcl.fct]])
|
| 1049 |
+
*noexcept-specifier*’s operand satisfies the rules for an `\opt{i}`
|
| 1050 |
+
above.
|
| 1051 |
+
|
| 1052 |
+
[*Note 2*: If a type matches such a form but contains no `T`s, `i`s, or
|
| 1053 |
+
`TT`s, deduction is not possible. — *end note*]
|
| 1054 |
+
|
| 1055 |
+
Similarly, `<T>` represents template argument lists where at least one
|
| 1056 |
+
argument contains a `T`, `<i>` represents template argument lists where
|
| 1057 |
+
at least one argument contains an `i` and `<>` represents template
|
| 1058 |
+
argument lists where no argument contains a `T` or an `i`.
|
| 1059 |
|
| 1060 |
If `P` has a form that contains `<T>` or `<i>`, then each argument Pᵢ of
|
| 1061 |
the respective template argument list of `P` is compared with the
|
| 1062 |
corresponding argument Aᵢ of the corresponding template argument list of
|
| 1063 |
`A`. If the template argument list of `P` contains a pack expansion that
|
|
|
|
| 1101 |
respectively, `Pᵢ` is adjusted if it is a forwarding reference
|
| 1102 |
[[temp.deduct.call]] and `Aᵢ` is an lvalue reference, in which case the
|
| 1103 |
type of `Pᵢ` is changed to be the template parameter type (i.e., `T&&`
|
| 1104 |
is changed to simply `T`).
|
| 1105 |
|
| 1106 |
+
[*Note 3*: As a result, when `Pᵢ` is `T&&` and `Aᵢ` is `X&`, the
|
| 1107 |
adjusted `Pᵢ` will be `T`, causing `T` to be deduced as
|
| 1108 |
`X&`. — *end note*]
|
| 1109 |
|
| 1110 |
[*Example 5*:
|
| 1111 |
|
|
|
|
| 1208 |
using V = S<int[42]>::Q; // OK; T was deduced as std::size_t from the type int[42]
|
| 1209 |
```
|
| 1210 |
|
| 1211 |
— *end example*]
|
| 1212 |
|
| 1213 |
+
The type of `B` in the *noexcept-specifier* `noexcept(B)` of a function
|
| 1214 |
+
type is `bool`.
|
| 1215 |
+
|
| 1216 |
[*Example 10*:
|
| 1217 |
|
| 1218 |
+
``` cpp
|
| 1219 |
+
template<bool> struct A { };
|
| 1220 |
+
template<auto> struct B;
|
| 1221 |
+
template<auto X, void (*F)() noexcept(X)> struct B<F> {
|
| 1222 |
+
A<X> ax;
|
| 1223 |
+
};
|
| 1224 |
+
void f_nothrow() noexcept;
|
| 1225 |
+
B<f_nothrow> bn; // OK, type of X deduced as bool
|
| 1226 |
+
```
|
| 1227 |
+
|
| 1228 |
+
— *end example*]
|
| 1229 |
+
|
| 1230 |
+
[*Example 11*:
|
| 1231 |
+
|
| 1232 |
``` cpp
|
| 1233 |
template<class T, T i> void f(int (&a)[i]);
|
| 1234 |
int v[10];
|
| 1235 |
void g() {
|
| 1236 |
+
f(v); // OK, T is std::size_t
|
| 1237 |
}
|
| 1238 |
```
|
| 1239 |
|
| 1240 |
— *end example*]
|
| 1241 |
|
| 1242 |
+
[*Note 4*:
|
| 1243 |
|
| 1244 |
Except for reference and pointer types, a major array bound is not part
|
| 1245 |
of a function parameter type and cannot be deduced from an argument:
|
| 1246 |
|
| 1247 |
``` cpp
|
|
|
|
| 1249 |
template<int i> void f2(int a[i][20]);
|
| 1250 |
template<int i> void f3(int (&a)[i][20]);
|
| 1251 |
|
| 1252 |
void g() {
|
| 1253 |
int v[10][20];
|
| 1254 |
+
f1(v); // OK, i deduced as 20
|
| 1255 |
f1<20>(v); // OK
|
| 1256 |
f2(v); // error: cannot deduce template-argument i
|
| 1257 |
f2<10>(v); // OK
|
| 1258 |
+
f3(v); // OK, i deduced as 10
|
| 1259 |
}
|
| 1260 |
```
|
| 1261 |
|
| 1262 |
— *end note*]
|
| 1263 |
|
| 1264 |
+
[*Note 5*:
|
| 1265 |
|
| 1266 |
If, in the declaration of a function template with a non-type template
|
| 1267 |
parameter, the non-type template parameter is used in a subexpression in
|
| 1268 |
the function parameter list, the expression is a non-deduced context as
|
| 1269 |
specified above.
|
| 1270 |
|
| 1271 |
+
[*Example 12*:
|
| 1272 |
|
| 1273 |
``` cpp
|
| 1274 |
template <int i> class A { ... };
|
| 1275 |
template <int i> void g(A<i+1>);
|
| 1276 |
template <int i> void f(A<i>, A<i+1>);
|
|
|
|
| 1285 |
|
| 1286 |
— *end example*]
|
| 1287 |
|
| 1288 |
— *end note*]
|
| 1289 |
|
| 1290 |
+
[*Note 6*:
|
| 1291 |
|
| 1292 |
Template parameters do not participate in template argument deduction if
|
| 1293 |
they are used only in non-deduced contexts. For example,
|
| 1294 |
|
| 1295 |
``` cpp
|
|
|
|
| 1309 |
|
| 1310 |
If `P` has a form that contains `<i>`, and if the type of `i` differs
|
| 1311 |
from the type of the corresponding template parameter of the template
|
| 1312 |
named by the enclosing *simple-template-id*, deduction fails. If `P` has
|
| 1313 |
a form that contains `[i]`, and if the type of `i` is not an integral
|
| 1314 |
+
type, deduction fails.[^14]
|
| 1315 |
|
| 1316 |
+
If `P` has a form that includes `noexcept(i)` and the type of `i` is not
|
| 1317 |
+
`bool`, deduction fails.
|
| 1318 |
+
|
| 1319 |
+
[*Example 13*:
|
| 1320 |
|
| 1321 |
``` cpp
|
| 1322 |
template<int i> class A { ... };
|
| 1323 |
template<short s> void f(A<s>);
|
| 1324 |
void k1() {
|
|
|
|
| 1329 |
|
| 1330 |
template<const short cs> class B { };
|
| 1331 |
template<short s> void g(B<s>);
|
| 1332 |
void k2() {
|
| 1333 |
B<1> b;
|
| 1334 |
+
g(b); // OK, cv-qualifiers are ignored on template parameter types
|
| 1335 |
}
|
| 1336 |
```
|
| 1337 |
|
| 1338 |
— *end example*]
|
| 1339 |
|
| 1340 |
A *template-argument* can be deduced from a function, pointer to
|
| 1341 |
function, or pointer-to-member-function type.
|
| 1342 |
|
| 1343 |
+
[*Example 14*:
|
| 1344 |
|
| 1345 |
``` cpp
|
| 1346 |
template<class T> void f(void(*)(T,int));
|
| 1347 |
template<class T> void foo(T,int);
|
| 1348 |
void g(int,int);
|
|
|
|
| 1350 |
|
| 1351 |
void h(int,int,int);
|
| 1352 |
void h(char,int);
|
| 1353 |
int m() {
|
| 1354 |
f(&g); // error: ambiguous
|
| 1355 |
+
f(&h); // OK, void h(char,int) is a unique match
|
| 1356 |
f(&foo); // error: type deduction fails because foo is a template
|
| 1357 |
}
|
| 1358 |
```
|
| 1359 |
|
| 1360 |
— *end example*]
|
| 1361 |
|
| 1362 |
A template *type-parameter* cannot be deduced from the type of a
|
| 1363 |
function default argument.
|
| 1364 |
|
| 1365 |
+
[*Example 15*:
|
| 1366 |
|
| 1367 |
``` cpp
|
| 1368 |
template <class T> void f(T = 5, T = 7);
|
| 1369 |
void g() {
|
| 1370 |
+
f(1); // OK, calls f<int>(1,7)
|
| 1371 |
f(); // error: cannot deduce T
|
| 1372 |
+
f<int>(); // OK, calls f<int>(5,7)
|
| 1373 |
}
|
| 1374 |
```
|
| 1375 |
|
| 1376 |
— *end example*]
|
| 1377 |
|
| 1378 |
The *template-argument* corresponding to a template *template-parameter*
|
| 1379 |
is deduced from the type of the *template-argument* of a class template
|
| 1380 |
specialization used in the argument list of a function call.
|
| 1381 |
|
| 1382 |
+
[*Example 16*:
|
| 1383 |
|
| 1384 |
``` cpp
|
| 1385 |
template <template <class T> class X> struct A { };
|
| 1386 |
template <template <class T> class X> void f(A<X>) { }
|
| 1387 |
template<class T> struct B { };
|
|
|
|
| 1389 |
f(ab); // calls f(A<B>)
|
| 1390 |
```
|
| 1391 |
|
| 1392 |
— *end example*]
|
| 1393 |
|
| 1394 |
+
[*Note 7*: Template argument deduction involving parameter packs
|
| 1395 |
[[temp.variadic]] can deduce zero or more arguments for each parameter
|
| 1396 |
pack. — *end note*]
|
| 1397 |
|
| 1398 |
+
[*Example 17*:
|
| 1399 |
|
| 1400 |
``` cpp
|
| 1401 |
template<class> struct X { };
|
| 1402 |
template<class R, class ... ArgTypes> struct X<R(int, ArgTypes ...)> { };
|
| 1403 |
template<class ... Types> struct Y { };
|
|
|
|
| 1407 |
void g(int, float);
|
| 1408 |
|
| 1409 |
X<int> x1; // uses primary template
|
| 1410 |
X<int(int, float, double)> x2; // uses partial specialization; ArgTypes contains float, double
|
| 1411 |
X<int(float, int)> x3; // uses primary template
|
| 1412 |
+
Y<> y1; // uses primary template; Types is empty
|
| 1413 |
Y<int&, float&, double&> y2; // uses partial specialization; T is int&, Types contains float, double
|
| 1414 |
Y<int, float, double> y3; // uses primary template; Types contains int, float, double
|
| 1415 |
int fv = f(g); // OK; Types contains int, float
|
| 1416 |
```
|
| 1417 |
|
|
|
|
| 1423 |
function template, template argument deduction is performed to identify
|
| 1424 |
the specialization to which the declaration refers. Specifically, this
|
| 1425 |
is done for explicit instantiations [[temp.explicit]], explicit
|
| 1426 |
specializations [[temp.expl.spec]], and certain friend declarations
|
| 1427 |
[[temp.friend]]. This is also done to determine whether a deallocation
|
| 1428 |
+
function template specialization matches a placement `operator new`
|
| 1429 |
+
[[basic.stc.dynamic.deallocation]], [[expr.new]]. In all these cases,
|
| 1430 |
`P` is the type of the function template being considered as a potential
|
| 1431 |
match and `A` is either the function type from the declaration or the
|
| 1432 |
type of the deallocation function that would match the placement
|
| 1433 |
`operator new` as described in [[expr.new]]. The deduction is done as
|
| 1434 |
described in [[temp.deduct.type]].
|