- tmp/tmp2hv0vwo8/{from.md → to.md} +219 -182
tmp/tmp2hv0vwo8/{from.md → to.md}
RENAMED
|
@@ -23,28 +23,14 @@ void g(double d) {
|
|
| 23 |
}
|
| 24 |
```
|
| 25 |
|
| 26 |
— *end example*]
|
| 27 |
|
| 28 |
-
When an explicit template argument list is specified, the
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
evaluating an explicitly specified template argument list with respect
|
| 33 |
-
to a given function template:
|
| 34 |
-
|
| 35 |
-
- The specified template arguments must match the template parameters in
|
| 36 |
-
kind (i.e., type, non-type, template). There must not be more
|
| 37 |
-
arguments than there are parameters unless at least one parameter is a
|
| 38 |
-
template parameter pack, and there shall be an argument for each
|
| 39 |
-
non-pack parameter. Otherwise, type deduction fails.
|
| 40 |
-
- Non-type arguments must match the types of the corresponding non-type
|
| 41 |
-
template parameters, or must be convertible to the types of the
|
| 42 |
-
corresponding non-type parameters as specified in
|
| 43 |
-
[[temp.arg.nontype]], otherwise type deduction fails.
|
| 44 |
-
- The specified template argument values are substituted for the
|
| 45 |
-
corresponding template parameters as specified below.
|
| 46 |
|
| 47 |
After this substitution is performed, the function parameter type
|
| 48 |
adjustments described in [[dcl.fct]] are performed.
|
| 49 |
|
| 50 |
[*Example 2*: A parameter type of “`void (const int, int[5])`” becomes
|
|
@@ -113,10 +99,13 @@ void g() {
|
|
| 113 |
When all template arguments have been deduced or obtained from default
|
| 114 |
template arguments, all uses of template parameters in the template
|
| 115 |
parameter list of the template and the function type are replaced with
|
| 116 |
the corresponding deduced or default argument values. If the
|
| 117 |
substitution results in an invalid type, as described above, type
|
|
|
|
|
|
|
|
|
|
| 118 |
deduction fails.
|
| 119 |
|
| 120 |
At certain points in the template argument deduction process it is
|
| 121 |
necessary to take a function type that makes use of template parameters
|
| 122 |
and replace those template parameters with the corresponding template
|
|
@@ -131,11 +120,14 @@ the function type and in template parameter declarations. The
|
|
| 131 |
expressions include not only constant expressions such as those that
|
| 132 |
appear in array bounds or as nontype template arguments but also general
|
| 133 |
expressions (i.e., non-constant expressions) inside `sizeof`,
|
| 134 |
`decltype`, and other contexts that allow non-constant expressions. The
|
| 135 |
substitution proceeds in lexical order and stops when a condition that
|
| 136 |
-
causes deduction to fail is encountered.
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
[*Note 3*: The equivalent substitution in exception specifications is
|
| 139 |
done only when the *noexcept-specifier* is instantiated, at which point
|
| 140 |
a program is ill-formed if the substitution results in an invalid type
|
| 141 |
or expression. — *end note*]
|
|
@@ -146,14 +138,18 @@ or expression. — *end note*]
|
|
| 146 |
template <class T> struct A { using X = typename T::X; };
|
| 147 |
template <class T> typename T::X f(typename A<T>::X);
|
| 148 |
template <class T> void f(...) { }
|
| 149 |
template <class T> auto g(typename A<T>::X) -> typename T::X;
|
| 150 |
template <class T> void g(...) { }
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
-
void
|
| 153 |
f<int>(0); // OK, substituting return type causes deduction to fail
|
| 154 |
g<int>(0); // error, substituting parameter type instantiates A<int>
|
|
|
|
| 155 |
}
|
| 156 |
```
|
| 157 |
|
| 158 |
— *end example*]
|
| 159 |
|
|
@@ -165,22 +161,64 @@ arguments.
|
|
| 165 |
[*Note 4*: If no diagnostic is required, the program is still
|
| 166 |
ill-formed. Access checking is done as part of the substitution
|
| 167 |
process. — *end note*]
|
| 168 |
|
| 169 |
Only invalid types and expressions in the immediate context of the
|
| 170 |
-
function type
|
| 171 |
-
failure.
|
| 172 |
|
| 173 |
[*Note 5*: The substitution into types and expressions can result in
|
| 174 |
effects such as the instantiation of class template specializations
|
| 175 |
and/or function template specializations, the generation of
|
| 176 |
implicitly-defined functions, etc. Such effects are not in the
|
| 177 |
“immediate context” and can result in the program being
|
| 178 |
ill-formed. — *end note*]
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
[*Example 6*:
|
| 181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
``` cpp
|
| 183 |
struct X { };
|
| 184 |
struct Y {
|
| 185 |
Y(X){}
|
| 186 |
};
|
|
@@ -192,30 +230,30 @@ X x1, x2;
|
|
| 192 |
X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
|
| 193 |
```
|
| 194 |
|
| 195 |
— *end example*]
|
| 196 |
|
| 197 |
-
[*Note
|
| 198 |
|
| 199 |
Type deduction may fail for the following reasons:
|
| 200 |
|
| 201 |
-
- Attempting to instantiate a pack expansion containing multiple
|
| 202 |
-
|
| 203 |
- Attempting to create an array with an element type that is `void`, a
|
| 204 |
-
function type, a reference type, or
|
| 205 |
-
|
| 206 |
-
\[*Example
|
| 207 |
``` cpp
|
| 208 |
template <class T> int f(T[5]);
|
| 209 |
int I = f<int>(0);
|
| 210 |
int j = f<void>(0); // invalid array
|
| 211 |
```
|
| 212 |
|
| 213 |
— *end example*]
|
| 214 |
- Attempting to use a type that is not a class or enumeration type in a
|
| 215 |
qualified name.
|
| 216 |
-
\[*Example
|
| 217 |
``` cpp
|
| 218 |
template <class T> int f(typename T::B*);
|
| 219 |
int i = f<int>(0);
|
| 220 |
```
|
| 221 |
|
|
@@ -226,11 +264,11 @@ Type deduction may fail for the following reasons:
|
|
| 226 |
- the specified member is not a type where a type is required, or
|
| 227 |
- the specified member is not a template where a template is required,
|
| 228 |
or
|
| 229 |
- the specified member is not a non-type where a non-type is required.
|
| 230 |
|
| 231 |
-
\[*Example
|
| 232 |
``` cpp
|
| 233 |
template <int I> struct X { };
|
| 234 |
template <template <class T> class> struct Z { };
|
| 235 |
template <class T> void f(typename T::Y*){}
|
| 236 |
template <class T> void g(X<T::N>*){}
|
|
@@ -256,19 +294,19 @@ Type deduction may fail for the following reasons:
|
|
| 256 |
— *end example*]
|
| 257 |
- Attempting to create a pointer to reference type.
|
| 258 |
- Attempting to create a reference to `void`.
|
| 259 |
- Attempting to create “pointer to member of `T`” when `T` is not a
|
| 260 |
class type.
|
| 261 |
-
\[*Example
|
| 262 |
``` cpp
|
| 263 |
template <class T> int f(int T::*);
|
| 264 |
int i = f<int>(0);
|
| 265 |
```
|
| 266 |
|
| 267 |
— *end example*]
|
| 268 |
- Attempting to give an invalid type to a non-type template parameter.
|
| 269 |
-
\[*Example
|
| 270 |
``` cpp
|
| 271 |
template <class T, T> struct S {};
|
| 272 |
template <class T> int f(S<T, T()>*);
|
| 273 |
struct X {};
|
| 274 |
int i0 = f<X>(0);
|
|
@@ -276,32 +314,30 @@ Type deduction may fail for the following reasons:
|
|
| 276 |
|
| 277 |
— *end example*]
|
| 278 |
- Attempting to perform an invalid conversion in either a template
|
| 279 |
argument expression, or an expression used in the function
|
| 280 |
declaration.
|
| 281 |
-
\[*Example
|
| 282 |
``` cpp
|
| 283 |
template <class T, T*> int f(int);
|
| 284 |
int i2 = f<int,1>(0); // can't conv 1 to int*
|
| 285 |
```
|
| 286 |
|
| 287 |
— *end example*]
|
| 288 |
- Attempting to create a function type in which a parameter has a type
|
| 289 |
of `void`, or in which the return type is a function type or array
|
| 290 |
type.
|
| 291 |
-
- Attempting to create a function type in which a parameter type or the
|
| 292 |
-
return type is an abstract class type ([[class.abstract]]).
|
| 293 |
|
| 294 |
— *end note*]
|
| 295 |
|
| 296 |
-
[*Example
|
| 297 |
|
| 298 |
In the following example, assuming a `signed char` cannot represent the
|
| 299 |
-
value 1000, a narrowing conversion
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
[[temp.arg.nontype]]
|
| 303 |
|
| 304 |
``` cpp
|
| 305 |
template <int> int f(int);
|
| 306 |
template <signed char> int f(int);
|
| 307 |
int i1 = f<1000>(0); // OK
|
|
@@ -315,84 +351,93 @@ int i2 = f<1>(0); // ambiguous; not narrowing
|
|
| 315 |
Template argument deduction is done by comparing each function template
|
| 316 |
parameter type (call it `P`) that contains *template-parameter*s that
|
| 317 |
participate in template argument deduction with the type of the
|
| 318 |
corresponding argument of the call (call it `A`) as described below. If
|
| 319 |
removing references and cv-qualifiers from `P` gives
|
| 320 |
-
`std::initializer_list<P
|
| 321 |
-
argument is a non-empty initializer list
|
| 322 |
-
deduction is performed instead for each element of the initializer list
|
| 323 |
-
taking `P
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
|
|
|
| 328 |
|
| 329 |
[*Example 1*:
|
| 330 |
|
| 331 |
``` cpp
|
| 332 |
template<class T> void f(std::initializer_list<T>);
|
| 333 |
-
f({1,2,3}); // T deduced
|
| 334 |
-
f({1,"asdf"}); // error: T deduced
|
| 335 |
|
| 336 |
template<class T> void g(T);
|
| 337 |
g({1,2,3}); // error: no argument deduced for T
|
| 338 |
|
| 339 |
template<class T, int N> void h(T const(&)[N]);
|
| 340 |
-
h({1,2,3}); // T deduced
|
| 341 |
|
| 342 |
template<class T> void j(T const(&)[3]);
|
| 343 |
-
j({42}); // T deduced
|
| 344 |
|
| 345 |
struct Aggr { int i; int j; };
|
| 346 |
template<int N> void k(Aggr const(&)[N]);
|
| 347 |
k({1,2,3}); // error: deduction fails, no conversion from int to Aggr
|
| 348 |
-
k({{1},{2},{3}}); // OK, N deduced
|
| 349 |
|
| 350 |
template<int M, int N> void m(int const(&)[M][N]);
|
| 351 |
-
m({{1,2},{3,4}}); // M and N both deduced
|
| 352 |
|
| 353 |
template<class T, int N> void n(T const(&)[N], T);
|
| 354 |
n({{1},{2},{3}},Aggr()); // OK, T is Aggr, N is 3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
```
|
| 356 |
|
| 357 |
— *end example*]
|
| 358 |
|
| 359 |
For a function parameter pack that occurs at the end of the
|
| 360 |
*parameter-declaration-list*, deduction is performed for each remaining
|
| 361 |
argument of the call, taking the type `P` of the *declarator-id* of the
|
| 362 |
function parameter pack as the corresponding function template parameter
|
| 363 |
type. Each deduction deduces template arguments for subsequent positions
|
| 364 |
in the template parameter packs expanded by the function parameter pack.
|
| 365 |
-
When a function parameter pack appears in a non-deduced context
|
| 366 |
-
[[temp.deduct.type]]
|
| 367 |
|
| 368 |
[*Example 2*:
|
| 369 |
|
| 370 |
``` cpp
|
| 371 |
template<class ... Types> void f(Types& ...);
|
| 372 |
template<class T1, class ... Types> void g(T1, Types ...);
|
| 373 |
template<class T1, class ... Types> void g1(Types ..., T1);
|
| 374 |
|
| 375 |
void h(int x, float& y) {
|
| 376 |
const int z = x;
|
| 377 |
-
f(x, y, z); // Types
|
| 378 |
-
g(x, y, z); // T1
|
| 379 |
g1(x, y, z); // error: Types is not deduced
|
| 380 |
g1<int, int, int>(x, y, z); // OK, no deduction occurs
|
| 381 |
}
|
| 382 |
```
|
| 383 |
|
| 384 |
— *end example*]
|
| 385 |
|
| 386 |
If `P` is not a reference type:
|
| 387 |
|
| 388 |
- If `A` is an array type, the pointer type produced by the
|
| 389 |
-
array-to-pointer standard conversion
|
| 390 |
-
|
| 391 |
- If `A` is a function type, the pointer type produced by the
|
| 392 |
-
function-to-pointer standard conversion
|
| 393 |
-
|
| 394 |
- If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s
|
| 395 |
type are ignored for type deduction.
|
| 396 |
|
| 397 |
If `P` is a cv-qualified type, the top-level cv-qualifiers of `P`’s type
|
| 398 |
are ignored for type deduction. If `P` is a reference type, the type
|
|
@@ -411,12 +456,12 @@ int n3 = g(i); // calls g<const int>(const volatile int&)
|
|
| 411 |
|
| 412 |
— *end example*]
|
| 413 |
|
| 414 |
A *forwarding reference* is an rvalue reference to a cv-unqualified
|
| 415 |
template parameter that does not represent a template parameter of a
|
| 416 |
-
class template (during class template argument deduction
|
| 417 |
-
[[over.match.class.deduct]])
|
| 418 |
argument is an lvalue, the type “lvalue reference to `A`” is used in
|
| 419 |
place of `A` for type deduction.
|
| 420 |
|
| 421 |
[*Example 4*:
|
| 422 |
|
|
@@ -452,19 +497,38 @@ values that will make the deduced `A` identical to `A` (after the type
|
|
| 452 |
that allow a difference:
|
| 453 |
|
| 454 |
- If the original `P` is a reference type, the deduced `A` (i.e., the
|
| 455 |
type referred to by the reference) can be more cv-qualified than the
|
| 456 |
transformed `A`.
|
| 457 |
-
- The transformed `A` can be another pointer or pointer
|
| 458 |
that can be converted to the deduced `A` via a function pointer
|
| 459 |
-
conversion
|
| 460 |
-
[[conv.qual]]
|
| 461 |
- If `P` is a class and `P` has the form *simple-template-id*, then the
|
| 462 |
-
transformed `A` can be a derived class of the deduced `A`.
|
| 463 |
-
if `P` is a pointer to a class of the form
|
| 464 |
-
transformed `A` can be a pointer to a
|
| 465 |
-
deduced `A`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 466 |
|
| 467 |
These alternatives are considered only if type deduction would otherwise
|
| 468 |
fail. If they yield more than one possible deduced `A`, the type
|
| 469 |
deduction fails.
|
| 470 |
|
|
@@ -472,23 +536,23 @@ deduction fails.
|
|
| 472 |
parameters of a function template, or is used only in a non-deduced
|
| 473 |
context, its corresponding *template-argument* cannot be deduced from a
|
| 474 |
function call and the *template-argument* must be explicitly
|
| 475 |
specified. — *end note*]
|
| 476 |
|
| 477 |
-
When `P` is a function type, function pointer type, or
|
| 478 |
-
function type:
|
| 479 |
|
| 480 |
- If the argument is an overload set containing one or more function
|
| 481 |
templates, the parameter is treated as a non-deduced context.
|
| 482 |
- If the argument is an overload set (not containing function
|
| 483 |
templates), trial argument deduction is attempted using each of the
|
| 484 |
members of the set. If deduction succeeds for only one of the overload
|
| 485 |
set members, that member is used as the argument value for the
|
| 486 |
deduction. If deduction succeeds for more than one member of the
|
| 487 |
overload set the parameter is treated as a non-deduced context.
|
| 488 |
|
| 489 |
-
[*Example
|
| 490 |
|
| 491 |
``` cpp
|
| 492 |
// Only one function of an overload set matches the call so the function parameter is a deduced context.
|
| 493 |
template <class T> int f(T (*p)(T));
|
| 494 |
int g(int);
|
|
@@ -496,11 +560,11 @@ int g(char);
|
|
| 496 |
int i = f(g); // calls f(int (*)(int))
|
| 497 |
```
|
| 498 |
|
| 499 |
— *end example*]
|
| 500 |
|
| 501 |
-
[*Example
|
| 502 |
|
| 503 |
``` cpp
|
| 504 |
// Ambiguous deduction causes the second function parameter to be a non-deduced context.
|
| 505 |
template <class T> int f(T, T (*p)(T));
|
| 506 |
int g(int);
|
|
@@ -508,11 +572,11 @@ char g(char);
|
|
| 508 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 509 |
```
|
| 510 |
|
| 511 |
— *end example*]
|
| 512 |
|
| 513 |
-
[*Example
|
| 514 |
|
| 515 |
``` cpp
|
| 516 |
// The overload set contains a template, causing the second function parameter to be a non-deduced context.
|
| 517 |
template <class T> int f(T, T (*p)(T));
|
| 518 |
char g(char);
|
|
@@ -535,11 +599,11 @@ explicitly-specified template arguments, if the corresponding argument
|
|
| 535 |
*template-parameter*s participate in template argument deduction, and
|
| 536 |
parameters that became non-dependent due to substitution of
|
| 537 |
explicitly-specified template arguments, will be checked during overload
|
| 538 |
resolution. — *end note*]
|
| 539 |
|
| 540 |
-
[*Example
|
| 541 |
|
| 542 |
``` cpp
|
| 543 |
template <class T> struct Z {
|
| 544 |
typedef typename T::x xx;
|
| 545 |
};
|
|
@@ -554,16 +618,17 @@ resolution. — *end note*]
|
|
| 554 |
— *end example*]
|
| 555 |
|
| 556 |
#### Deducing template arguments taking the address of a function template <a id="temp.deduct.funcaddr">[[temp.deduct.funcaddr]]</a>
|
| 557 |
|
| 558 |
Template arguments can be deduced from the type specified when taking
|
| 559 |
-
the address of an overloaded function
|
| 560 |
-
template’s function type and the
|
| 561 |
-
`P` and `A`, and the deduction is done as described
|
| 562 |
-
[[temp.deduct.type]].
|
|
|
|
| 563 |
|
| 564 |
-
A placeholder type
|
| 565 |
template is a non-deduced context. If template argument deduction
|
| 566 |
succeeds for such a function, the return type is determined from
|
| 567 |
instantiation of the function body.
|
| 568 |
|
| 569 |
#### Deducing conversion function template arguments <a id="temp.deduct.conv">[[temp.deduct.conv]]</a>
|
|
@@ -574,20 +639,20 @@ required as the result of the conversion (call it `A`; see
|
|
| 574 |
[[dcl.init]], [[over.match.conv]], and [[over.match.ref]] for the
|
| 575 |
determination of that type) as described in [[temp.deduct.type]].
|
| 576 |
|
| 577 |
If `P` is a reference type, the type referred to by `P` is used in place
|
| 578 |
of `P` for type deduction and for any further references to or
|
| 579 |
-
transformations of `P` in the remainder of this
|
| 580 |
|
| 581 |
If `A` is not a reference type:
|
| 582 |
|
| 583 |
- If `P` is an array type, the pointer type produced by the
|
| 584 |
-
array-to-pointer standard conversion
|
| 585 |
-
|
| 586 |
- If `P` is a function type, the pointer type produced by the
|
| 587 |
-
function-to-pointer standard conversion
|
| 588 |
-
|
| 589 |
- If `P` is a cv-qualified type, the top-level cv-qualifiers of `P`’s
|
| 590 |
type are ignored for type deduction.
|
| 591 |
|
| 592 |
If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s type
|
| 593 |
are ignored for type deduction. If `A` is a reference type, the type
|
|
@@ -596,46 +661,23 @@ referred to by `A` is used for type deduction.
|
|
| 596 |
In general, the deduction process attempts to find template argument
|
| 597 |
values that will make the deduced `A` identical to `A`. However, there
|
| 598 |
are four cases that allow a difference:
|
| 599 |
|
| 600 |
- If the original `A` is a reference type, `A` can be more cv-qualified
|
| 601 |
-
than the deduced `A` (i.e., the type referred to by the reference)
|
| 602 |
- If the original `A` is a function pointer type, `A` can be “pointer to
|
| 603 |
-
function” even if the deduced `A` is “pointer to noexcept function”.
|
| 604 |
-
- If the original `A` is a pointer
|
| 605 |
“pointer to member of type function” even if the deduced `A` is
|
| 606 |
-
“pointer to member of type noexcept function”.
|
| 607 |
-
- The deduced `A` can be another pointer or pointer
|
| 608 |
can be converted to `A` via a qualification conversion.
|
| 609 |
|
| 610 |
These alternatives are considered only if type deduction would otherwise
|
| 611 |
fail. If they yield more than one possible deduced `A`, the type
|
| 612 |
deduction fails.
|
| 613 |
|
| 614 |
-
When the deduction process requires a qualification conversion for a
|
| 615 |
-
pointer or pointer to member type as described above, the following
|
| 616 |
-
process is used to determine the deduced template argument values:
|
| 617 |
-
|
| 618 |
-
If `A` is a type
|
| 619 |
-
|
| 620 |
-
and `P` is a type
|
| 621 |
-
|
| 622 |
-
then the cv-unqualified `T1` and `T2` are used as the types of `A` and
|
| 623 |
-
`P` respectively for type deduction.
|
| 624 |
-
|
| 625 |
-
[*Example 1*:
|
| 626 |
-
|
| 627 |
-
``` cpp
|
| 628 |
-
struct A {
|
| 629 |
-
template <class T> operator T***();
|
| 630 |
-
};
|
| 631 |
-
A a;
|
| 632 |
-
const int * const * const * p1 = a; // T is deduced as int, not const int
|
| 633 |
-
```
|
| 634 |
-
|
| 635 |
-
— *end example*]
|
| 636 |
-
|
| 637 |
#### Deducing template arguments during partial ordering <a id="temp.deduct.partial">[[temp.deduct.partial]]</a>
|
| 638 |
|
| 639 |
Template argument deduction is done by comparing certain types
|
| 640 |
associated with the two function templates being compared.
|
| 641 |
|
|
@@ -656,21 +698,19 @@ as the parameter template.
|
|
| 656 |
|
| 657 |
The types used to determine the ordering depend on the context in which
|
| 658 |
the partial ordering is done:
|
| 659 |
|
| 660 |
- In the context of a function call, the types used are those function
|
| 661 |
-
parameter types for which the function call has arguments.[^
|
| 662 |
- In the context of a call to a conversion function, the return types of
|
| 663 |
the conversion function templates are used.
|
| 664 |
-
- In other contexts
|
| 665 |
-
|
| 666 |
|
| 667 |
Each type nominated above from the parameter template and the
|
| 668 |
corresponding type from the argument template are used as the types of
|
| 669 |
-
`P` and `A`.
|
| 670 |
-
participate in template argument deduction, that `P` is not used to
|
| 671 |
-
determine the ordering.
|
| 672 |
|
| 673 |
Before the partial ordering is done, certain transformations are
|
| 674 |
performed on the types used for partial ordering:
|
| 675 |
|
| 676 |
- If `P` is a reference type, `P` is replaced by the type referred to.
|
|
@@ -714,14 +754,13 @@ f(1, 2); // calls #3; non-variadic template #3 is more specialized
|
|
| 714 |
// than the variadic templates #1 and #2
|
| 715 |
```
|
| 716 |
|
| 717 |
— *end example*]
|
| 718 |
|
| 719 |
-
If, for a given type,
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
to above):
|
| 723 |
|
| 724 |
- if the type from the argument template was an lvalue reference and the
|
| 725 |
type from the parameter template was not, the parameter type is not
|
| 726 |
considered to be at least as specialized as the argument type;
|
| 727 |
otherwise,
|
|
@@ -736,18 +775,18 @@ from `F` is at least as specialized as the type from `G`. `F` is *more
|
|
| 736 |
specialized than* `G` if `F` is at least as specialized as `G` and `G`
|
| 737 |
is not at least as specialized as `F`.
|
| 738 |
|
| 739 |
If, after considering the above, function template `F` is at least as
|
| 740 |
specialized as function template `G` and vice-versa, and if `G` has a
|
| 741 |
-
trailing parameter pack for which `F` does not have a
|
| 742 |
-
parameter, and if `F` does not have a trailing
|
| 743 |
-
is more specialized than `G`.
|
| 744 |
|
| 745 |
-
In most cases,
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
|
| 750 |
[*Note 2*: A template parameter used in a non-deduced context is
|
| 751 |
considered used. — *end note*]
|
| 752 |
|
| 753 |
[*Example 2*:
|
|
@@ -806,32 +845,32 @@ array bound if it is not otherwise deduced.
|
|
| 806 |
A given type `P` can be composed from a number of other types,
|
| 807 |
templates, and non-type values:
|
| 808 |
|
| 809 |
- A function type includes the types of each of the function parameters
|
| 810 |
and the return type.
|
| 811 |
-
- A pointer
|
| 812 |
to and the type of the member pointed to.
|
| 813 |
- A type that is a specialization of a class template (e.g., `A<int>`)
|
| 814 |
includes the types, templates, and non-type values referenced by the
|
| 815 |
template argument list of the specialization.
|
| 816 |
- An array type includes the array element type and the value of the
|
| 817 |
array bound.
|
| 818 |
|
| 819 |
In most cases, the types, templates, and non-type values that are used
|
| 820 |
to compose `P` participate in template argument deduction. That is, they
|
| 821 |
-
may be used to determine the value of a template argument, and
|
| 822 |
-
|
| 823 |
-
In certain contexts, however, the
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
specified, template argument
|
|
|
|
| 828 |
|
| 829 |
-
[*Note 1*: Under [[temp.deduct.call]]
|
| 830 |
-
|
| 831 |
-
|
| 832 |
-
form. — *end note*]
|
| 833 |
|
| 834 |
The non-deduced contexts are:
|
| 835 |
|
| 836 |
- The *nested-name-specifier* of a type that was specified using a
|
| 837 |
*qualified-id*.
|
|
@@ -839,23 +878,21 @@ The non-deduced contexts are:
|
|
| 839 |
- A non-type template argument or an array bound in which a
|
| 840 |
subexpression references a template parameter.
|
| 841 |
- A template parameter used in the parameter type of a function
|
| 842 |
parameter that has a default argument that is being used in the call
|
| 843 |
for which argument deduction is being done.
|
| 844 |
-
- A function parameter for which
|
| 845 |
-
|
| 846 |
-
overloaded functions ([[over.over]]), and one or more of the
|
| 847 |
-
following apply:
|
| 848 |
- more than one function matches the function parameter type
|
| 849 |
(resulting in an ambiguous deduction), or
|
| 850 |
- no function matches the function parameter type, or
|
| 851 |
-
- the set
|
| 852 |
function templates.
|
| 853 |
- A function parameter for which the associated argument is an
|
| 854 |
-
initializer list
|
| 855 |
-
|
| 856 |
-
[[temp.deduct.call]]
|
| 857 |
\[*Example 1*:
|
| 858 |
``` cpp
|
| 859 |
template<class T> void g(T);
|
| 860 |
g({1,2,3}); // error: no argument deduced for T
|
| 861 |
```
|
|
@@ -945,11 +982,11 @@ A template type argument `T`, a template template argument `TT` or a
|
|
| 945 |
template non-type argument `i` can be deduced if `P` and `A` have one of
|
| 946 |
the following forms:
|
| 947 |
|
| 948 |
``` cpp
|
| 949 |
T
|
| 950 |
-
cv
|
| 951 |
T*
|
| 952 |
T&
|
| 953 |
T&&
|
| 954 |
T[integer-constant]
|
| 955 |
template-name<T> (where template-name refers to a class template)
|
|
@@ -971,12 +1008,12 @@ template-name<i> (where template-name refers to a class template)
|
|
| 971 |
TT<T>
|
| 972 |
TT<i>
|
| 973 |
TT<>
|
| 974 |
```
|
| 975 |
|
| 976 |
-
where `(T)` represents a parameter-type-list
|
| 977 |
-
|
| 978 |
parameter-type-list where no parameter type contains a `T`. Similarly,
|
| 979 |
`<T>` represents template argument lists where at least one argument
|
| 980 |
contains a `T`, `<i>` represents template argument lists where at least
|
| 981 |
one argument contains an `i` and `<>` represents template argument lists
|
| 982 |
where no argument contains a `T` or an `i`.
|
|
@@ -988,12 +1025,12 @@ corresponding argument Aᵢ of the corresponding template argument list of
|
|
| 988 |
is not the last template argument, the entire template argument list is
|
| 989 |
a non-deduced context. If `Pᵢ` is a pack expansion, then the pattern of
|
| 990 |
`Pᵢ` is compared with each remaining argument in the template argument
|
| 991 |
list of `A`. Each comparison deduces template arguments for subsequent
|
| 992 |
positions in the template parameter packs expanded by `Pᵢ`. During
|
| 993 |
-
partial ordering
|
| 994 |
-
|
| 995 |
|
| 996 |
- if `P` does not contain a template argument corresponding to `Aᵢ` then
|
| 997 |
`Aᵢ` is ignored;
|
| 998 |
- otherwise, if `Pᵢ` is not a pack expansion, template argument
|
| 999 |
deduction fails.
|
|
@@ -1013,19 +1050,19 @@ template struct A<int, int*>; // selects #2
|
|
| 1013 |
```
|
| 1014 |
|
| 1015 |
— *end example*]
|
| 1016 |
|
| 1017 |
Similarly, if `P` has a form that contains `(T)`, then each parameter
|
| 1018 |
-
type `Pᵢ` of the respective parameter-type-list
|
| 1019 |
compared with the corresponding parameter type `Aᵢ` of the corresponding
|
| 1020 |
parameter-type-list of `A`. If `P` and `A` are function types that
|
| 1021 |
-
originated from deduction when taking the address of a function
|
| 1022 |
-
|
| 1023 |
-
|
| 1024 |
-
|
| 1025 |
-
respectively, `Pᵢ` is adjusted if it is a forwarding reference
|
| 1026 |
-
[[temp.deduct.call]]
|
| 1027 |
type of `Pᵢ` is changed to be the template parameter type (i.e., `T&&`
|
| 1028 |
is changed to simply `T`).
|
| 1029 |
|
| 1030 |
[*Note 2*: As a result, when `Pᵢ` is `T&&` and `Aᵢ` is `X&`, the
|
| 1031 |
adjusted `Pᵢ` will be `T`, causing `T` to be deduced as
|
|
@@ -1048,11 +1085,11 @@ void g(int i) {
|
|
| 1048 |
If the *parameter-declaration* corresponding to `Pᵢ` is a function
|
| 1049 |
parameter pack, then the type of its *declarator-id* is compared with
|
| 1050 |
each remaining parameter type in the parameter-type-list of `A`. Each
|
| 1051 |
comparison deduces template arguments for subsequent positions in the
|
| 1052 |
template parameter packs expanded by the function parameter pack. During
|
| 1053 |
-
partial ordering
|
| 1054 |
function parameter pack:
|
| 1055 |
|
| 1056 |
- if `P` does not contain a function parameter type corresponding to
|
| 1057 |
`Aᵢ` then `Aᵢ` is ignored;
|
| 1058 |
- otherwise, if `Pᵢ` is not a function parameter pack, template argument
|
|
@@ -1110,11 +1147,11 @@ template<typename T> struct C;
|
|
| 1110 |
template<typename T, T n> struct C<A<n>> {
|
| 1111 |
using Q = T;
|
| 1112 |
};
|
| 1113 |
|
| 1114 |
using R = long;
|
| 1115 |
-
using R = C<A<2>>::Q; // OK; T was deduced
|
| 1116 |
// template argument value in the type A<2>
|
| 1117 |
```
|
| 1118 |
|
| 1119 |
— *end example*]
|
| 1120 |
|
|
@@ -1127,11 +1164,11 @@ template<typename T> struct S;
|
|
| 1127 |
template<typename T, T n> struct S<int[n]> {
|
| 1128 |
using Q = T;
|
| 1129 |
};
|
| 1130 |
|
| 1131 |
using V = decltype(sizeof 0);
|
| 1132 |
-
using V = S<int[42]>::Q; // OK; T was deduced
|
| 1133 |
```
|
| 1134 |
|
| 1135 |
— *end example*]
|
| 1136 |
|
| 1137 |
[*Example 10*:
|
|
@@ -1156,15 +1193,15 @@ template<int i> void f1(int a[10][i]);
|
|
| 1156 |
template<int i> void f2(int a[i][20]);
|
| 1157 |
template<int i> void f3(int (&a)[i][20]);
|
| 1158 |
|
| 1159 |
void g() {
|
| 1160 |
int v[10][20];
|
| 1161 |
-
f1(v); // OK: i deduced
|
| 1162 |
f1<20>(v); // OK
|
| 1163 |
f2(v); // error: cannot deduce template-argument i
|
| 1164 |
f2<10>(v); // OK
|
| 1165 |
-
f3(v); // OK: i deduced
|
| 1166 |
}
|
| 1167 |
```
|
| 1168 |
|
| 1169 |
— *end note*]
|
| 1170 |
|
|
@@ -1206,21 +1243,21 @@ T deduce(typename A<T>::X x, // T is not deduced here
|
|
| 1206 |
typename B<i>::Y y); // i is not deduced here
|
| 1207 |
A<int> a;
|
| 1208 |
B<77> b;
|
| 1209 |
|
| 1210 |
int x = deduce<77>(a.xm, 62, b.ym);
|
| 1211 |
-
// T
|
| 1212 |
-
// i is explicitly specified to be 77
|
| 1213 |
```
|
| 1214 |
|
| 1215 |
— *end note*]
|
| 1216 |
|
| 1217 |
If `P` has a form that contains `<i>`, and if the type of `i` differs
|
| 1218 |
from the type of the corresponding template parameter of the template
|
| 1219 |
named by the enclosing *simple-template-id*, deduction fails. If `P` has
|
| 1220 |
a form that contains `[i]`, and if the type of `i` is not an integral
|
| 1221 |
-
type, deduction fails.[^
|
| 1222 |
|
| 1223 |
[*Example 12*:
|
| 1224 |
|
| 1225 |
``` cpp
|
| 1226 |
template<int i> class A { ... };
|
|
@@ -1240,11 +1277,11 @@ void k2() {
|
|
| 1240 |
```
|
| 1241 |
|
| 1242 |
— *end example*]
|
| 1243 |
|
| 1244 |
A *template-argument* can be deduced from a function, pointer to
|
| 1245 |
-
function, or pointer
|
| 1246 |
|
| 1247 |
[*Example 13*:
|
| 1248 |
|
| 1249 |
``` cpp
|
| 1250 |
template<class T> void f(void(*)(T,int));
|
|
@@ -1293,12 +1330,12 @@ A<B> ab;
|
|
| 1293 |
f(ab); // calls f(A<B>)
|
| 1294 |
```
|
| 1295 |
|
| 1296 |
— *end example*]
|
| 1297 |
|
| 1298 |
-
[*Note 6*: Template argument deduction involving parameter packs
|
| 1299 |
-
[[temp.variadic]]
|
| 1300 |
pack. — *end note*]
|
| 1301 |
|
| 1302 |
[*Example 16*:
|
| 1303 |
|
| 1304 |
``` cpp
|
|
@@ -1324,21 +1361,21 @@ int fv = f(g); // OK; Types contains int, float
|
|
| 1324 |
#### Deducing template arguments from a function declaration <a id="temp.deduct.decl">[[temp.deduct.decl]]</a>
|
| 1325 |
|
| 1326 |
In a declaration whose *declarator-id* refers to a specialization of a
|
| 1327 |
function template, template argument deduction is performed to identify
|
| 1328 |
the specialization to which the declaration refers. Specifically, this
|
| 1329 |
-
is done for explicit instantiations
|
| 1330 |
-
specializations
|
| 1331 |
-
[[temp.friend]]
|
| 1332 |
function template specialization matches a placement `operator new` (
|
| 1333 |
-
[[basic.stc.dynamic.deallocation]],
|
| 1334 |
`P` is the type of the function template being considered as a potential
|
| 1335 |
match and `A` is either the function type from the declaration or the
|
| 1336 |
type of the deallocation function that would match the placement
|
| 1337 |
`operator new` as described in [[expr.new]]. The deduction is done as
|
| 1338 |
described in [[temp.deduct.type]].
|
| 1339 |
|
| 1340 |
If, for the set of function templates so considered, there is either no
|
| 1341 |
-
match or more than one match after partial ordering has been
|
| 1342 |
-
|
| 1343 |
-
|
| 1344 |
|
|
|
|
| 23 |
}
|
| 24 |
```
|
| 25 |
|
| 26 |
— *end example*]
|
| 27 |
|
| 28 |
+
When an explicit template argument list is specified, if the given
|
| 29 |
+
*template-id* is not valid [[temp.names]], type deduction fails.
|
| 30 |
+
Otherwise, the specified template argument values are substituted for
|
| 31 |
+
the corresponding template parameters as specified below.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
After this substitution is performed, the function parameter type
|
| 34 |
adjustments described in [[dcl.fct]] are performed.
|
| 35 |
|
| 36 |
[*Example 2*: A parameter type of “`void (const int, int[5])`” becomes
|
|
|
|
| 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 and the function type are replaced with
|
| 102 |
the corresponding deduced or default argument values. If the
|
| 103 |
substitution results in an invalid type, as described above, type
|
| 104 |
+
deduction fails. If the function template has associated constraints
|
| 105 |
+
[[temp.constr.decl]], those constraints are checked for satisfaction
|
| 106 |
+
[[temp.constr.constr]]. If the constraints are not satisfied, type
|
| 107 |
deduction fails.
|
| 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
|
|
|
|
| 120 |
expressions include not only constant expressions such as those that
|
| 121 |
appear in array bounds or as nontype template arguments but also general
|
| 122 |
expressions (i.e., non-constant expressions) inside `sizeof`,
|
| 123 |
`decltype`, and other contexts that allow non-constant expressions. The
|
| 124 |
substitution proceeds in lexical order and stops when a condition that
|
| 125 |
+
causes deduction to fail is encountered. If substitution into different
|
| 126 |
+
declarations of the same function template would cause template
|
| 127 |
+
instantiations to occur in a different order or not at all, the program
|
| 128 |
+
is ill-formed; no diagnostic required.
|
| 129 |
|
| 130 |
[*Note 3*: The equivalent substitution in exception specifications is
|
| 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*]
|
|
|
|
| 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(...) { }
|
| 141 |
template <class T> auto g(typename A<T>::X) -> typename T::X;
|
| 142 |
template <class T> void g(...) { }
|
| 143 |
+
template <class T> typename T::X h(typename A<T>::X);
|
| 144 |
+
template <class T> auto h(typename A<T>::X) -> typename T::X; // redeclaration
|
| 145 |
+
template <class T> void h(...) { }
|
| 146 |
|
| 147 |
+
void x() {
|
| 148 |
f<int>(0); // OK, substituting return type causes deduction to fail
|
| 149 |
g<int>(0); // error, substituting parameter type instantiates A<int>
|
| 150 |
+
h<int>(0); // ill-formed, no diagnostic required
|
| 151 |
}
|
| 152 |
```
|
| 153 |
|
| 154 |
— *end example*]
|
| 155 |
|
|
|
|
| 161 |
[*Note 4*: If no diagnostic is required, the program is still
|
| 162 |
ill-formed. Access checking is done as part of the substitution
|
| 163 |
process. — *end note*]
|
| 164 |
|
| 165 |
Only invalid types and expressions in the immediate context of the
|
| 166 |
+
function type, its template parameter types, and its
|
| 167 |
+
*explicit-specifier* can result in a deduction failure.
|
| 168 |
|
| 169 |
[*Note 5*: The substitution into types and expressions can result in
|
| 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 6*:
|
| 181 |
+
|
| 182 |
+
The intent is to avoid requiring implementations to deal with
|
| 183 |
+
substitution failure involving arbitrary statements.
|
| 184 |
+
|
| 185 |
[*Example 6*:
|
| 186 |
|
| 187 |
+
``` cpp
|
| 188 |
+
template <class T>
|
| 189 |
+
auto f(T) -> decltype([]() { T::invalid; } ());
|
| 190 |
+
void f(...);
|
| 191 |
+
f(0); // error: invalid expression not part of the immediate context
|
| 192 |
+
|
| 193 |
+
template <class T, std::size_t = sizeof([]() { T::invalid; })>
|
| 194 |
+
void g(T);
|
| 195 |
+
void g(...);
|
| 196 |
+
g(0); // error: invalid expression not part of the immediate context
|
| 197 |
+
|
| 198 |
+
template <class T>
|
| 199 |
+
auto h(T) -> decltype([x = T::invalid]() { });
|
| 200 |
+
void h(...);
|
| 201 |
+
h(0); // error: invalid expression not part of the immediate context
|
| 202 |
+
|
| 203 |
+
template <class T>
|
| 204 |
+
auto i(T) -> decltype([]() -> typename T::invalid { });
|
| 205 |
+
void i(...);
|
| 206 |
+
i(0); // error: invalid expression not part of the immediate context
|
| 207 |
+
|
| 208 |
+
template <class T>
|
| 209 |
+
auto j(T t) -> decltype([](auto x) -> decltype(x.invalid) { } (t)); // #1
|
| 210 |
+
void j(...); // #2
|
| 211 |
+
j(0); // deduction fails on #1, calls #2
|
| 212 |
+
```
|
| 213 |
+
|
| 214 |
+
— *end example*]
|
| 215 |
+
|
| 216 |
+
— *end note*]
|
| 217 |
+
|
| 218 |
+
[*Example 7*:
|
| 219 |
+
|
| 220 |
``` cpp
|
| 221 |
struct X { };
|
| 222 |
struct Y {
|
| 223 |
Y(X){}
|
| 224 |
};
|
|
|
|
| 230 |
X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
|
| 231 |
```
|
| 232 |
|
| 233 |
— *end example*]
|
| 234 |
|
| 235 |
+
[*Note 7*:
|
| 236 |
|
| 237 |
Type deduction may fail for the following reasons:
|
| 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 8*:
|
| 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 9*:
|
| 255 |
``` cpp
|
| 256 |
template <class T> int f(typename T::B*);
|
| 257 |
int i = f<int>(0);
|
| 258 |
```
|
| 259 |
|
|
|
|
| 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 10*:
|
| 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>*){}
|
|
|
|
| 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 11*:
|
| 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 12*:
|
| 308 |
``` cpp
|
| 309 |
template <class T, T> struct S {};
|
| 310 |
template <class T> int f(S<T, T()>*);
|
| 311 |
struct X {};
|
| 312 |
int i0 = f<X>(0);
|
|
|
|
| 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 13*:
|
| 320 |
``` cpp
|
| 321 |
template <class T, T*> int f(int);
|
| 322 |
int i2 = f<int,1>(0); // can't conv 1 to int*
|
| 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 14*:
|
| 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
|
| 338 |
+
[[temp.arg.nontype]].
|
| 339 |
|
| 340 |
``` cpp
|
| 341 |
template <int> int f(int);
|
| 342 |
template <signed char> int f(int);
|
| 343 |
int i1 = f<1000>(0); // OK
|
|
|
|
| 351 |
Template argument deduction is done by comparing each function template
|
| 352 |
parameter type (call it `P`) that contains *template-parameter*s that
|
| 353 |
participate in template argument deduction with the type of the
|
| 354 |
corresponding argument of the call (call it `A`) as described below. If
|
| 355 |
removing references and cv-qualifiers from `P` gives
|
| 356 |
+
`std::initializer_list<P^{\prime}>` or `P`'`[N]` for some `P`' and `N`
|
| 357 |
+
and the argument is a non-empty initializer list [[dcl.init.list]], then
|
| 358 |
+
deduction is performed instead for each element of the initializer list
|
| 359 |
+
independently, taking `P`' as separate function template parameter types
|
| 360 |
+
`P`'_i and the iᵗʰ initializer element as the corresponding argument. In
|
| 361 |
+
the `P`'`[N]` case, if `N` is a non-type template parameter, `N` is
|
| 362 |
+
deduced from the length of the initializer list. Otherwise, an
|
| 363 |
+
initializer list argument causes the parameter to be considered a
|
| 364 |
+
non-deduced context [[temp.deduct.type]].
|
| 365 |
|
| 366 |
[*Example 1*:
|
| 367 |
|
| 368 |
``` cpp
|
| 369 |
template<class T> void f(std::initializer_list<T>);
|
| 370 |
+
f({1,2,3}); // T deduced as int
|
| 371 |
+
f({1,"asdf"}); // error: T deduced as both int and const char*
|
| 372 |
|
| 373 |
template<class T> void g(T);
|
| 374 |
g({1,2,3}); // error: no argument deduced for T
|
| 375 |
|
| 376 |
template<class T, int N> void h(T const(&)[N]);
|
| 377 |
+
h({1,2,3}); // T deduced as int; N deduced as 3
|
| 378 |
|
| 379 |
template<class T> void j(T const(&)[3]);
|
| 380 |
+
j({42}); // T deduced as int; array bound not considered
|
| 381 |
|
| 382 |
struct Aggr { int i; int j; };
|
| 383 |
template<int N> void k(Aggr const(&)[N]);
|
| 384 |
k({1,2,3}); // error: deduction fails, no conversion from int to Aggr
|
| 385 |
+
k({{1},{2},{3}}); // OK, N deduced as 3
|
| 386 |
|
| 387 |
template<int M, int N> void m(int const(&)[M][N]);
|
| 388 |
+
m({{1,2},{3,4}}); // M and N both deduced as 2
|
| 389 |
|
| 390 |
template<class T, int N> void n(T const(&)[N], T);
|
| 391 |
n({{1},{2},{3}},Aggr()); // OK, T is Aggr, N is 3
|
| 392 |
+
|
| 393 |
+
template<typename T, int N> void o(T (* const (&)[N])(T)) { }
|
| 394 |
+
int f1(int);
|
| 395 |
+
int f4(int);
|
| 396 |
+
char f4(char);
|
| 397 |
+
o({ &f1, &f4 }); // OK, T deduced as int from first element, nothing
|
| 398 |
+
// deduced from second element, N deduced as 2
|
| 399 |
+
o({ &f1, static_cast<char(*)(char)>(&f4) }); // error: conflicting deductions for T
|
| 400 |
```
|
| 401 |
|
| 402 |
— *end example*]
|
| 403 |
|
| 404 |
For a function parameter pack that occurs at the end of the
|
| 405 |
*parameter-declaration-list*, deduction is performed for each remaining
|
| 406 |
argument of the call, taking the type `P` of the *declarator-id* of the
|
| 407 |
function parameter pack as the corresponding function template parameter
|
| 408 |
type. Each deduction deduces template arguments for subsequent positions
|
| 409 |
in the template parameter packs expanded by the function parameter pack.
|
| 410 |
+
When a function parameter pack appears in a non-deduced context
|
| 411 |
+
[[temp.deduct.type]], the type of that pack is never deduced.
|
| 412 |
|
| 413 |
[*Example 2*:
|
| 414 |
|
| 415 |
``` cpp
|
| 416 |
template<class ... Types> void f(Types& ...);
|
| 417 |
template<class T1, class ... Types> void g(T1, Types ...);
|
| 418 |
template<class T1, class ... Types> void g1(Types ..., T1);
|
| 419 |
|
| 420 |
void h(int x, float& y) {
|
| 421 |
const int z = x;
|
| 422 |
+
f(x, y, z); // Types deduced as int, float, const int
|
| 423 |
+
g(x, y, z); // T1 deduced as int; Types deduced as float, int
|
| 424 |
g1(x, y, z); // error: Types is not deduced
|
| 425 |
g1<int, int, int>(x, y, z); // OK, no deduction occurs
|
| 426 |
}
|
| 427 |
```
|
| 428 |
|
| 429 |
— *end example*]
|
| 430 |
|
| 431 |
If `P` is not a reference type:
|
| 432 |
|
| 433 |
- If `A` is an array type, the pointer type produced by the
|
| 434 |
+
array-to-pointer standard conversion [[conv.array]] is used in place
|
| 435 |
+
of `A` for type deduction; otherwise,
|
| 436 |
- If `A` is a function type, the pointer type produced by the
|
| 437 |
+
function-to-pointer standard conversion [[conv.func]] is used in place
|
| 438 |
+
of `A` for type deduction; otherwise,
|
| 439 |
- If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s
|
| 440 |
type are ignored for type deduction.
|
| 441 |
|
| 442 |
If `P` is a cv-qualified type, the top-level cv-qualifiers of `P`’s type
|
| 443 |
are ignored for type deduction. If `P` is a reference type, the type
|
|
|
|
| 456 |
|
| 457 |
— *end example*]
|
| 458 |
|
| 459 |
A *forwarding reference* is an rvalue reference to a cv-unqualified
|
| 460 |
template parameter that does not represent a template parameter of a
|
| 461 |
+
class template (during class template argument deduction
|
| 462 |
+
[[over.match.class.deduct]]). If `P` is a forwarding reference and the
|
| 463 |
argument is an lvalue, the type “lvalue reference to `A`” is used in
|
| 464 |
place of `A` for type deduction.
|
| 465 |
|
| 466 |
[*Example 4*:
|
| 467 |
|
|
|
|
| 497 |
that allow a difference:
|
| 498 |
|
| 499 |
- If the original `P` is a reference type, the deduced `A` (i.e., the
|
| 500 |
type referred to by the reference) can be more cv-qualified than the
|
| 501 |
transformed `A`.
|
| 502 |
+
- The transformed `A` can be another pointer or pointer-to-member type
|
| 503 |
that can be converted to the deduced `A` via a function pointer
|
| 504 |
+
conversion [[conv.fctptr]] and/or qualification conversion
|
| 505 |
+
[[conv.qual]].
|
| 506 |
- If `P` is a class and `P` has the form *simple-template-id*, then the
|
| 507 |
+
transformed `A` can be a derived class `D` of the deduced `A`.
|
| 508 |
+
Likewise, if `P` is a pointer to a class of the form
|
| 509 |
+
*simple-template-id*, the transformed `A` can be a pointer to a
|
| 510 |
+
derived class `D` pointed to by the deduced `A`. However, if there is
|
| 511 |
+
a class `C` that is a (direct or indirect) base class of `D` and
|
| 512 |
+
derived (directly or indirectly) from a class `B` and that would be a
|
| 513 |
+
valid deduced `A`, the deduced `A` cannot be `B` or pointer to `B`,
|
| 514 |
+
respectively.
|
| 515 |
+
\[*Example 5*:
|
| 516 |
+
``` cpp
|
| 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
|
| 532 |
fail. If they yield more than one possible deduced `A`, the type
|
| 533 |
deduction fails.
|
| 534 |
|
|
|
|
| 536 |
parameters of a function template, or is used only in a non-deduced
|
| 537 |
context, its corresponding *template-argument* cannot be deduced from a
|
| 538 |
function call and the *template-argument* must be explicitly
|
| 539 |
specified. — *end note*]
|
| 540 |
|
| 541 |
+
When `P` is a function type, function pointer type, or
|
| 542 |
+
pointer-to-member-function type:
|
| 543 |
|
| 544 |
- If the argument is an overload set containing one or more function
|
| 545 |
templates, the parameter is treated as a non-deduced context.
|
| 546 |
- If the argument is an overload set (not containing function
|
| 547 |
templates), trial argument deduction is attempted using each of the
|
| 548 |
members of the set. If deduction succeeds for only one of the overload
|
| 549 |
set members, that member is used as the argument value for the
|
| 550 |
deduction. If deduction succeeds for more than one member of the
|
| 551 |
overload set the parameter is treated as a non-deduced context.
|
| 552 |
|
| 553 |
+
[*Example 6*:
|
| 554 |
|
| 555 |
``` cpp
|
| 556 |
// Only one function of an overload set matches the call so the function parameter is a deduced context.
|
| 557 |
template <class T> int f(T (*p)(T));
|
| 558 |
int g(int);
|
|
|
|
| 560 |
int i = f(g); // calls f(int (*)(int))
|
| 561 |
```
|
| 562 |
|
| 563 |
— *end example*]
|
| 564 |
|
| 565 |
+
[*Example 7*:
|
| 566 |
|
| 567 |
``` cpp
|
| 568 |
// Ambiguous deduction causes the second function parameter to be a non-deduced context.
|
| 569 |
template <class T> int f(T, T (*p)(T));
|
| 570 |
int g(int);
|
|
|
|
| 572 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 573 |
```
|
| 574 |
|
| 575 |
— *end example*]
|
| 576 |
|
| 577 |
+
[*Example 8*:
|
| 578 |
|
| 579 |
``` cpp
|
| 580 |
// The overload set contains a template, causing the second function parameter to be a non-deduced context.
|
| 581 |
template <class T> int f(T, T (*p)(T));
|
| 582 |
char g(char);
|
|
|
|
| 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 |
};
|
|
|
|
| 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 overloaded function [[over.over]]. If there is a
|
| 624 |
+
target, the function template’s function type and the target type are
|
| 625 |
+
used as the types of `P` and `A`, and the deduction is done as described
|
| 626 |
+
in [[temp.deduct.type]]. Otherwise, deduction is performed with empty
|
| 627 |
+
sets of types P and A.
|
| 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>
|
|
|
|
| 639 |
[[dcl.init]], [[over.match.conv]], and [[over.match.ref]] for the
|
| 640 |
determination of that type) as described in [[temp.deduct.type]].
|
| 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 |
|
| 646 |
If `A` is not a reference type:
|
| 647 |
|
| 648 |
- If `P` is an array type, the pointer type produced by the
|
| 649 |
+
array-to-pointer standard conversion [[conv.array]] is used in place
|
| 650 |
+
of `P` for type deduction; otherwise,
|
| 651 |
- If `P` is a function type, the pointer type produced by the
|
| 652 |
+
function-to-pointer standard conversion [[conv.func]] is used in place
|
| 653 |
+
of `P` for type deduction; otherwise,
|
| 654 |
- If `P` is a cv-qualified type, the top-level cv-qualifiers of `P`’s
|
| 655 |
type are ignored for type deduction.
|
| 656 |
|
| 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
|
|
|
|
| 661 |
In general, the deduction process attempts to find template argument
|
| 662 |
values that will make the deduced `A` identical to `A`. However, there
|
| 663 |
are four cases that allow a difference:
|
| 664 |
|
| 665 |
- If the original `A` is a reference type, `A` can be more cv-qualified
|
| 666 |
+
than the deduced `A` (i.e., the type referred to by the reference).
|
| 667 |
- If the original `A` is a function pointer type, `A` can be “pointer to
|
| 668 |
+
function” even if the deduced `A` is “pointer to `noexcept` function”.
|
| 669 |
+
- If the original `A` is a pointer-to-member-function type, `A` can be
|
| 670 |
“pointer to member of type function” even if the deduced `A` is
|
| 671 |
+
“pointer to member of type `noexcept` function”.
|
| 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 alternatives are considered only if type deduction would otherwise
|
| 676 |
fail. If they yield more than one possible deduced `A`, the type
|
| 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.
|
| 683 |
|
|
|
|
| 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.[^12]
|
| 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 |
|
| 709 |
Each type nominated above from the parameter template and the
|
| 710 |
corresponding type from the argument template are used as the types of
|
| 711 |
+
`P` and `A`.
|
|
|
|
|
|
|
| 712 |
|
| 713 |
Before the partial ordering is done, certain transformations are
|
| 714 |
performed on the types used for partial ordering:
|
| 715 |
|
| 716 |
- If `P` is a reference type, `P` is replaced by the type referred to.
|
|
|
|
| 754 |
// than the variadic templates #1 and #2
|
| 755 |
```
|
| 756 |
|
| 757 |
— *end example*]
|
| 758 |
|
| 759 |
+
If, for a given type, the types are identical after the transformations
|
| 760 |
+
above and both `P` and `A` were reference types (before being replaced
|
| 761 |
+
with the type referred to above):
|
|
|
|
| 762 |
|
| 763 |
- if the type from the argument template was an lvalue reference and the
|
| 764 |
type from the parameter template was not, the parameter type is not
|
| 765 |
considered to be at least as specialized as the argument type;
|
| 766 |
otherwise,
|
|
|
|
| 775 |
specialized than* `G` if `F` is at least as specialized as `G` and `G`
|
| 776 |
is not at least as specialized as `F`.
|
| 777 |
|
| 778 |
If, after considering the above, function template `F` is at least as
|
| 779 |
specialized as function template `G` and vice-versa, and if `G` has a
|
| 780 |
+
trailing function parameter pack for which `F` does not have a
|
| 781 |
+
corresponding parameter, and if `F` does not have a trailing function
|
| 782 |
+
parameter pack, then `F` is more specialized than `G`.
|
| 783 |
|
| 784 |
+
In most cases, deduction fails if not all template parameters have
|
| 785 |
+
values, but for partial ordering purposes a template parameter may
|
| 786 |
+
remain without a value provided it is not used in the types being used
|
| 787 |
+
for partial ordering.
|
| 788 |
|
| 789 |
[*Note 2*: A template parameter used in a non-deduced context is
|
| 790 |
considered used. — *end note*]
|
| 791 |
|
| 792 |
[*Example 2*:
|
|
|
|
| 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 |
and the return type.
|
| 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.
|
| 855 |
- An array type includes the array element type and the value of the
|
| 856 |
array bound.
|
| 857 |
|
| 858 |
In most cases, the types, templates, and non-type values that are used
|
| 859 |
to compose `P` participate in template argument deduction. That is, they
|
| 860 |
+
may be used to determine the value of a template argument, and template
|
| 861 |
+
argument deduction fails if the value so determined is not consistent
|
| 862 |
+
with the values determined elsewhere. In certain contexts, however, the
|
| 863 |
+
value does not participate in type deduction, but instead uses the
|
| 864 |
+
values of template arguments that were either deduced elsewhere or
|
| 865 |
+
explicitly specified. If a template parameter is used only in
|
| 866 |
+
non-deduced contexts and is not explicitly specified, template argument
|
| 867 |
+
deduction fails.
|
| 868 |
|
| 869 |
+
[*Note 1*: Under [[temp.deduct.call]], if `P` contains no
|
| 870 |
+
*template-parameter*s that appear in deduced contexts, no deduction is
|
| 871 |
+
done, so `P` and `A` need not have the same form. — *end note*]
|
|
|
|
| 872 |
|
| 873 |
The non-deduced contexts are:
|
| 874 |
|
| 875 |
- The *nested-name-specifier* of a type that was specified using a
|
| 876 |
*qualified-id*.
|
|
|
|
| 878 |
- A non-type template argument or an array bound in which a
|
| 879 |
subexpression references a template parameter.
|
| 880 |
- A template parameter used in the parameter type of a function
|
| 881 |
parameter that has a default argument that is being used in the call
|
| 882 |
for which argument deduction is being done.
|
| 883 |
+
- A function parameter for which the associated argument is an overload
|
| 884 |
+
set [[over.over]], and one or more of the following apply:
|
|
|
|
|
|
|
| 885 |
- more than one function matches the function parameter type
|
| 886 |
(resulting in an ambiguous deduction), or
|
| 887 |
- no function matches the function parameter type, or
|
| 888 |
+
- the overload set supplied as an argument contains one or more
|
| 889 |
function templates.
|
| 890 |
- A function parameter for which the associated argument is an
|
| 891 |
+
initializer list [[dcl.init.list]] but the parameter does not have a
|
| 892 |
+
type for which deduction from an initializer list is specified
|
| 893 |
+
[[temp.deduct.call]].
|
| 894 |
\[*Example 1*:
|
| 895 |
``` cpp
|
| 896 |
template<class T> void g(T);
|
| 897 |
g({1,2,3}); // error: no argument deduced for T
|
| 898 |
```
|
|
|
|
| 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[integer-constant]
|
| 992 |
template-name<T> (where template-name refers to a class template)
|
|
|
|
| 1008 |
TT<T>
|
| 1009 |
TT<i>
|
| 1010 |
TT<>
|
| 1011 |
```
|
| 1012 |
|
| 1013 |
+
where `(T)` represents a parameter-type-list [[dcl.fct]] where at least
|
| 1014 |
+
one parameter type contains a `T`, and `()` represents a
|
| 1015 |
parameter-type-list where no parameter type contains a `T`. Similarly,
|
| 1016 |
`<T>` represents template argument lists where at least one argument
|
| 1017 |
contains a `T`, `<i>` represents template argument lists where at least
|
| 1018 |
one argument contains an `i` and `<>` represents template argument lists
|
| 1019 |
where no argument contains a `T` or an `i`.
|
|
|
|
| 1025 |
is not the last template argument, the entire template argument list is
|
| 1026 |
a non-deduced context. If `Pᵢ` is a pack expansion, then the pattern of
|
| 1027 |
`Pᵢ` is compared with each remaining argument in the template argument
|
| 1028 |
list of `A`. Each comparison deduces template arguments for subsequent
|
| 1029 |
positions in the template parameter packs expanded by `Pᵢ`. During
|
| 1030 |
+
partial ordering [[temp.deduct.partial]], if `Aᵢ` was originally a pack
|
| 1031 |
+
expansion:
|
| 1032 |
|
| 1033 |
- if `P` does not contain a template argument corresponding to `Aᵢ` then
|
| 1034 |
`Aᵢ` is ignored;
|
| 1035 |
- otherwise, if `Pᵢ` is not a pack expansion, template argument
|
| 1036 |
deduction fails.
|
|
|
|
| 1050 |
```
|
| 1051 |
|
| 1052 |
— *end example*]
|
| 1053 |
|
| 1054 |
Similarly, if `P` has a form that contains `(T)`, then each parameter
|
| 1055 |
+
type `Pᵢ` of the respective parameter-type-list [[dcl.fct]] of `P` is
|
| 1056 |
compared with the corresponding parameter type `Aᵢ` of the corresponding
|
| 1057 |
parameter-type-list of `A`. If `P` and `A` are function types that
|
| 1058 |
+
originated from deduction when taking the address of a function template
|
| 1059 |
+
[[temp.deduct.funcaddr]] or when deducing template arguments from a
|
| 1060 |
+
function declaration [[temp.deduct.decl]] and `Pᵢ` and `Aᵢ` are
|
| 1061 |
+
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 2*: As a result, when `Pᵢ` is `T&&` and `Aᵢ` is `X&`, the
|
| 1068 |
adjusted `Pᵢ` will be `T`, causing `T` to be deduced as
|
|
|
|
| 1085 |
If the *parameter-declaration* corresponding to `Pᵢ` is a function
|
| 1086 |
parameter pack, then the type of its *declarator-id* is compared with
|
| 1087 |
each remaining parameter type in the parameter-type-list of `A`. Each
|
| 1088 |
comparison deduces template arguments for subsequent positions in the
|
| 1089 |
template parameter packs expanded by the function parameter pack. During
|
| 1090 |
+
partial ordering [[temp.deduct.partial]], if `Aᵢ` was originally a
|
| 1091 |
function parameter pack:
|
| 1092 |
|
| 1093 |
- if `P` does not contain a function parameter type corresponding to
|
| 1094 |
`Aᵢ` then `Aᵢ` is ignored;
|
| 1095 |
- otherwise, if `Pᵢ` is not a function parameter pack, template argument
|
|
|
|
| 1147 |
template<typename T, T n> struct C<A<n>> {
|
| 1148 |
using Q = T;
|
| 1149 |
};
|
| 1150 |
|
| 1151 |
using R = long;
|
| 1152 |
+
using R = C<A<2>>::Q; // OK; T was deduced as long from the
|
| 1153 |
// template argument value in the type A<2>
|
| 1154 |
```
|
| 1155 |
|
| 1156 |
— *end example*]
|
| 1157 |
|
|
|
|
| 1164 |
template<typename T, T n> struct S<int[n]> {
|
| 1165 |
using Q = T;
|
| 1166 |
};
|
| 1167 |
|
| 1168 |
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*:
|
|
|
|
| 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: i deduced as 20
|
| 1199 |
f1<20>(v); // OK
|
| 1200 |
f2(v); // error: cannot deduce template-argument i
|
| 1201 |
f2<10>(v); // OK
|
| 1202 |
+
f3(v); // OK: i deduced as 10
|
| 1203 |
}
|
| 1204 |
```
|
| 1205 |
|
| 1206 |
— *end note*]
|
| 1207 |
|
|
|
|
| 1243 |
typename B<i>::Y y); // i is not deduced here
|
| 1244 |
A<int> a;
|
| 1245 |
B<77> b;
|
| 1246 |
|
| 1247 |
int x = deduce<77>(a.xm, 62, b.ym);
|
| 1248 |
+
// T deduced as int; a.xm must be convertible to A<int>::X
|
| 1249 |
+
// i is explicitly specified to be 77; b.ym must be convertible to B<77>::Y
|
| 1250 |
```
|
| 1251 |
|
| 1252 |
— *end note*]
|
| 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.[^13]
|
| 1259 |
|
| 1260 |
[*Example 12*:
|
| 1261 |
|
| 1262 |
``` cpp
|
| 1263 |
template<int i> class A { ... };
|
|
|
|
| 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 13*:
|
| 1285 |
|
| 1286 |
``` cpp
|
| 1287 |
template<class T> void f(void(*)(T,int));
|
|
|
|
| 1330 |
f(ab); // calls f(A<B>)
|
| 1331 |
```
|
| 1332 |
|
| 1333 |
— *end example*]
|
| 1334 |
|
| 1335 |
+
[*Note 6*: Template argument deduction involving parameter packs
|
| 1336 |
+
[[temp.variadic]] can deduce zero or more arguments for each parameter
|
| 1337 |
pack. — *end note*]
|
| 1338 |
|
| 1339 |
[*Example 16*:
|
| 1340 |
|
| 1341 |
``` cpp
|
|
|
|
| 1361 |
#### Deducing template arguments from a function declaration <a id="temp.deduct.decl">[[temp.deduct.decl]]</a>
|
| 1362 |
|
| 1363 |
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]]). In all these cases,
|
| 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]].
|
| 1376 |
|
| 1377 |
If, for the set of function templates so considered, there is either no
|
| 1378 |
+
match or more than one match after partial ordering has been considered
|
| 1379 |
+
[[temp.func.order]], deduction fails and, in the declaration cases, the
|
| 1380 |
+
program is ill-formed.
|
| 1381 |
|