- tmp/tmpnun4yio0/{from.md → to.md} +475 -258
tmp/tmpnun4yio0/{from.md → to.md}
RENAMED
|
@@ -30,13 +30,14 @@ Here `f<int>(int*)` has a static variable `s` of type `int` and
|
|
| 30 |
— *end example*]
|
| 31 |
|
| 32 |
### Explicit template argument specification <a id="temp.arg.explicit">[[temp.arg.explicit]]</a>
|
| 33 |
|
| 34 |
Template arguments can be specified when referring to a function
|
| 35 |
-
template specialization
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
[*Example 1*:
|
| 40 |
|
| 41 |
``` cpp
|
| 42 |
template<class T> void sort(Array<T>& v);
|
|
@@ -57,24 +58,28 @@ void g(double d) {
|
|
| 57 |
}
|
| 58 |
```
|
| 59 |
|
| 60 |
— *end example*]
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
A template argument list may be specified when referring to a
|
| 63 |
specialization of a function template
|
| 64 |
|
| 65 |
- when a function is called,
|
| 66 |
- when the address of a function is taken, when a function initializes a
|
| 67 |
reference to function, or when a pointer to member function is formed,
|
| 68 |
- in an explicit specialization,
|
| 69 |
- in an explicit instantiation, or
|
| 70 |
- in a friend declaration.
|
| 71 |
|
| 72 |
-
Trailing template arguments that can be deduced
|
| 73 |
obtained from default *template-argument*s may be omitted from the list
|
| 74 |
-
of explicit *template-argument*s. A trailing template parameter pack
|
| 75 |
-
[[temp.variadic]]
|
| 76 |
sequence of template arguments. If all of the template arguments can be
|
| 77 |
deduced, they may all be omitted; in this case, the empty template
|
| 78 |
argument list `<>` itself may also be omitted. In contexts where
|
| 79 |
deduction is done and fails, or in contexts where deduction is not done,
|
| 80 |
if a template argument list is specified and it, along with any default
|
|
@@ -86,27 +91,27 @@ template specialization.
|
|
| 86 |
|
| 87 |
``` cpp
|
| 88 |
template<class X, class Y> X f(Y);
|
| 89 |
template<class X, class Y, class ... Z> X g(Y);
|
| 90 |
void h() {
|
| 91 |
-
int i = f<int>(5.6); // Y
|
| 92 |
-
int j = f(5.6); //
|
| 93 |
-
f<void>(f<int, bool>); // Y for outer f deduced
|
| 94 |
-
f<void>(f<int>); //
|
| 95 |
-
int k = g<int>(5.6); // Y
|
| 96 |
-
f<void>(g<int, bool>); // Y for outer f
|
| 97 |
-
// Z
|
| 98 |
}
|
| 99 |
```
|
| 100 |
|
| 101 |
— *end example*]
|
| 102 |
|
| 103 |
[*Note 1*:
|
| 104 |
|
| 105 |
An empty template argument list can be used to indicate that a given use
|
| 106 |
refers to a specialization of a function template even when a
|
| 107 |
-
non-template function
|
| 108 |
used. For example:
|
| 109 |
|
| 110 |
``` cpp
|
| 111 |
template <class T> int f(T); // #1
|
| 112 |
int f(int); // #2
|
|
@@ -127,23 +132,23 @@ there are corresponding *template-parameter*s unless one of the
|
|
| 127 |
``` cpp
|
| 128 |
template<class X, class Y, class Z> X f(Y,Z);
|
| 129 |
template<class ... Args> void f2();
|
| 130 |
void g() {
|
| 131 |
f<int,const char*,double>("aa",3.0);
|
| 132 |
-
f<int,const char*>("aa",3.0); // Z
|
| 133 |
-
f<int>("aa",3.0); // Y
|
| 134 |
f("aa",3.0); // error: X cannot be deduced
|
| 135 |
f2<char, short, int, long>(); // OK
|
| 136 |
}
|
| 137 |
```
|
| 138 |
|
| 139 |
— *end example*]
|
| 140 |
|
| 141 |
-
Implicit conversions
|
| 142 |
-
|
| 143 |
-
parameter
|
| 144 |
-
|
| 145 |
|
| 146 |
[*Note 2*:
|
| 147 |
|
| 148 |
Template parameters do not participate in template argument deduction if
|
| 149 |
they are explicitly specified. For example,
|
|
@@ -161,63 +166,26 @@ void g() {
|
|
| 161 |
```
|
| 162 |
|
| 163 |
— *end note*]
|
| 164 |
|
| 165 |
[*Note 3*: Because the explicit template argument list follows the
|
| 166 |
-
function template name, and because
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
[*Note 4*:
|
| 172 |
-
|
| 173 |
-
For simple function names, argument dependent lookup (
|
| 174 |
-
[[basic.lookup.argdep]]) applies even when the function name is not
|
| 175 |
-
visible within the scope of the call. This is because the call still has
|
| 176 |
-
the syntactic form of a function call ([[basic.lookup.unqual]]). But
|
| 177 |
-
when a function template with explicit template arguments is used, the
|
| 178 |
-
call does not have the correct syntactic form unless there is a function
|
| 179 |
-
template with that name visible at the point of the call. If no such
|
| 180 |
-
name is visible, the call is not syntactically well-formed and
|
| 181 |
-
argument-dependent lookup does not apply. If some such name is visible,
|
| 182 |
-
argument dependent lookup applies and additional function templates may
|
| 183 |
-
be found in other namespaces.
|
| 184 |
-
|
| 185 |
-
[*Example 4*:
|
| 186 |
-
|
| 187 |
-
``` cpp
|
| 188 |
-
namespace A {
|
| 189 |
-
struct B { };
|
| 190 |
-
template<int X> void f(B);
|
| 191 |
-
}
|
| 192 |
-
namespace C {
|
| 193 |
-
template<class T> void f(T t);
|
| 194 |
-
}
|
| 195 |
-
void g(A::B b) {
|
| 196 |
-
f<3>(b); // ill-formed: not a function call
|
| 197 |
-
A::f<3>(b); // well-formed
|
| 198 |
-
C::f<3>(b); // ill-formed; argument dependent lookup applies only to unqualified names
|
| 199 |
-
using C::f;
|
| 200 |
-
f<3>(b); // well-formed because C::f is visible; then A::f is found by argument dependent lookup
|
| 201 |
-
}
|
| 202 |
-
```
|
| 203 |
-
|
| 204 |
-
— *end example*]
|
| 205 |
-
|
| 206 |
-
— *end note*]
|
| 207 |
|
| 208 |
Template argument deduction can extend the sequence of template
|
| 209 |
arguments corresponding to a template parameter pack, even when the
|
| 210 |
sequence contains explicitly specified template arguments.
|
| 211 |
|
| 212 |
-
[*Example
|
| 213 |
|
| 214 |
``` cpp
|
| 215 |
template<class ... Types> void f(Types ... values);
|
| 216 |
|
| 217 |
void g() {
|
| 218 |
-
f<int*, float*>(0, 0, 0); // Types
|
| 219 |
}
|
| 220 |
```
|
| 221 |
|
| 222 |
— *end example*]
|
| 223 |
|
|
@@ -246,28 +214,14 @@ void g(double d) {
|
|
| 246 |
}
|
| 247 |
```
|
| 248 |
|
| 249 |
— *end example*]
|
| 250 |
|
| 251 |
-
When an explicit template argument list is specified, the
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
evaluating an explicitly specified template argument list with respect
|
| 256 |
-
to a given function template:
|
| 257 |
-
|
| 258 |
-
- The specified template arguments must match the template parameters in
|
| 259 |
-
kind (i.e., type, non-type, template). There must not be more
|
| 260 |
-
arguments than there are parameters unless at least one parameter is a
|
| 261 |
-
template parameter pack, and there shall be an argument for each
|
| 262 |
-
non-pack parameter. Otherwise, type deduction fails.
|
| 263 |
-
- Non-type arguments must match the types of the corresponding non-type
|
| 264 |
-
template parameters, or must be convertible to the types of the
|
| 265 |
-
corresponding non-type parameters as specified in
|
| 266 |
-
[[temp.arg.nontype]], otherwise type deduction fails.
|
| 267 |
-
- The specified template argument values are substituted for the
|
| 268 |
-
corresponding template parameters as specified below.
|
| 269 |
|
| 270 |
After this substitution is performed, the function parameter type
|
| 271 |
adjustments described in [[dcl.fct]] are performed.
|
| 272 |
|
| 273 |
[*Example 2*: A parameter type of “`void (const int, int[5])`” becomes
|
|
@@ -336,10 +290,13 @@ void g() {
|
|
| 336 |
When all template arguments have been deduced or obtained from default
|
| 337 |
template arguments, all uses of template parameters in the template
|
| 338 |
parameter list of the template and the function type are replaced with
|
| 339 |
the corresponding deduced or default argument values. If the
|
| 340 |
substitution results in an invalid type, as described above, type
|
|
|
|
|
|
|
|
|
|
| 341 |
deduction fails.
|
| 342 |
|
| 343 |
At certain points in the template argument deduction process it is
|
| 344 |
necessary to take a function type that makes use of template parameters
|
| 345 |
and replace those template parameters with the corresponding template
|
|
@@ -354,11 +311,14 @@ the function type and in template parameter declarations. The
|
|
| 354 |
expressions include not only constant expressions such as those that
|
| 355 |
appear in array bounds or as nontype template arguments but also general
|
| 356 |
expressions (i.e., non-constant expressions) inside `sizeof`,
|
| 357 |
`decltype`, and other contexts that allow non-constant expressions. The
|
| 358 |
substitution proceeds in lexical order and stops when a condition that
|
| 359 |
-
causes deduction to fail is encountered.
|
|
|
|
|
|
|
|
|
|
| 360 |
|
| 361 |
[*Note 3*: The equivalent substitution in exception specifications is
|
| 362 |
done only when the *noexcept-specifier* is instantiated, at which point
|
| 363 |
a program is ill-formed if the substitution results in an invalid type
|
| 364 |
or expression. — *end note*]
|
|
@@ -369,14 +329,18 @@ or expression. — *end note*]
|
|
| 369 |
template <class T> struct A { using X = typename T::X; };
|
| 370 |
template <class T> typename T::X f(typename A<T>::X);
|
| 371 |
template <class T> void f(...) { }
|
| 372 |
template <class T> auto g(typename A<T>::X) -> typename T::X;
|
| 373 |
template <class T> void g(...) { }
|
|
|
|
|
|
|
|
|
|
| 374 |
|
| 375 |
-
void
|
| 376 |
f<int>(0); // OK, substituting return type causes deduction to fail
|
| 377 |
g<int>(0); // error, substituting parameter type instantiates A<int>
|
|
|
|
| 378 |
}
|
| 379 |
```
|
| 380 |
|
| 381 |
— *end example*]
|
| 382 |
|
|
@@ -388,22 +352,64 @@ arguments.
|
|
| 388 |
[*Note 4*: If no diagnostic is required, the program is still
|
| 389 |
ill-formed. Access checking is done as part of the substitution
|
| 390 |
process. — *end note*]
|
| 391 |
|
| 392 |
Only invalid types and expressions in the immediate context of the
|
| 393 |
-
function type
|
| 394 |
-
failure.
|
| 395 |
|
| 396 |
[*Note 5*: The substitution into types and expressions can result in
|
| 397 |
effects such as the instantiation of class template specializations
|
| 398 |
and/or function template specializations, the generation of
|
| 399 |
implicitly-defined functions, etc. Such effects are not in the
|
| 400 |
“immediate context” and can result in the program being
|
| 401 |
ill-formed. — *end note*]
|
| 402 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 403 |
[*Example 6*:
|
| 404 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 405 |
``` cpp
|
| 406 |
struct X { };
|
| 407 |
struct Y {
|
| 408 |
Y(X){}
|
| 409 |
};
|
|
@@ -415,30 +421,30 @@ X x1, x2;
|
|
| 415 |
X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
|
| 416 |
```
|
| 417 |
|
| 418 |
— *end example*]
|
| 419 |
|
| 420 |
-
[*Note
|
| 421 |
|
| 422 |
Type deduction may fail for the following reasons:
|
| 423 |
|
| 424 |
-
- Attempting to instantiate a pack expansion containing multiple
|
| 425 |
-
|
| 426 |
- Attempting to create an array with an element type that is `void`, a
|
| 427 |
-
function type, a reference type, or
|
| 428 |
-
|
| 429 |
-
\[*Example
|
| 430 |
``` cpp
|
| 431 |
template <class T> int f(T[5]);
|
| 432 |
int I = f<int>(0);
|
| 433 |
int j = f<void>(0); // invalid array
|
| 434 |
```
|
| 435 |
|
| 436 |
— *end example*]
|
| 437 |
- Attempting to use a type that is not a class or enumeration type in a
|
| 438 |
qualified name.
|
| 439 |
-
\[*Example
|
| 440 |
``` cpp
|
| 441 |
template <class T> int f(typename T::B*);
|
| 442 |
int i = f<int>(0);
|
| 443 |
```
|
| 444 |
|
|
@@ -449,11 +455,11 @@ Type deduction may fail for the following reasons:
|
|
| 449 |
- the specified member is not a type where a type is required, or
|
| 450 |
- the specified member is not a template where a template is required,
|
| 451 |
or
|
| 452 |
- the specified member is not a non-type where a non-type is required.
|
| 453 |
|
| 454 |
-
\[*Example
|
| 455 |
``` cpp
|
| 456 |
template <int I> struct X { };
|
| 457 |
template <template <class T> class> struct Z { };
|
| 458 |
template <class T> void f(typename T::Y*){}
|
| 459 |
template <class T> void g(X<T::N>*){}
|
|
@@ -479,19 +485,19 @@ Type deduction may fail for the following reasons:
|
|
| 479 |
— *end example*]
|
| 480 |
- Attempting to create a pointer to reference type.
|
| 481 |
- Attempting to create a reference to `void`.
|
| 482 |
- Attempting to create “pointer to member of `T`” when `T` is not a
|
| 483 |
class type.
|
| 484 |
-
\[*Example
|
| 485 |
``` cpp
|
| 486 |
template <class T> int f(int T::*);
|
| 487 |
int i = f<int>(0);
|
| 488 |
```
|
| 489 |
|
| 490 |
— *end example*]
|
| 491 |
- Attempting to give an invalid type to a non-type template parameter.
|
| 492 |
-
\[*Example
|
| 493 |
``` cpp
|
| 494 |
template <class T, T> struct S {};
|
| 495 |
template <class T> int f(S<T, T()>*);
|
| 496 |
struct X {};
|
| 497 |
int i0 = f<X>(0);
|
|
@@ -499,32 +505,30 @@ Type deduction may fail for the following reasons:
|
|
| 499 |
|
| 500 |
— *end example*]
|
| 501 |
- Attempting to perform an invalid conversion in either a template
|
| 502 |
argument expression, or an expression used in the function
|
| 503 |
declaration.
|
| 504 |
-
\[*Example
|
| 505 |
``` cpp
|
| 506 |
template <class T, T*> int f(int);
|
| 507 |
int i2 = f<int,1>(0); // can't conv 1 to int*
|
| 508 |
```
|
| 509 |
|
| 510 |
— *end example*]
|
| 511 |
- Attempting to create a function type in which a parameter has a type
|
| 512 |
of `void`, or in which the return type is a function type or array
|
| 513 |
type.
|
| 514 |
-
- Attempting to create a function type in which a parameter type or the
|
| 515 |
-
return type is an abstract class type ([[class.abstract]]).
|
| 516 |
|
| 517 |
— *end note*]
|
| 518 |
|
| 519 |
-
[*Example
|
| 520 |
|
| 521 |
In the following example, assuming a `signed char` cannot represent the
|
| 522 |
-
value 1000, a narrowing conversion
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
[[temp.arg.nontype]]
|
| 526 |
|
| 527 |
``` cpp
|
| 528 |
template <int> int f(int);
|
| 529 |
template <signed char> int f(int);
|
| 530 |
int i1 = f<1000>(0); // OK
|
|
@@ -538,84 +542,93 @@ int i2 = f<1>(0); // ambiguous; not narrowing
|
|
| 538 |
Template argument deduction is done by comparing each function template
|
| 539 |
parameter type (call it `P`) that contains *template-parameter*s that
|
| 540 |
participate in template argument deduction with the type of the
|
| 541 |
corresponding argument of the call (call it `A`) as described below. If
|
| 542 |
removing references and cv-qualifiers from `P` gives
|
| 543 |
-
`std::initializer_list<P
|
| 544 |
-
argument is a non-empty initializer list
|
| 545 |
-
deduction is performed instead for each element of the initializer list
|
| 546 |
-
taking `P
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
|
|
|
| 551 |
|
| 552 |
[*Example 1*:
|
| 553 |
|
| 554 |
``` cpp
|
| 555 |
template<class T> void f(std::initializer_list<T>);
|
| 556 |
-
f({1,2,3}); // T deduced
|
| 557 |
-
f({1,"asdf"}); // error: T deduced
|
| 558 |
|
| 559 |
template<class T> void g(T);
|
| 560 |
g({1,2,3}); // error: no argument deduced for T
|
| 561 |
|
| 562 |
template<class T, int N> void h(T const(&)[N]);
|
| 563 |
-
h({1,2,3}); // T deduced
|
| 564 |
|
| 565 |
template<class T> void j(T const(&)[3]);
|
| 566 |
-
j({42}); // T deduced
|
| 567 |
|
| 568 |
struct Aggr { int i; int j; };
|
| 569 |
template<int N> void k(Aggr const(&)[N]);
|
| 570 |
k({1,2,3}); // error: deduction fails, no conversion from int to Aggr
|
| 571 |
-
k({{1},{2},{3}}); // OK, N deduced
|
| 572 |
|
| 573 |
template<int M, int N> void m(int const(&)[M][N]);
|
| 574 |
-
m({{1,2},{3,4}}); // M and N both deduced
|
| 575 |
|
| 576 |
template<class T, int N> void n(T const(&)[N], T);
|
| 577 |
n({{1},{2},{3}},Aggr()); // OK, T is Aggr, N is 3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 578 |
```
|
| 579 |
|
| 580 |
— *end example*]
|
| 581 |
|
| 582 |
For a function parameter pack that occurs at the end of the
|
| 583 |
*parameter-declaration-list*, deduction is performed for each remaining
|
| 584 |
argument of the call, taking the type `P` of the *declarator-id* of the
|
| 585 |
function parameter pack as the corresponding function template parameter
|
| 586 |
type. Each deduction deduces template arguments for subsequent positions
|
| 587 |
in the template parameter packs expanded by the function parameter pack.
|
| 588 |
-
When a function parameter pack appears in a non-deduced context
|
| 589 |
-
[[temp.deduct.type]]
|
| 590 |
|
| 591 |
[*Example 2*:
|
| 592 |
|
| 593 |
``` cpp
|
| 594 |
template<class ... Types> void f(Types& ...);
|
| 595 |
template<class T1, class ... Types> void g(T1, Types ...);
|
| 596 |
template<class T1, class ... Types> void g1(Types ..., T1);
|
| 597 |
|
| 598 |
void h(int x, float& y) {
|
| 599 |
const int z = x;
|
| 600 |
-
f(x, y, z); // Types
|
| 601 |
-
g(x, y, z); // T1
|
| 602 |
g1(x, y, z); // error: Types is not deduced
|
| 603 |
g1<int, int, int>(x, y, z); // OK, no deduction occurs
|
| 604 |
}
|
| 605 |
```
|
| 606 |
|
| 607 |
— *end example*]
|
| 608 |
|
| 609 |
If `P` is not a reference type:
|
| 610 |
|
| 611 |
- If `A` is an array type, the pointer type produced by the
|
| 612 |
-
array-to-pointer standard conversion
|
| 613 |
-
|
| 614 |
- If `A` is a function type, the pointer type produced by the
|
| 615 |
-
function-to-pointer standard conversion
|
| 616 |
-
|
| 617 |
- If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s
|
| 618 |
type are ignored for type deduction.
|
| 619 |
|
| 620 |
If `P` is a cv-qualified type, the top-level cv-qualifiers of `P`’s type
|
| 621 |
are ignored for type deduction. If `P` is a reference type, the type
|
|
@@ -634,12 +647,12 @@ int n3 = g(i); // calls g<const int>(const volatile int&)
|
|
| 634 |
|
| 635 |
— *end example*]
|
| 636 |
|
| 637 |
A *forwarding reference* is an rvalue reference to a cv-unqualified
|
| 638 |
template parameter that does not represent a template parameter of a
|
| 639 |
-
class template (during class template argument deduction
|
| 640 |
-
[[over.match.class.deduct]])
|
| 641 |
argument is an lvalue, the type “lvalue reference to `A`” is used in
|
| 642 |
place of `A` for type deduction.
|
| 643 |
|
| 644 |
[*Example 4*:
|
| 645 |
|
|
@@ -675,19 +688,38 @@ values that will make the deduced `A` identical to `A` (after the type
|
|
| 675 |
that allow a difference:
|
| 676 |
|
| 677 |
- If the original `P` is a reference type, the deduced `A` (i.e., the
|
| 678 |
type referred to by the reference) can be more cv-qualified than the
|
| 679 |
transformed `A`.
|
| 680 |
-
- The transformed `A` can be another pointer or pointer
|
| 681 |
that can be converted to the deduced `A` via a function pointer
|
| 682 |
-
conversion
|
| 683 |
-
[[conv.qual]]
|
| 684 |
- If `P` is a class and `P` has the form *simple-template-id*, then the
|
| 685 |
-
transformed `A` can be a derived class of the deduced `A`.
|
| 686 |
-
if `P` is a pointer to a class of the form
|
| 687 |
-
transformed `A` can be a pointer to a
|
| 688 |
-
deduced `A`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 689 |
|
| 690 |
These alternatives are considered only if type deduction would otherwise
|
| 691 |
fail. If they yield more than one possible deduced `A`, the type
|
| 692 |
deduction fails.
|
| 693 |
|
|
@@ -695,23 +727,23 @@ deduction fails.
|
|
| 695 |
parameters of a function template, or is used only in a non-deduced
|
| 696 |
context, its corresponding *template-argument* cannot be deduced from a
|
| 697 |
function call and the *template-argument* must be explicitly
|
| 698 |
specified. — *end note*]
|
| 699 |
|
| 700 |
-
When `P` is a function type, function pointer type, or
|
| 701 |
-
function type:
|
| 702 |
|
| 703 |
- If the argument is an overload set containing one or more function
|
| 704 |
templates, the parameter is treated as a non-deduced context.
|
| 705 |
- If the argument is an overload set (not containing function
|
| 706 |
templates), trial argument deduction is attempted using each of the
|
| 707 |
members of the set. If deduction succeeds for only one of the overload
|
| 708 |
set members, that member is used as the argument value for the
|
| 709 |
deduction. If deduction succeeds for more than one member of the
|
| 710 |
overload set the parameter is treated as a non-deduced context.
|
| 711 |
|
| 712 |
-
[*Example
|
| 713 |
|
| 714 |
``` cpp
|
| 715 |
// Only one function of an overload set matches the call so the function parameter is a deduced context.
|
| 716 |
template <class T> int f(T (*p)(T));
|
| 717 |
int g(int);
|
|
@@ -719,11 +751,11 @@ int g(char);
|
|
| 719 |
int i = f(g); // calls f(int (*)(int))
|
| 720 |
```
|
| 721 |
|
| 722 |
— *end example*]
|
| 723 |
|
| 724 |
-
[*Example
|
| 725 |
|
| 726 |
``` cpp
|
| 727 |
// Ambiguous deduction causes the second function parameter to be a non-deduced context.
|
| 728 |
template <class T> int f(T, T (*p)(T));
|
| 729 |
int g(int);
|
|
@@ -731,11 +763,11 @@ char g(char);
|
|
| 731 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 732 |
```
|
| 733 |
|
| 734 |
— *end example*]
|
| 735 |
|
| 736 |
-
[*Example
|
| 737 |
|
| 738 |
``` cpp
|
| 739 |
// The overload set contains a template, causing the second function parameter to be a non-deduced context.
|
| 740 |
template <class T> int f(T, T (*p)(T));
|
| 741 |
char g(char);
|
|
@@ -758,11 +790,11 @@ explicitly-specified template arguments, if the corresponding argument
|
|
| 758 |
*template-parameter*s participate in template argument deduction, and
|
| 759 |
parameters that became non-dependent due to substitution of
|
| 760 |
explicitly-specified template arguments, will be checked during overload
|
| 761 |
resolution. — *end note*]
|
| 762 |
|
| 763 |
-
[*Example
|
| 764 |
|
| 765 |
``` cpp
|
| 766 |
template <class T> struct Z {
|
| 767 |
typedef typename T::x xx;
|
| 768 |
};
|
|
@@ -777,16 +809,17 @@ resolution. — *end note*]
|
|
| 777 |
— *end example*]
|
| 778 |
|
| 779 |
#### Deducing template arguments taking the address of a function template <a id="temp.deduct.funcaddr">[[temp.deduct.funcaddr]]</a>
|
| 780 |
|
| 781 |
Template arguments can be deduced from the type specified when taking
|
| 782 |
-
the address of an overloaded function
|
| 783 |
-
template’s function type and the
|
| 784 |
-
`P` and `A`, and the deduction is done as described
|
| 785 |
-
[[temp.deduct.type]].
|
|
|
|
| 786 |
|
| 787 |
-
A placeholder type
|
| 788 |
template is a non-deduced context. If template argument deduction
|
| 789 |
succeeds for such a function, the return type is determined from
|
| 790 |
instantiation of the function body.
|
| 791 |
|
| 792 |
#### Deducing conversion function template arguments <a id="temp.deduct.conv">[[temp.deduct.conv]]</a>
|
|
@@ -797,20 +830,20 @@ required as the result of the conversion (call it `A`; see
|
|
| 797 |
[[dcl.init]], [[over.match.conv]], and [[over.match.ref]] for the
|
| 798 |
determination of that type) as described in [[temp.deduct.type]].
|
| 799 |
|
| 800 |
If `P` is a reference type, the type referred to by `P` is used in place
|
| 801 |
of `P` for type deduction and for any further references to or
|
| 802 |
-
transformations of `P` in the remainder of this
|
| 803 |
|
| 804 |
If `A` is not a reference type:
|
| 805 |
|
| 806 |
- If `P` is an array type, the pointer type produced by the
|
| 807 |
-
array-to-pointer standard conversion
|
| 808 |
-
|
| 809 |
- If `P` is a function type, the pointer type produced by the
|
| 810 |
-
function-to-pointer standard conversion
|
| 811 |
-
|
| 812 |
- If `P` is a cv-qualified type, the top-level cv-qualifiers of `P`’s
|
| 813 |
type are ignored for type deduction.
|
| 814 |
|
| 815 |
If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s type
|
| 816 |
are ignored for type deduction. If `A` is a reference type, the type
|
|
@@ -819,46 +852,23 @@ referred to by `A` is used for type deduction.
|
|
| 819 |
In general, the deduction process attempts to find template argument
|
| 820 |
values that will make the deduced `A` identical to `A`. However, there
|
| 821 |
are four cases that allow a difference:
|
| 822 |
|
| 823 |
- If the original `A` is a reference type, `A` can be more cv-qualified
|
| 824 |
-
than the deduced `A` (i.e., the type referred to by the reference)
|
| 825 |
- If the original `A` is a function pointer type, `A` can be “pointer to
|
| 826 |
-
function” even if the deduced `A` is “pointer to noexcept function”.
|
| 827 |
-
- If the original `A` is a pointer
|
| 828 |
“pointer to member of type function” even if the deduced `A` is
|
| 829 |
-
“pointer to member of type noexcept function”.
|
| 830 |
-
- The deduced `A` can be another pointer or pointer
|
| 831 |
can be converted to `A` via a qualification conversion.
|
| 832 |
|
| 833 |
These alternatives are considered only if type deduction would otherwise
|
| 834 |
fail. If they yield more than one possible deduced `A`, the type
|
| 835 |
deduction fails.
|
| 836 |
|
| 837 |
-
When the deduction process requires a qualification conversion for a
|
| 838 |
-
pointer or pointer to member type as described above, the following
|
| 839 |
-
process is used to determine the deduced template argument values:
|
| 840 |
-
|
| 841 |
-
If `A` is a type
|
| 842 |
-
|
| 843 |
-
and `P` is a type
|
| 844 |
-
|
| 845 |
-
then the cv-unqualified `T1` and `T2` are used as the types of `A` and
|
| 846 |
-
`P` respectively for type deduction.
|
| 847 |
-
|
| 848 |
-
[*Example 1*:
|
| 849 |
-
|
| 850 |
-
``` cpp
|
| 851 |
-
struct A {
|
| 852 |
-
template <class T> operator T***();
|
| 853 |
-
};
|
| 854 |
-
A a;
|
| 855 |
-
const int * const * const * p1 = a; // T is deduced as int, not const int
|
| 856 |
-
```
|
| 857 |
-
|
| 858 |
-
— *end example*]
|
| 859 |
-
|
| 860 |
#### Deducing template arguments during partial ordering <a id="temp.deduct.partial">[[temp.deduct.partial]]</a>
|
| 861 |
|
| 862 |
Template argument deduction is done by comparing certain types
|
| 863 |
associated with the two function templates being compared.
|
| 864 |
|
|
@@ -879,21 +889,19 @@ as the parameter template.
|
|
| 879 |
|
| 880 |
The types used to determine the ordering depend on the context in which
|
| 881 |
the partial ordering is done:
|
| 882 |
|
| 883 |
- In the context of a function call, the types used are those function
|
| 884 |
-
parameter types for which the function call has arguments.[^
|
| 885 |
- In the context of a call to a conversion function, the return types of
|
| 886 |
the conversion function templates are used.
|
| 887 |
-
- In other contexts
|
| 888 |
-
|
| 889 |
|
| 890 |
Each type nominated above from the parameter template and the
|
| 891 |
corresponding type from the argument template are used as the types of
|
| 892 |
-
`P` and `A`.
|
| 893 |
-
participate in template argument deduction, that `P` is not used to
|
| 894 |
-
determine the ordering.
|
| 895 |
|
| 896 |
Before the partial ordering is done, certain transformations are
|
| 897 |
performed on the types used for partial ordering:
|
| 898 |
|
| 899 |
- If `P` is a reference type, `P` is replaced by the type referred to.
|
|
@@ -937,14 +945,13 @@ f(1, 2); // calls #3; non-variadic template #3 is more specialized
|
|
| 937 |
// than the variadic templates #1 and #2
|
| 938 |
```
|
| 939 |
|
| 940 |
— *end example*]
|
| 941 |
|
| 942 |
-
If, for a given type,
|
| 943 |
-
|
| 944 |
-
|
| 945 |
-
to above):
|
| 946 |
|
| 947 |
- if the type from the argument template was an lvalue reference and the
|
| 948 |
type from the parameter template was not, the parameter type is not
|
| 949 |
considered to be at least as specialized as the argument type;
|
| 950 |
otherwise,
|
|
@@ -959,18 +966,18 @@ from `F` is at least as specialized as the type from `G`. `F` is *more
|
|
| 959 |
specialized than* `G` if `F` is at least as specialized as `G` and `G`
|
| 960 |
is not at least as specialized as `F`.
|
| 961 |
|
| 962 |
If, after considering the above, function template `F` is at least as
|
| 963 |
specialized as function template `G` and vice-versa, and if `G` has a
|
| 964 |
-
trailing parameter pack for which `F` does not have a
|
| 965 |
-
parameter, and if `F` does not have a trailing
|
| 966 |
-
is more specialized than `G`.
|
| 967 |
|
| 968 |
-
In most cases,
|
| 969 |
-
|
| 970 |
-
|
| 971 |
-
|
| 972 |
|
| 973 |
[*Note 2*: A template parameter used in a non-deduced context is
|
| 974 |
considered used. — *end note*]
|
| 975 |
|
| 976 |
[*Example 2*:
|
|
@@ -1029,32 +1036,32 @@ array bound if it is not otherwise deduced.
|
|
| 1029 |
A given type `P` can be composed from a number of other types,
|
| 1030 |
templates, and non-type values:
|
| 1031 |
|
| 1032 |
- A function type includes the types of each of the function parameters
|
| 1033 |
and the return type.
|
| 1034 |
-
- A pointer
|
| 1035 |
to and the type of the member pointed to.
|
| 1036 |
- A type that is a specialization of a class template (e.g., `A<int>`)
|
| 1037 |
includes the types, templates, and non-type values referenced by the
|
| 1038 |
template argument list of the specialization.
|
| 1039 |
- An array type includes the array element type and the value of the
|
| 1040 |
array bound.
|
| 1041 |
|
| 1042 |
In most cases, the types, templates, and non-type values that are used
|
| 1043 |
to compose `P` participate in template argument deduction. That is, they
|
| 1044 |
-
may be used to determine the value of a template argument, and
|
| 1045 |
-
|
| 1046 |
-
In certain contexts, however, the
|
| 1047 |
-
|
| 1048 |
-
|
| 1049 |
-
|
| 1050 |
-
specified, template argument
|
|
|
|
| 1051 |
|
| 1052 |
-
[*Note 1*: Under [[temp.deduct.call]]
|
| 1053 |
-
|
| 1054 |
-
|
| 1055 |
-
form. — *end note*]
|
| 1056 |
|
| 1057 |
The non-deduced contexts are:
|
| 1058 |
|
| 1059 |
- The *nested-name-specifier* of a type that was specified using a
|
| 1060 |
*qualified-id*.
|
|
@@ -1062,23 +1069,21 @@ The non-deduced contexts are:
|
|
| 1062 |
- A non-type template argument or an array bound in which a
|
| 1063 |
subexpression references a template parameter.
|
| 1064 |
- A template parameter used in the parameter type of a function
|
| 1065 |
parameter that has a default argument that is being used in the call
|
| 1066 |
for which argument deduction is being done.
|
| 1067 |
-
- A function parameter for which
|
| 1068 |
-
|
| 1069 |
-
overloaded functions ([[over.over]]), and one or more of the
|
| 1070 |
-
following apply:
|
| 1071 |
- more than one function matches the function parameter type
|
| 1072 |
(resulting in an ambiguous deduction), or
|
| 1073 |
- no function matches the function parameter type, or
|
| 1074 |
-
- the set
|
| 1075 |
function templates.
|
| 1076 |
- A function parameter for which the associated argument is an
|
| 1077 |
-
initializer list
|
| 1078 |
-
|
| 1079 |
-
[[temp.deduct.call]]
|
| 1080 |
\[*Example 1*:
|
| 1081 |
``` cpp
|
| 1082 |
template<class T> void g(T);
|
| 1083 |
g({1,2,3}); // error: no argument deduced for T
|
| 1084 |
```
|
|
@@ -1168,11 +1173,11 @@ A template type argument `T`, a template template argument `TT` or a
|
|
| 1168 |
template non-type argument `i` can be deduced if `P` and `A` have one of
|
| 1169 |
the following forms:
|
| 1170 |
|
| 1171 |
``` cpp
|
| 1172 |
T
|
| 1173 |
-
cv
|
| 1174 |
T*
|
| 1175 |
T&
|
| 1176 |
T&&
|
| 1177 |
T[integer-constant]
|
| 1178 |
template-name<T> (where template-name refers to a class template)
|
|
@@ -1194,12 +1199,12 @@ template-name<i> (where template-name refers to a class template)
|
|
| 1194 |
TT<T>
|
| 1195 |
TT<i>
|
| 1196 |
TT<>
|
| 1197 |
```
|
| 1198 |
|
| 1199 |
-
where `(T)` represents a parameter-type-list
|
| 1200 |
-
|
| 1201 |
parameter-type-list where no parameter type contains a `T`. Similarly,
|
| 1202 |
`<T>` represents template argument lists where at least one argument
|
| 1203 |
contains a `T`, `<i>` represents template argument lists where at least
|
| 1204 |
one argument contains an `i` and `<>` represents template argument lists
|
| 1205 |
where no argument contains a `T` or an `i`.
|
|
@@ -1211,12 +1216,12 @@ corresponding argument Aᵢ of the corresponding template argument list of
|
|
| 1211 |
is not the last template argument, the entire template argument list is
|
| 1212 |
a non-deduced context. If `Pᵢ` is a pack expansion, then the pattern of
|
| 1213 |
`Pᵢ` is compared with each remaining argument in the template argument
|
| 1214 |
list of `A`. Each comparison deduces template arguments for subsequent
|
| 1215 |
positions in the template parameter packs expanded by `Pᵢ`. During
|
| 1216 |
-
partial ordering
|
| 1217 |
-
|
| 1218 |
|
| 1219 |
- if `P` does not contain a template argument corresponding to `Aᵢ` then
|
| 1220 |
`Aᵢ` is ignored;
|
| 1221 |
- otherwise, if `Pᵢ` is not a pack expansion, template argument
|
| 1222 |
deduction fails.
|
|
@@ -1236,19 +1241,19 @@ template struct A<int, int*>; // selects #2
|
|
| 1236 |
```
|
| 1237 |
|
| 1238 |
— *end example*]
|
| 1239 |
|
| 1240 |
Similarly, if `P` has a form that contains `(T)`, then each parameter
|
| 1241 |
-
type `Pᵢ` of the respective parameter-type-list
|
| 1242 |
compared with the corresponding parameter type `Aᵢ` of the corresponding
|
| 1243 |
parameter-type-list of `A`. If `P` and `A` are function types that
|
| 1244 |
-
originated from deduction when taking the address of a function
|
| 1245 |
-
|
| 1246 |
-
|
| 1247 |
-
|
| 1248 |
-
respectively, `Pᵢ` is adjusted if it is a forwarding reference
|
| 1249 |
-
[[temp.deduct.call]]
|
| 1250 |
type of `Pᵢ` is changed to be the template parameter type (i.e., `T&&`
|
| 1251 |
is changed to simply `T`).
|
| 1252 |
|
| 1253 |
[*Note 2*: As a result, when `Pᵢ` is `T&&` and `Aᵢ` is `X&`, the
|
| 1254 |
adjusted `Pᵢ` will be `T`, causing `T` to be deduced as
|
|
@@ -1271,11 +1276,11 @@ void g(int i) {
|
|
| 1271 |
If the *parameter-declaration* corresponding to `Pᵢ` is a function
|
| 1272 |
parameter pack, then the type of its *declarator-id* is compared with
|
| 1273 |
each remaining parameter type in the parameter-type-list of `A`. Each
|
| 1274 |
comparison deduces template arguments for subsequent positions in the
|
| 1275 |
template parameter packs expanded by the function parameter pack. During
|
| 1276 |
-
partial ordering
|
| 1277 |
function parameter pack:
|
| 1278 |
|
| 1279 |
- if `P` does not contain a function parameter type corresponding to
|
| 1280 |
`Aᵢ` then `Aᵢ` is ignored;
|
| 1281 |
- otherwise, if `Pᵢ` is not a function parameter pack, template argument
|
|
@@ -1333,11 +1338,11 @@ template<typename T> struct C;
|
|
| 1333 |
template<typename T, T n> struct C<A<n>> {
|
| 1334 |
using Q = T;
|
| 1335 |
};
|
| 1336 |
|
| 1337 |
using R = long;
|
| 1338 |
-
using R = C<A<2>>::Q; // OK; T was deduced
|
| 1339 |
// template argument value in the type A<2>
|
| 1340 |
```
|
| 1341 |
|
| 1342 |
— *end example*]
|
| 1343 |
|
|
@@ -1350,11 +1355,11 @@ template<typename T> struct S;
|
|
| 1350 |
template<typename T, T n> struct S<int[n]> {
|
| 1351 |
using Q = T;
|
| 1352 |
};
|
| 1353 |
|
| 1354 |
using V = decltype(sizeof 0);
|
| 1355 |
-
using V = S<int[42]>::Q; // OK; T was deduced
|
| 1356 |
```
|
| 1357 |
|
| 1358 |
— *end example*]
|
| 1359 |
|
| 1360 |
[*Example 10*:
|
|
@@ -1379,15 +1384,15 @@ template<int i> void f1(int a[10][i]);
|
|
| 1379 |
template<int i> void f2(int a[i][20]);
|
| 1380 |
template<int i> void f3(int (&a)[i][20]);
|
| 1381 |
|
| 1382 |
void g() {
|
| 1383 |
int v[10][20];
|
| 1384 |
-
f1(v); // OK: i deduced
|
| 1385 |
f1<20>(v); // OK
|
| 1386 |
f2(v); // error: cannot deduce template-argument i
|
| 1387 |
f2<10>(v); // OK
|
| 1388 |
-
f3(v); // OK: i deduced
|
| 1389 |
}
|
| 1390 |
```
|
| 1391 |
|
| 1392 |
— *end note*]
|
| 1393 |
|
|
@@ -1429,21 +1434,21 @@ T deduce(typename A<T>::X x, // T is not deduced here
|
|
| 1429 |
typename B<i>::Y y); // i is not deduced here
|
| 1430 |
A<int> a;
|
| 1431 |
B<77> b;
|
| 1432 |
|
| 1433 |
int x = deduce<77>(a.xm, 62, b.ym);
|
| 1434 |
-
// T
|
| 1435 |
-
// i is explicitly specified to be 77
|
| 1436 |
```
|
| 1437 |
|
| 1438 |
— *end note*]
|
| 1439 |
|
| 1440 |
If `P` has a form that contains `<i>`, and if the type of `i` differs
|
| 1441 |
from the type of the corresponding template parameter of the template
|
| 1442 |
named by the enclosing *simple-template-id*, deduction fails. If `P` has
|
| 1443 |
a form that contains `[i]`, and if the type of `i` is not an integral
|
| 1444 |
-
type, deduction fails.[^
|
| 1445 |
|
| 1446 |
[*Example 12*:
|
| 1447 |
|
| 1448 |
``` cpp
|
| 1449 |
template<int i> class A { ... };
|
|
@@ -1463,11 +1468,11 @@ void k2() {
|
|
| 1463 |
```
|
| 1464 |
|
| 1465 |
— *end example*]
|
| 1466 |
|
| 1467 |
A *template-argument* can be deduced from a function, pointer to
|
| 1468 |
-
function, or pointer
|
| 1469 |
|
| 1470 |
[*Example 13*:
|
| 1471 |
|
| 1472 |
``` cpp
|
| 1473 |
template<class T> void f(void(*)(T,int));
|
|
@@ -1516,12 +1521,12 @@ A<B> ab;
|
|
| 1516 |
f(ab); // calls f(A<B>)
|
| 1517 |
```
|
| 1518 |
|
| 1519 |
— *end example*]
|
| 1520 |
|
| 1521 |
-
[*Note 6*: Template argument deduction involving parameter packs
|
| 1522 |
-
[[temp.variadic]]
|
| 1523 |
pack. — *end note*]
|
| 1524 |
|
| 1525 |
[*Example 16*:
|
| 1526 |
|
| 1527 |
``` cpp
|
|
@@ -1547,49 +1552,47 @@ int fv = f(g); // OK; Types contains int, float
|
|
| 1547 |
#### Deducing template arguments from a function declaration <a id="temp.deduct.decl">[[temp.deduct.decl]]</a>
|
| 1548 |
|
| 1549 |
In a declaration whose *declarator-id* refers to a specialization of a
|
| 1550 |
function template, template argument deduction is performed to identify
|
| 1551 |
the specialization to which the declaration refers. Specifically, this
|
| 1552 |
-
is done for explicit instantiations
|
| 1553 |
-
specializations
|
| 1554 |
-
[[temp.friend]]
|
| 1555 |
function template specialization matches a placement `operator new` (
|
| 1556 |
-
[[basic.stc.dynamic.deallocation]],
|
| 1557 |
`P` is the type of the function template being considered as a potential
|
| 1558 |
match and `A` is either the function type from the declaration or the
|
| 1559 |
type of the deallocation function that would match the placement
|
| 1560 |
`operator new` as described in [[expr.new]]. The deduction is done as
|
| 1561 |
described in [[temp.deduct.type]].
|
| 1562 |
|
| 1563 |
If, for the set of function templates so considered, there is either no
|
| 1564 |
-
match or more than one match after partial ordering has been
|
| 1565 |
-
|
| 1566 |
-
|
| 1567 |
|
| 1568 |
### Overload resolution <a id="temp.over">[[temp.over]]</a>
|
| 1569 |
|
| 1570 |
-
|
| 1571 |
-
|
| 1572 |
-
|
| 1573 |
-
|
| 1574 |
-
|
| 1575 |
-
|
| 1576 |
-
|
| 1577 |
-
|
| 1578 |
-
|
| 1579 |
-
|
| 1580 |
-
and/or explicit) are used to synthesize the declaration of a single
|
| 1581 |
-
function template specialization which is added to the candidate
|
| 1582 |
functions set to be used in overload resolution. If, for a given
|
| 1583 |
function template, argument deduction fails or the synthesized function
|
| 1584 |
template specialization would be ill-formed, no such function is added
|
| 1585 |
to the set of candidate functions for that template. The complete set of
|
| 1586 |
candidate functions includes all the synthesized declarations and all of
|
| 1587 |
the non-template overloaded functions of the same name. The synthesized
|
| 1588 |
declarations are treated like any other functions in the remainder of
|
| 1589 |
overload resolution, except as explicitly noted in
|
| 1590 |
-
[[over.match.best]].[^
|
| 1591 |
|
| 1592 |
[*Example 1*:
|
| 1593 |
|
| 1594 |
``` cpp
|
| 1595 |
template<class T> T max(T a, T b) { return a>b?a:b; }
|
|
@@ -1671,5 +1674,219 @@ and not defined at the point of the call. The program will be ill-formed
|
|
| 1671 |
unless a specialization for `f<const char*>`, either implicitly or
|
| 1672 |
explicitly generated, is present in some translation unit.
|
| 1673 |
|
| 1674 |
— *end example*]
|
| 1675 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
— *end example*]
|
| 31 |
|
| 32 |
### Explicit template argument specification <a id="temp.arg.explicit">[[temp.arg.explicit]]</a>
|
| 33 |
|
| 34 |
Template arguments can be specified when referring to a function
|
| 35 |
+
template specialization that is not a specialization of a constructor
|
| 36 |
+
template by qualifying the function template name with the list of
|
| 37 |
+
*template-argument*s in the same way as *template-argument*s are
|
| 38 |
+
specified in uses of a class template specialization.
|
| 39 |
|
| 40 |
[*Example 1*:
|
| 41 |
|
| 42 |
``` cpp
|
| 43 |
template<class T> void sort(Array<T>& v);
|
|
|
|
| 58 |
}
|
| 59 |
```
|
| 60 |
|
| 61 |
— *end example*]
|
| 62 |
|
| 63 |
+
Template arguments shall not be specified when referring to a
|
| 64 |
+
specialization of a constructor template ([[class.ctor]],
|
| 65 |
+
[[class.qual]]).
|
| 66 |
+
|
| 67 |
A template argument list may be specified when referring to a
|
| 68 |
specialization of a function template
|
| 69 |
|
| 70 |
- when a function is called,
|
| 71 |
- when the address of a function is taken, when a function initializes a
|
| 72 |
reference to function, or when a pointer to member function is formed,
|
| 73 |
- in an explicit specialization,
|
| 74 |
- in an explicit instantiation, or
|
| 75 |
- in a friend declaration.
|
| 76 |
|
| 77 |
+
Trailing template arguments that can be deduced [[temp.deduct]] or
|
| 78 |
obtained from default *template-argument*s may be omitted from the list
|
| 79 |
+
of explicit *template-argument*s. A trailing template parameter pack
|
| 80 |
+
[[temp.variadic]] not otherwise deduced will be deduced as an empty
|
| 81 |
sequence of template arguments. If all of the template arguments can be
|
| 82 |
deduced, they may all be omitted; in this case, the empty template
|
| 83 |
argument list `<>` itself may also be omitted. In contexts where
|
| 84 |
deduction is done and fails, or in contexts where deduction is not done,
|
| 85 |
if a template argument list is specified and it, along with any default
|
|
|
|
| 91 |
|
| 92 |
``` cpp
|
| 93 |
template<class X, class Y> X f(Y);
|
| 94 |
template<class X, class Y, class ... Z> X g(Y);
|
| 95 |
void h() {
|
| 96 |
+
int i = f<int>(5.6); // Y deduced as double
|
| 97 |
+
int j = f(5.6); // error: X cannot be deduced
|
| 98 |
+
f<void>(f<int, bool>); // Y for outer f deduced as int (*)(bool)
|
| 99 |
+
f<void>(f<int>); // error: f<int> does not denote a single function template specialization
|
| 100 |
+
int k = g<int>(5.6); // Y deduced as double; Z deduced as an empty sequence
|
| 101 |
+
f<void>(g<int, bool>); // Y for outer f deduced as int (*)(bool),
|
| 102 |
+
// Z deduced as an empty sequence
|
| 103 |
}
|
| 104 |
```
|
| 105 |
|
| 106 |
— *end example*]
|
| 107 |
|
| 108 |
[*Note 1*:
|
| 109 |
|
| 110 |
An empty template argument list can be used to indicate that a given use
|
| 111 |
refers to a specialization of a function template even when a
|
| 112 |
+
non-template function [[dcl.fct]] is visible that would otherwise be
|
| 113 |
used. For example:
|
| 114 |
|
| 115 |
``` cpp
|
| 116 |
template <class T> int f(T); // #1
|
| 117 |
int f(int); // #2
|
|
|
|
| 132 |
``` cpp
|
| 133 |
template<class X, class Y, class Z> X f(Y,Z);
|
| 134 |
template<class ... Args> void f2();
|
| 135 |
void g() {
|
| 136 |
f<int,const char*,double>("aa",3.0);
|
| 137 |
+
f<int,const char*>("aa",3.0); // Z deduced as double
|
| 138 |
+
f<int>("aa",3.0); // Y deduced as const char*; Z deduced as double
|
| 139 |
f("aa",3.0); // error: X cannot be deduced
|
| 140 |
f2<char, short, int, long>(); // OK
|
| 141 |
}
|
| 142 |
```
|
| 143 |
|
| 144 |
— *end example*]
|
| 145 |
|
| 146 |
+
Implicit conversions [[conv]] will be performed on a function argument
|
| 147 |
+
to convert it to the type of the corresponding function parameter if the
|
| 148 |
+
parameter type contains no *template-parameter*s that participate in
|
| 149 |
+
template argument deduction.
|
| 150 |
|
| 151 |
[*Note 2*:
|
| 152 |
|
| 153 |
Template parameters do not participate in template argument deduction if
|
| 154 |
they are explicitly specified. For example,
|
|
|
|
| 166 |
```
|
| 167 |
|
| 168 |
— *end note*]
|
| 169 |
|
| 170 |
[*Note 3*: Because the explicit template argument list follows the
|
| 171 |
+
function template name, and because constructor templates [[class.ctor]]
|
| 172 |
+
are named without using a function name [[class.qual]], there is no way
|
| 173 |
+
to provide an explicit template argument list for these function
|
| 174 |
+
templates. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
Template argument deduction can extend the sequence of template
|
| 177 |
arguments corresponding to a template parameter pack, even when the
|
| 178 |
sequence contains explicitly specified template arguments.
|
| 179 |
|
| 180 |
+
[*Example 4*:
|
| 181 |
|
| 182 |
``` cpp
|
| 183 |
template<class ... Types> void f(Types ... values);
|
| 184 |
|
| 185 |
void g() {
|
| 186 |
+
f<int*, float*>(0, 0, 0); // Types deduced as the sequence int*, float*, int
|
| 187 |
}
|
| 188 |
```
|
| 189 |
|
| 190 |
— *end example*]
|
| 191 |
|
|
|
|
| 214 |
}
|
| 215 |
```
|
| 216 |
|
| 217 |
— *end example*]
|
| 218 |
|
| 219 |
+
When an explicit template argument list is specified, if the given
|
| 220 |
+
*template-id* is not valid [[temp.names]], type deduction fails.
|
| 221 |
+
Otherwise, the specified template argument values are substituted for
|
| 222 |
+
the corresponding template parameters as specified below.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
After this substitution is performed, the function parameter type
|
| 225 |
adjustments described in [[dcl.fct]] are performed.
|
| 226 |
|
| 227 |
[*Example 2*: A parameter type of “`void (const int, int[5])`” becomes
|
|
|
|
| 290 |
When all template arguments have been deduced or obtained from default
|
| 291 |
template arguments, all uses of template parameters in the template
|
| 292 |
parameter list of the template and the function type are replaced with
|
| 293 |
the corresponding deduced or default argument values. If the
|
| 294 |
substitution results in an invalid type, as described above, type
|
| 295 |
+
deduction fails. If the function template has associated constraints
|
| 296 |
+
[[temp.constr.decl]], those constraints are checked for satisfaction
|
| 297 |
+
[[temp.constr.constr]]. If the constraints are not satisfied, type
|
| 298 |
deduction fails.
|
| 299 |
|
| 300 |
At certain points in the template argument deduction process it is
|
| 301 |
necessary to take a function type that makes use of template parameters
|
| 302 |
and replace those template parameters with the corresponding template
|
|
|
|
| 311 |
expressions include not only constant expressions such as those that
|
| 312 |
appear in array bounds or as nontype template arguments but also general
|
| 313 |
expressions (i.e., non-constant expressions) inside `sizeof`,
|
| 314 |
`decltype`, and other contexts that allow non-constant expressions. The
|
| 315 |
substitution proceeds in lexical order and stops when a condition that
|
| 316 |
+
causes deduction to fail is encountered. If substitution into different
|
| 317 |
+
declarations of the same function template would cause template
|
| 318 |
+
instantiations to occur in a different order or not at all, the program
|
| 319 |
+
is ill-formed; no diagnostic required.
|
| 320 |
|
| 321 |
[*Note 3*: The equivalent substitution in exception specifications is
|
| 322 |
done only when the *noexcept-specifier* is instantiated, at which point
|
| 323 |
a program is ill-formed if the substitution results in an invalid type
|
| 324 |
or expression. — *end note*]
|
|
|
|
| 329 |
template <class T> struct A { using X = typename T::X; };
|
| 330 |
template <class T> typename T::X f(typename A<T>::X);
|
| 331 |
template <class T> void f(...) { }
|
| 332 |
template <class T> auto g(typename A<T>::X) -> typename T::X;
|
| 333 |
template <class T> void g(...) { }
|
| 334 |
+
template <class T> typename T::X h(typename A<T>::X);
|
| 335 |
+
template <class T> auto h(typename A<T>::X) -> typename T::X; // redeclaration
|
| 336 |
+
template <class T> void h(...) { }
|
| 337 |
|
| 338 |
+
void x() {
|
| 339 |
f<int>(0); // OK, substituting return type causes deduction to fail
|
| 340 |
g<int>(0); // error, substituting parameter type instantiates A<int>
|
| 341 |
+
h<int>(0); // ill-formed, no diagnostic required
|
| 342 |
}
|
| 343 |
```
|
| 344 |
|
| 345 |
— *end example*]
|
| 346 |
|
|
|
|
| 352 |
[*Note 4*: If no diagnostic is required, the program is still
|
| 353 |
ill-formed. Access checking is done as part of the substitution
|
| 354 |
process. — *end note*]
|
| 355 |
|
| 356 |
Only invalid types and expressions in the immediate context of the
|
| 357 |
+
function type, its template parameter types, and its
|
| 358 |
+
*explicit-specifier* can result in a deduction failure.
|
| 359 |
|
| 360 |
[*Note 5*: The substitution into types and expressions can result in
|
| 361 |
effects such as the instantiation of class template specializations
|
| 362 |
and/or function template specializations, the generation of
|
| 363 |
implicitly-defined functions, etc. Such effects are not in the
|
| 364 |
“immediate context” and can result in the program being
|
| 365 |
ill-formed. — *end note*]
|
| 366 |
|
| 367 |
+
A *lambda-expression* appearing in a function type or a template
|
| 368 |
+
parameter is not considered part of the immediate context for the
|
| 369 |
+
purposes of template argument deduction.
|
| 370 |
+
|
| 371 |
+
[*Note 6*:
|
| 372 |
+
|
| 373 |
+
The intent is to avoid requiring implementations to deal with
|
| 374 |
+
substitution failure involving arbitrary statements.
|
| 375 |
+
|
| 376 |
[*Example 6*:
|
| 377 |
|
| 378 |
+
``` cpp
|
| 379 |
+
template <class T>
|
| 380 |
+
auto f(T) -> decltype([]() { T::invalid; } ());
|
| 381 |
+
void f(...);
|
| 382 |
+
f(0); // error: invalid expression not part of the immediate context
|
| 383 |
+
|
| 384 |
+
template <class T, std::size_t = sizeof([]() { T::invalid; })>
|
| 385 |
+
void g(T);
|
| 386 |
+
void g(...);
|
| 387 |
+
g(0); // error: invalid expression not part of the immediate context
|
| 388 |
+
|
| 389 |
+
template <class T>
|
| 390 |
+
auto h(T) -> decltype([x = T::invalid]() { });
|
| 391 |
+
void h(...);
|
| 392 |
+
h(0); // error: invalid expression not part of the immediate context
|
| 393 |
+
|
| 394 |
+
template <class T>
|
| 395 |
+
auto i(T) -> decltype([]() -> typename T::invalid { });
|
| 396 |
+
void i(...);
|
| 397 |
+
i(0); // error: invalid expression not part of the immediate context
|
| 398 |
+
|
| 399 |
+
template <class T>
|
| 400 |
+
auto j(T t) -> decltype([](auto x) -> decltype(x.invalid) { } (t)); // #1
|
| 401 |
+
void j(...); // #2
|
| 402 |
+
j(0); // deduction fails on #1, calls #2
|
| 403 |
+
```
|
| 404 |
+
|
| 405 |
+
— *end example*]
|
| 406 |
+
|
| 407 |
+
— *end note*]
|
| 408 |
+
|
| 409 |
+
[*Example 7*:
|
| 410 |
+
|
| 411 |
``` cpp
|
| 412 |
struct X { };
|
| 413 |
struct Y {
|
| 414 |
Y(X){}
|
| 415 |
};
|
|
|
|
| 421 |
X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
|
| 422 |
```
|
| 423 |
|
| 424 |
— *end example*]
|
| 425 |
|
| 426 |
+
[*Note 7*:
|
| 427 |
|
| 428 |
Type deduction may fail for the following reasons:
|
| 429 |
|
| 430 |
+
- Attempting to instantiate a pack expansion containing multiple packs
|
| 431 |
+
of differing lengths.
|
| 432 |
- Attempting to create an array with an element type that is `void`, a
|
| 433 |
+
function type, or a reference type, or attempting to create an array
|
| 434 |
+
with a size that is zero or negative.
|
| 435 |
+
\[*Example 8*:
|
| 436 |
``` cpp
|
| 437 |
template <class T> int f(T[5]);
|
| 438 |
int I = f<int>(0);
|
| 439 |
int j = f<void>(0); // invalid array
|
| 440 |
```
|
| 441 |
|
| 442 |
— *end example*]
|
| 443 |
- Attempting to use a type that is not a class or enumeration type in a
|
| 444 |
qualified name.
|
| 445 |
+
\[*Example 9*:
|
| 446 |
``` cpp
|
| 447 |
template <class T> int f(typename T::B*);
|
| 448 |
int i = f<int>(0);
|
| 449 |
```
|
| 450 |
|
|
|
|
| 455 |
- the specified member is not a type where a type is required, or
|
| 456 |
- the specified member is not a template where a template is required,
|
| 457 |
or
|
| 458 |
- the specified member is not a non-type where a non-type is required.
|
| 459 |
|
| 460 |
+
\[*Example 10*:
|
| 461 |
``` cpp
|
| 462 |
template <int I> struct X { };
|
| 463 |
template <template <class T> class> struct Z { };
|
| 464 |
template <class T> void f(typename T::Y*){}
|
| 465 |
template <class T> void g(X<T::N>*){}
|
|
|
|
| 485 |
— *end example*]
|
| 486 |
- Attempting to create a pointer to reference type.
|
| 487 |
- Attempting to create a reference to `void`.
|
| 488 |
- Attempting to create “pointer to member of `T`” when `T` is not a
|
| 489 |
class type.
|
| 490 |
+
\[*Example 11*:
|
| 491 |
``` cpp
|
| 492 |
template <class T> int f(int T::*);
|
| 493 |
int i = f<int>(0);
|
| 494 |
```
|
| 495 |
|
| 496 |
— *end example*]
|
| 497 |
- Attempting to give an invalid type to a non-type template parameter.
|
| 498 |
+
\[*Example 12*:
|
| 499 |
``` cpp
|
| 500 |
template <class T, T> struct S {};
|
| 501 |
template <class T> int f(S<T, T()>*);
|
| 502 |
struct X {};
|
| 503 |
int i0 = f<X>(0);
|
|
|
|
| 505 |
|
| 506 |
— *end example*]
|
| 507 |
- Attempting to perform an invalid conversion in either a template
|
| 508 |
argument expression, or an expression used in the function
|
| 509 |
declaration.
|
| 510 |
+
\[*Example 13*:
|
| 511 |
``` cpp
|
| 512 |
template <class T, T*> int f(int);
|
| 513 |
int i2 = f<int,1>(0); // can't conv 1 to int*
|
| 514 |
```
|
| 515 |
|
| 516 |
— *end example*]
|
| 517 |
- Attempting to create a function type in which a parameter has a type
|
| 518 |
of `void`, or in which the return type is a function type or array
|
| 519 |
type.
|
|
|
|
|
|
|
| 520 |
|
| 521 |
— *end note*]
|
| 522 |
|
| 523 |
+
[*Example 14*:
|
| 524 |
|
| 525 |
In the following example, assuming a `signed char` cannot represent the
|
| 526 |
+
value 1000, a narrowing conversion [[dcl.init.list]] would be required
|
| 527 |
+
to convert the *template-argument* of type `int` to `signed char`,
|
| 528 |
+
therefore substitution fails for the second template
|
| 529 |
+
[[temp.arg.nontype]].
|
| 530 |
|
| 531 |
``` cpp
|
| 532 |
template <int> int f(int);
|
| 533 |
template <signed char> int f(int);
|
| 534 |
int i1 = f<1000>(0); // OK
|
|
|
|
| 542 |
Template argument deduction is done by comparing each function template
|
| 543 |
parameter type (call it `P`) that contains *template-parameter*s that
|
| 544 |
participate in template argument deduction with the type of the
|
| 545 |
corresponding argument of the call (call it `A`) as described below. If
|
| 546 |
removing references and cv-qualifiers from `P` gives
|
| 547 |
+
`std::initializer_list<P^{\prime}>` or `P`'`[N]` for some `P`' and `N`
|
| 548 |
+
and the argument is a non-empty initializer list [[dcl.init.list]], then
|
| 549 |
+
deduction is performed instead for each element of the initializer list
|
| 550 |
+
independently, taking `P`' as separate function template parameter types
|
| 551 |
+
`P`'_i and the iᵗʰ initializer element as the corresponding argument. In
|
| 552 |
+
the `P`'`[N]` case, if `N` is a non-type template parameter, `N` is
|
| 553 |
+
deduced from the length of the initializer list. Otherwise, an
|
| 554 |
+
initializer list argument causes the parameter to be considered a
|
| 555 |
+
non-deduced context [[temp.deduct.type]].
|
| 556 |
|
| 557 |
[*Example 1*:
|
| 558 |
|
| 559 |
``` cpp
|
| 560 |
template<class T> void f(std::initializer_list<T>);
|
| 561 |
+
f({1,2,3}); // T deduced as int
|
| 562 |
+
f({1,"asdf"}); // error: T deduced as both int and const char*
|
| 563 |
|
| 564 |
template<class T> void g(T);
|
| 565 |
g({1,2,3}); // error: no argument deduced for T
|
| 566 |
|
| 567 |
template<class T, int N> void h(T const(&)[N]);
|
| 568 |
+
h({1,2,3}); // T deduced as int; N deduced as 3
|
| 569 |
|
| 570 |
template<class T> void j(T const(&)[3]);
|
| 571 |
+
j({42}); // T deduced as int; array bound not considered
|
| 572 |
|
| 573 |
struct Aggr { int i; int j; };
|
| 574 |
template<int N> void k(Aggr const(&)[N]);
|
| 575 |
k({1,2,3}); // error: deduction fails, no conversion from int to Aggr
|
| 576 |
+
k({{1},{2},{3}}); // OK, N deduced as 3
|
| 577 |
|
| 578 |
template<int M, int N> void m(int const(&)[M][N]);
|
| 579 |
+
m({{1,2},{3,4}}); // M and N both deduced as 2
|
| 580 |
|
| 581 |
template<class T, int N> void n(T const(&)[N], T);
|
| 582 |
n({{1},{2},{3}},Aggr()); // OK, T is Aggr, N is 3
|
| 583 |
+
|
| 584 |
+
template<typename T, int N> void o(T (* const (&)[N])(T)) { }
|
| 585 |
+
int f1(int);
|
| 586 |
+
int f4(int);
|
| 587 |
+
char f4(char);
|
| 588 |
+
o({ &f1, &f4 }); // OK, T deduced as int from first element, nothing
|
| 589 |
+
// deduced from second element, N deduced as 2
|
| 590 |
+
o({ &f1, static_cast<char(*)(char)>(&f4) }); // error: conflicting deductions for T
|
| 591 |
```
|
| 592 |
|
| 593 |
— *end example*]
|
| 594 |
|
| 595 |
For a function parameter pack that occurs at the end of the
|
| 596 |
*parameter-declaration-list*, deduction is performed for each remaining
|
| 597 |
argument of the call, taking the type `P` of the *declarator-id* of the
|
| 598 |
function parameter pack as the corresponding function template parameter
|
| 599 |
type. Each deduction deduces template arguments for subsequent positions
|
| 600 |
in the template parameter packs expanded by the function parameter pack.
|
| 601 |
+
When a function parameter pack appears in a non-deduced context
|
| 602 |
+
[[temp.deduct.type]], the type of that pack is never deduced.
|
| 603 |
|
| 604 |
[*Example 2*:
|
| 605 |
|
| 606 |
``` cpp
|
| 607 |
template<class ... Types> void f(Types& ...);
|
| 608 |
template<class T1, class ... Types> void g(T1, Types ...);
|
| 609 |
template<class T1, class ... Types> void g1(Types ..., T1);
|
| 610 |
|
| 611 |
void h(int x, float& y) {
|
| 612 |
const int z = x;
|
| 613 |
+
f(x, y, z); // Types deduced as int, float, const int
|
| 614 |
+
g(x, y, z); // T1 deduced as int; Types deduced as float, int
|
| 615 |
g1(x, y, z); // error: Types is not deduced
|
| 616 |
g1<int, int, int>(x, y, z); // OK, no deduction occurs
|
| 617 |
}
|
| 618 |
```
|
| 619 |
|
| 620 |
— *end example*]
|
| 621 |
|
| 622 |
If `P` is not a reference type:
|
| 623 |
|
| 624 |
- If `A` is an array type, the pointer type produced by the
|
| 625 |
+
array-to-pointer standard conversion [[conv.array]] is used in place
|
| 626 |
+
of `A` for type deduction; otherwise,
|
| 627 |
- If `A` is a function type, the pointer type produced by the
|
| 628 |
+
function-to-pointer standard conversion [[conv.func]] is used in place
|
| 629 |
+
of `A` for type deduction; otherwise,
|
| 630 |
- If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s
|
| 631 |
type are ignored for type deduction.
|
| 632 |
|
| 633 |
If `P` is a cv-qualified type, the top-level cv-qualifiers of `P`’s type
|
| 634 |
are ignored for type deduction. If `P` is a reference type, the type
|
|
|
|
| 647 |
|
| 648 |
— *end example*]
|
| 649 |
|
| 650 |
A *forwarding reference* is an rvalue reference to a cv-unqualified
|
| 651 |
template parameter that does not represent a template parameter of a
|
| 652 |
+
class template (during class template argument deduction
|
| 653 |
+
[[over.match.class.deduct]]). If `P` is a forwarding reference and the
|
| 654 |
argument is an lvalue, the type “lvalue reference to `A`” is used in
|
| 655 |
place of `A` for type deduction.
|
| 656 |
|
| 657 |
[*Example 4*:
|
| 658 |
|
|
|
|
| 688 |
that allow a difference:
|
| 689 |
|
| 690 |
- If the original `P` is a reference type, the deduced `A` (i.e., the
|
| 691 |
type referred to by the reference) can be more cv-qualified than the
|
| 692 |
transformed `A`.
|
| 693 |
+
- The transformed `A` can be another pointer or pointer-to-member type
|
| 694 |
that can be converted to the deduced `A` via a function pointer
|
| 695 |
+
conversion [[conv.fctptr]] and/or qualification conversion
|
| 696 |
+
[[conv.qual]].
|
| 697 |
- If `P` is a class and `P` has the form *simple-template-id*, then the
|
| 698 |
+
transformed `A` can be a derived class `D` of the deduced `A`.
|
| 699 |
+
Likewise, if `P` is a pointer to a class of the form
|
| 700 |
+
*simple-template-id*, the transformed `A` can be a pointer to a
|
| 701 |
+
derived class `D` pointed to by the deduced `A`. However, if there is
|
| 702 |
+
a class `C` that is a (direct or indirect) base class of `D` and
|
| 703 |
+
derived (directly or indirectly) from a class `B` and that would be a
|
| 704 |
+
valid deduced `A`, the deduced `A` cannot be `B` or pointer to `B`,
|
| 705 |
+
respectively.
|
| 706 |
+
\[*Example 5*:
|
| 707 |
+
``` cpp
|
| 708 |
+
template <typename... T> struct X;
|
| 709 |
+
template <> struct X<> {};
|
| 710 |
+
template <typename T, typename... Ts>
|
| 711 |
+
struct X<T, Ts...> : X<Ts...> {};
|
| 712 |
+
struct D : X<int> {};
|
| 713 |
+
|
| 714 |
+
template <typename... T>
|
| 715 |
+
int f(const X<T...>&);
|
| 716 |
+
int x = f(D()); // calls f<int>, not f<>
|
| 717 |
+
// B is X<>, C is X<int>
|
| 718 |
+
```
|
| 719 |
+
|
| 720 |
+
— *end example*]
|
| 721 |
|
| 722 |
These alternatives are considered only if type deduction would otherwise
|
| 723 |
fail. If they yield more than one possible deduced `A`, the type
|
| 724 |
deduction fails.
|
| 725 |
|
|
|
|
| 727 |
parameters of a function template, or is used only in a non-deduced
|
| 728 |
context, its corresponding *template-argument* cannot be deduced from a
|
| 729 |
function call and the *template-argument* must be explicitly
|
| 730 |
specified. — *end note*]
|
| 731 |
|
| 732 |
+
When `P` is a function type, function pointer type, or
|
| 733 |
+
pointer-to-member-function type:
|
| 734 |
|
| 735 |
- If the argument is an overload set containing one or more function
|
| 736 |
templates, the parameter is treated as a non-deduced context.
|
| 737 |
- If the argument is an overload set (not containing function
|
| 738 |
templates), trial argument deduction is attempted using each of the
|
| 739 |
members of the set. If deduction succeeds for only one of the overload
|
| 740 |
set members, that member is used as the argument value for the
|
| 741 |
deduction. If deduction succeeds for more than one member of the
|
| 742 |
overload set the parameter is treated as a non-deduced context.
|
| 743 |
|
| 744 |
+
[*Example 6*:
|
| 745 |
|
| 746 |
``` cpp
|
| 747 |
// Only one function of an overload set matches the call so the function parameter is a deduced context.
|
| 748 |
template <class T> int f(T (*p)(T));
|
| 749 |
int g(int);
|
|
|
|
| 751 |
int i = f(g); // calls f(int (*)(int))
|
| 752 |
```
|
| 753 |
|
| 754 |
— *end example*]
|
| 755 |
|
| 756 |
+
[*Example 7*:
|
| 757 |
|
| 758 |
``` cpp
|
| 759 |
// Ambiguous deduction causes the second function parameter to be a non-deduced context.
|
| 760 |
template <class T> int f(T, T (*p)(T));
|
| 761 |
int g(int);
|
|
|
|
| 763 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 764 |
```
|
| 765 |
|
| 766 |
— *end example*]
|
| 767 |
|
| 768 |
+
[*Example 8*:
|
| 769 |
|
| 770 |
``` cpp
|
| 771 |
// The overload set contains a template, causing the second function parameter to be a non-deduced context.
|
| 772 |
template <class T> int f(T, T (*p)(T));
|
| 773 |
char g(char);
|
|
|
|
| 790 |
*template-parameter*s participate in template argument deduction, and
|
| 791 |
parameters that became non-dependent due to substitution of
|
| 792 |
explicitly-specified template arguments, will be checked during overload
|
| 793 |
resolution. — *end note*]
|
| 794 |
|
| 795 |
+
[*Example 9*:
|
| 796 |
|
| 797 |
``` cpp
|
| 798 |
template <class T> struct Z {
|
| 799 |
typedef typename T::x xx;
|
| 800 |
};
|
|
|
|
| 809 |
— *end example*]
|
| 810 |
|
| 811 |
#### Deducing template arguments taking the address of a function template <a id="temp.deduct.funcaddr">[[temp.deduct.funcaddr]]</a>
|
| 812 |
|
| 813 |
Template arguments can be deduced from the type specified when taking
|
| 814 |
+
the address of an overloaded function [[over.over]]. If there is a
|
| 815 |
+
target, the function template’s function type and the target type are
|
| 816 |
+
used as the types of `P` and `A`, and the deduction is done as described
|
| 817 |
+
in [[temp.deduct.type]]. Otherwise, deduction is performed with empty
|
| 818 |
+
sets of types P and A.
|
| 819 |
|
| 820 |
+
A placeholder type [[dcl.spec.auto]] in the return type of a function
|
| 821 |
template is a non-deduced context. If template argument deduction
|
| 822 |
succeeds for such a function, the return type is determined from
|
| 823 |
instantiation of the function body.
|
| 824 |
|
| 825 |
#### Deducing conversion function template arguments <a id="temp.deduct.conv">[[temp.deduct.conv]]</a>
|
|
|
|
| 830 |
[[dcl.init]], [[over.match.conv]], and [[over.match.ref]] for the
|
| 831 |
determination of that type) as described in [[temp.deduct.type]].
|
| 832 |
|
| 833 |
If `P` is a reference type, the type referred to by `P` is used in place
|
| 834 |
of `P` for type deduction and for any further references to or
|
| 835 |
+
transformations of `P` in the remainder of this subclause.
|
| 836 |
|
| 837 |
If `A` is not a reference type:
|
| 838 |
|
| 839 |
- If `P` is an array type, the pointer type produced by the
|
| 840 |
+
array-to-pointer standard conversion [[conv.array]] is used in place
|
| 841 |
+
of `P` for type deduction; otherwise,
|
| 842 |
- If `P` is a function type, the pointer type produced by the
|
| 843 |
+
function-to-pointer standard conversion [[conv.func]] is used in place
|
| 844 |
+
of `P` for type deduction; otherwise,
|
| 845 |
- If `P` is a cv-qualified type, the top-level cv-qualifiers of `P`’s
|
| 846 |
type are ignored for type deduction.
|
| 847 |
|
| 848 |
If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s type
|
| 849 |
are ignored for type deduction. If `A` is a reference type, the type
|
|
|
|
| 852 |
In general, the deduction process attempts to find template argument
|
| 853 |
values that will make the deduced `A` identical to `A`. However, there
|
| 854 |
are four cases that allow a difference:
|
| 855 |
|
| 856 |
- If the original `A` is a reference type, `A` can be more cv-qualified
|
| 857 |
+
than the deduced `A` (i.e., the type referred to by the reference).
|
| 858 |
- If the original `A` is a function pointer type, `A` can be “pointer to
|
| 859 |
+
function” even if the deduced `A` is “pointer to `noexcept` function”.
|
| 860 |
+
- If the original `A` is a pointer-to-member-function type, `A` can be
|
| 861 |
“pointer to member of type function” even if the deduced `A` is
|
| 862 |
+
“pointer to member of type `noexcept` function”.
|
| 863 |
+
- The deduced `A` can be another pointer or pointer-to-member type that
|
| 864 |
can be converted to `A` via a qualification conversion.
|
| 865 |
|
| 866 |
These alternatives are considered only if type deduction would otherwise
|
| 867 |
fail. If they yield more than one possible deduced `A`, the type
|
| 868 |
deduction fails.
|
| 869 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 870 |
#### Deducing template arguments during partial ordering <a id="temp.deduct.partial">[[temp.deduct.partial]]</a>
|
| 871 |
|
| 872 |
Template argument deduction is done by comparing certain types
|
| 873 |
associated with the two function templates being compared.
|
| 874 |
|
|
|
|
| 889 |
|
| 890 |
The types used to determine the ordering depend on the context in which
|
| 891 |
the partial ordering is done:
|
| 892 |
|
| 893 |
- In the context of a function call, the types used are those function
|
| 894 |
+
parameter types for which the function call has arguments.[^12]
|
| 895 |
- In the context of a call to a conversion function, the return types of
|
| 896 |
the conversion function templates are used.
|
| 897 |
+
- In other contexts [[temp.func.order]] the function template’s function
|
| 898 |
+
type is used.
|
| 899 |
|
| 900 |
Each type nominated above from the parameter template and the
|
| 901 |
corresponding type from the argument template are used as the types of
|
| 902 |
+
`P` and `A`.
|
|
|
|
|
|
|
| 903 |
|
| 904 |
Before the partial ordering is done, certain transformations are
|
| 905 |
performed on the types used for partial ordering:
|
| 906 |
|
| 907 |
- If `P` is a reference type, `P` is replaced by the type referred to.
|
|
|
|
| 945 |
// than the variadic templates #1 and #2
|
| 946 |
```
|
| 947 |
|
| 948 |
— *end example*]
|
| 949 |
|
| 950 |
+
If, for a given type, the types are identical after the transformations
|
| 951 |
+
above and both `P` and `A` were reference types (before being replaced
|
| 952 |
+
with the type referred to above):
|
|
|
|
| 953 |
|
| 954 |
- if the type from the argument template was an lvalue reference and the
|
| 955 |
type from the parameter template was not, the parameter type is not
|
| 956 |
considered to be at least as specialized as the argument type;
|
| 957 |
otherwise,
|
|
|
|
| 966 |
specialized than* `G` if `F` is at least as specialized as `G` and `G`
|
| 967 |
is not at least as specialized as `F`.
|
| 968 |
|
| 969 |
If, after considering the above, function template `F` is at least as
|
| 970 |
specialized as function template `G` and vice-versa, and if `G` has a
|
| 971 |
+
trailing function parameter pack for which `F` does not have a
|
| 972 |
+
corresponding parameter, and if `F` does not have a trailing function
|
| 973 |
+
parameter pack, then `F` is more specialized than `G`.
|
| 974 |
|
| 975 |
+
In most cases, deduction fails if not all template parameters have
|
| 976 |
+
values, but for partial ordering purposes a template parameter may
|
| 977 |
+
remain without a value provided it is not used in the types being used
|
| 978 |
+
for partial ordering.
|
| 979 |
|
| 980 |
[*Note 2*: A template parameter used in a non-deduced context is
|
| 981 |
considered used. — *end note*]
|
| 982 |
|
| 983 |
[*Example 2*:
|
|
|
|
| 1036 |
A given type `P` can be composed from a number of other types,
|
| 1037 |
templates, and non-type values:
|
| 1038 |
|
| 1039 |
- A function type includes the types of each of the function parameters
|
| 1040 |
and the return type.
|
| 1041 |
+
- A pointer-to-member type includes the type of the class object pointed
|
| 1042 |
to and the type of the member pointed to.
|
| 1043 |
- A type that is a specialization of a class template (e.g., `A<int>`)
|
| 1044 |
includes the types, templates, and non-type values referenced by the
|
| 1045 |
template argument list of the specialization.
|
| 1046 |
- An array type includes the array element type and the value of the
|
| 1047 |
array bound.
|
| 1048 |
|
| 1049 |
In most cases, the types, templates, and non-type values that are used
|
| 1050 |
to compose `P` participate in template argument deduction. That is, they
|
| 1051 |
+
may be used to determine the value of a template argument, and template
|
| 1052 |
+
argument deduction fails if the value so determined is not consistent
|
| 1053 |
+
with the values determined elsewhere. In certain contexts, however, the
|
| 1054 |
+
value does not participate in type deduction, but instead uses the
|
| 1055 |
+
values of template arguments that were either deduced elsewhere or
|
| 1056 |
+
explicitly specified. If a template parameter is used only in
|
| 1057 |
+
non-deduced contexts and is not explicitly specified, template argument
|
| 1058 |
+
deduction fails.
|
| 1059 |
|
| 1060 |
+
[*Note 1*: Under [[temp.deduct.call]], if `P` contains no
|
| 1061 |
+
*template-parameter*s that appear in deduced contexts, no deduction is
|
| 1062 |
+
done, so `P` and `A` need not have the same form. — *end note*]
|
|
|
|
| 1063 |
|
| 1064 |
The non-deduced contexts are:
|
| 1065 |
|
| 1066 |
- The *nested-name-specifier* of a type that was specified using a
|
| 1067 |
*qualified-id*.
|
|
|
|
| 1069 |
- A non-type template argument or an array bound in which a
|
| 1070 |
subexpression references a template parameter.
|
| 1071 |
- A template parameter used in the parameter type of a function
|
| 1072 |
parameter that has a default argument that is being used in the call
|
| 1073 |
for which argument deduction is being done.
|
| 1074 |
+
- A function parameter for which the associated argument is an overload
|
| 1075 |
+
set [[over.over]], and one or more of the following apply:
|
|
|
|
|
|
|
| 1076 |
- more than one function matches the function parameter type
|
| 1077 |
(resulting in an ambiguous deduction), or
|
| 1078 |
- no function matches the function parameter type, or
|
| 1079 |
+
- the overload set supplied as an argument contains one or more
|
| 1080 |
function templates.
|
| 1081 |
- A function parameter for which the associated argument is an
|
| 1082 |
+
initializer list [[dcl.init.list]] but the parameter does not have a
|
| 1083 |
+
type for which deduction from an initializer list is specified
|
| 1084 |
+
[[temp.deduct.call]].
|
| 1085 |
\[*Example 1*:
|
| 1086 |
``` cpp
|
| 1087 |
template<class T> void g(T);
|
| 1088 |
g({1,2,3}); // error: no argument deduced for T
|
| 1089 |
```
|
|
|
|
| 1173 |
template non-type argument `i` can be deduced if `P` and `A` have one of
|
| 1174 |
the following forms:
|
| 1175 |
|
| 1176 |
``` cpp
|
| 1177 |
T
|
| 1178 |
+
cv T
|
| 1179 |
T*
|
| 1180 |
T&
|
| 1181 |
T&&
|
| 1182 |
T[integer-constant]
|
| 1183 |
template-name<T> (where template-name refers to a class template)
|
|
|
|
| 1199 |
TT<T>
|
| 1200 |
TT<i>
|
| 1201 |
TT<>
|
| 1202 |
```
|
| 1203 |
|
| 1204 |
+
where `(T)` represents a parameter-type-list [[dcl.fct]] where at least
|
| 1205 |
+
one parameter type contains a `T`, and `()` represents a
|
| 1206 |
parameter-type-list where no parameter type contains a `T`. Similarly,
|
| 1207 |
`<T>` represents template argument lists where at least one argument
|
| 1208 |
contains a `T`, `<i>` represents template argument lists where at least
|
| 1209 |
one argument contains an `i` and `<>` represents template argument lists
|
| 1210 |
where no argument contains a `T` or an `i`.
|
|
|
|
| 1216 |
is not the last template argument, the entire template argument list is
|
| 1217 |
a non-deduced context. If `Pᵢ` is a pack expansion, then the pattern of
|
| 1218 |
`Pᵢ` is compared with each remaining argument in the template argument
|
| 1219 |
list of `A`. Each comparison deduces template arguments for subsequent
|
| 1220 |
positions in the template parameter packs expanded by `Pᵢ`. During
|
| 1221 |
+
partial ordering [[temp.deduct.partial]], if `Aᵢ` was originally a pack
|
| 1222 |
+
expansion:
|
| 1223 |
|
| 1224 |
- if `P` does not contain a template argument corresponding to `Aᵢ` then
|
| 1225 |
`Aᵢ` is ignored;
|
| 1226 |
- otherwise, if `Pᵢ` is not a pack expansion, template argument
|
| 1227 |
deduction fails.
|
|
|
|
| 1241 |
```
|
| 1242 |
|
| 1243 |
— *end example*]
|
| 1244 |
|
| 1245 |
Similarly, if `P` has a form that contains `(T)`, then each parameter
|
| 1246 |
+
type `Pᵢ` of the respective parameter-type-list [[dcl.fct]] of `P` is
|
| 1247 |
compared with the corresponding parameter type `Aᵢ` of the corresponding
|
| 1248 |
parameter-type-list of `A`. If `P` and `A` are function types that
|
| 1249 |
+
originated from deduction when taking the address of a function template
|
| 1250 |
+
[[temp.deduct.funcaddr]] or when deducing template arguments from a
|
| 1251 |
+
function declaration [[temp.deduct.decl]] and `Pᵢ` and `Aᵢ` are
|
| 1252 |
+
parameters of the top-level parameter-type-list of `P` and `A`,
|
| 1253 |
+
respectively, `Pᵢ` is adjusted if it is a forwarding reference
|
| 1254 |
+
[[temp.deduct.call]] and `Aᵢ` is an lvalue reference, in which case the
|
| 1255 |
type of `Pᵢ` is changed to be the template parameter type (i.e., `T&&`
|
| 1256 |
is changed to simply `T`).
|
| 1257 |
|
| 1258 |
[*Note 2*: As a result, when `Pᵢ` is `T&&` and `Aᵢ` is `X&`, the
|
| 1259 |
adjusted `Pᵢ` will be `T`, causing `T` to be deduced as
|
|
|
|
| 1276 |
If the *parameter-declaration* corresponding to `Pᵢ` is a function
|
| 1277 |
parameter pack, then the type of its *declarator-id* is compared with
|
| 1278 |
each remaining parameter type in the parameter-type-list of `A`. Each
|
| 1279 |
comparison deduces template arguments for subsequent positions in the
|
| 1280 |
template parameter packs expanded by the function parameter pack. During
|
| 1281 |
+
partial ordering [[temp.deduct.partial]], if `Aᵢ` was originally a
|
| 1282 |
function parameter pack:
|
| 1283 |
|
| 1284 |
- if `P` does not contain a function parameter type corresponding to
|
| 1285 |
`Aᵢ` then `Aᵢ` is ignored;
|
| 1286 |
- otherwise, if `Pᵢ` is not a function parameter pack, template argument
|
|
|
|
| 1338 |
template<typename T, T n> struct C<A<n>> {
|
| 1339 |
using Q = T;
|
| 1340 |
};
|
| 1341 |
|
| 1342 |
using R = long;
|
| 1343 |
+
using R = C<A<2>>::Q; // OK; T was deduced as long from the
|
| 1344 |
// template argument value in the type A<2>
|
| 1345 |
```
|
| 1346 |
|
| 1347 |
— *end example*]
|
| 1348 |
|
|
|
|
| 1355 |
template<typename T, T n> struct S<int[n]> {
|
| 1356 |
using Q = T;
|
| 1357 |
};
|
| 1358 |
|
| 1359 |
using V = decltype(sizeof 0);
|
| 1360 |
+
using V = S<int[42]>::Q; // OK; T was deduced as std::size_t from the type int[42]
|
| 1361 |
```
|
| 1362 |
|
| 1363 |
— *end example*]
|
| 1364 |
|
| 1365 |
[*Example 10*:
|
|
|
|
| 1384 |
template<int i> void f2(int a[i][20]);
|
| 1385 |
template<int i> void f3(int (&a)[i][20]);
|
| 1386 |
|
| 1387 |
void g() {
|
| 1388 |
int v[10][20];
|
| 1389 |
+
f1(v); // OK: i deduced as 20
|
| 1390 |
f1<20>(v); // OK
|
| 1391 |
f2(v); // error: cannot deduce template-argument i
|
| 1392 |
f2<10>(v); // OK
|
| 1393 |
+
f3(v); // OK: i deduced as 10
|
| 1394 |
}
|
| 1395 |
```
|
| 1396 |
|
| 1397 |
— *end note*]
|
| 1398 |
|
|
|
|
| 1434 |
typename B<i>::Y y); // i is not deduced here
|
| 1435 |
A<int> a;
|
| 1436 |
B<77> b;
|
| 1437 |
|
| 1438 |
int x = deduce<77>(a.xm, 62, b.ym);
|
| 1439 |
+
// T deduced as int; a.xm must be convertible to A<int>::X
|
| 1440 |
+
// i is explicitly specified to be 77; b.ym must be convertible to B<77>::Y
|
| 1441 |
```
|
| 1442 |
|
| 1443 |
— *end note*]
|
| 1444 |
|
| 1445 |
If `P` has a form that contains `<i>`, and if the type of `i` differs
|
| 1446 |
from the type of the corresponding template parameter of the template
|
| 1447 |
named by the enclosing *simple-template-id*, deduction fails. If `P` has
|
| 1448 |
a form that contains `[i]`, and if the type of `i` is not an integral
|
| 1449 |
+
type, deduction fails.[^13]
|
| 1450 |
|
| 1451 |
[*Example 12*:
|
| 1452 |
|
| 1453 |
``` cpp
|
| 1454 |
template<int i> class A { ... };
|
|
|
|
| 1468 |
```
|
| 1469 |
|
| 1470 |
— *end example*]
|
| 1471 |
|
| 1472 |
A *template-argument* can be deduced from a function, pointer to
|
| 1473 |
+
function, or pointer-to-member-function type.
|
| 1474 |
|
| 1475 |
[*Example 13*:
|
| 1476 |
|
| 1477 |
``` cpp
|
| 1478 |
template<class T> void f(void(*)(T,int));
|
|
|
|
| 1521 |
f(ab); // calls f(A<B>)
|
| 1522 |
```
|
| 1523 |
|
| 1524 |
— *end example*]
|
| 1525 |
|
| 1526 |
+
[*Note 6*: Template argument deduction involving parameter packs
|
| 1527 |
+
[[temp.variadic]] can deduce zero or more arguments for each parameter
|
| 1528 |
pack. — *end note*]
|
| 1529 |
|
| 1530 |
[*Example 16*:
|
| 1531 |
|
| 1532 |
``` cpp
|
|
|
|
| 1552 |
#### Deducing template arguments from a function declaration <a id="temp.deduct.decl">[[temp.deduct.decl]]</a>
|
| 1553 |
|
| 1554 |
In a declaration whose *declarator-id* refers to a specialization of a
|
| 1555 |
function template, template argument deduction is performed to identify
|
| 1556 |
the specialization to which the declaration refers. Specifically, this
|
| 1557 |
+
is done for explicit instantiations [[temp.explicit]], explicit
|
| 1558 |
+
specializations [[temp.expl.spec]], and certain friend declarations
|
| 1559 |
+
[[temp.friend]]. This is also done to determine whether a deallocation
|
| 1560 |
function template specialization matches a placement `operator new` (
|
| 1561 |
+
[[basic.stc.dynamic.deallocation]], [[expr.new]]). In all these cases,
|
| 1562 |
`P` is the type of the function template being considered as a potential
|
| 1563 |
match and `A` is either the function type from the declaration or the
|
| 1564 |
type of the deallocation function that would match the placement
|
| 1565 |
`operator new` as described in [[expr.new]]. The deduction is done as
|
| 1566 |
described in [[temp.deduct.type]].
|
| 1567 |
|
| 1568 |
If, for the set of function templates so considered, there is either no
|
| 1569 |
+
match or more than one match after partial ordering has been considered
|
| 1570 |
+
[[temp.func.order]], deduction fails and, in the declaration cases, the
|
| 1571 |
+
program is ill-formed.
|
| 1572 |
|
| 1573 |
### Overload resolution <a id="temp.over">[[temp.over]]</a>
|
| 1574 |
|
| 1575 |
+
When a call to the name of a function or function template is written
|
| 1576 |
+
(explicitly, or implicitly using the operator notation), template
|
| 1577 |
+
argument deduction [[temp.deduct]] and checking of any explicit template
|
| 1578 |
+
arguments [[temp.arg]] are performed for each function template to find
|
| 1579 |
+
the template argument values (if any) that can be used with that
|
| 1580 |
+
function template to instantiate a function template specialization that
|
| 1581 |
+
can be invoked with the call arguments. For each function template, if
|
| 1582 |
+
the argument deduction and checking succeeds, the *template-argument*s
|
| 1583 |
+
(deduced and/or explicit) are used to synthesize the declaration of a
|
| 1584 |
+
single function template specialization which is added to the candidate
|
|
|
|
|
|
|
| 1585 |
functions set to be used in overload resolution. If, for a given
|
| 1586 |
function template, argument deduction fails or the synthesized function
|
| 1587 |
template specialization would be ill-formed, no such function is added
|
| 1588 |
to the set of candidate functions for that template. The complete set of
|
| 1589 |
candidate functions includes all the synthesized declarations and all of
|
| 1590 |
the non-template overloaded functions of the same name. The synthesized
|
| 1591 |
declarations are treated like any other functions in the remainder of
|
| 1592 |
overload resolution, except as explicitly noted in
|
| 1593 |
+
[[over.match.best]].[^14]
|
| 1594 |
|
| 1595 |
[*Example 1*:
|
| 1596 |
|
| 1597 |
``` cpp
|
| 1598 |
template<class T> T max(T a, T b) { return a>b?a:b; }
|
|
|
|
| 1674 |
unless a specialization for `f<const char*>`, either implicitly or
|
| 1675 |
explicitly generated, is present in some translation unit.
|
| 1676 |
|
| 1677 |
— *end example*]
|
| 1678 |
|
| 1679 |
+
<!-- Link reference definitions -->
|
| 1680 |
+
[basic.def]: basic.md#basic.def
|
| 1681 |
+
[basic.def.odr]: basic.md#basic.def.odr
|
| 1682 |
+
[basic.link]: basic.md#basic.link
|
| 1683 |
+
[basic.lookup]: basic.md#basic.lookup
|
| 1684 |
+
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
| 1685 |
+
[basic.lookup.classref]: basic.md#basic.lookup.classref
|
| 1686 |
+
[basic.lookup.qual]: basic.md#basic.lookup.qual
|
| 1687 |
+
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 1688 |
+
[basic.scope]: basic.md#basic.scope
|
| 1689 |
+
[basic.scope.hiding]: basic.md#basic.scope.hiding
|
| 1690 |
+
[basic.scope.namespace]: basic.md#basic.scope.namespace
|
| 1691 |
+
[basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
|
| 1692 |
+
[basic.types]: basic.md#basic.types
|
| 1693 |
+
[class.access]: class.md#class.access
|
| 1694 |
+
[class.base.init]: class.md#class.base.init
|
| 1695 |
+
[class.conv.fct]: class.md#class.conv.fct
|
| 1696 |
+
[class.ctor]: class.md#class.ctor
|
| 1697 |
+
[class.default.ctor]: class.md#class.default.ctor
|
| 1698 |
+
[class.derived]: class.md#class.derived
|
| 1699 |
+
[class.dtor]: class.md#class.dtor
|
| 1700 |
+
[class.friend]: class.md#class.friend
|
| 1701 |
+
[class.local]: class.md#class.local
|
| 1702 |
+
[class.mem]: class.md#class.mem
|
| 1703 |
+
[class.member.lookup]: class.md#class.member.lookup
|
| 1704 |
+
[class.pre]: class.md#class.pre
|
| 1705 |
+
[class.qual]: basic.md#class.qual
|
| 1706 |
+
[class.temporary]: basic.md#class.temporary
|
| 1707 |
+
[conv]: expr.md#conv
|
| 1708 |
+
[conv.array]: expr.md#conv.array
|
| 1709 |
+
[conv.fctptr]: expr.md#conv.fctptr
|
| 1710 |
+
[conv.func]: expr.md#conv.func
|
| 1711 |
+
[conv.lval]: expr.md#conv.lval
|
| 1712 |
+
[conv.qual]: expr.md#conv.qual
|
| 1713 |
+
[dcl.align]: dcl.md#dcl.align
|
| 1714 |
+
[dcl.attr.grammar]: dcl.md#dcl.attr.grammar
|
| 1715 |
+
[dcl.decl]: dcl.md#dcl.decl
|
| 1716 |
+
[dcl.enum]: dcl.md#dcl.enum
|
| 1717 |
+
[dcl.fct]: dcl.md#dcl.fct
|
| 1718 |
+
[dcl.fct.def.general]: dcl.md#dcl.fct.def.general
|
| 1719 |
+
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 1720 |
+
[dcl.init]: dcl.md#dcl.init
|
| 1721 |
+
[dcl.init.list]: dcl.md#dcl.init.list
|
| 1722 |
+
[dcl.meaning]: dcl.md#dcl.meaning
|
| 1723 |
+
[dcl.pre]: dcl.md#dcl.pre
|
| 1724 |
+
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
| 1725 |
+
[dcl.stc]: dcl.md#dcl.stc
|
| 1726 |
+
[dcl.struct.bind]: dcl.md#dcl.struct.bind
|
| 1727 |
+
[dcl.type.class.deduct]: dcl.md#dcl.type.class.deduct
|
| 1728 |
+
[dcl.type.elab]: dcl.md#dcl.type.elab
|
| 1729 |
+
[dcl.type.simple]: dcl.md#dcl.type.simple
|
| 1730 |
+
[except.spec]: except.md#except.spec
|
| 1731 |
+
[expr.const]: expr.md#expr.const
|
| 1732 |
+
[expr.context]: expr.md#expr.context
|
| 1733 |
+
[expr.log.and]: expr.md#expr.log.and
|
| 1734 |
+
[expr.log.or]: expr.md#expr.log.or
|
| 1735 |
+
[expr.new]: expr.md#expr.new
|
| 1736 |
+
[expr.prim.fold]: expr.md#expr.prim.fold
|
| 1737 |
+
[expr.prim.id]: expr.md#expr.prim.id
|
| 1738 |
+
[expr.prim.id.unqual]: expr.md#expr.prim.id.unqual
|
| 1739 |
+
[expr.prim.lambda.capture]: expr.md#expr.prim.lambda.capture
|
| 1740 |
+
[expr.prim.lambda.closure]: expr.md#expr.prim.lambda.closure
|
| 1741 |
+
[expr.ref]: expr.md#expr.ref
|
| 1742 |
+
[expr.sizeof]: expr.md#expr.sizeof
|
| 1743 |
+
[expr.typeid]: expr.md#expr.typeid
|
| 1744 |
+
[expr.unary.op]: expr.md#expr.unary.op
|
| 1745 |
+
[implimits]: limits.md#implimits
|
| 1746 |
+
[intro.defs]: intro.md#intro.defs
|
| 1747 |
+
[intro.object]: basic.md#intro.object
|
| 1748 |
+
[lex.string]: lex.md#lex.string
|
| 1749 |
+
[namespace.def]: dcl.md#namespace.def
|
| 1750 |
+
[namespace.memdef]: dcl.md#namespace.memdef
|
| 1751 |
+
[namespace.udecl]: dcl.md#namespace.udecl
|
| 1752 |
+
[over.ics.rank]: over.md#over.ics.rank
|
| 1753 |
+
[over.match]: over.md#over.match
|
| 1754 |
+
[over.match.best]: over.md#over.match.best
|
| 1755 |
+
[over.match.class.deduct]: over.md#over.match.class.deduct
|
| 1756 |
+
[over.match.conv]: over.md#over.match.conv
|
| 1757 |
+
[over.match.oper]: over.md#over.match.oper
|
| 1758 |
+
[over.match.ref]: over.md#over.match.ref
|
| 1759 |
+
[over.match.viable]: over.md#over.match.viable
|
| 1760 |
+
[over.over]: over.md#over.over
|
| 1761 |
+
[special]: class.md#special
|
| 1762 |
+
[stmt.if]: stmt.md#stmt.if
|
| 1763 |
+
[support.types]: support.md#support.types
|
| 1764 |
+
[temp]: #temp
|
| 1765 |
+
[temp.alias]: #temp.alias
|
| 1766 |
+
[temp.arg]: #temp.arg
|
| 1767 |
+
[temp.arg.explicit]: #temp.arg.explicit
|
| 1768 |
+
[temp.arg.nontype]: #temp.arg.nontype
|
| 1769 |
+
[temp.arg.template]: #temp.arg.template
|
| 1770 |
+
[temp.arg.type]: #temp.arg.type
|
| 1771 |
+
[temp.class]: #temp.class
|
| 1772 |
+
[temp.class.order]: #temp.class.order
|
| 1773 |
+
[temp.class.spec]: #temp.class.spec
|
| 1774 |
+
[temp.class.spec.match]: #temp.class.spec.match
|
| 1775 |
+
[temp.class.spec.mfunc]: #temp.class.spec.mfunc
|
| 1776 |
+
[temp.concept]: #temp.concept
|
| 1777 |
+
[temp.constr]: #temp.constr
|
| 1778 |
+
[temp.constr.atomic]: #temp.constr.atomic
|
| 1779 |
+
[temp.constr.constr]: #temp.constr.constr
|
| 1780 |
+
[temp.constr.decl]: #temp.constr.decl
|
| 1781 |
+
[temp.constr.normal]: #temp.constr.normal
|
| 1782 |
+
[temp.constr.op]: #temp.constr.op
|
| 1783 |
+
[temp.constr.order]: #temp.constr.order
|
| 1784 |
+
[temp.decls]: #temp.decls
|
| 1785 |
+
[temp.deduct]: #temp.deduct
|
| 1786 |
+
[temp.deduct.call]: #temp.deduct.call
|
| 1787 |
+
[temp.deduct.conv]: #temp.deduct.conv
|
| 1788 |
+
[temp.deduct.decl]: #temp.deduct.decl
|
| 1789 |
+
[temp.deduct.funcaddr]: #temp.deduct.funcaddr
|
| 1790 |
+
[temp.deduct.guide]: #temp.deduct.guide
|
| 1791 |
+
[temp.deduct.partial]: #temp.deduct.partial
|
| 1792 |
+
[temp.deduct.type]: #temp.deduct.type
|
| 1793 |
+
[temp.dep]: #temp.dep
|
| 1794 |
+
[temp.dep.candidate]: #temp.dep.candidate
|
| 1795 |
+
[temp.dep.constexpr]: #temp.dep.constexpr
|
| 1796 |
+
[temp.dep.expr]: #temp.dep.expr
|
| 1797 |
+
[temp.dep.res]: #temp.dep.res
|
| 1798 |
+
[temp.dep.temp]: #temp.dep.temp
|
| 1799 |
+
[temp.dep.type]: #temp.dep.type
|
| 1800 |
+
[temp.expl.spec]: #temp.expl.spec
|
| 1801 |
+
[temp.explicit]: #temp.explicit
|
| 1802 |
+
[temp.fct]: #temp.fct
|
| 1803 |
+
[temp.fct.spec]: #temp.fct.spec
|
| 1804 |
+
[temp.fold.empty]: #temp.fold.empty
|
| 1805 |
+
[temp.friend]: #temp.friend
|
| 1806 |
+
[temp.func.order]: #temp.func.order
|
| 1807 |
+
[temp.inject]: #temp.inject
|
| 1808 |
+
[temp.inst]: #temp.inst
|
| 1809 |
+
[temp.local]: #temp.local
|
| 1810 |
+
[temp.mem]: #temp.mem
|
| 1811 |
+
[temp.mem.class]: #temp.mem.class
|
| 1812 |
+
[temp.mem.enum]: #temp.mem.enum
|
| 1813 |
+
[temp.mem.func]: #temp.mem.func
|
| 1814 |
+
[temp.names]: #temp.names
|
| 1815 |
+
[temp.nondep]: #temp.nondep
|
| 1816 |
+
[temp.over]: #temp.over
|
| 1817 |
+
[temp.over.link]: #temp.over.link
|
| 1818 |
+
[temp.param]: #temp.param
|
| 1819 |
+
[temp.point]: #temp.point
|
| 1820 |
+
[temp.pre]: #temp.pre
|
| 1821 |
+
[temp.res]: #temp.res
|
| 1822 |
+
[temp.spec]: #temp.spec
|
| 1823 |
+
[temp.static]: #temp.static
|
| 1824 |
+
[temp.type]: #temp.type
|
| 1825 |
+
[temp.variadic]: #temp.variadic
|
| 1826 |
+
|
| 1827 |
+
[^1]: Since template *template-parameter*s and template
|
| 1828 |
+
*template-argument*s are treated as types for descriptive purposes,
|
| 1829 |
+
the terms *non-type parameter* and *non-type argument* are used to
|
| 1830 |
+
refer to non-type, non-template parameters and arguments.
|
| 1831 |
+
|
| 1832 |
+
[^2]: A `>` that encloses the *type-id* of a `dynamic_cast`,
|
| 1833 |
+
`static_cast`, `reinterpret_cast` or `const_cast`, or which encloses
|
| 1834 |
+
the *template-argument*s of a subsequent *template-id*, is
|
| 1835 |
+
considered nested for the purpose of this description.
|
| 1836 |
+
|
| 1837 |
+
[^3]: There is no such ambiguity in a default *template-argument*
|
| 1838 |
+
because the form of the *template-parameter* determines the
|
| 1839 |
+
allowable forms of the *template-argument*.
|
| 1840 |
+
|
| 1841 |
+
[^4]: A constraint is in disjunctive normal form when it is a
|
| 1842 |
+
disjunction of clauses where each clause is a conjunction of atomic
|
| 1843 |
+
constraints.
|
| 1844 |
+
|
| 1845 |
+
\[*Example 5*: For atomic constraints A, B, and C, the disjunctive
|
| 1846 |
+
normal form of the constraint A ∧ (B ∨ C) is (A ∧ B) ∨ (A ∧ C). Its
|
| 1847 |
+
disjunctive clauses are (A ∧ B) and (A ∧ C). — *end example*]
|
| 1848 |
+
|
| 1849 |
+
[^5]: A constraint is in conjunctive normal form when it is a
|
| 1850 |
+
conjunction of clauses where each clause is a disjunction of atomic
|
| 1851 |
+
constraints.
|
| 1852 |
+
|
| 1853 |
+
\[*Example 6*: For atomic constraints A, B, and C, the constraint
|
| 1854 |
+
A ∧ (B ∨ C) is in conjunctive normal form. Its conjunctive clauses
|
| 1855 |
+
are A and (B ∨ C). — *end example*]
|
| 1856 |
+
|
| 1857 |
+
[^6]: The identity of enumerators is not preserved.
|
| 1858 |
+
|
| 1859 |
+
[^7]: An array as a *template-parameter* decays to a pointer.
|
| 1860 |
+
|
| 1861 |
+
[^8]: There is no way in which they could be used.
|
| 1862 |
+
|
| 1863 |
+
[^9]: That is, declarations of non-template functions do not merely
|
| 1864 |
+
guide overload resolution of function template specializations with
|
| 1865 |
+
the same name. If such a non-template function is odr-used
|
| 1866 |
+
[[basic.def.odr]] in a program, it must be defined; it will not be
|
| 1867 |
+
implicitly instantiated using the function template definition.
|
| 1868 |
+
|
| 1869 |
+
[^10]: This includes friend function declarations.
|
| 1870 |
+
|
| 1871 |
+
[^11]: Friend declarations do not introduce new names into any scope,
|
| 1872 |
+
either when the template is declared or when it is instantiated.
|
| 1873 |
+
|
| 1874 |
+
[^12]: Default arguments are not considered to be arguments in this
|
| 1875 |
+
context; they only become arguments after a function has been
|
| 1876 |
+
selected.
|
| 1877 |
+
|
| 1878 |
+
[^13]: Although the *template-argument* corresponding to a
|
| 1879 |
+
*template-parameter* of type `bool` may be deduced from an array
|
| 1880 |
+
bound, the resulting value will always be `true` because the array
|
| 1881 |
+
bound will be nonzero.
|
| 1882 |
+
|
| 1883 |
+
[^14]: The parameters of function template specializations contain no
|
| 1884 |
+
template parameter types. The set of conversions allowed on deduced
|
| 1885 |
+
arguments is limited, because the argument deduction process
|
| 1886 |
+
produces function templates with parameters that either match the
|
| 1887 |
+
call arguments exactly or differ only in ways that can be bridged by
|
| 1888 |
+
the allowed limited conversions. Non-deduced arguments allow the
|
| 1889 |
+
full range of conversions. Note also that [[over.match.best]]
|
| 1890 |
+
specifies that a non-template function will be given preference over
|
| 1891 |
+
a template specialization if the two functions are otherwise equally
|
| 1892 |
+
good candidates for an overload match.
|