- tmp/tmpfi30ylwc/{from.md → to.md} +127 -93
tmp/tmpfi30ylwc/{from.md → to.md}
RENAMED
|
@@ -26,13 +26,14 @@ void g(double d) {
|
|
| 26 |
```
|
| 27 |
|
| 28 |
— *end example*]
|
| 29 |
|
| 30 |
When an explicit template argument list is specified, if the given
|
| 31 |
-
*template-id* is not valid
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
After this substitution is performed, the function parameter type
|
| 36 |
adjustments described in [[dcl.fct]] are performed.
|
| 37 |
|
| 38 |
[*Example 2*: A parameter type of “`void (const int, int[5])`” becomes
|
|
@@ -157,29 +158,33 @@ when any template arguments that were deduced or obtained from default
|
|
| 157 |
arguments are substituted.
|
| 158 |
|
| 159 |
The *deduction substitution loci* are
|
| 160 |
|
| 161 |
- the function type outside of the *noexcept-specifier*,
|
| 162 |
-
- the *explicit-specifier*,
|
| 163 |
-
- the template parameter declarations
|
|
|
|
|
|
|
| 164 |
|
| 165 |
The substitution occurs in all types and expressions that are used in
|
| 166 |
the deduction substitution loci. The expressions include not only
|
| 167 |
constant expressions such as those that appear in array bounds or as
|
| 168 |
-
|
| 169 |
non-constant expressions) inside `sizeof`, `decltype`, and other
|
| 170 |
contexts that allow non-constant expressions. The substitution proceeds
|
| 171 |
in lexical order and stops when a condition that causes deduction to
|
| 172 |
fail is encountered. If substitution into different declarations of the
|
| 173 |
same function template would cause template instantiations to occur in a
|
| 174 |
different order or not at all, the program is ill-formed; no diagnostic
|
| 175 |
required.
|
| 176 |
|
| 177 |
-
[*Note 4*: The equivalent substitution in exception specifications
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
|
|
|
|
|
|
| 181 |
|
| 182 |
[*Example 6*:
|
| 183 |
|
| 184 |
``` cpp
|
| 185 |
template <class T> struct A { using X = typename T::X; };
|
|
@@ -217,13 +222,12 @@ effects such as the instantiation of class template specializations
|
|
| 217 |
and/or function template specializations, the generation of
|
| 218 |
implicitly-defined functions, etc. Such effects are not in the
|
| 219 |
“immediate context” and can result in the program being
|
| 220 |
ill-formed. — *end note*]
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
purposes of template argument deduction.
|
| 225 |
|
| 226 |
[*Note 7*:
|
| 227 |
|
| 228 |
The intent is to avoid requiring implementations to deal with
|
| 229 |
substitution failure involving arbitrary statements.
|
|
@@ -308,11 +312,12 @@ Type deduction can fail for the following reasons:
|
|
| 308 |
*qualified-id* when that type does not contain the specified member,
|
| 309 |
or
|
| 310 |
- the specified member is not a type where a type is required, or
|
| 311 |
- the specified member is not a template where a template is required,
|
| 312 |
or
|
| 313 |
-
- the specified member is not a non-type
|
|
|
|
| 314 |
|
| 315 |
\[*Example 11*:
|
| 316 |
``` cpp
|
| 317 |
template <int I> struct X { };
|
| 318 |
template <template <class T> class> struct Z { };
|
|
@@ -330,11 +335,11 @@ Type deduction can fail for the following reasons:
|
|
| 330 |
|
| 331 |
int main() {
|
| 332 |
// Deduction fails in each of these cases:
|
| 333 |
f<A>(0); // A does not contain a member Y
|
| 334 |
f<B>(0); // The Y member of B is not a type
|
| 335 |
-
g<C>(0); // The N member of C is not a non-type
|
| 336 |
h<D>(0); // The TT member of D is not a template
|
| 337 |
}
|
| 338 |
```
|
| 339 |
|
| 340 |
— *end example*]
|
|
@@ -347,19 +352,19 @@ Type deduction can fail for the following reasons:
|
|
| 347 |
template <class T> int f(int T::*);
|
| 348 |
int i = f<int>(0);
|
| 349 |
```
|
| 350 |
|
| 351 |
— *end example*]
|
| 352 |
-
- Attempting to give an invalid type to a
|
| 353 |
\[*Example 13*:
|
| 354 |
``` cpp
|
| 355 |
template <class T, T> struct S {};
|
| 356 |
template <class T> int f(S<T, T{}>*); // #1
|
| 357 |
class X {
|
| 358 |
int m;
|
| 359 |
};
|
| 360 |
-
int i0 = f<X>(0); // #1 uses a value of non-structural type X as a
|
| 361 |
```
|
| 362 |
|
| 363 |
— *end example*]
|
| 364 |
- Attempting to perform an invalid conversion in either a template
|
| 365 |
argument expression, or an expression used in the function
|
|
@@ -372,10 +377,13 @@ Type deduction can fail for the following reasons:
|
|
| 372 |
|
| 373 |
— *end example*]
|
| 374 |
- Attempting to create a function type in which a parameter has a type
|
| 375 |
of `void`, or in which the return type is a function type or array
|
| 376 |
type.
|
|
|
|
|
|
|
|
|
|
| 377 |
|
| 378 |
— *end note*]
|
| 379 |
|
| 380 |
[*Example 15*:
|
| 381 |
|
|
@@ -395,23 +403,23 @@ int i2 = f<1>(0); // ambiguous; not narrowing
|
|
| 395 |
— *end example*]
|
| 396 |
|
| 397 |
#### Deducing template arguments from a function call <a id="temp.deduct.call">[[temp.deduct.call]]</a>
|
| 398 |
|
| 399 |
Template argument deduction is done by comparing each function template
|
| 400 |
-
parameter type (call it `P`) that contains
|
| 401 |
participate in template argument deduction with the type of the
|
| 402 |
corresponding argument of the call (call it `A`) as described below. If
|
| 403 |
removing references and cv-qualifiers from `P` gives
|
| 404 |
-
|
| 405 |
-
and the argument is a non-empty initializer list
|
| 406 |
-
deduction is performed instead for each element
|
| 407 |
-
independently, taking `P`' as separate function
|
| 408 |
-
`P`'_i and the iᵗʰ initializer element as the
|
| 409 |
-
the `P`'`[N]` case, if `N` is a
|
| 410 |
-
deduced from the length of the initializer
|
| 411 |
-
initializer list argument causes the parameter to be
|
| 412 |
-
non-deduced context [[temp.deduct.type]].
|
| 413 |
|
| 414 |
[*Example 1*:
|
| 415 |
|
| 416 |
``` cpp
|
| 417 |
template<class T> void f(std::initializer_list<T>);
|
|
@@ -549,19 +557,20 @@ that allow a difference:
|
|
| 549 |
transformed `A`.
|
| 550 |
- The transformed `A` can be another pointer or pointer-to-member type
|
| 551 |
that can be converted to the deduced `A` via a function pointer
|
| 552 |
conversion [[conv.fctptr]] and/or qualification conversion
|
| 553 |
[[conv.qual]].
|
| 554 |
-
- If `P` is a class and `P` has the form *simple-template-id*
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
|
|
|
| 563 |
\[*Example 5*:
|
| 564 |
``` cpp
|
| 565 |
template <typename... T> struct X;
|
| 566 |
template <> struct X<> {};
|
| 567 |
template <typename T, typename... Ts>
|
|
@@ -580,11 +589,11 @@ that allow a difference:
|
|
| 580 |
|
| 581 |
These alternatives are considered only if type deduction would otherwise
|
| 582 |
fail. If they yield more than one possible deduced `A`, the type
|
| 583 |
deduction fails.
|
| 584 |
|
| 585 |
-
[*Note 1*: If a
|
| 586 |
parameters of a function template, or is used only in a non-deduced
|
| 587 |
context, its corresponding *template-argument* cannot be deduced from a
|
| 588 |
function call and the *template-argument* must be explicitly
|
| 589 |
specified. — *end note*]
|
| 590 |
|
|
@@ -593,14 +602,14 @@ pointer-to-member-function type:
|
|
| 593 |
|
| 594 |
- If the argument is an overload set containing one or more function
|
| 595 |
templates, the parameter is treated as a non-deduced context.
|
| 596 |
- If the argument is an overload set (not containing function
|
| 597 |
templates), trial argument deduction is attempted using each of the
|
| 598 |
-
members of the set
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
|
| 603 |
[*Example 6*:
|
| 604 |
|
| 605 |
``` cpp
|
| 606 |
// Only one function of an overload set matches the call so the function parameter is a deduced context.
|
|
@@ -634,10 +643,26 @@ template <class T> T g(T);
|
|
| 634 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 635 |
```
|
| 636 |
|
| 637 |
— *end example*]
|
| 638 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 639 |
#### Deducing template arguments taking the address of a function template <a id="temp.deduct.funcaddr">[[temp.deduct.funcaddr]]</a>
|
| 640 |
|
| 641 |
Template arguments can be deduced from the type specified when taking
|
| 642 |
the address of an overload set [[over.over]]. If there is a target, the
|
| 643 |
function template’s function type and the target type are used as the
|
|
@@ -717,11 +742,11 @@ as the parameter template.
|
|
| 717 |
|
| 718 |
The types used to determine the ordering depend on the context in which
|
| 719 |
the partial ordering is done:
|
| 720 |
|
| 721 |
- In the context of a function call, the types used are those function
|
| 722 |
-
parameter types for which the function call has arguments.[^
|
| 723 |
- In the context of a call to a conversion function, the return types of
|
| 724 |
the conversion function templates are used.
|
| 725 |
- In other contexts [[temp.func.order]] the function template’s function
|
| 726 |
type is used.
|
| 727 |
|
|
@@ -844,13 +869,13 @@ g(Tuple<int>()); // calls #3
|
|
| 844 |
|
| 845 |
Template arguments can be deduced in several different contexts, but in
|
| 846 |
each case a type that is specified in terms of template parameters (call
|
| 847 |
it `P`) is compared with an actual type (call it `A`), and an attempt is
|
| 848 |
made to find template argument values (a type for a type parameter, a
|
| 849 |
-
value for a
|
| 850 |
-
that will make `P`, after substitution of the
|
| 851 |
-
the deduced `A`), compatible with `A`.
|
| 852 |
|
| 853 |
In some cases, the deduction is done using a single set of types `P` and
|
| 854 |
`A`, in other cases, there will be a set of corresponding types `P` and
|
| 855 |
`A`. Type deduction is done independently for each `P/A` pair, and the
|
| 856 |
deduced template argument values are then combined. If type deduction
|
|
@@ -860,52 +885,57 @@ pairs yield different deduced values, or if any template argument
|
|
| 860 |
remains neither deduced nor explicitly specified, template argument
|
| 861 |
deduction fails. The type of a type parameter is only deduced from an
|
| 862 |
array bound if it is not otherwise deduced.
|
| 863 |
|
| 864 |
A given type `P` can be composed from a number of other types,
|
| 865 |
-
templates, and
|
| 866 |
|
| 867 |
- A function type includes the types of each of the function parameters,
|
| 868 |
the return type, and its exception specification.
|
| 869 |
- A pointer-to-member type includes the type of the class object pointed
|
| 870 |
to and the type of the member pointed to.
|
| 871 |
- A type that is a specialization of a class template (e.g., `A<int>`)
|
| 872 |
-
includes the types, templates, and
|
| 873 |
-
template argument list of the specialization.
|
| 874 |
- An array type includes the array element type and the value of the
|
| 875 |
array bound.
|
| 876 |
|
| 877 |
-
In most cases, the types, templates, and
|
| 878 |
-
to compose `P` participate in template argument
|
| 879 |
-
may be used to determine the value of a
|
| 880 |
-
argument deduction fails if the value so
|
| 881 |
-
with the values determined elsewhere. In
|
| 882 |
-
value does not participate in type
|
| 883 |
-
values of template arguments that were
|
| 884 |
-
explicitly specified. If a template
|
| 885 |
-
non-deduced contexts and is not explicitly
|
| 886 |
-
deduction fails.
|
| 887 |
|
| 888 |
-
[*Note 1*: Under [[temp.deduct.call]], if `P` contains no
|
| 889 |
-
|
| 890 |
-
|
| 891 |
|
| 892 |
The non-deduced contexts are:
|
| 893 |
|
| 894 |
- The *nested-name-specifier* of a type that was specified using a
|
| 895 |
*qualified-id*.
|
|
|
|
|
|
|
| 896 |
- The *expression* of a *decltype-specifier*.
|
| 897 |
-
-
|
|
|
|
| 898 |
subexpression references a template parameter.
|
| 899 |
- A template parameter used in the parameter type of a function
|
| 900 |
parameter that has a default argument that is being used in the call
|
| 901 |
for which argument deduction is being done.
|
| 902 |
- A function parameter for which the associated argument is an overload
|
| 903 |
-
set
|
| 904 |
-
-
|
|
|
|
| 905 |
(resulting in an ambiguous deduction), or
|
| 906 |
-
- no function
|
|
|
|
| 907 |
- the overload set supplied as an argument contains one or more
|
| 908 |
function templates.
|
| 909 |
- A function parameter for which the associated argument is an
|
| 910 |
initializer list [[dcl.init.list]] but the parameter does not have a
|
| 911 |
type for which deduction from an initializer list is specified
|
|
@@ -1014,13 +1044,15 @@ void t() {
|
|
| 1014 |
}
|
| 1015 |
```
|
| 1016 |
|
| 1017 |
— *end example*]
|
| 1018 |
|
| 1019 |
-
A
|
| 1020 |
-
template
|
| 1021 |
-
|
|
|
|
|
|
|
| 1022 |
|
| 1023 |
``` cpp
|
| 1024 |
\opt{cv} T
|
| 1025 |
T*
|
| 1026 |
T&
|
|
@@ -1029,47 +1061,48 @@ T&&
|
|
| 1029 |
\opt{T}(\opt{T}) noexcept(\opt{i})
|
| 1030 |
\opt{T} \opt{T}::*
|
| 1031 |
\opt{TT}<T>
|
| 1032 |
\opt{TT}<i>
|
| 1033 |
\opt{TT}<TT>
|
|
|
|
| 1034 |
\opt{TT}<>
|
| 1035 |
```
|
| 1036 |
|
| 1037 |
where
|
| 1038 |
|
| 1039 |
- `\opt{T}` represents a type or parameter-type-list that either
|
| 1040 |
satisfies these rules recursively, is a non-deduced context in `P` or
|
| 1041 |
`A`, or is the same non-dependent type in `P` and `A`,
|
| 1042 |
-
- `\opt{TT}` represents either a class template or a template template
|
| 1043 |
-
parameter,
|
| 1044 |
- `\opt{i}` represents an expression that either is an `i`, is
|
| 1045 |
value-dependent in `P` or `A`, or has the same constant value in `P`
|
| 1046 |
-
and `A`,
|
|
|
|
|
|
|
| 1047 |
- `noexcept(\opt{i})` represents an exception specification
|
| 1048 |
[[except.spec]] in which the (possibly-implicit, see [[dcl.fct]])
|
| 1049 |
*noexcept-specifier*’s operand satisfies the rules for an `\opt{i}`
|
| 1050 |
above.
|
| 1051 |
|
| 1052 |
[*Note 2*: If a type matches such a form but contains no `T`s, `i`s, or
|
| 1053 |
`TT`s, deduction is not possible. — *end note*]
|
| 1054 |
|
| 1055 |
-
Similarly, `<
|
| 1056 |
-
argument contains
|
| 1057 |
-
|
| 1058 |
-
|
| 1059 |
|
| 1060 |
-
If `P` has a form that contains `<T>`
|
| 1061 |
-
the respective template argument list of `P` is
|
| 1062 |
-
corresponding argument Aᵢ of the corresponding
|
| 1063 |
-
`A`. If the template argument list of `P`
|
| 1064 |
-
is not the last template argument, the
|
| 1065 |
-
a non-deduced context. If `Pᵢ` is a
|
| 1066 |
-
`Pᵢ` is compared with each remaining
|
| 1067 |
-
list of `A`. Each comparison deduces
|
| 1068 |
-
positions in the template parameter
|
| 1069 |
-
partial ordering [[temp.deduct.partial]],
|
| 1070 |
-
expansion:
|
| 1071 |
|
| 1072 |
- if `P` does not contain a template argument corresponding to `Aᵢ` then
|
| 1073 |
`Aᵢ` is ignored;
|
| 1074 |
- otherwise, if `Pᵢ` is not a pack expansion, template argument
|
| 1075 |
deduction fails.
|
|
@@ -1170,11 +1203,11 @@ where type is `X<int>` and `T` is `char[6]`.
|
|
| 1170 |
— *end example*]
|
| 1171 |
|
| 1172 |
Template arguments cannot be deduced from function arguments involving
|
| 1173 |
constructs other than the ones specified above.
|
| 1174 |
|
| 1175 |
-
When the value of the argument corresponding to a
|
| 1176 |
parameter `P` that is declared with a dependent type is deduced from an
|
| 1177 |
expression, the template parameters in the type of `P` are deduced from
|
| 1178 |
the type of the value.
|
| 1179 |
|
| 1180 |
[*Example 8*:
|
|
@@ -1261,12 +1294,12 @@ void g() {
|
|
| 1261 |
|
| 1262 |
— *end note*]
|
| 1263 |
|
| 1264 |
[*Note 5*:
|
| 1265 |
|
| 1266 |
-
If, in the declaration of a function template with a
|
| 1267 |
-
parameter, the
|
| 1268 |
the function parameter list, the expression is a non-deduced context as
|
| 1269 |
specified above.
|
| 1270 |
|
| 1271 |
[*Example 12*:
|
| 1272 |
|
|
@@ -1307,13 +1340,14 @@ int x = deduce<77>(a.xm, 62, b.ym);
|
|
| 1307 |
|
| 1308 |
— *end note*]
|
| 1309 |
|
| 1310 |
If `P` has a form that contains `<i>`, and if the type of `i` differs
|
| 1311 |
from the type of the corresponding template parameter of the template
|
| 1312 |
-
named by the enclosing *simple-template-id*
|
| 1313 |
-
|
| 1314 |
-
|
|
|
|
| 1315 |
|
| 1316 |
If `P` has a form that includes `noexcept(i)` and the type of `i` is not
|
| 1317 |
`bool`, deduction fails.
|
| 1318 |
|
| 1319 |
[*Example 13*:
|
|
@@ -1373,11 +1407,11 @@ void g() {
|
|
| 1373 |
}
|
| 1374 |
```
|
| 1375 |
|
| 1376 |
— *end example*]
|
| 1377 |
|
| 1378 |
-
The *template-argument* corresponding to a template
|
| 1379 |
is deduced from the type of the *template-argument* of a class template
|
| 1380 |
specialization used in the argument list of a function call.
|
| 1381 |
|
| 1382 |
[*Example 16*:
|
| 1383 |
|
|
|
|
| 26 |
```
|
| 27 |
|
| 28 |
— *end example*]
|
| 29 |
|
| 30 |
When an explicit template argument list is specified, if the given
|
| 31 |
+
*template-id* or *splice-specialization-specifier* is not valid
|
| 32 |
+
[[temp.names]], type deduction fails. Otherwise, the specified template
|
| 33 |
+
argument values are substituted for the corresponding template
|
| 34 |
+
parameters as specified below.
|
| 35 |
|
| 36 |
After this substitution is performed, the function parameter type
|
| 37 |
adjustments described in [[dcl.fct]] are performed.
|
| 38 |
|
| 39 |
[*Example 2*: A parameter type of “`void (const int, int[5])`” becomes
|
|
|
|
| 158 |
arguments are substituted.
|
| 159 |
|
| 160 |
The *deduction substitution loci* are
|
| 161 |
|
| 162 |
- the function type outside of the *noexcept-specifier*,
|
| 163 |
+
- the *explicit-specifier*,
|
| 164 |
+
- the template parameter declarations, and
|
| 165 |
+
- the template argument list of a partial specialization
|
| 166 |
+
[[temp.spec.partial.general]].
|
| 167 |
|
| 168 |
The substitution occurs in all types and expressions that are used in
|
| 169 |
the deduction substitution loci. The expressions include not only
|
| 170 |
constant expressions such as those that appear in array bounds or as
|
| 171 |
+
constant template arguments but also general expressions (i.e.,
|
| 172 |
non-constant expressions) inside `sizeof`, `decltype`, and other
|
| 173 |
contexts that allow non-constant expressions. The substitution proceeds
|
| 174 |
in lexical order and stops when a condition that causes deduction to
|
| 175 |
fail is encountered. If substitution into different declarations of the
|
| 176 |
same function template would cause template instantiations to occur in a
|
| 177 |
different order or not at all, the program is ill-formed; no diagnostic
|
| 178 |
required.
|
| 179 |
|
| 180 |
+
[*Note 4*: The equivalent substitution in exception specifications
|
| 181 |
+
[[except.spec]] and function contract assertions [[dcl.contract.func]]
|
| 182 |
+
is done only when the *noexcept-specifier* or
|
| 183 |
+
*function-contract-specifier*, respectively, is instantiated, at which
|
| 184 |
+
point a program is ill-formed if the substitution results in an invalid
|
| 185 |
+
type or expression. — *end note*]
|
| 186 |
|
| 187 |
[*Example 6*:
|
| 188 |
|
| 189 |
``` cpp
|
| 190 |
template <class T> struct A { using X = typename T::X; };
|
|
|
|
| 222 |
and/or function template specializations, the generation of
|
| 223 |
implicitly-defined functions, etc. Such effects are not in the
|
| 224 |
“immediate context” and can result in the program being
|
| 225 |
ill-formed. — *end note*]
|
| 226 |
|
| 227 |
+
When substituting into a *lambda-expression*, substitution into its body
|
| 228 |
+
is not in the immediate context.
|
|
|
|
| 229 |
|
| 230 |
[*Note 7*:
|
| 231 |
|
| 232 |
The intent is to avoid requiring implementations to deal with
|
| 233 |
substitution failure involving arbitrary statements.
|
|
|
|
| 312 |
*qualified-id* when that type does not contain the specified member,
|
| 313 |
or
|
| 314 |
- the specified member is not a type where a type is required, or
|
| 315 |
- the specified member is not a template where a template is required,
|
| 316 |
or
|
| 317 |
+
- the specified member is not a non-type, non-template where a
|
| 318 |
+
non-type, non-template is required.
|
| 319 |
|
| 320 |
\[*Example 11*:
|
| 321 |
``` cpp
|
| 322 |
template <int I> struct X { };
|
| 323 |
template <template <class T> class> struct Z { };
|
|
|
|
| 335 |
|
| 336 |
int main() {
|
| 337 |
// Deduction fails in each of these cases:
|
| 338 |
f<A>(0); // A does not contain a member Y
|
| 339 |
f<B>(0); // The Y member of B is not a type
|
| 340 |
+
g<C>(0); // The N member of C is not a non-type, non-template name
|
| 341 |
h<D>(0); // The TT member of D is not a template
|
| 342 |
}
|
| 343 |
```
|
| 344 |
|
| 345 |
— *end example*]
|
|
|
|
| 352 |
template <class T> int f(int T::*);
|
| 353 |
int i = f<int>(0);
|
| 354 |
```
|
| 355 |
|
| 356 |
— *end example*]
|
| 357 |
+
- Attempting to give an invalid type to a constant template parameter.
|
| 358 |
\[*Example 13*:
|
| 359 |
``` cpp
|
| 360 |
template <class T, T> struct S {};
|
| 361 |
template <class T> int f(S<T, T{}>*); // #1
|
| 362 |
class X {
|
| 363 |
int m;
|
| 364 |
};
|
| 365 |
+
int i0 = f<X>(0); // #1 uses a value of non-structural type X as a constant template argument
|
| 366 |
```
|
| 367 |
|
| 368 |
— *end example*]
|
| 369 |
- Attempting to perform an invalid conversion in either a template
|
| 370 |
argument expression, or an expression used in the function
|
|
|
|
| 377 |
|
| 378 |
— *end example*]
|
| 379 |
- Attempting to create a function type in which a parameter has a type
|
| 380 |
of `void`, or in which the return type is a function type or array
|
| 381 |
type.
|
| 382 |
+
- Attempting to give to an explicit object parameter of a lambda’s
|
| 383 |
+
function call operator a type not permitted for such
|
| 384 |
+
[[expr.prim.lambda.closure]].
|
| 385 |
|
| 386 |
— *end note*]
|
| 387 |
|
| 388 |
[*Example 15*:
|
| 389 |
|
|
|
|
| 403 |
— *end example*]
|
| 404 |
|
| 405 |
#### Deducing template arguments from a function call <a id="temp.deduct.call">[[temp.deduct.call]]</a>
|
| 406 |
|
| 407 |
Template argument deduction is done by comparing each function template
|
| 408 |
+
parameter type (call it `P`) that contains template parameters that
|
| 409 |
participate in template argument deduction with the type of the
|
| 410 |
corresponding argument of the call (call it `A`) as described below. If
|
| 411 |
removing references and cv-qualifiers from `P` gives
|
| 412 |
+
$\tcode{std::initializer_list<P}^{\prime}\tcode{>}$ or `P`'`[N]` for
|
| 413 |
+
some `P`' and `N` and the argument is a non-empty initializer list
|
| 414 |
+
[[dcl.init.list]], then deduction is performed instead for each element
|
| 415 |
+
of the initializer list independently, taking `P`' as separate function
|
| 416 |
+
template parameter types `P`'_i and the iᵗʰ initializer element as the
|
| 417 |
+
corresponding argument. In the `P`'`[N]` case, if `N` is a constant
|
| 418 |
+
template parameter, `N` is deduced from the length of the initializer
|
| 419 |
+
list. Otherwise, an initializer list argument causes the parameter to be
|
| 420 |
+
considered a non-deduced context [[temp.deduct.type]].
|
| 421 |
|
| 422 |
[*Example 1*:
|
| 423 |
|
| 424 |
``` cpp
|
| 425 |
template<class T> void f(std::initializer_list<T>);
|
|
|
|
| 557 |
transformed `A`.
|
| 558 |
- The transformed `A` can be another pointer or pointer-to-member type
|
| 559 |
that can be converted to the deduced `A` via a function pointer
|
| 560 |
conversion [[conv.fctptr]] and/or qualification conversion
|
| 561 |
[[conv.qual]].
|
| 562 |
+
- If `P` is a class and `P` has the form *simple-template-id* or
|
| 563 |
+
`typename`ₒₚₜ *splice-specialization-specifier*, then the transformed
|
| 564 |
+
`A` can be a derived class `D` of the deduced `A`. Likewise, if `P` is
|
| 565 |
+
a pointer to a class of the form *simple-template-id* or
|
| 566 |
+
`typename`ₒₚₜ *splice-specialization-specifier*, the transformed `A`
|
| 567 |
+
can be a pointer to a derived class `D` of the class pointed to by the
|
| 568 |
+
deduced `A`. However, if there is a class `C` that is a (direct or
|
| 569 |
+
indirect) base class of `D` and derived (directly or indirectly) from
|
| 570 |
+
a class `B` and that would be a valid deduced `A`, the deduced `A`
|
| 571 |
+
cannot be `B` or pointer to `B`, respectively.
|
| 572 |
\[*Example 5*:
|
| 573 |
``` cpp
|
| 574 |
template <typename... T> struct X;
|
| 575 |
template <> struct X<> {};
|
| 576 |
template <typename T, typename... Ts>
|
|
|
|
| 589 |
|
| 590 |
These alternatives are considered only if type deduction would otherwise
|
| 591 |
fail. If they yield more than one possible deduced `A`, the type
|
| 592 |
deduction fails.
|
| 593 |
|
| 594 |
+
[*Note 1*: If a template parameter is not used in any of the function
|
| 595 |
parameters of a function template, or is used only in a non-deduced
|
| 596 |
context, its corresponding *template-argument* cannot be deduced from a
|
| 597 |
function call and the *template-argument* must be explicitly
|
| 598 |
specified. — *end note*]
|
| 599 |
|
|
|
|
| 602 |
|
| 603 |
- If the argument is an overload set containing one or more function
|
| 604 |
templates, the parameter is treated as a non-deduced context.
|
| 605 |
- If the argument is an overload set (not containing function
|
| 606 |
templates), trial argument deduction is attempted using each of the
|
| 607 |
+
members of the set whose associated constraints [[temp.constr.constr]]
|
| 608 |
+
are satisfied. If all successful deductions yield the same deduced
|
| 609 |
+
`A`, that deduced `A` is the result of deduction; otherwise, the
|
| 610 |
+
parameter is treated as a non-deduced context.
|
| 611 |
|
| 612 |
[*Example 6*:
|
| 613 |
|
| 614 |
``` cpp
|
| 615 |
// Only one function of an overload set matches the call so the function parameter is a deduced context.
|
|
|
|
| 643 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 644 |
```
|
| 645 |
|
| 646 |
— *end example*]
|
| 647 |
|
| 648 |
+
[*Example 9*:
|
| 649 |
+
|
| 650 |
+
``` cpp
|
| 651 |
+
// All arguments for placeholder type deduction[dcl.type.auto.deduct] yield the same deduced type.
|
| 652 |
+
template<bool B> struct X {
|
| 653 |
+
static void f(short) requires B; // #1
|
| 654 |
+
static void f(short); // #2
|
| 655 |
+
};
|
| 656 |
+
void test() {
|
| 657 |
+
auto x = &X<true>::f; // OK, deduces void(*)(short), selects #1
|
| 658 |
+
auto y = &X<false>::f; // OK, deduces void(*)(short), selects #2
|
| 659 |
+
}
|
| 660 |
+
```
|
| 661 |
+
|
| 662 |
+
— *end example*]
|
| 663 |
+
|
| 664 |
#### Deducing template arguments taking the address of a function template <a id="temp.deduct.funcaddr">[[temp.deduct.funcaddr]]</a>
|
| 665 |
|
| 666 |
Template arguments can be deduced from the type specified when taking
|
| 667 |
the address of an overload set [[over.over]]. If there is a target, the
|
| 668 |
function template’s function type and the target type are used as the
|
|
|
|
| 742 |
|
| 743 |
The types used to determine the ordering depend on the context in which
|
| 744 |
the partial ordering is done:
|
| 745 |
|
| 746 |
- In the context of a function call, the types used are those function
|
| 747 |
+
parameter types for which the function call has arguments.[^12]
|
| 748 |
- In the context of a call to a conversion function, the return types of
|
| 749 |
the conversion function templates are used.
|
| 750 |
- In other contexts [[temp.func.order]] the function template’s function
|
| 751 |
type is used.
|
| 752 |
|
|
|
|
| 869 |
|
| 870 |
Template arguments can be deduced in several different contexts, but in
|
| 871 |
each case a type that is specified in terms of template parameters (call
|
| 872 |
it `P`) is compared with an actual type (call it `A`), and an attempt is
|
| 873 |
made to find template argument values (a type for a type parameter, a
|
| 874 |
+
value for a constant template parameter, or a template for a template
|
| 875 |
+
template parameter) that will make `P`, after substitution of the
|
| 876 |
+
deduced values (call it the deduced `A`), compatible with `A`.
|
| 877 |
|
| 878 |
In some cases, the deduction is done using a single set of types `P` and
|
| 879 |
`A`, in other cases, there will be a set of corresponding types `P` and
|
| 880 |
`A`. Type deduction is done independently for each `P/A` pair, and the
|
| 881 |
deduced template argument values are then combined. If type deduction
|
|
|
|
| 885 |
remains neither deduced nor explicitly specified, template argument
|
| 886 |
deduction fails. The type of a type parameter is only deduced from an
|
| 887 |
array bound if it is not otherwise deduced.
|
| 888 |
|
| 889 |
A given type `P` can be composed from a number of other types,
|
| 890 |
+
templates, and constant template argument values:
|
| 891 |
|
| 892 |
- A function type includes the types of each of the function parameters,
|
| 893 |
the return type, and its exception specification.
|
| 894 |
- A pointer-to-member type includes the type of the class object pointed
|
| 895 |
to and the type of the member pointed to.
|
| 896 |
- A type that is a specialization of a class template (e.g., `A<int>`)
|
| 897 |
+
includes the types, templates, and constant template argument values
|
| 898 |
+
referenced by the template argument list of the specialization.
|
| 899 |
- An array type includes the array element type and the value of the
|
| 900 |
array bound.
|
| 901 |
|
| 902 |
+
In most cases, the types, templates, and constant template argument
|
| 903 |
+
values that are used to compose `P` participate in template argument
|
| 904 |
+
deduction. That is, they may be used to determine the value of a
|
| 905 |
+
template argument, and template argument deduction fails if the value so
|
| 906 |
+
determined is not consistent with the values determined elsewhere. In
|
| 907 |
+
certain contexts, however, the value does not participate in type
|
| 908 |
+
deduction, but instead uses the values of template arguments that were
|
| 909 |
+
either deduced elsewhere or explicitly specified. If a template
|
| 910 |
+
parameter is used only in non-deduced contexts and is not explicitly
|
| 911 |
+
specified, template argument deduction fails.
|
| 912 |
|
| 913 |
+
[*Note 1*: Under [[temp.deduct.call]], if `P` contains no template
|
| 914 |
+
parameters that appear in deduced contexts, no deduction is done, so `P`
|
| 915 |
+
and `A` need not have the same form. — *end note*]
|
| 916 |
|
| 917 |
The non-deduced contexts are:
|
| 918 |
|
| 919 |
- The *nested-name-specifier* of a type that was specified using a
|
| 920 |
*qualified-id*.
|
| 921 |
+
- A *pack-index-specifier* or a *pack-index-expression*.
|
| 922 |
+
- A *type-constraint*.
|
| 923 |
- The *expression* of a *decltype-specifier*.
|
| 924 |
+
- The *constant-expression* of a *splice-specifier*.
|
| 925 |
+
- A constant template argument or an array bound in which a
|
| 926 |
subexpression references a template parameter.
|
| 927 |
- A template parameter used in the parameter type of a function
|
| 928 |
parameter that has a default argument that is being used in the call
|
| 929 |
for which argument deduction is being done.
|
| 930 |
- A function parameter for which the associated argument is an overload
|
| 931 |
+
set such that one or more of the following apply:
|
| 932 |
+
- functions whose associated constraints are satisfied and that do not
|
| 933 |
+
all have the same function type match the function parameter type
|
| 934 |
(resulting in an ambiguous deduction), or
|
| 935 |
+
- no function whose associated constraints are satisfied matches the
|
| 936 |
+
function parameter type, or
|
| 937 |
- the overload set supplied as an argument contains one or more
|
| 938 |
function templates.
|
| 939 |
- A function parameter for which the associated argument is an
|
| 940 |
initializer list [[dcl.init.list]] but the parameter does not have a
|
| 941 |
type for which deduction from an initializer list is specified
|
|
|
|
| 1044 |
}
|
| 1045 |
```
|
| 1046 |
|
| 1047 |
— *end example*]
|
| 1048 |
|
| 1049 |
+
A type template argument `T`, a constant template argument `i`, a
|
| 1050 |
+
template template argument `TT` denoting a class template or an alias
|
| 1051 |
+
template, or a template template argument `VV` denoting a variable
|
| 1052 |
+
template or a concept can be deduced if `P` and `A` have one of the
|
| 1053 |
+
following forms:
|
| 1054 |
|
| 1055 |
``` cpp
|
| 1056 |
\opt{cv} T
|
| 1057 |
T*
|
| 1058 |
T&
|
|
|
|
| 1061 |
\opt{T}(\opt{T}) noexcept(\opt{i})
|
| 1062 |
\opt{T} \opt{T}::*
|
| 1063 |
\opt{TT}<T>
|
| 1064 |
\opt{TT}<i>
|
| 1065 |
\opt{TT}<TT>
|
| 1066 |
+
\opt{TT}<VV>
|
| 1067 |
\opt{TT}<>
|
| 1068 |
```
|
| 1069 |
|
| 1070 |
where
|
| 1071 |
|
| 1072 |
- `\opt{T}` represents a type or parameter-type-list that either
|
| 1073 |
satisfies these rules recursively, is a non-deduced context in `P` or
|
| 1074 |
`A`, or is the same non-dependent type in `P` and `A`,
|
|
|
|
|
|
|
| 1075 |
- `\opt{i}` represents an expression that either is an `i`, is
|
| 1076 |
value-dependent in `P` or `A`, or has the same constant value in `P`
|
| 1077 |
+
and `A`,
|
| 1078 |
+
- `\opt{TT}` represents either a class template or a template template
|
| 1079 |
+
parameter, and
|
| 1080 |
- `noexcept(\opt{i})` represents an exception specification
|
| 1081 |
[[except.spec]] in which the (possibly-implicit, see [[dcl.fct]])
|
| 1082 |
*noexcept-specifier*’s operand satisfies the rules for an `\opt{i}`
|
| 1083 |
above.
|
| 1084 |
|
| 1085 |
[*Note 2*: If a type matches such a form but contains no `T`s, `i`s, or
|
| 1086 |
`TT`s, deduction is not possible. — *end note*]
|
| 1087 |
|
| 1088 |
+
Similarly, `<X>` represents template argument lists where at least one
|
| 1089 |
+
argument contains an X, where X is one of `T`, `i`, `TT`, or `VV`; and
|
| 1090 |
+
`<>` represents template argument lists where no argument contains a
|
| 1091 |
+
`T`, an `i`, a `TT`, or a `VV`.
|
| 1092 |
|
| 1093 |
+
If `P` has a form that contains `<T>`, `<i>`, `<TT>`, or `<VV>`, then
|
| 1094 |
+
each argument Pᵢ of the respective template argument list of `P` is
|
| 1095 |
+
compared with the corresponding argument Aᵢ of the corresponding
|
| 1096 |
+
template argument list of `A`. If the template argument list of `P`
|
| 1097 |
+
contains a pack expansion that is not the last template argument, the
|
| 1098 |
+
entire template argument list is a non-deduced context. If `Pᵢ` is a
|
| 1099 |
+
pack expansion, then the pattern of `Pᵢ` is compared with each remaining
|
| 1100 |
+
argument in the template argument list of `A`. Each comparison deduces
|
| 1101 |
+
template arguments for subsequent positions in the template parameter
|
| 1102 |
+
packs expanded by `Pᵢ`. During partial ordering [[temp.deduct.partial]],
|
| 1103 |
+
if `Aᵢ` was originally a pack expansion:
|
| 1104 |
|
| 1105 |
- if `P` does not contain a template argument corresponding to `Aᵢ` then
|
| 1106 |
`Aᵢ` is ignored;
|
| 1107 |
- otherwise, if `Pᵢ` is not a pack expansion, template argument
|
| 1108 |
deduction fails.
|
|
|
|
| 1203 |
— *end example*]
|
| 1204 |
|
| 1205 |
Template arguments cannot be deduced from function arguments involving
|
| 1206 |
constructs other than the ones specified above.
|
| 1207 |
|
| 1208 |
+
When the value of the argument corresponding to a constant template
|
| 1209 |
parameter `P` that is declared with a dependent type is deduced from an
|
| 1210 |
expression, the template parameters in the type of `P` are deduced from
|
| 1211 |
the type of the value.
|
| 1212 |
|
| 1213 |
[*Example 8*:
|
|
|
|
| 1294 |
|
| 1295 |
— *end note*]
|
| 1296 |
|
| 1297 |
[*Note 5*:
|
| 1298 |
|
| 1299 |
+
If, in the declaration of a function template with a constant template
|
| 1300 |
+
parameter, the constant template parameter is used in a subexpression in
|
| 1301 |
the function parameter list, the expression is a non-deduced context as
|
| 1302 |
specified above.
|
| 1303 |
|
| 1304 |
[*Example 12*:
|
| 1305 |
|
|
|
|
| 1340 |
|
| 1341 |
— *end note*]
|
| 1342 |
|
| 1343 |
If `P` has a form that contains `<i>`, and if the type of `i` differs
|
| 1344 |
from the type of the corresponding template parameter of the template
|
| 1345 |
+
named by the enclosing *simple-template-id* or
|
| 1346 |
+
*splice-specialization-specifier*, deduction fails. If `P` has a form
|
| 1347 |
+
that contains `[i]`, and if the type of `i` is not an integral type,
|
| 1348 |
+
deduction fails.[^13]
|
| 1349 |
|
| 1350 |
If `P` has a form that includes `noexcept(i)` and the type of `i` is not
|
| 1351 |
`bool`, deduction fails.
|
| 1352 |
|
| 1353 |
[*Example 13*:
|
|
|
|
| 1407 |
}
|
| 1408 |
```
|
| 1409 |
|
| 1410 |
— *end example*]
|
| 1411 |
|
| 1412 |
+
The *template-argument* corresponding to a template template parameter
|
| 1413 |
is deduced from the type of the *template-argument* of a class template
|
| 1414 |
specialization used in the argument list of a function call.
|
| 1415 |
|
| 1416 |
[*Example 16*:
|
| 1417 |
|