- tmp/tmped057c7w/{from.md → to.md} +150 -40
tmp/tmped057c7w/{from.md → to.md}
RENAMED
|
@@ -14,11 +14,12 @@ In some cases, the deduction is done using a single set of types `P` and
|
|
| 14 |
deduced template argument values are then combined. If type deduction
|
| 15 |
cannot be done for any `P/A` pair, or if for any pair the deduction
|
| 16 |
leads to more than one possible set of deduced values, or if different
|
| 17 |
pairs yield different deduced values, or if any template argument
|
| 18 |
remains neither deduced nor explicitly specified, template argument
|
| 19 |
-
deduction fails.
|
|
|
|
| 20 |
|
| 21 |
A given type `P` can be composed from a number of other types,
|
| 22 |
templates, and non-type values:
|
| 23 |
|
| 24 |
- A function type includes the types of each of the function parameters
|
|
@@ -39,10 +40,15 @@ In certain contexts, however, the value does not participate in type
|
|
| 39 |
deduction, but instead uses the values of template arguments that were
|
| 40 |
either deduced elsewhere or explicitly specified. If a template
|
| 41 |
parameter is used only in non-deduced contexts and is not explicitly
|
| 42 |
specified, template argument deduction fails.
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
The non-deduced contexts are:
|
| 45 |
|
| 46 |
- The *nested-name-specifier* of a type that was specified using a
|
| 47 |
*qualified-id*.
|
| 48 |
- The *expression* of a *decltype-specifier*.
|
|
@@ -60,35 +66,43 @@ The non-deduced contexts are:
|
|
| 60 |
- no function matches the function parameter type, or
|
| 61 |
- the set of functions supplied as an argument contains one or more
|
| 62 |
function templates.
|
| 63 |
- A function parameter for which the associated argument is an
|
| 64 |
initializer list ([[dcl.init.list]]) but the parameter does not have
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 67 |
``` cpp
|
| 68 |
template<class T> void g(T);
|
| 69 |
g({1,2,3}); // error: no argument deduced for T
|
| 70 |
```
|
|
|
|
|
|
|
| 71 |
- A function parameter pack that does not occur at the end of the
|
| 72 |
*parameter-declaration-list*.
|
| 73 |
|
| 74 |
When a type name is specified in a way that includes a non-deduced
|
| 75 |
context, all of the types that comprise that type name are also
|
| 76 |
non-deduced. However, a compound type can include both deduced and
|
| 77 |
-
non-deduced types.
|
|
|
|
|
|
|
| 78 |
`T2` are non-deduced. Likewise, if a type is specified as
|
| 79 |
`A<I+J>::X<T>`, `I`, `J`, and `T` are non-deduced. If a type is
|
| 80 |
specified as `void` `f(typename` `A<T>::B,` `A<T>)`, the `T` in
|
| 81 |
-
`A<T>::B` is non-deduced but the `T` in `A<T>` is
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
Here is an example in which different parameter/argument pairs produce
|
| 84 |
inconsistent template argument deductions:
|
| 85 |
|
| 86 |
``` cpp
|
| 87 |
-
template<class T> void f(T x, T y) {
|
| 88 |
-
struct A {
|
| 89 |
-
struct B : A {
|
| 90 |
void g(A a, B b) {
|
| 91 |
f(a,b); // error: T could be A or B
|
| 92 |
f(b,a); // error: T could be A or B
|
| 93 |
f(a,a); // OK: T is A
|
| 94 |
f(b,b); // OK: T is B
|
|
@@ -139,10 +153,12 @@ void t() {
|
|
| 139 |
f(d); // calls f(B<int>&)
|
| 140 |
f(d2); // calls f(B<int>&)
|
| 141 |
}
|
| 142 |
```
|
| 143 |
|
|
|
|
|
|
|
| 144 |
A template type argument `T`, a template template argument `TT` or a
|
| 145 |
template non-type argument `i` can be deduced if `P` and `A` have one of
|
| 146 |
the following forms:
|
| 147 |
|
| 148 |
``` cpp
|
|
@@ -171,20 +187,20 @@ template-name<i> (where template-name refers to a class template)
|
|
| 171 |
TT<T>
|
| 172 |
TT<i>
|
| 173 |
TT<>
|
| 174 |
```
|
| 175 |
|
| 176 |
-
where `(T)` represents a
|
| 177 |
-
parameter type contains a `T`, and `()` represents a
|
| 178 |
-
|
| 179 |
`<T>` represents template argument lists where at least one argument
|
| 180 |
contains a `T`, `<i>` represents template argument lists where at least
|
| 181 |
one argument contains an `i` and `<>` represents template argument lists
|
| 182 |
where no argument contains a `T` or an `i`.
|
| 183 |
|
| 184 |
If `P` has a form that contains `<T>` or `<i>`, then each argument Pᵢ of
|
| 185 |
-
the respective template argument list `P` is compared with the
|
| 186 |
corresponding argument Aᵢ of the corresponding template argument list of
|
| 187 |
`A`. If the template argument list of `P` contains a pack expansion that
|
| 188 |
is not the last template argument, the entire template argument list is
|
| 189 |
a non-deduced context. If `Pᵢ` is a pack expansion, then the pattern of
|
| 190 |
`Pᵢ` is compared with each remaining argument in the template argument
|
|
@@ -196,10 +212,12 @@ pack expansion:
|
|
| 196 |
- if `P` does not contain a template argument corresponding to `Aᵢ` then
|
| 197 |
`Aᵢ` is ignored;
|
| 198 |
- otherwise, if `Pᵢ` is not a pack expansion, template argument
|
| 199 |
deduction fails.
|
| 200 |
|
|
|
|
|
|
|
| 201 |
``` cpp
|
| 202 |
template<class T1, class... Z> class S; // #1
|
| 203 |
template<class T1, class... Z> class S<T1, const Z&...> { }; // #2
|
| 204 |
template<class T1, class T2> class S<T1, const T2&> { }; // #3
|
| 205 |
S<int, const int&> s; // both #2 and #3 match; #3 is more specialized
|
|
@@ -208,24 +226,30 @@ template<class T, class... U> struct A { }; // #1
|
|
| 208 |
template<class T1, class T2, class... U> struct A<T1, T2*, U...> { }; // #2
|
| 209 |
template<class T1, class T2> struct A<T1, T2> { }; // #3
|
| 210 |
template struct A<int, int*>; // selects #2
|
| 211 |
```
|
| 212 |
|
|
|
|
|
|
|
| 213 |
Similarly, if `P` has a form that contains `(T)`, then each parameter
|
| 214 |
-
type `Pᵢ` of the respective
|
| 215 |
-
with the corresponding parameter type `Aᵢ` of the corresponding
|
| 216 |
-
|
| 217 |
originated from deduction when taking the address of a function
|
| 218 |
template ([[temp.deduct.funcaddr]]) or when deducing template arguments
|
| 219 |
from a function declaration ([[temp.deduct.decl]]) and `Pᵢ` and `Aᵢ`
|
| 220 |
-
are parameters of the top-level
|
| 221 |
-
respectively, `Pᵢ` is adjusted if it is
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
``` cpp
|
| 229 |
template <class T> void f(T&&);
|
| 230 |
template <> void f(int&) { } // #1
|
| 231 |
template <> void f(int&&) { } // #2
|
|
@@ -233,32 +257,40 @@ void g(int i) {
|
|
| 233 |
f(i); // calls f<int&>(int&), i.e., #1
|
| 234 |
f(0); // calls f<int>(int&&), i.e., #2
|
| 235 |
}
|
| 236 |
```
|
| 237 |
|
|
|
|
|
|
|
| 238 |
If the *parameter-declaration* corresponding to `Pᵢ` is a function
|
| 239 |
parameter pack, then the type of its *declarator-id* is compared with
|
| 240 |
-
each remaining parameter type in the
|
| 241 |
comparison deduces template arguments for subsequent positions in the
|
| 242 |
template parameter packs expanded by the function parameter pack. During
|
| 243 |
partial ordering ([[temp.deduct.partial]]), if `Aᵢ` was originally a
|
| 244 |
function parameter pack:
|
| 245 |
|
| 246 |
- if `P` does not contain a function parameter type corresponding to
|
| 247 |
`Aᵢ` then `Aᵢ` is ignored;
|
| 248 |
- otherwise, if `Pᵢ` is not a function parameter pack, template argument
|
| 249 |
deduction fails.
|
| 250 |
|
|
|
|
|
|
|
| 251 |
``` cpp
|
| 252 |
template<class T, class... U> void f(T*, U...) { } // #1
|
| 253 |
template<class T> void f(T) { } // #2
|
| 254 |
template void f(int*); // selects #1
|
| 255 |
```
|
| 256 |
|
|
|
|
|
|
|
| 257 |
These forms can be used in the same way as `T` is for further
|
| 258 |
composition of types.
|
| 259 |
|
|
|
|
|
|
|
| 260 |
``` cpp
|
| 261 |
X<int> (*)(char[6])
|
| 262 |
```
|
| 263 |
|
| 264 |
is of the form
|
|
@@ -273,22 +305,67 @@ which is a variant of
|
|
| 273 |
type (*)(T)
|
| 274 |
```
|
| 275 |
|
| 276 |
where type is `X<int>` and `T` is `char[6]`.
|
| 277 |
|
|
|
|
|
|
|
| 278 |
Template arguments cannot be deduced from function arguments involving
|
| 279 |
constructs other than the ones specified above.
|
| 280 |
|
| 281 |
-
|
| 282 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
|
| 284 |
``` cpp
|
| 285 |
-
template<
|
| 286 |
-
|
| 287 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
```
|
| 289 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
Except for reference and pointer types, a major array bound is not part
|
| 291 |
of a function parameter type and cannot be deduced from an argument:
|
| 292 |
|
| 293 |
``` cpp
|
| 294 |
template<int i> void f1(int a[10][i]);
|
|
@@ -303,17 +380,23 @@ void g() {
|
|
| 303 |
f2<10>(v); // OK
|
| 304 |
f3(v); // OK: i deduced to be 10
|
| 305 |
}
|
| 306 |
```
|
| 307 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
If, in the declaration of a function template with a non-type template
|
| 309 |
parameter, the non-type template parameter is used in a subexpression in
|
| 310 |
the function parameter list, the expression is a non-deduced context as
|
| 311 |
specified above.
|
| 312 |
|
|
|
|
|
|
|
| 313 |
``` cpp
|
| 314 |
-
template <int i> class A {
|
| 315 |
template <int i> void g(A<i+1>);
|
| 316 |
template <int i> void f(A<i>, A<i+1>);
|
| 317 |
void k() {
|
| 318 |
A<1> a1;
|
| 319 |
A<2> a2;
|
|
@@ -321,10 +404,16 @@ void k() {
|
|
| 321 |
g<0>(a1); // OK
|
| 322 |
f(a1, a2); // OK
|
| 323 |
}
|
| 324 |
```
|
| 325 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
Template parameters do not participate in template argument deduction if
|
| 327 |
they are used only in non-deduced contexts. For example,
|
| 328 |
|
| 329 |
``` cpp
|
| 330 |
template<int i, typename T>
|
|
@@ -333,23 +422,26 @@ T deduce(typename A<T>::X x, // T is not deduced here
|
|
| 333 |
typename B<i>::Y y); // i is not deduced here
|
| 334 |
A<int> a;
|
| 335 |
B<77> b;
|
| 336 |
|
| 337 |
int x = deduce<77>(a.xm, 62, b.ym);
|
| 338 |
-
// T is deduced to be int, a.xm must be convertible to
|
| 339 |
-
//
|
| 340 |
-
// i is explicitly specified to be 77, b.ym must be convertible
|
| 341 |
-
// to B<77>::Y
|
| 342 |
```
|
| 343 |
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
|
| 349 |
``` cpp
|
| 350 |
-
template<int i> class A {
|
| 351 |
template<short s> void f(A<s>);
|
| 352 |
void k1() {
|
| 353 |
A<1> a;
|
| 354 |
f(a); // error: deduction fails for conversion from int to short
|
| 355 |
f<1>(a); // OK
|
|
@@ -361,13 +453,17 @@ void k2() {
|
|
| 361 |
B<1> b;
|
| 362 |
g(b); // OK: cv-qualifiers are ignored on template parameter types
|
| 363 |
}
|
| 364 |
```
|
| 365 |
|
|
|
|
|
|
|
| 366 |
A *template-argument* can be deduced from a function, pointer to
|
| 367 |
function, or pointer to member function type.
|
| 368 |
|
|
|
|
|
|
|
| 369 |
``` cpp
|
| 370 |
template<class T> void f(void(*)(T,int));
|
| 371 |
template<class T> void foo(T,int);
|
| 372 |
void g(int,int);
|
| 373 |
void g(char,int);
|
|
@@ -379,37 +475,49 @@ int m() {
|
|
| 379 |
f(&h); // OK: void h(char,int) is a unique match
|
| 380 |
f(&foo); // error: type deduction fails because foo is a template
|
| 381 |
}
|
| 382 |
```
|
| 383 |
|
|
|
|
|
|
|
| 384 |
A template *type-parameter* cannot be deduced from the type of a
|
| 385 |
function default argument.
|
| 386 |
|
|
|
|
|
|
|
| 387 |
``` cpp
|
| 388 |
template <class T> void f(T = 5, T = 7);
|
| 389 |
void g() {
|
| 390 |
f(1); // OK: call f<int>(1,7)
|
| 391 |
f(); // error: cannot deduce T
|
| 392 |
f<int>(); // OK: call f<int>(5,7)
|
| 393 |
}
|
| 394 |
```
|
| 395 |
|
|
|
|
|
|
|
| 396 |
The *template-argument* corresponding to a template *template-parameter*
|
| 397 |
is deduced from the type of the *template-argument* of a class template
|
| 398 |
specialization used in the argument list of a function call.
|
| 399 |
|
|
|
|
|
|
|
| 400 |
``` cpp
|
| 401 |
template <template <class T> class X> struct A { };
|
| 402 |
template <template <class T> class X> void f(A<X>) { }
|
| 403 |
template<class T> struct B { };
|
| 404 |
A<B> ab;
|
| 405 |
f(ab); // calls f(A<B>)
|
| 406 |
```
|
| 407 |
|
| 408 |
-
|
|
|
|
|
|
|
| 409 |
[[temp.variadic]]) can deduce zero or more arguments for each parameter
|
| 410 |
-
pack.
|
|
|
|
|
|
|
| 411 |
|
| 412 |
``` cpp
|
| 413 |
template<class> struct X { };
|
| 414 |
template<class R, class ... ArgTypes> struct X<R(int, ArgTypes ...)> { };
|
| 415 |
template<class ... Types> struct Y { };
|
|
@@ -425,5 +533,7 @@ Y<> y1; // use primary template; Types is empty
|
|
| 425 |
Y<int&, float&, double&> y2; // uses partial specialization; T is int&, Types contains float, double
|
| 426 |
Y<int, float, double> y3; // uses primary template; Types contains int, float, double
|
| 427 |
int fv = f(g); // OK; Types contains int, float
|
| 428 |
```
|
| 429 |
|
|
|
|
|
|
|
|
|
| 14 |
deduced template argument values are then combined. If type deduction
|
| 15 |
cannot be done for any `P/A` pair, or if for any pair the deduction
|
| 16 |
leads to more than one possible set of deduced values, or if different
|
| 17 |
pairs yield different deduced values, or if any template argument
|
| 18 |
remains neither deduced nor explicitly specified, template argument
|
| 19 |
+
deduction fails. The type of a type parameter is only deduced from an
|
| 20 |
+
array bound if it is not otherwise deduced.
|
| 21 |
|
| 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
|
|
|
|
| 40 |
deduction, but instead uses the values of template arguments that were
|
| 41 |
either deduced elsewhere or explicitly specified. If a template
|
| 42 |
parameter is used only in non-deduced contexts and is not explicitly
|
| 43 |
specified, template argument deduction fails.
|
| 44 |
|
| 45 |
+
[*Note 1*: Under [[temp.deduct.call]] and [[temp.deduct.partial]], if
|
| 46 |
+
`P` contains no *template-parameter*s that appear in deduced contexts,
|
| 47 |
+
no deduction is done, so `P` and `A` need not have the same
|
| 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*.
|
| 54 |
- The *expression* of a *decltype-specifier*.
|
|
|
|
| 66 |
- no function matches the function parameter type, or
|
| 67 |
- the set of functions supplied as an argument contains one or more
|
| 68 |
function templates.
|
| 69 |
- A function parameter for which the associated argument is an
|
| 70 |
initializer list ([[dcl.init.list]]) but the parameter does not have
|
| 71 |
+
a type for which deduction from an initializer list is specified (
|
| 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 |
```
|
| 78 |
+
|
| 79 |
+
— *end example*]
|
| 80 |
- A function parameter pack that does not occur at the end of the
|
| 81 |
*parameter-declaration-list*.
|
| 82 |
|
| 83 |
When a type name is specified in a way that includes a non-deduced
|
| 84 |
context, all of the types that comprise that type name are also
|
| 85 |
non-deduced. However, a compound type can include both deduced and
|
| 86 |
+
non-deduced types.
|
| 87 |
+
|
| 88 |
+
[*Example 2*: If a type is specified as `A<T>::B<T2>`, both `T` and
|
| 89 |
`T2` are non-deduced. Likewise, if a type is specified as
|
| 90 |
`A<I+J>::X<T>`, `I`, `J`, and `T` are non-deduced. If a type is
|
| 91 |
specified as `void` `f(typename` `A<T>::B,` `A<T>)`, the `T` in
|
| 92 |
+
`A<T>::B` is non-deduced but the `T` in `A<T>` is
|
| 93 |
+
deduced. — *end example*]
|
| 94 |
+
|
| 95 |
+
[*Example 3*:
|
| 96 |
|
| 97 |
Here is an example in which different parameter/argument pairs produce
|
| 98 |
inconsistent template argument deductions:
|
| 99 |
|
| 100 |
``` cpp
|
| 101 |
+
template<class T> void f(T x, T y) { ... }
|
| 102 |
+
struct A { ... };
|
| 103 |
+
struct B : A { ... };
|
| 104 |
void g(A a, B b) {
|
| 105 |
f(a,b); // error: T could be A or B
|
| 106 |
f(b,a); // error: T could be A or B
|
| 107 |
f(a,a); // OK: T is A
|
| 108 |
f(b,b); // OK: T is B
|
|
|
|
| 153 |
f(d); // calls f(B<int>&)
|
| 154 |
f(d2); // calls f(B<int>&)
|
| 155 |
}
|
| 156 |
```
|
| 157 |
|
| 158 |
+
— *end example*]
|
| 159 |
+
|
| 160 |
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
|
|
|
|
| 187 |
TT<T>
|
| 188 |
TT<i>
|
| 189 |
TT<>
|
| 190 |
```
|
| 191 |
|
| 192 |
+
where `(T)` represents a parameter-type-list ([[dcl.fct]]) where at
|
| 193 |
+
least one parameter type contains a `T`, and `()` represents a
|
| 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`.
|
| 199 |
|
| 200 |
If `P` has a form that contains `<T>` or `<i>`, then each argument Pᵢ of
|
| 201 |
+
the respective template argument list of `P` is compared with the
|
| 202 |
corresponding argument Aᵢ of the corresponding template argument list of
|
| 203 |
`A`. If the template argument list of `P` contains a pack expansion that
|
| 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
|
|
|
|
| 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.
|
| 216 |
|
| 217 |
+
[*Example 4*:
|
| 218 |
+
|
| 219 |
``` cpp
|
| 220 |
template<class T1, class... Z> class S; // #1
|
| 221 |
template<class T1, class... Z> class S<T1, const Z&...> { }; // #2
|
| 222 |
template<class T1, class T2> class S<T1, const T2&> { }; // #3
|
| 223 |
S<int, const int&> s; // both #2 and #3 match; #3 is more specialized
|
|
|
|
| 226 |
template<class T1, class T2, class... U> struct A<T1, T2*, U...> { }; // #2
|
| 227 |
template<class T1, class T2> struct A<T1, T2> { }; // #3
|
| 228 |
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 ([[dcl.fct]]) of `P` is
|
| 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 |
template ([[temp.deduct.funcaddr]]) or when deducing template arguments
|
| 239 |
from a function declaration ([[temp.deduct.decl]]) and `Pᵢ` and `Aᵢ`
|
| 240 |
+
are parameters of the top-level parameter-type-list of `P` and `A`,
|
| 241 |
+
respectively, `Pᵢ` is adjusted if it is a forwarding reference (
|
| 242 |
+
[[temp.deduct.call]]) and `Aᵢ` is an lvalue reference, in which case the
|
| 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
|
| 248 |
+
`X&`. — *end note*]
|
| 249 |
+
|
| 250 |
+
[*Example 5*:
|
| 251 |
|
| 252 |
``` cpp
|
| 253 |
template <class T> void f(T&&);
|
| 254 |
template <> void f(int&) { } // #1
|
| 255 |
template <> void f(int&&) { } // #2
|
|
|
|
| 257 |
f(i); // calls f<int&>(int&), i.e., #1
|
| 258 |
f(0); // calls f<int>(int&&), i.e., #2
|
| 259 |
}
|
| 260 |
```
|
| 261 |
|
| 262 |
+
— *end example*]
|
| 263 |
+
|
| 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 ([[temp.deduct.partial]]), if `Aᵢ` was originally a
|
| 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
|
| 275 |
deduction fails.
|
| 276 |
|
| 277 |
+
[*Example 6*:
|
| 278 |
+
|
| 279 |
``` cpp
|
| 280 |
template<class T, class... U> void f(T*, U...) { } // #1
|
| 281 |
template<class T> void f(T) { } // #2
|
| 282 |
template void f(int*); // selects #1
|
| 283 |
```
|
| 284 |
|
| 285 |
+
— *end example*]
|
| 286 |
+
|
| 287 |
These forms can be used in the same way as `T` is for further
|
| 288 |
composition of types.
|
| 289 |
|
| 290 |
+
[*Example 7*:
|
| 291 |
+
|
| 292 |
``` cpp
|
| 293 |
X<int> (*)(char[6])
|
| 294 |
```
|
| 295 |
|
| 296 |
is of the form
|
|
|
|
| 305 |
type (*)(T)
|
| 306 |
```
|
| 307 |
|
| 308 |
where type is `X<int>` and `T` is `char[6]`.
|
| 309 |
|
| 310 |
+
— *end example*]
|
| 311 |
+
|
| 312 |
Template arguments cannot be deduced from function arguments involving
|
| 313 |
constructs other than the ones specified above.
|
| 314 |
|
| 315 |
+
When the value of the argument corresponding to a non-type template
|
| 316 |
+
parameter `P` that is declared with a dependent type is deduced from an
|
| 317 |
+
expression, the template parameters in the type of `P` are deduced from
|
| 318 |
+
the type of the value.
|
| 319 |
+
|
| 320 |
+
[*Example 8*:
|
| 321 |
|
| 322 |
``` cpp
|
| 323 |
+
template<long n> struct A { };
|
| 324 |
+
|
| 325 |
+
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 to long from the
|
| 332 |
+
// template argument value in the type A<2>
|
| 333 |
```
|
| 334 |
|
| 335 |
+
— *end example*]
|
| 336 |
+
|
| 337 |
+
The type of `N` in the type `T[N]` is `std::size_t`.
|
| 338 |
+
|
| 339 |
+
[*Example 9*:
|
| 340 |
+
|
| 341 |
+
``` cpp
|
| 342 |
+
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 to std::size_t from the type int[42]
|
| 349 |
+
```
|
| 350 |
+
|
| 351 |
+
— *end example*]
|
| 352 |
+
|
| 353 |
+
[*Example 10*:
|
| 354 |
+
|
| 355 |
+
``` cpp
|
| 356 |
+
template<class T, T i> void f(int (&a)[i]);
|
| 357 |
+
int v[10];
|
| 358 |
+
void g() {
|
| 359 |
+
f(v); // OK: T is std::size_t
|
| 360 |
+
}
|
| 361 |
+
```
|
| 362 |
+
|
| 363 |
+
— *end example*]
|
| 364 |
+
|
| 365 |
+
[*Note 3*:
|
| 366 |
+
|
| 367 |
Except for reference and pointer types, a major array bound is not part
|
| 368 |
of a function parameter type and cannot be deduced from an argument:
|
| 369 |
|
| 370 |
``` cpp
|
| 371 |
template<int i> void f1(int a[10][i]);
|
|
|
|
| 380 |
f2<10>(v); // OK
|
| 381 |
f3(v); // OK: i deduced to be 10
|
| 382 |
}
|
| 383 |
```
|
| 384 |
|
| 385 |
+
— *end note*]
|
| 386 |
+
|
| 387 |
+
[*Note 4*:
|
| 388 |
+
|
| 389 |
If, in the declaration of a function template with a non-type template
|
| 390 |
parameter, the non-type template parameter is used in a subexpression in
|
| 391 |
the function parameter list, the expression is a non-deduced context as
|
| 392 |
specified above.
|
| 393 |
|
| 394 |
+
[*Example 11*:
|
| 395 |
+
|
| 396 |
``` cpp
|
| 397 |
+
template <int i> class A { ... };
|
| 398 |
template <int i> void g(A<i+1>);
|
| 399 |
template <int i> void f(A<i>, A<i+1>);
|
| 400 |
void k() {
|
| 401 |
A<1> a1;
|
| 402 |
A<2> a2;
|
|
|
|
| 404 |
g<0>(a1); // OK
|
| 405 |
f(a1, a2); // OK
|
| 406 |
}
|
| 407 |
```
|
| 408 |
|
| 409 |
+
— *end example*]
|
| 410 |
+
|
| 411 |
+
— *end note*]
|
| 412 |
+
|
| 413 |
+
[*Note 5*:
|
| 414 |
+
|
| 415 |
Template parameters do not participate in template argument deduction if
|
| 416 |
they are used only in non-deduced contexts. For example,
|
| 417 |
|
| 418 |
``` cpp
|
| 419 |
template<int i, typename T>
|
|
|
|
| 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 is deduced to be int, a.xm must be convertible to A<int>::X
|
| 428 |
+
// i is explicitly specified to be 77, b.ym must be convertible to B<77>::Y
|
|
|
|
|
|
|
| 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.[^8]
|
| 438 |
+
|
| 439 |
+
[*Example 12*:
|
| 440 |
|
| 441 |
``` cpp
|
| 442 |
+
template<int i> class A { ... };
|
| 443 |
template<short s> void f(A<s>);
|
| 444 |
void k1() {
|
| 445 |
A<1> a;
|
| 446 |
f(a); // error: deduction fails for conversion from int to short
|
| 447 |
f<1>(a); // OK
|
|
|
|
| 453 |
B<1> b;
|
| 454 |
g(b); // OK: cv-qualifiers are ignored on template parameter types
|
| 455 |
}
|
| 456 |
```
|
| 457 |
|
| 458 |
+
— *end example*]
|
| 459 |
+
|
| 460 |
A *template-argument* can be deduced from a function, pointer to
|
| 461 |
function, or pointer to member function type.
|
| 462 |
|
| 463 |
+
[*Example 13*:
|
| 464 |
+
|
| 465 |
``` cpp
|
| 466 |
template<class T> void f(void(*)(T,int));
|
| 467 |
template<class T> void foo(T,int);
|
| 468 |
void g(int,int);
|
| 469 |
void g(char,int);
|
|
|
|
| 475 |
f(&h); // OK: void h(char,int) is a unique match
|
| 476 |
f(&foo); // error: type deduction fails because foo is a template
|
| 477 |
}
|
| 478 |
```
|
| 479 |
|
| 480 |
+
— *end example*]
|
| 481 |
+
|
| 482 |
A template *type-parameter* cannot be deduced from the type of a
|
| 483 |
function default argument.
|
| 484 |
|
| 485 |
+
[*Example 14*:
|
| 486 |
+
|
| 487 |
``` cpp
|
| 488 |
template <class T> void f(T = 5, T = 7);
|
| 489 |
void g() {
|
| 490 |
f(1); // OK: call f<int>(1,7)
|
| 491 |
f(); // error: cannot deduce T
|
| 492 |
f<int>(); // OK: call f<int>(5,7)
|
| 493 |
}
|
| 494 |
```
|
| 495 |
|
| 496 |
+
— *end example*]
|
| 497 |
+
|
| 498 |
The *template-argument* corresponding to a template *template-parameter*
|
| 499 |
is deduced from the type of the *template-argument* of a class template
|
| 500 |
specialization used in the argument list of a function call.
|
| 501 |
|
| 502 |
+
[*Example 15*:
|
| 503 |
+
|
| 504 |
``` cpp
|
| 505 |
template <template <class T> class X> struct A { };
|
| 506 |
template <template <class T> class X> void f(A<X>) { }
|
| 507 |
template<class T> struct B { };
|
| 508 |
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]]) can deduce zero or more arguments for each parameter
|
| 516 |
+
pack. — *end note*]
|
| 517 |
+
|
| 518 |
+
[*Example 16*:
|
| 519 |
|
| 520 |
``` cpp
|
| 521 |
template<class> struct X { };
|
| 522 |
template<class R, class ... ArgTypes> struct X<R(int, ArgTypes ...)> { };
|
| 523 |
template<class ... Types> struct Y { };
|
|
|
|
| 533 |
Y<int&, float&, double&> y2; // uses partial specialization; T is int&, Types contains float, double
|
| 534 |
Y<int, float, double> y3; // uses primary template; Types contains int, float, double
|
| 535 |
int fv = f(g); // OK; Types contains int, float
|
| 536 |
```
|
| 537 |
|
| 538 |
+
— *end example*]
|
| 539 |
+
|