- tmp/tmpj4jxlzss/{from.md → to.md} +289 -222
tmp/tmpj4jxlzss/{from.md → to.md}
RENAMED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
## Function template specializations <a id="temp.fct.spec">[[temp.fct.spec]]</a>
|
| 2 |
|
|
|
|
|
|
|
| 3 |
A function instantiated from a function template is called a function
|
| 4 |
template specialization; so is an explicit specialization of a function
|
| 5 |
template. Template arguments can be explicitly specified when naming the
|
| 6 |
function template specialization, deduced from the context (e.g.,
|
| 7 |
deduced from the function arguments in a call to the function template
|
|
@@ -59,12 +61,11 @@ void g(double d) {
|
|
| 59 |
```
|
| 60 |
|
| 61 |
— *end example*]
|
| 62 |
|
| 63 |
Template arguments shall not be specified when referring to a
|
| 64 |
-
specialization of a constructor template
|
| 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,
|
|
@@ -74,20 +75,19 @@ specialization of a function template
|
|
| 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.
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
deduced
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
template
|
| 87 |
-
|
| 88 |
-
template specialization.
|
| 89 |
|
| 90 |
[*Example 2*:
|
| 91 |
|
| 92 |
``` cpp
|
| 93 |
template<class X, class Y> X f(Y);
|
|
@@ -103,11 +103,11 @@ void h() {
|
|
| 103 |
}
|
| 104 |
```
|
| 105 |
|
| 106 |
— *end example*]
|
| 107 |
|
| 108 |
-
[*Note
|
| 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:
|
|
@@ -146,11 +146,11 @@ void g() {
|
|
| 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
|
| 152 |
|
| 153 |
Template parameters do not participate in template argument deduction if
|
| 154 |
they are explicitly specified. For example,
|
| 155 |
|
| 156 |
``` cpp
|
|
@@ -165,11 +165,11 @@ void g() {
|
|
| 165 |
}
|
| 166 |
```
|
| 167 |
|
| 168 |
— *end note*]
|
| 169 |
|
| 170 |
-
[*Note
|
| 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 |
|
|
@@ -189,10 +189,12 @@ void g() {
|
|
| 189 |
|
| 190 |
— *end example*]
|
| 191 |
|
| 192 |
### Template argument deduction <a id="temp.deduct">[[temp.deduct]]</a>
|
| 193 |
|
|
|
|
|
|
|
| 194 |
When a function template specialization is referenced, all of the
|
| 195 |
template arguments shall have values. The values can be explicitly
|
| 196 |
specified or, in some cases, be deduced from the use or obtained from
|
| 197 |
default *template-argument*s.
|
| 198 |
|
|
@@ -287,45 +289,90 @@ void g() {
|
|
| 287 |
|
| 288 |
— *end example*]
|
| 289 |
|
| 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
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
[[temp.constr.
|
| 297 |
-
|
| 298 |
-
deduction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 303 |
arguments. This is done at the beginning of template argument deduction
|
| 304 |
when any explicitly specified template arguments are substituted into
|
| 305 |
the function type, and again at the end of template argument deduction
|
| 306 |
when any template arguments that were deduced or obtained from default
|
| 307 |
arguments are substituted.
|
| 308 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
The substitution occurs in all types and expressions that are used in
|
| 310 |
-
the
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
|
| 321 |
-
[*Note
|
| 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*]
|
| 325 |
|
| 326 |
-
[*Example
|
| 327 |
|
| 328 |
``` cpp
|
| 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(...) { }
|
|
@@ -344,38 +391,37 @@ void x() {
|
|
| 344 |
|
| 345 |
— *end example*]
|
| 346 |
|
| 347 |
If a substitution results in an invalid type or expression, type
|
| 348 |
deduction fails. An invalid type or expression is one that would be
|
| 349 |
-
ill-formed, with a diagnostic required, if written
|
| 350 |
-
arguments.
|
| 351 |
|
| 352 |
-
[*Note
|
| 353 |
ill-formed. Access checking is done as part of the substitution
|
| 354 |
process. — *end note*]
|
| 355 |
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
*explicit-specifier* can result in a deduction failure.
|
| 359 |
|
| 360 |
-
[*Note
|
| 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
|
| 372 |
|
| 373 |
The intent is to avoid requiring implementations to deal with
|
| 374 |
substitution failure involving arbitrary statements.
|
| 375 |
|
| 376 |
-
[*Example
|
| 377 |
|
| 378 |
``` cpp
|
| 379 |
template <class T>
|
| 380 |
auto f(T) -> decltype([]() { T::invalid; } ());
|
| 381 |
void f(...);
|
|
@@ -404,11 +450,11 @@ j(0); // deduction fails on #1, calls #2
|
|
| 404 |
|
| 405 |
— *end example*]
|
| 406 |
|
| 407 |
— *end note*]
|
| 408 |
|
| 409 |
-
[*Example
|
| 410 |
|
| 411 |
``` cpp
|
| 412 |
struct X { };
|
| 413 |
struct Y {
|
| 414 |
Y(X) {}
|
|
@@ -421,30 +467,30 @@ X x1, x2;
|
|
| 421 |
X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
|
| 422 |
```
|
| 423 |
|
| 424 |
— *end example*]
|
| 425 |
|
| 426 |
-
[*Note
|
| 427 |
|
| 428 |
-
Type deduction
|
| 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
|
| 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
|
| 446 |
``` cpp
|
| 447 |
template <class T> int f(typename T::B*);
|
| 448 |
int i = f<int>(0);
|
| 449 |
```
|
| 450 |
|
|
@@ -455,17 +501,17 @@ Type deduction may fail for the following reasons:
|
|
| 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
|
| 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>*) {}
|
| 466 |
-
template <class T> void h(Z<T::
|
| 467 |
struct A {};
|
| 468 |
struct B { int Y; };
|
| 469 |
struct C {
|
| 470 |
typedef int N;
|
| 471 |
};
|
|
@@ -485,44 +531,46 @@ Type deduction may fail for the following reasons:
|
|
| 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
|
| 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
|
| 499 |
``` cpp
|
| 500 |
template <class T, T> struct S {};
|
| 501 |
-
template <class T> int f(S<T, T
|
| 502 |
-
|
| 503 |
-
|
|
|
|
|
|
|
| 504 |
```
|
| 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
|
| 511 |
``` cpp
|
| 512 |
template <class T, T*> int f(int);
|
| 513 |
-
int i2 = f<int,1>(0); // can't
|
| 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
|
| 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
|
|
@@ -708,15 +756,17 @@ that allow a difference:
|
|
| 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
|
|
@@ -775,62 +825,33 @@ template <class T> T g(T);
|
|
| 775 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 776 |
```
|
| 777 |
|
| 778 |
— *end example*]
|
| 779 |
|
| 780 |
-
If deduction succeeds for all parameters that contain
|
| 781 |
-
*template-parameter*s that participate in template argument deduction,
|
| 782 |
-
and all template arguments are explicitly specified, deduced, or
|
| 783 |
-
obtained from default template arguments, remaining parameters are then
|
| 784 |
-
compared with the corresponding arguments. For each remaining parameter
|
| 785 |
-
`P` with a type that was non-dependent before substitution of any
|
| 786 |
-
explicitly-specified template arguments, if the corresponding argument
|
| 787 |
-
`A` cannot be implicitly converted to `P`, deduction fails.
|
| 788 |
-
|
| 789 |
-
[*Note 2*: Parameters with dependent types in which no
|
| 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 |
-
};
|
| 801 |
-
template <class T> typename Z<T>::xx f(void *, T); // #1
|
| 802 |
-
template <class T> void f(int, T); // #2
|
| 803 |
-
struct A {} a;
|
| 804 |
-
int main() {
|
| 805 |
-
f(1, a); // OK, deduction fails for #1 because there is no conversion from int to void*
|
| 806 |
-
}
|
| 807 |
-
```
|
| 808 |
-
|
| 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
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 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>
|
| 826 |
|
| 827 |
Template argument deduction is done by comparing the return type of the
|
| 828 |
-
conversion function template (call it `P`) with the type
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
|
|
|
|
|
|
| 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 |
|
|
@@ -848,26 +869,24 @@ If `A` is not a reference type:
|
|
| 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
|
| 850 |
referred to by `A` is used for type deduction.
|
| 851 |
|
| 852 |
In general, the deduction process attempts to find template argument
|
| 853 |
-
values that will make the deduced `A` identical to `A`. However,
|
| 854 |
-
|
| 855 |
|
| 856 |
-
- If the original `A` is a reference type,
|
| 857 |
-
|
| 858 |
-
- If the original `A` is a function pointer
|
| 859 |
-
function
|
| 860 |
-
|
| 861 |
-
|
| 862 |
-
|
| 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
|
| 867 |
-
fail. If
|
| 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.
|
|
@@ -889,11 +908,11 @@ as the parameter template.
|
|
| 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.[^
|
| 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 |
|
|
@@ -1034,12 +1053,12 @@ deduction fails. The type of a type parameter is only deduced from an
|
|
| 1034 |
array bound if it is not otherwise deduced.
|
| 1035 |
|
| 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 |
-
|
| 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.
|
|
@@ -1112,14 +1131,14 @@ inconsistent template argument deductions:
|
|
| 1112 |
``` cpp
|
| 1113 |
template<class T> void f(T x, T y) { ... }
|
| 1114 |
struct A { ... };
|
| 1115 |
struct B : A { ... };
|
| 1116 |
void g(A a, B b) {
|
| 1117 |
-
f(a,b); // error: T
|
| 1118 |
-
f(b,a); // error: T
|
| 1119 |
-
f(a,a); // OK
|
| 1120 |
-
f(b,b); // OK
|
| 1121 |
}
|
| 1122 |
```
|
| 1123 |
|
| 1124 |
Here is an example where two template arguments are deduced from a
|
| 1125 |
single function parameter/argument pair. This can lead to conflicts that
|
|
@@ -1131,13 +1150,32 @@ template <class T, class U> void f( T (*)( T, U, U ) );
|
|
| 1131 |
int g1( int, float, float);
|
| 1132 |
char g2( int, float, float);
|
| 1133 |
int g3( int, char, float);
|
| 1134 |
|
| 1135 |
void r() {
|
| 1136 |
-
f(g1); // OK
|
| 1137 |
-
f(g2); // error: T
|
| 1138 |
-
f(g3); // error: U
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1139 |
}
|
| 1140 |
```
|
| 1141 |
|
| 1142 |
Here is an example where a qualification conversion applies between the
|
| 1143 |
argument type on the function call and the deduced template argument
|
|
@@ -1167,49 +1205,50 @@ void t() {
|
|
| 1167 |
}
|
| 1168 |
```
|
| 1169 |
|
| 1170 |
— *end example*]
|
| 1171 |
|
| 1172 |
-
A template type argument `T`, a template template argument `TT` or a
|
| 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[
|
| 1183 |
-
|
| 1184 |
-
|
| 1185 |
-
T
|
| 1186 |
-
|
| 1187 |
-
|
| 1188 |
-
|
| 1189 |
-
T T::*
|
| 1190 |
-
T (type::*)()
|
| 1191 |
-
type (T::*)()
|
| 1192 |
-
type (type::*)(T)
|
| 1193 |
-
type (T::*)(T)
|
| 1194 |
-
T (type::*)(T)
|
| 1195 |
-
T (T::*)()
|
| 1196 |
-
T (T::*)(T)
|
| 1197 |
-
type[i]
|
| 1198 |
-
template-name<i> (where template-name refers to a class template)
|
| 1199 |
-
TT<T>
|
| 1200 |
-
TT<i>
|
| 1201 |
-
TT<>
|
| 1202 |
```
|
| 1203 |
|
| 1204 |
-
where
|
| 1205 |
-
|
| 1206 |
-
|
| 1207 |
-
|
| 1208 |
-
|
| 1209 |
-
|
| 1210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1211 |
|
| 1212 |
If `P` has a form that contains `<T>` or `<i>`, then each argument Pᵢ of
|
| 1213 |
the respective template argument list of `P` is compared with the
|
| 1214 |
corresponding argument Aᵢ of the corresponding template argument list of
|
| 1215 |
`A`. If the template argument list of `P` contains a pack expansion that
|
|
@@ -1253,11 +1292,11 @@ 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
|
| 1259 |
adjusted `Pᵢ` will be `T`, causing `T` to be deduced as
|
| 1260 |
`X&`. — *end note*]
|
| 1261 |
|
| 1262 |
[*Example 5*:
|
| 1263 |
|
|
@@ -1360,23 +1399,40 @@ 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*:
|
| 1366 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1367 |
``` cpp
|
| 1368 |
template<class T, T i> void f(int (&a)[i]);
|
| 1369 |
int v[10];
|
| 1370 |
void g() {
|
| 1371 |
-
f(v); // OK
|
| 1372 |
}
|
| 1373 |
```
|
| 1374 |
|
| 1375 |
— *end example*]
|
| 1376 |
|
| 1377 |
-
[*Note
|
| 1378 |
|
| 1379 |
Except for reference and pointer types, a major array bound is not part
|
| 1380 |
of a function parameter type and cannot be deduced from an argument:
|
| 1381 |
|
| 1382 |
``` cpp
|
|
@@ -1384,28 +1440,28 @@ template<int i> void f1(int a[10][i]);
|
|
| 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
|
| 1390 |
f1<20>(v); // OK
|
| 1391 |
f2(v); // error: cannot deduce template-argument i
|
| 1392 |
f2<10>(v); // OK
|
| 1393 |
-
f3(v); // OK
|
| 1394 |
}
|
| 1395 |
```
|
| 1396 |
|
| 1397 |
— *end note*]
|
| 1398 |
|
| 1399 |
-
[*Note
|
| 1400 |
|
| 1401 |
If, in the declaration of a function template with a non-type template
|
| 1402 |
parameter, the non-type template parameter is used in a subexpression in
|
| 1403 |
the function parameter list, the expression is a non-deduced context as
|
| 1404 |
specified above.
|
| 1405 |
|
| 1406 |
-
[*Example
|
| 1407 |
|
| 1408 |
``` cpp
|
| 1409 |
template <int i> class A { ... };
|
| 1410 |
template <int i> void g(A<i+1>);
|
| 1411 |
template <int i> void f(A<i>, A<i+1>);
|
|
@@ -1420,11 +1476,11 @@ void k() {
|
|
| 1420 |
|
| 1421 |
— *end example*]
|
| 1422 |
|
| 1423 |
— *end note*]
|
| 1424 |
|
| 1425 |
-
[*Note
|
| 1426 |
|
| 1427 |
Template parameters do not participate in template argument deduction if
|
| 1428 |
they are used only in non-deduced contexts. For example,
|
| 1429 |
|
| 1430 |
``` cpp
|
|
@@ -1444,13 +1500,16 @@ int x = deduce<77>(a.xm, 62, b.ym);
|
|
| 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.[^
|
| 1450 |
|
| 1451 |
-
|
|
|
|
|
|
|
|
|
|
| 1452 |
|
| 1453 |
``` cpp
|
| 1454 |
template<int i> class A { ... };
|
| 1455 |
template<short s> void f(A<s>);
|
| 1456 |
void k1() {
|
|
@@ -1461,20 +1520,20 @@ void k1() {
|
|
| 1461 |
|
| 1462 |
template<const short cs> class B { };
|
| 1463 |
template<short s> void g(B<s>);
|
| 1464 |
void k2() {
|
| 1465 |
B<1> b;
|
| 1466 |
-
g(b); // OK
|
| 1467 |
}
|
| 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
|
| 1476 |
|
| 1477 |
``` cpp
|
| 1478 |
template<class T> void f(void(*)(T,int));
|
| 1479 |
template<class T> void foo(T,int);
|
| 1480 |
void g(int,int);
|
|
@@ -1482,38 +1541,38 @@ void g(char,int);
|
|
| 1482 |
|
| 1483 |
void h(int,int,int);
|
| 1484 |
void h(char,int);
|
| 1485 |
int m() {
|
| 1486 |
f(&g); // error: ambiguous
|
| 1487 |
-
f(&h); // OK
|
| 1488 |
f(&foo); // error: type deduction fails because foo is a template
|
| 1489 |
}
|
| 1490 |
```
|
| 1491 |
|
| 1492 |
— *end example*]
|
| 1493 |
|
| 1494 |
A template *type-parameter* cannot be deduced from the type of a
|
| 1495 |
function default argument.
|
| 1496 |
|
| 1497 |
-
[*Example
|
| 1498 |
|
| 1499 |
``` cpp
|
| 1500 |
template <class T> void f(T = 5, T = 7);
|
| 1501 |
void g() {
|
| 1502 |
-
f(1); // OK
|
| 1503 |
f(); // error: cannot deduce T
|
| 1504 |
-
f<int>(); // OK
|
| 1505 |
}
|
| 1506 |
```
|
| 1507 |
|
| 1508 |
— *end example*]
|
| 1509 |
|
| 1510 |
The *template-argument* corresponding to a template *template-parameter*
|
| 1511 |
is deduced from the type of the *template-argument* of a class template
|
| 1512 |
specialization used in the argument list of a function call.
|
| 1513 |
|
| 1514 |
-
[*Example
|
| 1515 |
|
| 1516 |
``` cpp
|
| 1517 |
template <template <class T> class X> struct A { };
|
| 1518 |
template <template <class T> class X> void f(A<X>) { }
|
| 1519 |
template<class T> struct B { };
|
|
@@ -1521,15 +1580,15 @@ A<B> ab;
|
|
| 1521 |
f(ab); // calls f(A<B>)
|
| 1522 |
```
|
| 1523 |
|
| 1524 |
— *end example*]
|
| 1525 |
|
| 1526 |
-
[*Note
|
| 1527 |
[[temp.variadic]] can deduce zero or more arguments for each parameter
|
| 1528 |
pack. — *end note*]
|
| 1529 |
|
| 1530 |
-
[*Example
|
| 1531 |
|
| 1532 |
``` cpp
|
| 1533 |
template<class> struct X { };
|
| 1534 |
template<class R, class ... ArgTypes> struct X<R(int, ArgTypes ...)> { };
|
| 1535 |
template<class ... Types> struct Y { };
|
|
@@ -1539,11 +1598,11 @@ template<class ... Types> int f(void (*)(Types ...));
|
|
| 1539 |
void g(int, float);
|
| 1540 |
|
| 1541 |
X<int> x1; // uses primary template
|
| 1542 |
X<int(int, float, double)> x2; // uses partial specialization; ArgTypes contains float, double
|
| 1543 |
X<int(float, int)> x3; // uses primary template
|
| 1544 |
-
Y<> y1; //
|
| 1545 |
Y<int&, float&, double&> y2; // uses partial specialization; T is int&, Types contains float, double
|
| 1546 |
Y<int, float, double> y3; // uses primary template; Types contains int, float, double
|
| 1547 |
int fv = f(g); // OK; Types contains int, float
|
| 1548 |
```
|
| 1549 |
|
|
@@ -1555,12 +1614,12 @@ 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]]
|
| 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]].
|
|
@@ -1570,29 +1629,30 @@ 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
|
| 1576 |
-
|
| 1577 |
-
|
| 1578 |
-
|
| 1579 |
-
|
| 1580 |
-
|
| 1581 |
-
|
|
|
|
| 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
|
| 1591 |
declarations are treated like any other functions in the remainder of
|
| 1592 |
overload resolution, except as explicitly noted in
|
| 1593 |
-
[[over.match.best]].[^
|
| 1594 |
|
| 1595 |
[*Example 1*:
|
| 1596 |
|
| 1597 |
``` cpp
|
| 1598 |
template<class T> T max(T a, T b) { return a>b?a:b; }
|
|
@@ -1609,11 +1669,11 @@ Adding the non-template function
|
|
| 1609 |
``` cpp
|
| 1610 |
int max(int,int);
|
| 1611 |
```
|
| 1612 |
|
| 1613 |
to the example above would resolve the third call, by providing a
|
| 1614 |
-
function that
|
| 1615 |
conversion of `char` to `int` for `c`.
|
| 1616 |
|
| 1617 |
— *end example*]
|
| 1618 |
|
| 1619 |
[*Example 2*:
|
|
@@ -1663,46 +1723,42 @@ a template specialization is a candidate.
|
|
| 1663 |
|
| 1664 |
``` cpp
|
| 1665 |
template<class T> void f(T); // declaration
|
| 1666 |
|
| 1667 |
void g() {
|
| 1668 |
-
f("Annemarie"); //
|
| 1669 |
}
|
| 1670 |
```
|
| 1671 |
|
| 1672 |
-
The call
|
| 1673 |
and not defined at the point of the call. The program will be ill-formed
|
| 1674 |
-
unless a specialization for `f<const char*>`
|
| 1675 |
-
|
| 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]:
|
| 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
|
|
@@ -1711,11 +1767,10 @@ explicitly generated, is present in some translation unit.
|
|
| 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
|
|
@@ -1725,106 +1780,119 @@ explicitly generated, is present in some translation unit.
|
|
| 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 |
-
[
|
| 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.
|
| 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.
|
| 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.
|
|
@@ -1838,51 +1906,50 @@ explicitly generated, is present in some translation unit.
|
|
| 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).
|
| 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).
|
| 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
|
| 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 |
-
[[
|
| 1867 |
implicitly instantiated using the function template definition.
|
| 1868 |
|
| 1869 |
[^10]: This includes friend function declarations.
|
| 1870 |
|
| 1871 |
-
[^11]:
|
| 1872 |
-
|
| 1873 |
|
| 1874 |
-
[^12]:
|
|
|
|
|
|
|
|
|
|
| 1875 |
context; they only become arguments after a function has been
|
| 1876 |
selected.
|
| 1877 |
|
| 1878 |
-
[^
|
| 1879 |
-
*template-parameter* of type `bool`
|
| 1880 |
bound, the resulting value will always be `true` because the array
|
| 1881 |
bound will be nonzero.
|
| 1882 |
|
| 1883 |
-
[^
|
| 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
|
|
|
|
| 1 |
## Function template specializations <a id="temp.fct.spec">[[temp.fct.spec]]</a>
|
| 2 |
|
| 3 |
+
### General <a id="temp.fct.spec.general">[[temp.fct.spec.general]]</a>
|
| 4 |
+
|
| 5 |
A function instantiated from a function template is called a function
|
| 6 |
template specialization; so is an explicit specialization of a function
|
| 7 |
template. Template arguments can be explicitly specified when naming the
|
| 8 |
function template specialization, deduced from the context (e.g.,
|
| 9 |
deduced from the function arguments in a call to the function template
|
|
|
|
| 61 |
```
|
| 62 |
|
| 63 |
— *end example*]
|
| 64 |
|
| 65 |
Template arguments shall not be specified when referring to a
|
| 66 |
+
specialization of a constructor template [[class.ctor]], [[class.qual]].
|
|
|
|
| 67 |
|
| 68 |
A template argument list may be specified when referring to a
|
| 69 |
specialization of a function template
|
| 70 |
|
| 71 |
- when a function is called,
|
|
|
|
| 75 |
- in an explicit instantiation, or
|
| 76 |
- in a friend declaration.
|
| 77 |
|
| 78 |
Trailing template arguments that can be deduced [[temp.deduct]] or
|
| 79 |
obtained from default *template-argument*s may be omitted from the list
|
| 80 |
+
of explicit *template-argument*s.
|
| 81 |
+
|
| 82 |
+
[*Note 1*: A trailing template parameter pack [[temp.variadic]] not
|
| 83 |
+
otherwise deduced will be deduced as an empty sequence of template
|
| 84 |
+
arguments. — *end note*]
|
| 85 |
+
|
| 86 |
+
If all of the template arguments can be deduced or obtained from default
|
| 87 |
+
*template-argument*s, they may all be omitted; in this case, the empty
|
| 88 |
+
template argument list `<>` itself may also be omitted.
|
|
|
|
| 89 |
|
| 90 |
[*Example 2*:
|
| 91 |
|
| 92 |
``` cpp
|
| 93 |
template<class X, class Y> X f(Y);
|
|
|
|
| 103 |
}
|
| 104 |
```
|
| 105 |
|
| 106 |
— *end example*]
|
| 107 |
|
| 108 |
+
[*Note 2*:
|
| 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:
|
|
|
|
| 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 3*:
|
| 152 |
|
| 153 |
Template parameters do not participate in template argument deduction if
|
| 154 |
they are explicitly specified. For example,
|
| 155 |
|
| 156 |
``` cpp
|
|
|
|
| 165 |
}
|
| 166 |
```
|
| 167 |
|
| 168 |
— *end note*]
|
| 169 |
|
| 170 |
+
[*Note 4*: 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 |
|
|
|
|
| 189 |
|
| 190 |
— *end example*]
|
| 191 |
|
| 192 |
### Template argument deduction <a id="temp.deduct">[[temp.deduct]]</a>
|
| 193 |
|
| 194 |
+
#### General <a id="temp.deduct.general">[[temp.deduct.general]]</a>
|
| 195 |
+
|
| 196 |
When a function template specialization is referenced, all of the
|
| 197 |
template arguments shall have values. The values can be explicitly
|
| 198 |
specified or, in some cases, be deduced from the use or obtained from
|
| 199 |
default *template-argument*s.
|
| 200 |
|
|
|
|
| 289 |
|
| 290 |
— *end example*]
|
| 291 |
|
| 292 |
When all template arguments have been deduced or obtained from default
|
| 293 |
template arguments, all uses of template parameters in the template
|
| 294 |
+
parameter list of the template are replaced with the corresponding
|
| 295 |
+
deduced or default argument values. If the substitution results in an
|
| 296 |
+
invalid type, as described above, type deduction fails. If the function
|
| 297 |
+
template has associated constraints [[temp.constr.decl]], those
|
| 298 |
+
constraints are checked for satisfaction [[temp.constr.constr]]. If the
|
| 299 |
+
constraints are not satisfied, type deduction fails. In the context of a
|
| 300 |
+
function call, if type deduction has not yet failed, then for those
|
| 301 |
+
function parameters for which the function call has arguments, each
|
| 302 |
+
function parameter with a type that was non-dependent before
|
| 303 |
+
substitution of any explicitly-specified template arguments is checked
|
| 304 |
+
against its corresponding argument; if the corresponding argument cannot
|
| 305 |
+
be implicitly converted to the parameter type, type deduction fails.
|
| 306 |
+
|
| 307 |
+
[*Note 3*: Overload resolution will check the other parameters,
|
| 308 |
+
including parameters with dependent types in which no template
|
| 309 |
+
parameters participate in template argument deduction and parameters
|
| 310 |
+
that became non-dependent due to substitution of explicitly-specified
|
| 311 |
+
template arguments. — *end note*]
|
| 312 |
+
|
| 313 |
+
If type deduction has not yet failed, then all uses of template
|
| 314 |
+
parameters in the function type are replaced with the corresponding
|
| 315 |
+
deduced or default argument values. If the substitution results in an
|
| 316 |
+
invalid type, as described above, type deduction fails.
|
| 317 |
+
|
| 318 |
+
[*Example 5*:
|
| 319 |
+
|
| 320 |
+
``` cpp
|
| 321 |
+
template <class T> struct Z {
|
| 322 |
+
typedef typename T::x xx;
|
| 323 |
+
};
|
| 324 |
+
template <class T> concept C = requires { typename T::A; };
|
| 325 |
+
template <C T> typename Z<T>::xx f(void *, T); // #1
|
| 326 |
+
template <class T> void f(int, T); // #2
|
| 327 |
+
struct A {} a;
|
| 328 |
+
struct ZZ {
|
| 329 |
+
template <class T, class = typename Z<T>::xx> operator T *();
|
| 330 |
+
operator int();
|
| 331 |
+
};
|
| 332 |
+
int main() {
|
| 333 |
+
ZZ zz;
|
| 334 |
+
f(1, a); // OK, deduction fails for #1 because there is no conversion from int to void*
|
| 335 |
+
f(zz, 42); // OK, deduction fails for #1 because C<int> is not satisfied
|
| 336 |
+
}
|
| 337 |
+
```
|
| 338 |
+
|
| 339 |
+
— *end example*]
|
| 340 |
|
| 341 |
At certain points in the template argument deduction process it is
|
| 342 |
necessary to take a function type that makes use of template parameters
|
| 343 |
and replace those template parameters with the corresponding template
|
| 344 |
arguments. This is done at the beginning of template argument deduction
|
| 345 |
when any explicitly specified template arguments are substituted into
|
| 346 |
the function type, and again at the end of template argument deduction
|
| 347 |
when any template arguments that were deduced or obtained from default
|
| 348 |
arguments are substituted.
|
| 349 |
|
| 350 |
+
The *deduction substitution loci* are
|
| 351 |
+
|
| 352 |
+
- the function type outside of the *noexcept-specifier*,
|
| 353 |
+
- the *explicit-specifier*, and
|
| 354 |
+
- the template parameter declarations.
|
| 355 |
+
|
| 356 |
The substitution occurs in all types and expressions that are used in
|
| 357 |
+
the deduction substitution loci. The expressions include not only
|
| 358 |
+
constant expressions such as those that appear in array bounds or as
|
| 359 |
+
nontype template arguments but also general expressions (i.e.,
|
| 360 |
+
non-constant expressions) inside `sizeof`, `decltype`, and other
|
| 361 |
+
contexts that allow non-constant expressions. The substitution proceeds
|
| 362 |
+
in lexical order and stops when a condition that causes deduction to
|
| 363 |
+
fail is encountered. If substitution into different declarations of the
|
| 364 |
+
same function template would cause template instantiations to occur in a
|
| 365 |
+
different order or not at all, the program is ill-formed; no diagnostic
|
| 366 |
+
required.
|
| 367 |
|
| 368 |
+
[*Note 4*: The equivalent substitution in exception specifications is
|
| 369 |
done only when the *noexcept-specifier* is instantiated, at which point
|
| 370 |
a program is ill-formed if the substitution results in an invalid type
|
| 371 |
or expression. — *end note*]
|
| 372 |
|
| 373 |
+
[*Example 6*:
|
| 374 |
|
| 375 |
``` cpp
|
| 376 |
template <class T> struct A { using X = typename T::X; };
|
| 377 |
template <class T> typename T::X f(typename A<T>::X);
|
| 378 |
template <class T> void f(...) { }
|
|
|
|
| 391 |
|
| 392 |
— *end example*]
|
| 393 |
|
| 394 |
If a substitution results in an invalid type or expression, type
|
| 395 |
deduction fails. An invalid type or expression is one that would be
|
| 396 |
+
ill-formed, with a diagnostic required, if written in the same context
|
| 397 |
+
using the substituted arguments.
|
| 398 |
|
| 399 |
+
[*Note 5*: If no diagnostic is required, the program is still
|
| 400 |
ill-formed. Access checking is done as part of the substitution
|
| 401 |
process. — *end note*]
|
| 402 |
|
| 403 |
+
Invalid types and expressions can result in a deduction failure only in
|
| 404 |
+
the immediate context of the deduction substitution loci.
|
|
|
|
| 405 |
|
| 406 |
+
[*Note 6*: The substitution into types and expressions can result in
|
| 407 |
effects such as the instantiation of class template specializations
|
| 408 |
and/or function template specializations, the generation of
|
| 409 |
implicitly-defined functions, etc. Such effects are not in the
|
| 410 |
“immediate context” and can result in the program being
|
| 411 |
ill-formed. — *end note*]
|
| 412 |
|
| 413 |
A *lambda-expression* appearing in a function type or a template
|
| 414 |
parameter is not considered part of the immediate context for the
|
| 415 |
purposes of template argument deduction.
|
| 416 |
|
| 417 |
+
[*Note 7*:
|
| 418 |
|
| 419 |
The intent is to avoid requiring implementations to deal with
|
| 420 |
substitution failure involving arbitrary statements.
|
| 421 |
|
| 422 |
+
[*Example 7*:
|
| 423 |
|
| 424 |
``` cpp
|
| 425 |
template <class T>
|
| 426 |
auto f(T) -> decltype([]() { T::invalid; } ());
|
| 427 |
void f(...);
|
|
|
|
| 450 |
|
| 451 |
— *end example*]
|
| 452 |
|
| 453 |
— *end note*]
|
| 454 |
|
| 455 |
+
[*Example 8*:
|
| 456 |
|
| 457 |
``` cpp
|
| 458 |
struct X { };
|
| 459 |
struct Y {
|
| 460 |
Y(X) {}
|
|
|
|
| 467 |
X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
|
| 468 |
```
|
| 469 |
|
| 470 |
— *end example*]
|
| 471 |
|
| 472 |
+
[*Note 8*:
|
| 473 |
|
| 474 |
+
Type deduction can fail for the following reasons:
|
| 475 |
|
| 476 |
- Attempting to instantiate a pack expansion containing multiple packs
|
| 477 |
of differing lengths.
|
| 478 |
- Attempting to create an array with an element type that is `void`, a
|
| 479 |
function type, or a reference type, or attempting to create an array
|
| 480 |
with a size that is zero or negative.
|
| 481 |
+
\[*Example 9*:
|
| 482 |
``` cpp
|
| 483 |
template <class T> int f(T[5]);
|
| 484 |
int I = f<int>(0);
|
| 485 |
int j = f<void>(0); // invalid array
|
| 486 |
```
|
| 487 |
|
| 488 |
— *end example*]
|
| 489 |
- Attempting to use a type that is not a class or enumeration type in a
|
| 490 |
qualified name.
|
| 491 |
+
\[*Example 10*:
|
| 492 |
``` cpp
|
| 493 |
template <class T> int f(typename T::B*);
|
| 494 |
int i = f<int>(0);
|
| 495 |
```
|
| 496 |
|
|
|
|
| 501 |
- the specified member is not a type where a type is required, or
|
| 502 |
- the specified member is not a template where a template is required,
|
| 503 |
or
|
| 504 |
- the specified member is not a non-type where a non-type is required.
|
| 505 |
|
| 506 |
+
\[*Example 11*:
|
| 507 |
``` cpp
|
| 508 |
template <int I> struct X { };
|
| 509 |
template <template <class T> class> struct Z { };
|
| 510 |
template <class T> void f(typename T::Y*) {}
|
| 511 |
template <class T> void g(X<T::N>*) {}
|
| 512 |
+
template <class T> void h(Z<T::TT>*) {}
|
| 513 |
struct A {};
|
| 514 |
struct B { int Y; };
|
| 515 |
struct C {
|
| 516 |
typedef int N;
|
| 517 |
};
|
|
|
|
| 531 |
— *end example*]
|
| 532 |
- Attempting to create a pointer to reference type.
|
| 533 |
- Attempting to create a reference to `void`.
|
| 534 |
- Attempting to create “pointer to member of `T`” when `T` is not a
|
| 535 |
class type.
|
| 536 |
+
\[*Example 12*:
|
| 537 |
``` cpp
|
| 538 |
template <class T> int f(int T::*);
|
| 539 |
int i = f<int>(0);
|
| 540 |
```
|
| 541 |
|
| 542 |
— *end example*]
|
| 543 |
- Attempting to give an invalid type to a non-type template parameter.
|
| 544 |
+
\[*Example 13*:
|
| 545 |
``` cpp
|
| 546 |
template <class T, T> struct S {};
|
| 547 |
+
template <class T> int f(S<T, T{}>*); // #1
|
| 548 |
+
class X {
|
| 549 |
+
int m;
|
| 550 |
+
};
|
| 551 |
+
int i0 = f<X>(0); // #1 uses a value of non-structural type X as a non-type template argument
|
| 552 |
```
|
| 553 |
|
| 554 |
— *end example*]
|
| 555 |
- Attempting to perform an invalid conversion in either a template
|
| 556 |
argument expression, or an expression used in the function
|
| 557 |
declaration.
|
| 558 |
+
\[*Example 14*:
|
| 559 |
``` cpp
|
| 560 |
template <class T, T*> int f(int);
|
| 561 |
+
int i2 = f<int,1>(0); // can't convert 1 to int*
|
| 562 |
```
|
| 563 |
|
| 564 |
— *end example*]
|
| 565 |
- Attempting to create a function type in which a parameter has a type
|
| 566 |
of `void`, or in which the return type is a function type or array
|
| 567 |
type.
|
| 568 |
|
| 569 |
— *end note*]
|
| 570 |
|
| 571 |
+
[*Example 15*:
|
| 572 |
|
| 573 |
In the following example, assuming a `signed char` cannot represent the
|
| 574 |
value 1000, a narrowing conversion [[dcl.init.list]] would be required
|
| 575 |
to convert the *template-argument* of type `int` to `signed char`,
|
| 576 |
therefore substitution fails for the second template
|
|
|
|
| 756 |
template <typename... T> struct X;
|
| 757 |
template <> struct X<> {};
|
| 758 |
template <typename T, typename... Ts>
|
| 759 |
struct X<T, Ts...> : X<Ts...> {};
|
| 760 |
struct D : X<int> {};
|
| 761 |
+
struct E : X<>, X<int> {};
|
| 762 |
|
| 763 |
template <typename... T>
|
| 764 |
int f(const X<T...>&);
|
| 765 |
int x = f(D()); // calls f<int>, not f<>
|
| 766 |
// B is X<>, C is X<int>
|
| 767 |
+
int z = f(E()); // calls f<int>, not f<>
|
| 768 |
```
|
| 769 |
|
| 770 |
— *end example*]
|
| 771 |
|
| 772 |
These alternatives are considered only if type deduction would otherwise
|
|
|
|
| 825 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 826 |
```
|
| 827 |
|
| 828 |
— *end example*]
|
| 829 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 830 |
#### Deducing template arguments taking the address of a function template <a id="temp.deduct.funcaddr">[[temp.deduct.funcaddr]]</a>
|
| 831 |
|
| 832 |
Template arguments can be deduced from the type specified when taking
|
| 833 |
+
the address of an overload set [[over.over]]. If there is a target, the
|
| 834 |
+
function template’s function type and the target type are used as the
|
| 835 |
+
types of `P` and `A`, and the deduction is done as described in
|
| 836 |
+
[[temp.deduct.type]]. Otherwise, deduction is performed with empty sets
|
| 837 |
+
of types `P` and `A`.
|
| 838 |
|
| 839 |
A placeholder type [[dcl.spec.auto]] in the return type of a function
|
| 840 |
template is a non-deduced context. If template argument deduction
|
| 841 |
succeeds for such a function, the return type is determined from
|
| 842 |
instantiation of the function body.
|
| 843 |
|
| 844 |
#### Deducing conversion function template arguments <a id="temp.deduct.conv">[[temp.deduct.conv]]</a>
|
| 845 |
|
| 846 |
Template argument deduction is done by comparing the return type of the
|
| 847 |
+
conversion function template (call it `P`) with the type specified by
|
| 848 |
+
the *conversion-type-id* of the *conversion-function-id* being looked up
|
| 849 |
+
(call it `A`) as described in [[temp.deduct.type]]. If the
|
| 850 |
+
*conversion-function-id* is constructed during overload resolution
|
| 851 |
+
[[over.match.funcs]], the rules in the remainder of this subclause
|
| 852 |
+
apply.
|
| 853 |
|
| 854 |
If `P` is a reference type, the type referred to by `P` is used in place
|
| 855 |
of `P` for type deduction and for any further references to or
|
| 856 |
transformations of `P` in the remainder of this subclause.
|
| 857 |
|
|
|
|
| 869 |
If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s type
|
| 870 |
are ignored for type deduction. If `A` is a reference type, the type
|
| 871 |
referred to by `A` is used for type deduction.
|
| 872 |
|
| 873 |
In general, the deduction process attempts to find template argument
|
| 874 |
+
values that will make the deduced `A` identical to `A`. However, certain
|
| 875 |
+
attributes of `A` may be ignored:
|
| 876 |
|
| 877 |
+
- If the original `A` is a reference type, any cv-qualifiers of `A`
|
| 878 |
+
(i.e., the type referred to by the reference).
|
| 879 |
+
- If the original `A` is a function pointer or
|
| 880 |
+
pointer-to-member-function type with a potentially-throwing exception
|
| 881 |
+
specification [[except.spec]], the exception specification.
|
| 882 |
+
- Any cv-qualifiers in `A` that can be restored by a qualification
|
| 883 |
+
conversion.
|
|
|
|
|
|
|
| 884 |
|
| 885 |
+
These attributes are ignored only if type deduction would otherwise
|
| 886 |
+
fail. If ignoring them allows more than one possible deduced `A`, the
|
| 887 |
+
type deduction fails.
|
| 888 |
|
| 889 |
#### Deducing template arguments during partial ordering <a id="temp.deduct.partial">[[temp.deduct.partial]]</a>
|
| 890 |
|
| 891 |
Template argument deduction is done by comparing certain types
|
| 892 |
associated with the two function templates being compared.
|
|
|
|
| 908 |
|
| 909 |
The types used to determine the ordering depend on the context in which
|
| 910 |
the partial ordering is done:
|
| 911 |
|
| 912 |
- In the context of a function call, the types used are those function
|
| 913 |
+
parameter types for which the function call has arguments.[^13]
|
| 914 |
- In the context of a call to a conversion function, the return types of
|
| 915 |
the conversion function templates are used.
|
| 916 |
- In other contexts [[temp.func.order]] the function template’s function
|
| 917 |
type is used.
|
| 918 |
|
|
|
|
| 1053 |
array bound if it is not otherwise deduced.
|
| 1054 |
|
| 1055 |
A given type `P` can be composed from a number of other types,
|
| 1056 |
templates, and non-type values:
|
| 1057 |
|
| 1058 |
+
- A function type includes the types of each of the function parameters,
|
| 1059 |
+
the return type, and its exception specification.
|
| 1060 |
- A pointer-to-member type includes the type of the class object pointed
|
| 1061 |
to and the type of the member pointed to.
|
| 1062 |
- A type that is a specialization of a class template (e.g., `A<int>`)
|
| 1063 |
includes the types, templates, and non-type values referenced by the
|
| 1064 |
template argument list of the specialization.
|
|
|
|
| 1131 |
``` cpp
|
| 1132 |
template<class T> void f(T x, T y) { ... }
|
| 1133 |
struct A { ... };
|
| 1134 |
struct B : A { ... };
|
| 1135 |
void g(A a, B b) {
|
| 1136 |
+
f(a,b); // error: T deduced as both A and B
|
| 1137 |
+
f(b,a); // error: T deduced as both A and B
|
| 1138 |
+
f(a,a); // OK, T is A
|
| 1139 |
+
f(b,b); // OK, T is B
|
| 1140 |
}
|
| 1141 |
```
|
| 1142 |
|
| 1143 |
Here is an example where two template arguments are deduced from a
|
| 1144 |
single function parameter/argument pair. This can lead to conflicts that
|
|
|
|
| 1150 |
int g1( int, float, float);
|
| 1151 |
char g2( int, float, float);
|
| 1152 |
int g3( int, char, float);
|
| 1153 |
|
| 1154 |
void r() {
|
| 1155 |
+
f(g1); // OK, T is int and U is float
|
| 1156 |
+
f(g2); // error: T deduced as both char and int
|
| 1157 |
+
f(g3); // error: U deduced as both char and float
|
| 1158 |
+
}
|
| 1159 |
+
```
|
| 1160 |
+
|
| 1161 |
+
Here is an example where the exception specification of a function type
|
| 1162 |
+
is deduced:
|
| 1163 |
+
|
| 1164 |
+
``` cpp
|
| 1165 |
+
template<bool E> void f1(void (*)() noexcept(E));
|
| 1166 |
+
template<bool> struct A { };
|
| 1167 |
+
template<bool B> void f2(void (*)(A<B>) noexcept(B));
|
| 1168 |
+
|
| 1169 |
+
void g1();
|
| 1170 |
+
void g2() noexcept;
|
| 1171 |
+
void g3(A<true>);
|
| 1172 |
+
|
| 1173 |
+
void h() {
|
| 1174 |
+
f1(g1); // OK, E is false
|
| 1175 |
+
f1(g2); // OK, E is true
|
| 1176 |
+
f2(g3); // error: B deduced as both true and false
|
| 1177 |
}
|
| 1178 |
```
|
| 1179 |
|
| 1180 |
Here is an example where a qualification conversion applies between the
|
| 1181 |
argument type on the function call and the deduced template argument
|
|
|
|
| 1205 |
}
|
| 1206 |
```
|
| 1207 |
|
| 1208 |
— *end example*]
|
| 1209 |
|
| 1210 |
+
A template type argument `T`, a template template argument `TT`, or a
|
| 1211 |
template non-type argument `i` can be deduced if `P` and `A` have one of
|
| 1212 |
the following forms:
|
| 1213 |
|
| 1214 |
``` cpp
|
| 1215 |
+
\opt{cv} T
|
|
|
|
| 1216 |
T*
|
| 1217 |
T&
|
| 1218 |
T&&
|
| 1219 |
+
\opt{T}[\opt{i}]
|
| 1220 |
+
\opt{T}(\opt{T}) noexcept(\opt{i})
|
| 1221 |
+
\opt{T} \opt{T}::*
|
| 1222 |
+
\opt{TT}<T>
|
| 1223 |
+
\opt{TT}<i>
|
| 1224 |
+
\opt{TT}<TT>
|
| 1225 |
+
\opt{TT}<>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1226 |
```
|
| 1227 |
|
| 1228 |
+
where
|
| 1229 |
+
|
| 1230 |
+
- `\opt{T}` represents a type or parameter-type-list that either
|
| 1231 |
+
satisfies these rules recursively, is a non-deduced context in `P` or
|
| 1232 |
+
`A`, or is the same non-dependent type in `P` and `A`,
|
| 1233 |
+
- `\opt{TT}` represents either a class template or a template template
|
| 1234 |
+
parameter,
|
| 1235 |
+
- `\opt{i}` represents an expression that either is an `i`, is
|
| 1236 |
+
value-dependent in `P` or `A`, or has the same constant value in `P`
|
| 1237 |
+
and `A`, and
|
| 1238 |
+
- `noexcept(\opt{i})` represents an exception specification
|
| 1239 |
+
[[except.spec]] in which the (possibly-implicit, see [[dcl.fct]])
|
| 1240 |
+
*noexcept-specifier*’s operand satisfies the rules for an `\opt{i}`
|
| 1241 |
+
above.
|
| 1242 |
+
|
| 1243 |
+
[*Note 2*: If a type matches such a form but contains no `T`s, `i`s, or
|
| 1244 |
+
`TT`s, deduction is not possible. — *end note*]
|
| 1245 |
+
|
| 1246 |
+
Similarly, `<T>` represents template argument lists where at least one
|
| 1247 |
+
argument contains a `T`, `<i>` represents template argument lists where
|
| 1248 |
+
at least one argument contains an `i` and `<>` represents template
|
| 1249 |
+
argument lists where no argument contains a `T` or an `i`.
|
| 1250 |
|
| 1251 |
If `P` has a form that contains `<T>` or `<i>`, then each argument Pᵢ of
|
| 1252 |
the respective template argument list of `P` is compared with the
|
| 1253 |
corresponding argument Aᵢ of the corresponding template argument list of
|
| 1254 |
`A`. If the template argument list of `P` contains a pack expansion that
|
|
|
|
| 1292 |
respectively, `Pᵢ` is adjusted if it is a forwarding reference
|
| 1293 |
[[temp.deduct.call]] and `Aᵢ` is an lvalue reference, in which case the
|
| 1294 |
type of `Pᵢ` is changed to be the template parameter type (i.e., `T&&`
|
| 1295 |
is changed to simply `T`).
|
| 1296 |
|
| 1297 |
+
[*Note 3*: As a result, when `Pᵢ` is `T&&` and `Aᵢ` is `X&`, the
|
| 1298 |
adjusted `Pᵢ` will be `T`, causing `T` to be deduced as
|
| 1299 |
`X&`. — *end note*]
|
| 1300 |
|
| 1301 |
[*Example 5*:
|
| 1302 |
|
|
|
|
| 1399 |
using V = S<int[42]>::Q; // OK; T was deduced as std::size_t from the type int[42]
|
| 1400 |
```
|
| 1401 |
|
| 1402 |
— *end example*]
|
| 1403 |
|
| 1404 |
+
The type of `B` in the *noexcept-specifier* `noexcept(B)` of a function
|
| 1405 |
+
type is `bool`.
|
| 1406 |
+
|
| 1407 |
[*Example 10*:
|
| 1408 |
|
| 1409 |
+
``` cpp
|
| 1410 |
+
template<bool> struct A { };
|
| 1411 |
+
template<auto> struct B;
|
| 1412 |
+
template<auto X, void (*F)() noexcept(X)> struct B<F> {
|
| 1413 |
+
A<X> ax;
|
| 1414 |
+
};
|
| 1415 |
+
void f_nothrow() noexcept;
|
| 1416 |
+
B<f_nothrow> bn; // OK, type of X deduced as bool
|
| 1417 |
+
```
|
| 1418 |
+
|
| 1419 |
+
— *end example*]
|
| 1420 |
+
|
| 1421 |
+
[*Example 11*:
|
| 1422 |
+
|
| 1423 |
``` cpp
|
| 1424 |
template<class T, T i> void f(int (&a)[i]);
|
| 1425 |
int v[10];
|
| 1426 |
void g() {
|
| 1427 |
+
f(v); // OK, T is std::size_t
|
| 1428 |
}
|
| 1429 |
```
|
| 1430 |
|
| 1431 |
— *end example*]
|
| 1432 |
|
| 1433 |
+
[*Note 4*:
|
| 1434 |
|
| 1435 |
Except for reference and pointer types, a major array bound is not part
|
| 1436 |
of a function parameter type and cannot be deduced from an argument:
|
| 1437 |
|
| 1438 |
``` cpp
|
|
|
|
| 1440 |
template<int i> void f2(int a[i][20]);
|
| 1441 |
template<int i> void f3(int (&a)[i][20]);
|
| 1442 |
|
| 1443 |
void g() {
|
| 1444 |
int v[10][20];
|
| 1445 |
+
f1(v); // OK, i deduced as 20
|
| 1446 |
f1<20>(v); // OK
|
| 1447 |
f2(v); // error: cannot deduce template-argument i
|
| 1448 |
f2<10>(v); // OK
|
| 1449 |
+
f3(v); // OK, i deduced as 10
|
| 1450 |
}
|
| 1451 |
```
|
| 1452 |
|
| 1453 |
— *end note*]
|
| 1454 |
|
| 1455 |
+
[*Note 5*:
|
| 1456 |
|
| 1457 |
If, in the declaration of a function template with a non-type template
|
| 1458 |
parameter, the non-type template parameter is used in a subexpression in
|
| 1459 |
the function parameter list, the expression is a non-deduced context as
|
| 1460 |
specified above.
|
| 1461 |
|
| 1462 |
+
[*Example 12*:
|
| 1463 |
|
| 1464 |
``` cpp
|
| 1465 |
template <int i> class A { ... };
|
| 1466 |
template <int i> void g(A<i+1>);
|
| 1467 |
template <int i> void f(A<i>, A<i+1>);
|
|
|
|
| 1476 |
|
| 1477 |
— *end example*]
|
| 1478 |
|
| 1479 |
— *end note*]
|
| 1480 |
|
| 1481 |
+
[*Note 6*:
|
| 1482 |
|
| 1483 |
Template parameters do not participate in template argument deduction if
|
| 1484 |
they are used only in non-deduced contexts. For example,
|
| 1485 |
|
| 1486 |
``` cpp
|
|
|
|
| 1500 |
|
| 1501 |
If `P` has a form that contains `<i>`, and if the type of `i` differs
|
| 1502 |
from the type of the corresponding template parameter of the template
|
| 1503 |
named by the enclosing *simple-template-id*, deduction fails. If `P` has
|
| 1504 |
a form that contains `[i]`, and if the type of `i` is not an integral
|
| 1505 |
+
type, deduction fails.[^14]
|
| 1506 |
|
| 1507 |
+
If `P` has a form that includes `noexcept(i)` and the type of `i` is not
|
| 1508 |
+
`bool`, deduction fails.
|
| 1509 |
+
|
| 1510 |
+
[*Example 13*:
|
| 1511 |
|
| 1512 |
``` cpp
|
| 1513 |
template<int i> class A { ... };
|
| 1514 |
template<short s> void f(A<s>);
|
| 1515 |
void k1() {
|
|
|
|
| 1520 |
|
| 1521 |
template<const short cs> class B { };
|
| 1522 |
template<short s> void g(B<s>);
|
| 1523 |
void k2() {
|
| 1524 |
B<1> b;
|
| 1525 |
+
g(b); // OK, cv-qualifiers are ignored on template parameter types
|
| 1526 |
}
|
| 1527 |
```
|
| 1528 |
|
| 1529 |
— *end example*]
|
| 1530 |
|
| 1531 |
A *template-argument* can be deduced from a function, pointer to
|
| 1532 |
function, or pointer-to-member-function type.
|
| 1533 |
|
| 1534 |
+
[*Example 14*:
|
| 1535 |
|
| 1536 |
``` cpp
|
| 1537 |
template<class T> void f(void(*)(T,int));
|
| 1538 |
template<class T> void foo(T,int);
|
| 1539 |
void g(int,int);
|
|
|
|
| 1541 |
|
| 1542 |
void h(int,int,int);
|
| 1543 |
void h(char,int);
|
| 1544 |
int m() {
|
| 1545 |
f(&g); // error: ambiguous
|
| 1546 |
+
f(&h); // OK, void h(char,int) is a unique match
|
| 1547 |
f(&foo); // error: type deduction fails because foo is a template
|
| 1548 |
}
|
| 1549 |
```
|
| 1550 |
|
| 1551 |
— *end example*]
|
| 1552 |
|
| 1553 |
A template *type-parameter* cannot be deduced from the type of a
|
| 1554 |
function default argument.
|
| 1555 |
|
| 1556 |
+
[*Example 15*:
|
| 1557 |
|
| 1558 |
``` cpp
|
| 1559 |
template <class T> void f(T = 5, T = 7);
|
| 1560 |
void g() {
|
| 1561 |
+
f(1); // OK, calls f<int>(1,7)
|
| 1562 |
f(); // error: cannot deduce T
|
| 1563 |
+
f<int>(); // OK, calls f<int>(5,7)
|
| 1564 |
}
|
| 1565 |
```
|
| 1566 |
|
| 1567 |
— *end example*]
|
| 1568 |
|
| 1569 |
The *template-argument* corresponding to a template *template-parameter*
|
| 1570 |
is deduced from the type of the *template-argument* of a class template
|
| 1571 |
specialization used in the argument list of a function call.
|
| 1572 |
|
| 1573 |
+
[*Example 16*:
|
| 1574 |
|
| 1575 |
``` cpp
|
| 1576 |
template <template <class T> class X> struct A { };
|
| 1577 |
template <template <class T> class X> void f(A<X>) { }
|
| 1578 |
template<class T> struct B { };
|
|
|
|
| 1580 |
f(ab); // calls f(A<B>)
|
| 1581 |
```
|
| 1582 |
|
| 1583 |
— *end example*]
|
| 1584 |
|
| 1585 |
+
[*Note 7*: Template argument deduction involving parameter packs
|
| 1586 |
[[temp.variadic]] can deduce zero or more arguments for each parameter
|
| 1587 |
pack. — *end note*]
|
| 1588 |
|
| 1589 |
+
[*Example 17*:
|
| 1590 |
|
| 1591 |
``` cpp
|
| 1592 |
template<class> struct X { };
|
| 1593 |
template<class R, class ... ArgTypes> struct X<R(int, ArgTypes ...)> { };
|
| 1594 |
template<class ... Types> struct Y { };
|
|
|
|
| 1598 |
void g(int, float);
|
| 1599 |
|
| 1600 |
X<int> x1; // uses primary template
|
| 1601 |
X<int(int, float, double)> x2; // uses partial specialization; ArgTypes contains float, double
|
| 1602 |
X<int(float, int)> x3; // uses primary template
|
| 1603 |
+
Y<> y1; // uses primary template; Types is empty
|
| 1604 |
Y<int&, float&, double&> y2; // uses partial specialization; T is int&, Types contains float, double
|
| 1605 |
Y<int, float, double> y3; // uses primary template; Types contains int, float, double
|
| 1606 |
int fv = f(g); // OK; Types contains int, float
|
| 1607 |
```
|
| 1608 |
|
|
|
|
| 1614 |
function template, template argument deduction is performed to identify
|
| 1615 |
the specialization to which the declaration refers. Specifically, this
|
| 1616 |
is done for explicit instantiations [[temp.explicit]], explicit
|
| 1617 |
specializations [[temp.expl.spec]], and certain friend declarations
|
| 1618 |
[[temp.friend]]. This is also done to determine whether a deallocation
|
| 1619 |
+
function template specialization matches a placement `operator new`
|
| 1620 |
+
[[basic.stc.dynamic.deallocation]], [[expr.new]]. In all these cases,
|
| 1621 |
`P` is the type of the function template being considered as a potential
|
| 1622 |
match and `A` is either the function type from the declaration or the
|
| 1623 |
type of the deallocation function that would match the placement
|
| 1624 |
`operator new` as described in [[expr.new]]. The deduction is done as
|
| 1625 |
described in [[temp.deduct.type]].
|
|
|
|
| 1629 |
[[temp.func.order]], deduction fails and, in the declaration cases, the
|
| 1630 |
program is ill-formed.
|
| 1631 |
|
| 1632 |
### Overload resolution <a id="temp.over">[[temp.over]]</a>
|
| 1633 |
|
| 1634 |
+
When a call of a function or function template is written (explicitly,
|
| 1635 |
+
or implicitly using the operator notation), template argument deduction
|
| 1636 |
+
[[temp.deduct]] and checking of any explicit template arguments
|
| 1637 |
+
[[temp.arg]] are performed for each function template to find the
|
| 1638 |
+
template argument values (if any) that can be used with that function
|
| 1639 |
+
template to instantiate a function template specialization that can be
|
| 1640 |
+
invoked with the call arguments or, for conversion function templates,
|
| 1641 |
+
that can convert to the required type. For each function template, if
|
| 1642 |
the argument deduction and checking succeeds, the *template-argument*s
|
| 1643 |
(deduced and/or explicit) are used to synthesize the declaration of a
|
| 1644 |
single function template specialization which is added to the candidate
|
| 1645 |
functions set to be used in overload resolution. If, for a given
|
| 1646 |
function template, argument deduction fails or the synthesized function
|
| 1647 |
template specialization would be ill-formed, no such function is added
|
| 1648 |
to the set of candidate functions for that template. The complete set of
|
| 1649 |
candidate functions includes all the synthesized declarations and all of
|
| 1650 |
+
the non-template functions found by name lookup. The synthesized
|
| 1651 |
declarations are treated like any other functions in the remainder of
|
| 1652 |
overload resolution, except as explicitly noted in
|
| 1653 |
+
[[over.match.best]].[^15]
|
| 1654 |
|
| 1655 |
[*Example 1*:
|
| 1656 |
|
| 1657 |
``` cpp
|
| 1658 |
template<class T> T max(T a, T b) { return a>b?a:b; }
|
|
|
|
| 1669 |
``` cpp
|
| 1670 |
int max(int,int);
|
| 1671 |
```
|
| 1672 |
|
| 1673 |
to the example above would resolve the third call, by providing a
|
| 1674 |
+
function that can be called for `max(a,c)` after using the standard
|
| 1675 |
conversion of `char` to `int` for `c`.
|
| 1676 |
|
| 1677 |
— *end example*]
|
| 1678 |
|
| 1679 |
[*Example 2*:
|
|
|
|
| 1723 |
|
| 1724 |
``` cpp
|
| 1725 |
template<class T> void f(T); // declaration
|
| 1726 |
|
| 1727 |
void g() {
|
| 1728 |
+
f("Annemarie"); // calls f<const char*>
|
| 1729 |
}
|
| 1730 |
```
|
| 1731 |
|
| 1732 |
+
The call to `f` is well-formed even if the template `f` is only declared
|
| 1733 |
and not defined at the point of the call. The program will be ill-formed
|
| 1734 |
+
unless a specialization for `f<const char*>` is explicitly instantiated
|
| 1735 |
+
in some translation unit [[temp.pre]].
|
| 1736 |
|
| 1737 |
— *end example*]
|
| 1738 |
|
| 1739 |
<!-- Link reference definitions -->
|
| 1740 |
[basic.def]: basic.md#basic.def
|
| 1741 |
[basic.def.odr]: basic.md#basic.def.odr
|
| 1742 |
[basic.link]: basic.md#basic.link
|
| 1743 |
[basic.lookup]: basic.md#basic.lookup
|
| 1744 |
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
|
|
|
| 1745 |
[basic.lookup.qual]: basic.md#basic.lookup.qual
|
|
|
|
|
|
|
|
|
|
| 1746 |
[basic.scope.namespace]: basic.md#basic.scope.namespace
|
| 1747 |
+
[basic.scope.scope]: basic.md#basic.scope.scope
|
| 1748 |
[basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
|
| 1749 |
[basic.types]: basic.md#basic.types
|
| 1750 |
[class.access]: class.md#class.access
|
| 1751 |
[class.base.init]: class.md#class.base.init
|
| 1752 |
[class.conv.fct]: class.md#class.conv.fct
|
| 1753 |
[class.ctor]: class.md#class.ctor
|
| 1754 |
[class.default.ctor]: class.md#class.default.ctor
|
| 1755 |
[class.derived]: class.md#class.derived
|
| 1756 |
[class.dtor]: class.md#class.dtor
|
|
|
|
| 1757 |
[class.local]: class.md#class.local
|
| 1758 |
[class.mem]: class.md#class.mem
|
| 1759 |
+
[class.member.lookup]: basic.md#class.member.lookup
|
| 1760 |
[class.pre]: class.md#class.pre
|
| 1761 |
[class.qual]: basic.md#class.qual
|
| 1762 |
[class.temporary]: basic.md#class.temporary
|
| 1763 |
[conv]: expr.md#conv
|
| 1764 |
[conv.array]: expr.md#conv.array
|
|
|
|
| 1767 |
[conv.lval]: expr.md#conv.lval
|
| 1768 |
[conv.qual]: expr.md#conv.qual
|
| 1769 |
[dcl.align]: dcl.md#dcl.align
|
| 1770 |
[dcl.attr.grammar]: dcl.md#dcl.attr.grammar
|
| 1771 |
[dcl.decl]: dcl.md#dcl.decl
|
|
|
|
| 1772 |
[dcl.fct]: dcl.md#dcl.fct
|
| 1773 |
[dcl.fct.def.general]: dcl.md#dcl.fct.def.general
|
| 1774 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 1775 |
[dcl.init]: dcl.md#dcl.init
|
| 1776 |
[dcl.init.list]: dcl.md#dcl.init.list
|
|
|
|
| 1780 |
[dcl.stc]: dcl.md#dcl.stc
|
| 1781 |
[dcl.struct.bind]: dcl.md#dcl.struct.bind
|
| 1782 |
[dcl.type.class.deduct]: dcl.md#dcl.type.class.deduct
|
| 1783 |
[dcl.type.elab]: dcl.md#dcl.type.elab
|
| 1784 |
[dcl.type.simple]: dcl.md#dcl.type.simple
|
| 1785 |
+
[depr.template.template]: future.md#depr.template.template
|
| 1786 |
[except.spec]: except.md#except.spec
|
| 1787 |
[expr.const]: expr.md#expr.const
|
| 1788 |
[expr.context]: expr.md#expr.context
|
| 1789 |
[expr.log.and]: expr.md#expr.log.and
|
| 1790 |
[expr.log.or]: expr.md#expr.log.or
|
| 1791 |
[expr.new]: expr.md#expr.new
|
| 1792 |
[expr.prim.fold]: expr.md#expr.prim.fold
|
| 1793 |
[expr.prim.id]: expr.md#expr.prim.id
|
| 1794 |
+
[expr.prim.id.qual]: expr.md#expr.prim.id.qual
|
| 1795 |
[expr.prim.id.unqual]: expr.md#expr.prim.id.unqual
|
| 1796 |
[expr.prim.lambda.capture]: expr.md#expr.prim.lambda.capture
|
| 1797 |
[expr.prim.lambda.closure]: expr.md#expr.prim.lambda.closure
|
| 1798 |
+
[expr.prim.this]: expr.md#expr.prim.this
|
| 1799 |
[expr.ref]: expr.md#expr.ref
|
| 1800 |
[expr.sizeof]: expr.md#expr.sizeof
|
| 1801 |
+
[expr.type.conv]: expr.md#expr.type.conv
|
| 1802 |
[expr.typeid]: expr.md#expr.typeid
|
| 1803 |
[expr.unary.op]: expr.md#expr.unary.op
|
| 1804 |
[implimits]: limits.md#implimits
|
| 1805 |
[intro.defs]: intro.md#intro.defs
|
| 1806 |
[intro.object]: basic.md#intro.object
|
| 1807 |
[lex.string]: lex.md#lex.string
|
| 1808 |
+
[module.unit]: module.md#module.unit
|
|
|
|
| 1809 |
[namespace.udecl]: dcl.md#namespace.udecl
|
|
|
|
| 1810 |
[over.match]: over.md#over.match
|
| 1811 |
[over.match.best]: over.md#over.match.best
|
| 1812 |
[over.match.class.deduct]: over.md#over.match.class.deduct
|
| 1813 |
+
[over.match.funcs]: over.md#over.match.funcs
|
| 1814 |
[over.match.oper]: over.md#over.match.oper
|
|
|
|
| 1815 |
[over.match.viable]: over.md#over.match.viable
|
| 1816 |
[over.over]: over.md#over.over
|
| 1817 |
[special]: class.md#special
|
| 1818 |
[stmt.if]: stmt.md#stmt.if
|
| 1819 |
[support.types]: support.md#support.types
|
| 1820 |
[temp]: #temp
|
| 1821 |
[temp.alias]: #temp.alias
|
| 1822 |
[temp.arg]: #temp.arg
|
| 1823 |
[temp.arg.explicit]: #temp.arg.explicit
|
| 1824 |
+
[temp.arg.general]: #temp.arg.general
|
| 1825 |
[temp.arg.nontype]: #temp.arg.nontype
|
| 1826 |
[temp.arg.template]: #temp.arg.template
|
| 1827 |
[temp.arg.type]: #temp.arg.type
|
| 1828 |
[temp.class]: #temp.class
|
| 1829 |
+
[temp.class.general]: #temp.class.general
|
|
|
|
|
|
|
|
|
|
| 1830 |
[temp.concept]: #temp.concept
|
| 1831 |
[temp.constr]: #temp.constr
|
| 1832 |
[temp.constr.atomic]: #temp.constr.atomic
|
| 1833 |
[temp.constr.constr]: #temp.constr.constr
|
| 1834 |
+
[temp.constr.constr.general]: #temp.constr.constr.general
|
| 1835 |
[temp.constr.decl]: #temp.constr.decl
|
| 1836 |
+
[temp.constr.general]: #temp.constr.general
|
| 1837 |
[temp.constr.normal]: #temp.constr.normal
|
| 1838 |
[temp.constr.op]: #temp.constr.op
|
| 1839 |
[temp.constr.order]: #temp.constr.order
|
| 1840 |
[temp.decls]: #temp.decls
|
| 1841 |
+
[temp.decls.general]: #temp.decls.general
|
| 1842 |
[temp.deduct]: #temp.deduct
|
| 1843 |
[temp.deduct.call]: #temp.deduct.call
|
| 1844 |
[temp.deduct.conv]: #temp.deduct.conv
|
| 1845 |
[temp.deduct.decl]: #temp.deduct.decl
|
| 1846 |
[temp.deduct.funcaddr]: #temp.deduct.funcaddr
|
| 1847 |
+
[temp.deduct.general]: #temp.deduct.general
|
| 1848 |
[temp.deduct.guide]: #temp.deduct.guide
|
| 1849 |
[temp.deduct.partial]: #temp.deduct.partial
|
| 1850 |
[temp.deduct.type]: #temp.deduct.type
|
| 1851 |
[temp.dep]: #temp.dep
|
| 1852 |
[temp.dep.candidate]: #temp.dep.candidate
|
| 1853 |
[temp.dep.constexpr]: #temp.dep.constexpr
|
| 1854 |
[temp.dep.expr]: #temp.dep.expr
|
| 1855 |
+
[temp.dep.general]: #temp.dep.general
|
| 1856 |
[temp.dep.res]: #temp.dep.res
|
| 1857 |
[temp.dep.temp]: #temp.dep.temp
|
| 1858 |
[temp.dep.type]: #temp.dep.type
|
| 1859 |
[temp.expl.spec]: #temp.expl.spec
|
| 1860 |
[temp.explicit]: #temp.explicit
|
| 1861 |
[temp.fct]: #temp.fct
|
| 1862 |
+
[temp.fct.general]: #temp.fct.general
|
| 1863 |
[temp.fct.spec]: #temp.fct.spec
|
| 1864 |
+
[temp.fct.spec.general]: #temp.fct.spec.general
|
| 1865 |
[temp.fold.empty]: #temp.fold.empty
|
| 1866 |
[temp.friend]: #temp.friend
|
| 1867 |
[temp.func.order]: #temp.func.order
|
|
|
|
| 1868 |
[temp.inst]: #temp.inst
|
| 1869 |
[temp.local]: #temp.local
|
| 1870 |
[temp.mem]: #temp.mem
|
| 1871 |
[temp.mem.class]: #temp.mem.class
|
| 1872 |
[temp.mem.enum]: #temp.mem.enum
|
| 1873 |
[temp.mem.func]: #temp.mem.func
|
| 1874 |
[temp.names]: #temp.names
|
|
|
|
| 1875 |
[temp.over]: #temp.over
|
| 1876 |
[temp.over.link]: #temp.over.link
|
| 1877 |
[temp.param]: #temp.param
|
| 1878 |
[temp.point]: #temp.point
|
| 1879 |
[temp.pre]: #temp.pre
|
| 1880 |
[temp.res]: #temp.res
|
| 1881 |
+
[temp.res.general]: #temp.res.general
|
| 1882 |
[temp.spec]: #temp.spec
|
| 1883 |
+
[temp.spec.general]: #temp.spec.general
|
| 1884 |
+
[temp.spec.partial]: #temp.spec.partial
|
| 1885 |
+
[temp.spec.partial.general]: #temp.spec.partial.general
|
| 1886 |
+
[temp.spec.partial.match]: #temp.spec.partial.match
|
| 1887 |
+
[temp.spec.partial.member]: #temp.spec.partial.member
|
| 1888 |
+
[temp.spec.partial.order]: #temp.spec.partial.order
|
| 1889 |
[temp.static]: #temp.static
|
| 1890 |
[temp.type]: #temp.type
|
| 1891 |
[temp.variadic]: #temp.variadic
|
| 1892 |
+
[term.incomplete.type]: basic.md#term.incomplete.type
|
| 1893 |
+
[term.odr.use]: basic.md#term.odr.use
|
| 1894 |
|
| 1895 |
[^1]: Since template *template-parameter*s and template
|
| 1896 |
*template-argument*s are treated as types for descriptive purposes,
|
| 1897 |
the terms *non-type parameter* and *non-type argument* are used to
|
| 1898 |
refer to non-type, non-template parameters and arguments.
|
|
|
|
| 1906 |
because the form of the *template-parameter* determines the
|
| 1907 |
allowable forms of the *template-argument*.
|
| 1908 |
|
| 1909 |
[^4]: A constraint is in disjunctive normal form when it is a
|
| 1910 |
disjunction of clauses where each clause is a conjunction of atomic
|
| 1911 |
+
constraints. For atomic constraints A, B, and C, the disjunctive
|
|
|
|
|
|
|
| 1912 |
normal form of the constraint A ∧ (B ∨ C) is (A ∧ B) ∨ (A ∧ C). Its
|
| 1913 |
+
disjunctive clauses are (A ∧ B) and (A ∧ C).
|
| 1914 |
|
| 1915 |
[^5]: A constraint is in conjunctive normal form when it is a
|
| 1916 |
conjunction of clauses where each clause is a disjunction of atomic
|
| 1917 |
+
constraints. For atomic constraints A, B, and C, the constraint
|
|
|
|
|
|
|
| 1918 |
A ∧ (B ∨ C) is in conjunctive normal form. Its conjunctive clauses
|
| 1919 |
+
are A and (B ∨ C).
|
| 1920 |
|
| 1921 |
[^6]: The identity of enumerators is not preserved.
|
| 1922 |
|
| 1923 |
[^7]: An array as a *template-parameter* decays to a pointer.
|
| 1924 |
|
| 1925 |
+
[^8]: There is no context in which they would be used.
|
| 1926 |
|
| 1927 |
[^9]: That is, declarations of non-template functions do not merely
|
| 1928 |
guide overload resolution of function template specializations with
|
| 1929 |
the same name. If such a non-template function is odr-used
|
| 1930 |
+
[[term.odr.use]] in a program, it must be defined; it will not be
|
| 1931 |
implicitly instantiated using the function template definition.
|
| 1932 |
|
| 1933 |
[^10]: This includes friend function declarations.
|
| 1934 |
|
| 1935 |
+
[^11]: Every instantiation of a class template declares a different set
|
| 1936 |
+
of assignment operators.
|
| 1937 |
|
| 1938 |
+
[^12]: This includes an injected-class-name [[class.pre]] of a class
|
| 1939 |
+
template used without a *template-argument-list*.
|
| 1940 |
+
|
| 1941 |
+
[^13]: Default arguments are not considered to be arguments in this
|
| 1942 |
context; they only become arguments after a function has been
|
| 1943 |
selected.
|
| 1944 |
|
| 1945 |
+
[^14]: Although the *template-argument* corresponding to a
|
| 1946 |
+
*template-parameter* of type `bool` can be deduced from an array
|
| 1947 |
bound, the resulting value will always be `true` because the array
|
| 1948 |
bound will be nonzero.
|
| 1949 |
|
| 1950 |
+
[^15]: The parameters of function template specializations contain no
|
| 1951 |
template parameter types. The set of conversions allowed on deduced
|
| 1952 |
arguments is limited, because the argument deduction process
|
| 1953 |
produces function templates with parameters that either match the
|
| 1954 |
call arguments exactly or differ only in ways that can be bridged by
|
| 1955 |
the allowed limited conversions. Non-deduced arguments allow the
|