tmp/tmpfjbok2ym/{from.md → to.md}
RENAMED
|
@@ -22,32 +22,32 @@ array bound if it is not otherwise deduced.
|
|
| 22 |
A given type `P` can be composed from a number of other types,
|
| 23 |
templates, and non-type values:
|
| 24 |
|
| 25 |
- A function type includes the types of each of the function parameters
|
| 26 |
and the return type.
|
| 27 |
-
- A pointer
|
| 28 |
to and the type of the member pointed to.
|
| 29 |
- A type that is a specialization of a class template (e.g., `A<int>`)
|
| 30 |
includes the types, templates, and non-type values referenced by the
|
| 31 |
template argument list of the specialization.
|
| 32 |
- An array type includes the array element type and the value of the
|
| 33 |
array bound.
|
| 34 |
|
| 35 |
In most cases, the types, templates, and non-type values that are used
|
| 36 |
to compose `P` participate in template argument deduction. That is, they
|
| 37 |
-
may be used to determine the value of a template argument, and
|
| 38 |
-
|
| 39 |
-
In certain contexts, however, the
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
specified, template argument
|
|
|
|
| 44 |
|
| 45 |
-
[*Note 1*: Under [[temp.deduct.call]]
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
form. — *end note*]
|
| 49 |
|
| 50 |
The non-deduced contexts are:
|
| 51 |
|
| 52 |
- The *nested-name-specifier* of a type that was specified using a
|
| 53 |
*qualified-id*.
|
|
@@ -55,23 +55,21 @@ The non-deduced contexts are:
|
|
| 55 |
- A non-type template argument or an array bound in which a
|
| 56 |
subexpression references a template parameter.
|
| 57 |
- A template parameter used in the parameter type of a function
|
| 58 |
parameter that has a default argument that is being used in the call
|
| 59 |
for which argument deduction is being done.
|
| 60 |
-
- A function parameter for which
|
| 61 |
-
|
| 62 |
-
overloaded functions ([[over.over]]), and one or more of the
|
| 63 |
-
following apply:
|
| 64 |
- more than one function matches the function parameter type
|
| 65 |
(resulting in an ambiguous deduction), or
|
| 66 |
- no function matches the function parameter type, or
|
| 67 |
-
- the set
|
| 68 |
function templates.
|
| 69 |
- A function parameter for which the associated argument is an
|
| 70 |
-
initializer list
|
| 71 |
-
|
| 72 |
-
[[temp.deduct.call]]
|
| 73 |
\[*Example 1*:
|
| 74 |
``` cpp
|
| 75 |
template<class T> void g(T);
|
| 76 |
g({1,2,3}); // error: no argument deduced for T
|
| 77 |
```
|
|
@@ -161,11 +159,11 @@ A template type argument `T`, a template template argument `TT` or a
|
|
| 161 |
template non-type argument `i` can be deduced if `P` and `A` have one of
|
| 162 |
the following forms:
|
| 163 |
|
| 164 |
``` cpp
|
| 165 |
T
|
| 166 |
-
cv
|
| 167 |
T*
|
| 168 |
T&
|
| 169 |
T&&
|
| 170 |
T[integer-constant]
|
| 171 |
template-name<T> (where template-name refers to a class template)
|
|
@@ -187,12 +185,12 @@ template-name<i> (where template-name refers to a class template)
|
|
| 187 |
TT<T>
|
| 188 |
TT<i>
|
| 189 |
TT<>
|
| 190 |
```
|
| 191 |
|
| 192 |
-
where `(T)` represents a parameter-type-list
|
| 193 |
-
|
| 194 |
parameter-type-list where no parameter type contains a `T`. Similarly,
|
| 195 |
`<T>` represents template argument lists where at least one argument
|
| 196 |
contains a `T`, `<i>` represents template argument lists where at least
|
| 197 |
one argument contains an `i` and `<>` represents template argument lists
|
| 198 |
where no argument contains a `T` or an `i`.
|
|
@@ -204,12 +202,12 @@ corresponding argument Aᵢ of the corresponding template argument list of
|
|
| 204 |
is not the last template argument, the entire template argument list is
|
| 205 |
a non-deduced context. If `Pᵢ` is a pack expansion, then the pattern of
|
| 206 |
`Pᵢ` is compared with each remaining argument in the template argument
|
| 207 |
list of `A`. Each comparison deduces template arguments for subsequent
|
| 208 |
positions in the template parameter packs expanded by `Pᵢ`. During
|
| 209 |
-
partial ordering
|
| 210 |
-
|
| 211 |
|
| 212 |
- if `P` does not contain a template argument corresponding to `Aᵢ` then
|
| 213 |
`Aᵢ` is ignored;
|
| 214 |
- otherwise, if `Pᵢ` is not a pack expansion, template argument
|
| 215 |
deduction fails.
|
|
@@ -229,19 +227,19 @@ template struct A<int, int*>; // selects #2
|
|
| 229 |
```
|
| 230 |
|
| 231 |
— *end example*]
|
| 232 |
|
| 233 |
Similarly, if `P` has a form that contains `(T)`, then each parameter
|
| 234 |
-
type `Pᵢ` of the respective parameter-type-list
|
| 235 |
compared with the corresponding parameter type `Aᵢ` of the corresponding
|
| 236 |
parameter-type-list of `A`. If `P` and `A` are function types that
|
| 237 |
-
originated from deduction when taking the address of a function
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
respectively, `Pᵢ` is adjusted if it is a forwarding reference
|
| 242 |
-
[[temp.deduct.call]]
|
| 243 |
type of `Pᵢ` is changed to be the template parameter type (i.e., `T&&`
|
| 244 |
is changed to simply `T`).
|
| 245 |
|
| 246 |
[*Note 2*: As a result, when `Pᵢ` is `T&&` and `Aᵢ` is `X&`, the
|
| 247 |
adjusted `Pᵢ` will be `T`, causing `T` to be deduced as
|
|
@@ -264,11 +262,11 @@ void g(int i) {
|
|
| 264 |
If the *parameter-declaration* corresponding to `Pᵢ` is a function
|
| 265 |
parameter pack, then the type of its *declarator-id* is compared with
|
| 266 |
each remaining parameter type in the parameter-type-list of `A`. Each
|
| 267 |
comparison deduces template arguments for subsequent positions in the
|
| 268 |
template parameter packs expanded by the function parameter pack. During
|
| 269 |
-
partial ordering
|
| 270 |
function parameter pack:
|
| 271 |
|
| 272 |
- if `P` does not contain a function parameter type corresponding to
|
| 273 |
`Aᵢ` then `Aᵢ` is ignored;
|
| 274 |
- otherwise, if `Pᵢ` is not a function parameter pack, template argument
|
|
@@ -326,11 +324,11 @@ template<typename T> struct C;
|
|
| 326 |
template<typename T, T n> struct C<A<n>> {
|
| 327 |
using Q = T;
|
| 328 |
};
|
| 329 |
|
| 330 |
using R = long;
|
| 331 |
-
using R = C<A<2>>::Q; // OK; T was deduced
|
| 332 |
// template argument value in the type A<2>
|
| 333 |
```
|
| 334 |
|
| 335 |
— *end example*]
|
| 336 |
|
|
@@ -343,11 +341,11 @@ template<typename T> struct S;
|
|
| 343 |
template<typename T, T n> struct S<int[n]> {
|
| 344 |
using Q = T;
|
| 345 |
};
|
| 346 |
|
| 347 |
using V = decltype(sizeof 0);
|
| 348 |
-
using V = S<int[42]>::Q; // OK; T was deduced
|
| 349 |
```
|
| 350 |
|
| 351 |
— *end example*]
|
| 352 |
|
| 353 |
[*Example 10*:
|
|
@@ -372,15 +370,15 @@ template<int i> void f1(int a[10][i]);
|
|
| 372 |
template<int i> void f2(int a[i][20]);
|
| 373 |
template<int i> void f3(int (&a)[i][20]);
|
| 374 |
|
| 375 |
void g() {
|
| 376 |
int v[10][20];
|
| 377 |
-
f1(v); // OK: i deduced
|
| 378 |
f1<20>(v); // OK
|
| 379 |
f2(v); // error: cannot deduce template-argument i
|
| 380 |
f2<10>(v); // OK
|
| 381 |
-
f3(v); // OK: i deduced
|
| 382 |
}
|
| 383 |
```
|
| 384 |
|
| 385 |
— *end note*]
|
| 386 |
|
|
@@ -422,21 +420,21 @@ T deduce(typename A<T>::X x, // T is not deduced here
|
|
| 422 |
typename B<i>::Y y); // i is not deduced here
|
| 423 |
A<int> a;
|
| 424 |
B<77> b;
|
| 425 |
|
| 426 |
int x = deduce<77>(a.xm, 62, b.ym);
|
| 427 |
-
// T
|
| 428 |
-
// i is explicitly specified to be 77
|
| 429 |
```
|
| 430 |
|
| 431 |
— *end note*]
|
| 432 |
|
| 433 |
If `P` has a form that contains `<i>`, and if the type of `i` differs
|
| 434 |
from the type of the corresponding template parameter of the template
|
| 435 |
named by the enclosing *simple-template-id*, deduction fails. If `P` has
|
| 436 |
a form that contains `[i]`, and if the type of `i` is not an integral
|
| 437 |
-
type, deduction fails.[^
|
| 438 |
|
| 439 |
[*Example 12*:
|
| 440 |
|
| 441 |
``` cpp
|
| 442 |
template<int i> class A { ... };
|
|
@@ -456,11 +454,11 @@ void k2() {
|
|
| 456 |
```
|
| 457 |
|
| 458 |
— *end example*]
|
| 459 |
|
| 460 |
A *template-argument* can be deduced from a function, pointer to
|
| 461 |
-
function, or pointer
|
| 462 |
|
| 463 |
[*Example 13*:
|
| 464 |
|
| 465 |
``` cpp
|
| 466 |
template<class T> void f(void(*)(T,int));
|
|
@@ -509,12 +507,12 @@ A<B> ab;
|
|
| 509 |
f(ab); // calls f(A<B>)
|
| 510 |
```
|
| 511 |
|
| 512 |
— *end example*]
|
| 513 |
|
| 514 |
-
[*Note 6*: Template argument deduction involving parameter packs
|
| 515 |
-
[[temp.variadic]]
|
| 516 |
pack. — *end note*]
|
| 517 |
|
| 518 |
[*Example 16*:
|
| 519 |
|
| 520 |
``` cpp
|
|
|
|
| 22 |
A given type `P` can be composed from a number of other types,
|
| 23 |
templates, and non-type values:
|
| 24 |
|
| 25 |
- A function type includes the types of each of the function parameters
|
| 26 |
and the return type.
|
| 27 |
+
- A pointer-to-member type includes the type of the class object pointed
|
| 28 |
to and the type of the member pointed to.
|
| 29 |
- A type that is a specialization of a class template (e.g., `A<int>`)
|
| 30 |
includes the types, templates, and non-type values referenced by the
|
| 31 |
template argument list of the specialization.
|
| 32 |
- An array type includes the array element type and the value of the
|
| 33 |
array bound.
|
| 34 |
|
| 35 |
In most cases, the types, templates, and non-type values that are used
|
| 36 |
to compose `P` participate in template argument deduction. That is, they
|
| 37 |
+
may be used to determine the value of a template argument, and template
|
| 38 |
+
argument deduction fails if the value so determined is not consistent
|
| 39 |
+
with the values determined elsewhere. In certain contexts, however, the
|
| 40 |
+
value does not participate in type deduction, but instead uses the
|
| 41 |
+
values of template arguments that were either deduced elsewhere or
|
| 42 |
+
explicitly specified. If a template parameter is used only in
|
| 43 |
+
non-deduced contexts and is not explicitly specified, template argument
|
| 44 |
+
deduction fails.
|
| 45 |
|
| 46 |
+
[*Note 1*: Under [[temp.deduct.call]], if `P` contains no
|
| 47 |
+
*template-parameter*s that appear in deduced contexts, no deduction is
|
| 48 |
+
done, so `P` and `A` need not have the same form. — *end note*]
|
|
|
|
| 49 |
|
| 50 |
The non-deduced contexts are:
|
| 51 |
|
| 52 |
- The *nested-name-specifier* of a type that was specified using a
|
| 53 |
*qualified-id*.
|
|
|
|
| 55 |
- A non-type template argument or an array bound in which a
|
| 56 |
subexpression references a template parameter.
|
| 57 |
- A template parameter used in the parameter type of a function
|
| 58 |
parameter that has a default argument that is being used in the call
|
| 59 |
for which argument deduction is being done.
|
| 60 |
+
- A function parameter for which the associated argument is an overload
|
| 61 |
+
set [[over.over]], and one or more of the following apply:
|
|
|
|
|
|
|
| 62 |
- more than one function matches the function parameter type
|
| 63 |
(resulting in an ambiguous deduction), or
|
| 64 |
- no function matches the function parameter type, or
|
| 65 |
+
- the overload set supplied as an argument contains one or more
|
| 66 |
function templates.
|
| 67 |
- A function parameter for which the associated argument is an
|
| 68 |
+
initializer list [[dcl.init.list]] but the parameter does not have a
|
| 69 |
+
type for which deduction from an initializer list is specified
|
| 70 |
+
[[temp.deduct.call]].
|
| 71 |
\[*Example 1*:
|
| 72 |
``` cpp
|
| 73 |
template<class T> void g(T);
|
| 74 |
g({1,2,3}); // error: no argument deduced for T
|
| 75 |
```
|
|
|
|
| 159 |
template non-type argument `i` can be deduced if `P` and `A` have one of
|
| 160 |
the following forms:
|
| 161 |
|
| 162 |
``` cpp
|
| 163 |
T
|
| 164 |
+
cv T
|
| 165 |
T*
|
| 166 |
T&
|
| 167 |
T&&
|
| 168 |
T[integer-constant]
|
| 169 |
template-name<T> (where template-name refers to a class template)
|
|
|
|
| 185 |
TT<T>
|
| 186 |
TT<i>
|
| 187 |
TT<>
|
| 188 |
```
|
| 189 |
|
| 190 |
+
where `(T)` represents a parameter-type-list [[dcl.fct]] where at least
|
| 191 |
+
one parameter type contains a `T`, and `()` represents a
|
| 192 |
parameter-type-list where no parameter type contains a `T`. Similarly,
|
| 193 |
`<T>` represents template argument lists where at least one argument
|
| 194 |
contains a `T`, `<i>` represents template argument lists where at least
|
| 195 |
one argument contains an `i` and `<>` represents template argument lists
|
| 196 |
where no argument contains a `T` or an `i`.
|
|
|
|
| 202 |
is not the last template argument, the entire template argument list is
|
| 203 |
a non-deduced context. If `Pᵢ` is a pack expansion, then the pattern of
|
| 204 |
`Pᵢ` is compared with each remaining argument in the template argument
|
| 205 |
list of `A`. Each comparison deduces template arguments for subsequent
|
| 206 |
positions in the template parameter packs expanded by `Pᵢ`. During
|
| 207 |
+
partial ordering [[temp.deduct.partial]], if `Aᵢ` was originally a pack
|
| 208 |
+
expansion:
|
| 209 |
|
| 210 |
- if `P` does not contain a template argument corresponding to `Aᵢ` then
|
| 211 |
`Aᵢ` is ignored;
|
| 212 |
- otherwise, if `Pᵢ` is not a pack expansion, template argument
|
| 213 |
deduction fails.
|
|
|
|
| 227 |
```
|
| 228 |
|
| 229 |
— *end example*]
|
| 230 |
|
| 231 |
Similarly, if `P` has a form that contains `(T)`, then each parameter
|
| 232 |
+
type `Pᵢ` of the respective parameter-type-list [[dcl.fct]] of `P` is
|
| 233 |
compared with the corresponding parameter type `Aᵢ` of the corresponding
|
| 234 |
parameter-type-list of `A`. If `P` and `A` are function types that
|
| 235 |
+
originated from deduction when taking the address of a function template
|
| 236 |
+
[[temp.deduct.funcaddr]] or when deducing template arguments from a
|
| 237 |
+
function declaration [[temp.deduct.decl]] and `Pᵢ` and `Aᵢ` are
|
| 238 |
+
parameters of the top-level parameter-type-list of `P` and `A`,
|
| 239 |
+
respectively, `Pᵢ` is adjusted if it is a forwarding reference
|
| 240 |
+
[[temp.deduct.call]] and `Aᵢ` is an lvalue reference, in which case the
|
| 241 |
type of `Pᵢ` is changed to be the template parameter type (i.e., `T&&`
|
| 242 |
is changed to simply `T`).
|
| 243 |
|
| 244 |
[*Note 2*: As a result, when `Pᵢ` is `T&&` and `Aᵢ` is `X&`, the
|
| 245 |
adjusted `Pᵢ` will be `T`, causing `T` to be deduced as
|
|
|
|
| 262 |
If the *parameter-declaration* corresponding to `Pᵢ` is a function
|
| 263 |
parameter pack, then the type of its *declarator-id* is compared with
|
| 264 |
each remaining parameter type in the parameter-type-list of `A`. Each
|
| 265 |
comparison deduces template arguments for subsequent positions in the
|
| 266 |
template parameter packs expanded by the function parameter pack. During
|
| 267 |
+
partial ordering [[temp.deduct.partial]], if `Aᵢ` was originally a
|
| 268 |
function parameter pack:
|
| 269 |
|
| 270 |
- if `P` does not contain a function parameter type corresponding to
|
| 271 |
`Aᵢ` then `Aᵢ` is ignored;
|
| 272 |
- otherwise, if `Pᵢ` is not a function parameter pack, template argument
|
|
|
|
| 324 |
template<typename T, T n> struct C<A<n>> {
|
| 325 |
using Q = T;
|
| 326 |
};
|
| 327 |
|
| 328 |
using R = long;
|
| 329 |
+
using R = C<A<2>>::Q; // OK; T was deduced as long from the
|
| 330 |
// template argument value in the type A<2>
|
| 331 |
```
|
| 332 |
|
| 333 |
— *end example*]
|
| 334 |
|
|
|
|
| 341 |
template<typename T, T n> struct S<int[n]> {
|
| 342 |
using Q = T;
|
| 343 |
};
|
| 344 |
|
| 345 |
using V = decltype(sizeof 0);
|
| 346 |
+
using V = S<int[42]>::Q; // OK; T was deduced as std::size_t from the type int[42]
|
| 347 |
```
|
| 348 |
|
| 349 |
— *end example*]
|
| 350 |
|
| 351 |
[*Example 10*:
|
|
|
|
| 370 |
template<int i> void f2(int a[i][20]);
|
| 371 |
template<int i> void f3(int (&a)[i][20]);
|
| 372 |
|
| 373 |
void g() {
|
| 374 |
int v[10][20];
|
| 375 |
+
f1(v); // OK: i deduced as 20
|
| 376 |
f1<20>(v); // OK
|
| 377 |
f2(v); // error: cannot deduce template-argument i
|
| 378 |
f2<10>(v); // OK
|
| 379 |
+
f3(v); // OK: i deduced as 10
|
| 380 |
}
|
| 381 |
```
|
| 382 |
|
| 383 |
— *end note*]
|
| 384 |
|
|
|
|
| 420 |
typename B<i>::Y y); // i is not deduced here
|
| 421 |
A<int> a;
|
| 422 |
B<77> b;
|
| 423 |
|
| 424 |
int x = deduce<77>(a.xm, 62, b.ym);
|
| 425 |
+
// T deduced as int; a.xm must be convertible to A<int>::X
|
| 426 |
+
// i is explicitly specified to be 77; b.ym must be convertible to B<77>::Y
|
| 427 |
```
|
| 428 |
|
| 429 |
— *end note*]
|
| 430 |
|
| 431 |
If `P` has a form that contains `<i>`, and if the type of `i` differs
|
| 432 |
from the type of the corresponding template parameter of the template
|
| 433 |
named by the enclosing *simple-template-id*, deduction fails. If `P` has
|
| 434 |
a form that contains `[i]`, and if the type of `i` is not an integral
|
| 435 |
+
type, deduction fails.[^13]
|
| 436 |
|
| 437 |
[*Example 12*:
|
| 438 |
|
| 439 |
``` cpp
|
| 440 |
template<int i> class A { ... };
|
|
|
|
| 454 |
```
|
| 455 |
|
| 456 |
— *end example*]
|
| 457 |
|
| 458 |
A *template-argument* can be deduced from a function, pointer to
|
| 459 |
+
function, or pointer-to-member-function type.
|
| 460 |
|
| 461 |
[*Example 13*:
|
| 462 |
|
| 463 |
``` cpp
|
| 464 |
template<class T> void f(void(*)(T,int));
|
|
|
|
| 507 |
f(ab); // calls f(A<B>)
|
| 508 |
```
|
| 509 |
|
| 510 |
— *end example*]
|
| 511 |
|
| 512 |
+
[*Note 6*: Template argument deduction involving parameter packs
|
| 513 |
+
[[temp.variadic]] can deduce zero or more arguments for each parameter
|
| 514 |
pack. — *end note*]
|
| 515 |
|
| 516 |
[*Example 16*:
|
| 517 |
|
| 518 |
``` cpp
|