- tmp/tmp3rwmpo6i/{from.md → to.md} +457 -155
tmp/tmp3rwmpo6i/{from.md → to.md}
RENAMED
|
@@ -3,10 +3,12 @@
|
|
| 3 |
When a function template specialization is referenced, all of the
|
| 4 |
template arguments shall have values. The values can be explicitly
|
| 5 |
specified or, in some cases, be deduced from the use or obtained from
|
| 6 |
default *template-argument*s.
|
| 7 |
|
|
|
|
|
|
|
| 8 |
``` cpp
|
| 9 |
void f(Array<dcomplex>& cv, Array<int>& ci) {
|
| 10 |
sort(cv); // calls sort(Array<dcomplex>&)
|
| 11 |
sort(ci); // calls sort(Array<int>&)
|
| 12 |
}
|
|
@@ -19,10 +21,12 @@ void g(double d) {
|
|
| 19 |
int i = convert<int>(d); // calls convert<int,double>(double)
|
| 20 |
int c = convert<char>(d); // calls convert<char,double>(double)
|
| 21 |
}
|
| 22 |
```
|
| 23 |
|
|
|
|
|
|
|
| 24 |
When an explicit template argument list is specified, the template
|
| 25 |
arguments must be compatible with the template parameter list and must
|
| 26 |
result in a valid function type as described below; otherwise type
|
| 27 |
deduction fails. Specifically, the following steps are performed when
|
| 28 |
evaluating an explicitly specified template argument list with respect
|
|
@@ -39,15 +43,20 @@ to a given function template:
|
|
| 39 |
[[temp.arg.nontype]], otherwise type deduction fails.
|
| 40 |
- The specified template argument values are substituted for the
|
| 41 |
corresponding template parameters as specified below.
|
| 42 |
|
| 43 |
After this substitution is performed, the function parameter type
|
| 44 |
-
adjustments described in [[dcl.fct]] are performed.
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
``` cpp
|
| 51 |
template <class T> void f(T t);
|
| 52 |
template <class X> void g(const X x);
|
| 53 |
template <class Z> void h(Z, Z*);
|
|
@@ -68,21 +77,26 @@ int main() {
|
|
| 68 |
// #5: function type is h(int, const int*)
|
| 69 |
h<const int>(1,0);
|
| 70 |
}
|
| 71 |
```
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
The resulting substituted and adjusted function type is used as the type
|
| 77 |
of the function template for template argument deduction. If a template
|
| 78 |
argument has not been deduced and its corresponding template parameter
|
| 79 |
has a default argument, the template argument is determined by
|
| 80 |
substituting the template arguments determined for preceding template
|
| 81 |
parameters into the default argument. If the substitution results in an
|
| 82 |
invalid type, as described above, type deduction fails.
|
| 83 |
|
|
|
|
|
|
|
| 84 |
``` cpp
|
| 85 |
template <class T, class U = double>
|
| 86 |
void f(T t = 0, U u = 0);
|
| 87 |
|
| 88 |
void g() {
|
|
@@ -92,10 +106,12 @@ void g() {
|
|
| 92 |
f<int>(); // f<int,double>(0,0)
|
| 93 |
f<int,char>(); // f<int,char>(0,0)
|
| 94 |
}
|
| 95 |
```
|
| 96 |
|
|
|
|
|
|
|
| 97 |
When all template arguments have been deduced or obtained from default
|
| 98 |
template arguments, all uses of template parameters in the template
|
| 99 |
parameter list of the template and the function type are replaced with
|
| 100 |
the corresponding deduced or default argument values. If the
|
| 101 |
substitution results in an invalid type, as described above, type
|
|
@@ -115,14 +131,18 @@ the function type and in template parameter declarations. The
|
|
| 115 |
expressions include not only constant expressions such as those that
|
| 116 |
appear in array bounds or as nontype template arguments but also general
|
| 117 |
expressions (i.e., non-constant expressions) inside `sizeof`,
|
| 118 |
`decltype`, and other contexts that allow non-constant expressions. The
|
| 119 |
substitution proceeds in lexical order and stops when a condition that
|
| 120 |
-
causes deduction to fail is encountered.
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
``` cpp
|
| 126 |
template <class T> struct A { using X = typename T::X; };
|
| 127 |
template <class T> typename T::X f(typename A<T>::X);
|
| 128 |
template <class T> void f(...) { }
|
|
@@ -133,22 +153,33 @@ void h() {
|
|
| 133 |
f<int>(0); // OK, substituting return type causes deduction to fail
|
| 134 |
g<int>(0); // error, substituting parameter type instantiates A<int>
|
| 135 |
}
|
| 136 |
```
|
| 137 |
|
|
|
|
|
|
|
| 138 |
If a substitution results in an invalid type or expression, type
|
| 139 |
deduction fails. An invalid type or expression is one that would be
|
| 140 |
ill-formed, with a diagnostic required, if written using the substituted
|
| 141 |
-
arguments.
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
Only invalid types and expressions in the immediate context of the
|
| 144 |
function type and its template parameter types can result in a deduction
|
| 145 |
-
failure.
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
``` cpp
|
| 152 |
struct X { };
|
| 153 |
struct Y {
|
| 154 |
Y(X){}
|
|
@@ -159,36 +190,47 @@ X f(Y, Y); // #2
|
|
| 159 |
|
| 160 |
X x1, x2;
|
| 161 |
X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
|
| 162 |
```
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
Type deduction may fail for the following reasons:
|
| 165 |
|
| 166 |
- Attempting to instantiate a pack expansion containing multiple
|
| 167 |
parameter packs of differing lengths.
|
| 168 |
- Attempting to create an array with an element type that is `void`, a
|
| 169 |
function type, a reference type, or an abstract class type, or
|
| 170 |
attempting to create an array with a size that is zero or negative.
|
|
|
|
| 171 |
``` cpp
|
| 172 |
template <class T> int f(T[5]);
|
| 173 |
int I = f<int>(0);
|
| 174 |
int j = f<void>(0); // invalid array
|
| 175 |
```
|
|
|
|
|
|
|
| 176 |
- Attempting to use a type that is not a class or enumeration type in a
|
| 177 |
qualified name.
|
|
|
|
| 178 |
``` cpp
|
| 179 |
template <class T> int f(typename T::B*);
|
| 180 |
int i = f<int>(0);
|
| 181 |
```
|
|
|
|
|
|
|
| 182 |
- Attempting to use a type in a *nested-name-specifier* of a
|
| 183 |
*qualified-id* when that type does not contain the specified member,
|
| 184 |
or
|
| 185 |
- the specified member is not a type where a type is required, or
|
| 186 |
- the specified member is not a template where a template is required,
|
| 187 |
or
|
| 188 |
- the specified member is not a non-type where a non-type is required.
|
| 189 |
|
|
|
|
| 190 |
``` cpp
|
| 191 |
template <int I> struct X { };
|
| 192 |
template <template <class T> class> struct Z { };
|
| 193 |
template <class T> void f(typename T::Y*){}
|
| 194 |
template <class T> void g(X<T::N>*){}
|
|
@@ -208,82 +250,124 @@ Type deduction may fail for the following reasons:
|
|
| 208 |
f<B>(0); // The Y member of B is not a type
|
| 209 |
g<C>(0); // The N member of C is not a non-type
|
| 210 |
h<D>(0); // The TT member of D is not a template
|
| 211 |
}
|
| 212 |
```
|
|
|
|
|
|
|
| 213 |
- Attempting to create a pointer to reference type.
|
| 214 |
- Attempting to create a reference to `void`.
|
| 215 |
- Attempting to create “pointer to member of `T`” when `T` is not a
|
| 216 |
class type.
|
|
|
|
| 217 |
``` cpp
|
| 218 |
template <class T> int f(int T::*);
|
| 219 |
int i = f<int>(0);
|
| 220 |
```
|
|
|
|
|
|
|
| 221 |
- Attempting to give an invalid type to a non-type template parameter.
|
|
|
|
| 222 |
``` cpp
|
| 223 |
template <class T, T> struct S {};
|
| 224 |
template <class T> int f(S<T, T()>*);
|
| 225 |
struct X {};
|
| 226 |
int i0 = f<X>(0);
|
| 227 |
```
|
|
|
|
|
|
|
| 228 |
- Attempting to perform an invalid conversion in either a template
|
| 229 |
argument expression, or an expression used in the function
|
| 230 |
declaration.
|
|
|
|
| 231 |
``` cpp
|
| 232 |
template <class T, T*> int f(int);
|
| 233 |
int i2 = f<int,1>(0); // can't conv 1 to int*
|
| 234 |
```
|
|
|
|
|
|
|
| 235 |
- Attempting to create a function type in which a parameter has a type
|
| 236 |
of `void`, or in which the return type is a function type or array
|
| 237 |
type.
|
| 238 |
- Attempting to create a function type in which a parameter type or the
|
| 239 |
return type is an abstract class type ([[class.abstract]]).
|
| 240 |
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
``` cpp
|
| 249 |
template <int> int f(int);
|
| 250 |
template <signed char> int f(int);
|
| 251 |
-
int i1 = f<
|
| 252 |
-
int i2 = f<
|
| 253 |
```
|
| 254 |
|
|
|
|
|
|
|
| 255 |
#### Deducing template arguments from a function call <a id="temp.deduct.call">[[temp.deduct.call]]</a>
|
| 256 |
|
| 257 |
Template argument deduction is done by comparing each function template
|
| 258 |
-
parameter type (call it `P`)
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
|
| 268 |
``` cpp
|
| 269 |
template<class T> void f(std::initializer_list<T>);
|
| 270 |
f({1,2,3}); // T deduced to int
|
| 271 |
f({1,"asdf"}); // error: T deduced to both int and const char*
|
| 272 |
|
| 273 |
template<class T> void g(T);
|
| 274 |
g({1,2,3}); // error: no argument deduced for T
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
```
|
| 276 |
|
|
|
|
|
|
|
| 277 |
For a function parameter pack that occurs at the end of the
|
| 278 |
-
*parameter-declaration-list*,
|
| 279 |
-
the call
|
| 280 |
-
function parameter pack
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
non-deduced context (
|
| 284 |
-
pack is never deduced.
|
|
|
|
|
|
|
| 285 |
|
| 286 |
``` cpp
|
| 287 |
template<class ... Types> void f(Types& ...);
|
| 288 |
template<class T1, class ... Types> void g(T1, Types ...);
|
| 289 |
template<class T1, class ... Types> void g1(Types ..., T1);
|
|
@@ -292,112 +376,185 @@ void h(int x, float& y) {
|
|
| 292 |
const int z = x;
|
| 293 |
f(x, y, z); // Types is deduced to int, float, const int
|
| 294 |
g(x, y, z); // T1 is deduced to int; Types is deduced to float, int
|
| 295 |
g1(x, y, z); // error: Types is not deduced
|
| 296 |
g1<int, int, int>(x, y, z); // OK, no deduction occurs
|
| 297 |
-
|
| 298 |
}
|
| 299 |
```
|
| 300 |
|
|
|
|
|
|
|
| 301 |
If `P` is not a reference type:
|
| 302 |
|
| 303 |
- If `A` is an array type, the pointer type produced by the
|
| 304 |
array-to-pointer standard conversion ([[conv.array]]) is used in
|
| 305 |
place of `A` for type deduction; otherwise,
|
| 306 |
- If `A` is a function type, the pointer type produced by the
|
| 307 |
function-to-pointer standard conversion ([[conv.func]]) is used in
|
| 308 |
place of `A` for type deduction; otherwise,
|
| 309 |
-
- If `A` is a cv-qualified type, the top
|
| 310 |
type are ignored for type deduction.
|
| 311 |
|
| 312 |
-
If `P` is a cv-qualified type, the top
|
| 313 |
are ignored for type deduction. If `P` is a reference type, the type
|
| 314 |
-
referred to by `P` is used for type deduction.
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
|
| 319 |
``` cpp
|
| 320 |
-
template <class T> int f(T&&);
|
| 321 |
template <class T> int g(const T&&);
|
| 322 |
int i;
|
| 323 |
int n1 = f(i); // calls f<int&>(int&)
|
| 324 |
int n2 = f(0); // calls f<int>(int&&)
|
| 325 |
int n3 = g(i); // error: would call g<int>(const int&&), which
|
| 326 |
// would bind an rvalue reference to an lvalue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
```
|
| 328 |
|
|
|
|
|
|
|
| 329 |
In general, the deduction process attempts to find template argument
|
| 330 |
values that will make the deduced `A` identical to `A` (after the type
|
| 331 |
`A` is transformed as described above). However, there are three cases
|
| 332 |
that allow a difference:
|
| 333 |
|
| 334 |
- If the original `P` is a reference type, the deduced `A` (i.e., the
|
| 335 |
type referred to by the reference) can be more cv-qualified than the
|
| 336 |
transformed `A`.
|
| 337 |
- The transformed `A` can be another pointer or pointer to member type
|
| 338 |
-
that can be converted to the deduced `A` via a
|
| 339 |
-
conversion ([[conv.
|
|
|
|
| 340 |
- If `P` is a class and `P` has the form *simple-template-id*, then the
|
| 341 |
transformed `A` can be a derived class of the deduced `A`. Likewise,
|
| 342 |
if `P` is a pointer to a class of the form *simple-template-id*, the
|
| 343 |
transformed `A` can be a pointer to a derived class pointed to by the
|
| 344 |
deduced `A`.
|
| 345 |
|
| 346 |
-
as specified in [[temp.arg.explicit]], implicit conversions will be
|
| 347 |
-
performed on a function argument to convert it to the type of the
|
| 348 |
-
corresponding function parameter if the parameter contains no
|
| 349 |
-
*template-parameter*s that participate in template argument deduction.
|
| 350 |
-
Such conversions are also allowed, in addition to the ones described in
|
| 351 |
-
the preceding list.
|
| 352 |
-
|
| 353 |
These alternatives are considered only if type deduction would otherwise
|
| 354 |
fail. If they yield more than one possible deduced `A`, the type
|
| 355 |
-
deduction fails.
|
| 356 |
-
function parameters of a function template, or is used only in a
|
| 357 |
-
non-deduced context, its corresponding *template-argument* cannot be
|
| 358 |
-
deduced from a function call and the *template-argument* must be
|
| 359 |
-
explicitly specified.
|
| 360 |
|
| 361 |
-
|
| 362 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
|
| 364 |
- If the argument is an overload set containing one or more function
|
| 365 |
templates, the parameter is treated as a non-deduced context.
|
| 366 |
- If the argument is an overload set (not containing function
|
| 367 |
templates), trial argument deduction is attempted using each of the
|
| 368 |
members of the set. If deduction succeeds for only one of the overload
|
| 369 |
set members, that member is used as the argument value for the
|
| 370 |
deduction. If deduction succeeds for more than one member of the
|
| 371 |
overload set the parameter is treated as a non-deduced context.
|
|
|
|
|
|
|
|
|
|
| 372 |
``` cpp
|
| 373 |
-
|
| 374 |
-
// parameter is a deduced context.
|
| 375 |
template <class T> int f(T (*p)(T));
|
| 376 |
int g(int);
|
| 377 |
int g(char);
|
| 378 |
int i = f(g); // calls f(int (*)(int))
|
| 379 |
```
|
| 380 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
``` cpp
|
| 382 |
-
|
| 383 |
-
// non-deduced context.
|
| 384 |
template <class T> int f(T, T (*p)(T));
|
| 385 |
int g(int);
|
| 386 |
char g(char);
|
| 387 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 388 |
```
|
| 389 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 390 |
``` cpp
|
| 391 |
-
|
| 392 |
-
// parameter to be a non-deduced context.
|
| 393 |
template <class T> int f(T, T (*p)(T));
|
| 394 |
char g(char);
|
| 395 |
template <class T> T g(T);
|
| 396 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 397 |
```
|
| 398 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 399 |
#### Deducing template arguments taking the address of a function template <a id="temp.deduct.funcaddr">[[temp.deduct.funcaddr]]</a>
|
| 400 |
|
| 401 |
Template arguments can be deduced from the type specified when taking
|
| 402 |
the address of an overloaded function ([[over.over]]). The function
|
| 403 |
template’s function type and the specified type are used as the types of
|
|
@@ -427,23 +584,28 @@ If `A` is not a reference type:
|
|
| 427 |
array-to-pointer standard conversion ([[conv.array]]) is used in
|
| 428 |
place of `P` for type deduction; otherwise,
|
| 429 |
- If `P` is a function type, the pointer type produced by the
|
| 430 |
function-to-pointer standard conversion ([[conv.func]]) is used in
|
| 431 |
place of `P` for type deduction; otherwise,
|
| 432 |
-
- If `P` is a cv-qualified type, the top
|
| 433 |
type are ignored for type deduction.
|
| 434 |
|
| 435 |
-
If `A` is a cv-qualified type, the top
|
| 436 |
are ignored for type deduction. If `A` is a reference type, the type
|
| 437 |
referred to by `A` is used for type deduction.
|
| 438 |
|
| 439 |
In general, the deduction process attempts to find template argument
|
| 440 |
values that will make the deduced `A` identical to `A`. However, there
|
| 441 |
-
are
|
| 442 |
|
| 443 |
- If the original `A` is a reference type, `A` can be more cv-qualified
|
| 444 |
than the deduced `A` (i.e., the type referred to by the reference)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 445 |
- The deduced `A` can be another pointer or pointer to member type that
|
| 446 |
can be converted to `A` via a qualification conversion.
|
| 447 |
|
| 448 |
These alternatives are considered only if type deduction would otherwise
|
| 449 |
fail. If they yield more than one possible deduced `A`, the type
|
|
@@ -455,36 +617,44 @@ process is used to determine the deduced template argument values:
|
|
| 455 |
|
| 456 |
If `A` is a type
|
| 457 |
|
| 458 |
and `P` is a type
|
| 459 |
|
| 460 |
-
|
| 461 |
-
respectively for type deduction.
|
|
|
|
|
|
|
| 462 |
|
| 463 |
``` cpp
|
| 464 |
struct A {
|
| 465 |
template <class T> operator T***();
|
| 466 |
};
|
| 467 |
A a;
|
| 468 |
const int * const * const * p1 = a; // T is deduced as int, not const int
|
| 469 |
```
|
| 470 |
|
|
|
|
|
|
|
| 471 |
#### Deducing template arguments during partial ordering <a id="temp.deduct.partial">[[temp.deduct.partial]]</a>
|
| 472 |
|
| 473 |
Template argument deduction is done by comparing certain types
|
| 474 |
associated with the two function templates being compared.
|
| 475 |
|
| 476 |
Two sets of types are used to determine the partial ordering. For each
|
| 477 |
of the templates involved there is the original function type and the
|
| 478 |
-
transformed function type.
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 486 |
|
| 487 |
The types used to determine the ordering depend on the context in which
|
| 488 |
the partial ordering is done:
|
| 489 |
|
| 490 |
- In the context of a function call, the types used are those function
|
|
@@ -494,11 +664,13 @@ the partial ordering is done:
|
|
| 494 |
- In other contexts ([[temp.func.order]]) the function template’s
|
| 495 |
function type is used.
|
| 496 |
|
| 497 |
Each type nominated above from the parameter template and the
|
| 498 |
corresponding type from the argument template are used as the types of
|
| 499 |
-
`P` and `A`.
|
|
|
|
|
|
|
| 500 |
|
| 501 |
Before the partial ordering is done, certain transformations are
|
| 502 |
performed on the types used for partial ordering:
|
| 503 |
|
| 504 |
- If `P` is a reference type, `P` is replaced by the type referred to.
|
|
@@ -515,70 +687,88 @@ Remove any top-level cv-qualifiers:
|
|
| 515 |
- If `P` is a cv-qualified type, `P` is replaced by the cv-unqualified
|
| 516 |
version of `P`.
|
| 517 |
- If `A` is a cv-qualified type, `A` is replaced by the cv-unqualified
|
| 518 |
version of `A`.
|
| 519 |
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
deduction succeeds for a given type,
|
| 529 |
-
is considered to be at least as
|
| 530 |
-
parameter template.
|
|
|
|
|
|
|
| 531 |
|
| 532 |
``` cpp
|
| 533 |
template<class... Args> void f(Args... args); // #1
|
| 534 |
template<class T1, class... Args> void f(T1 a1, Args... args); // #2
|
| 535 |
template<class T1, class T2> void f(T1 a1, T2 a2); // #3
|
| 536 |
|
| 537 |
f(); // calls #1
|
| 538 |
f(1, 2, 3); // calls #2
|
| 539 |
-
f(1, 2);
|
| 540 |
-
|
| 541 |
```
|
| 542 |
|
|
|
|
|
|
|
| 543 |
If, for a given type, deduction succeeds in both directions (i.e., the
|
| 544 |
types are identical after the transformations above) and both `P` and
|
| 545 |
`A` were reference types (before being replaced with the type referred
|
| 546 |
to above):
|
| 547 |
|
| 548 |
- if the type from the argument template was an lvalue reference and the
|
| 549 |
-
type from the parameter template was not, the
|
| 550 |
-
considered to be
|
|
|
|
| 551 |
- if the type from the argument template is more cv-qualified than the
|
| 552 |
-
type from the parameter template (as described above), the
|
| 553 |
-
type is considered to be
|
| 554 |
-
|
| 555 |
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 562 |
|
| 563 |
In most cases, all template parameters must have values in order for
|
| 564 |
deduction to succeed, but for partial ordering purposes a template
|
| 565 |
parameter may remain without a value provided it is not used in the
|
| 566 |
-
types being used for partial ordering.
|
| 567 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 568 |
|
| 569 |
``` cpp
|
| 570 |
template <class T> T f(int); // #1
|
| 571 |
template <class T, class U> T f(U); // #2
|
| 572 |
void g() {
|
| 573 |
f<int>(1); // calls #1
|
| 574 |
}
|
| 575 |
```
|
| 576 |
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 580 |
|
| 581 |
``` cpp
|
| 582 |
template<class ...> struct Tuple { };
|
| 583 |
template<class ... Types> void g(Tuple<Types ...>); // #1
|
| 584 |
template<class T1, class ... Types> void g(Tuple<T1, Types ...>); // #2
|
|
@@ -588,10 +778,12 @@ g(Tuple<>()); // calls #1
|
|
| 588 |
g(Tuple<int, float>()); // calls #2
|
| 589 |
g(Tuple<int, float&>()); // calls #3
|
| 590 |
g(Tuple<int>()); // calls #3
|
| 591 |
```
|
| 592 |
|
|
|
|
|
|
|
| 593 |
#### Deducing template arguments from a type <a id="temp.deduct.type">[[temp.deduct.type]]</a>
|
| 594 |
|
| 595 |
Template arguments can be deduced in several different contexts, but in
|
| 596 |
each case a type that is specified in terms of template parameters (call
|
| 597 |
it `P`) is compared with an actual type (call it `A`), and an attempt is
|
|
@@ -606,11 +798,12 @@ In some cases, the deduction is done using a single set of types `P` and
|
|
| 606 |
deduced template argument values are then combined. If type deduction
|
| 607 |
cannot be done for any `P/A` pair, or if for any pair the deduction
|
| 608 |
leads to more than one possible set of deduced values, or if different
|
| 609 |
pairs yield different deduced values, or if any template argument
|
| 610 |
remains neither deduced nor explicitly specified, template argument
|
| 611 |
-
deduction fails.
|
|
|
|
| 612 |
|
| 613 |
A given type `P` can be composed from a number of other types,
|
| 614 |
templates, and non-type values:
|
| 615 |
|
| 616 |
- A function type includes the types of each of the function parameters
|
|
@@ -631,10 +824,15 @@ In certain contexts, however, the value does not participate in type
|
|
| 631 |
deduction, but instead uses the values of template arguments that were
|
| 632 |
either deduced elsewhere or explicitly specified. If a template
|
| 633 |
parameter is used only in non-deduced contexts and is not explicitly
|
| 634 |
specified, template argument deduction fails.
|
| 635 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 636 |
The non-deduced contexts are:
|
| 637 |
|
| 638 |
- The *nested-name-specifier* of a type that was specified using a
|
| 639 |
*qualified-id*.
|
| 640 |
- The *expression* of a *decltype-specifier*.
|
|
@@ -652,35 +850,43 @@ The non-deduced contexts are:
|
|
| 652 |
- no function matches the function parameter type, or
|
| 653 |
- the set of functions supplied as an argument contains one or more
|
| 654 |
function templates.
|
| 655 |
- A function parameter for which the associated argument is an
|
| 656 |
initializer list ([[dcl.init.list]]) but the parameter does not have
|
| 657 |
-
|
| 658 |
-
|
|
|
|
| 659 |
``` cpp
|
| 660 |
template<class T> void g(T);
|
| 661 |
g({1,2,3}); // error: no argument deduced for T
|
| 662 |
```
|
|
|
|
|
|
|
| 663 |
- A function parameter pack that does not occur at the end of the
|
| 664 |
*parameter-declaration-list*.
|
| 665 |
|
| 666 |
When a type name is specified in a way that includes a non-deduced
|
| 667 |
context, all of the types that comprise that type name are also
|
| 668 |
non-deduced. However, a compound type can include both deduced and
|
| 669 |
-
non-deduced types.
|
|
|
|
|
|
|
| 670 |
`T2` are non-deduced. Likewise, if a type is specified as
|
| 671 |
`A<I+J>::X<T>`, `I`, `J`, and `T` are non-deduced. If a type is
|
| 672 |
specified as `void` `f(typename` `A<T>::B,` `A<T>)`, the `T` in
|
| 673 |
-
`A<T>::B` is non-deduced but the `T` in `A<T>` is
|
|
|
|
|
|
|
|
|
|
| 674 |
|
| 675 |
Here is an example in which different parameter/argument pairs produce
|
| 676 |
inconsistent template argument deductions:
|
| 677 |
|
| 678 |
``` cpp
|
| 679 |
-
template<class T> void f(T x, T y) {
|
| 680 |
-
struct A {
|
| 681 |
-
struct B : A {
|
| 682 |
void g(A a, B b) {
|
| 683 |
f(a,b); // error: T could be A or B
|
| 684 |
f(b,a); // error: T could be A or B
|
| 685 |
f(a,a); // OK: T is A
|
| 686 |
f(b,b); // OK: T is B
|
|
@@ -731,10 +937,12 @@ void t() {
|
|
| 731 |
f(d); // calls f(B<int>&)
|
| 732 |
f(d2); // calls f(B<int>&)
|
| 733 |
}
|
| 734 |
```
|
| 735 |
|
|
|
|
|
|
|
| 736 |
A template type argument `T`, a template template argument `TT` or a
|
| 737 |
template non-type argument `i` can be deduced if `P` and `A` have one of
|
| 738 |
the following forms:
|
| 739 |
|
| 740 |
``` cpp
|
|
@@ -763,20 +971,20 @@ template-name<i> (where template-name refers to a class template)
|
|
| 763 |
TT<T>
|
| 764 |
TT<i>
|
| 765 |
TT<>
|
| 766 |
```
|
| 767 |
|
| 768 |
-
where `(T)` represents a
|
| 769 |
-
parameter type contains a `T`, and `()` represents a
|
| 770 |
-
|
| 771 |
`<T>` represents template argument lists where at least one argument
|
| 772 |
contains a `T`, `<i>` represents template argument lists where at least
|
| 773 |
one argument contains an `i` and `<>` represents template argument lists
|
| 774 |
where no argument contains a `T` or an `i`.
|
| 775 |
|
| 776 |
If `P` has a form that contains `<T>` or `<i>`, then each argument Pᵢ of
|
| 777 |
-
the respective template argument list `P` is compared with the
|
| 778 |
corresponding argument Aᵢ of the corresponding template argument list of
|
| 779 |
`A`. If the template argument list of `P` contains a pack expansion that
|
| 780 |
is not the last template argument, the entire template argument list is
|
| 781 |
a non-deduced context. If `Pᵢ` is a pack expansion, then the pattern of
|
| 782 |
`Pᵢ` is compared with each remaining argument in the template argument
|
|
@@ -788,10 +996,12 @@ pack expansion:
|
|
| 788 |
- if `P` does not contain a template argument corresponding to `Aᵢ` then
|
| 789 |
`Aᵢ` is ignored;
|
| 790 |
- otherwise, if `Pᵢ` is not a pack expansion, template argument
|
| 791 |
deduction fails.
|
| 792 |
|
|
|
|
|
|
|
| 793 |
``` cpp
|
| 794 |
template<class T1, class... Z> class S; // #1
|
| 795 |
template<class T1, class... Z> class S<T1, const Z&...> { }; // #2
|
| 796 |
template<class T1, class T2> class S<T1, const T2&> { }; // #3
|
| 797 |
S<int, const int&> s; // both #2 and #3 match; #3 is more specialized
|
|
@@ -800,24 +1010,30 @@ template<class T, class... U> struct A { }; // #1
|
|
| 800 |
template<class T1, class T2, class... U> struct A<T1, T2*, U...> { }; // #2
|
| 801 |
template<class T1, class T2> struct A<T1, T2> { }; // #3
|
| 802 |
template struct A<int, int*>; // selects #2
|
| 803 |
```
|
| 804 |
|
|
|
|
|
|
|
| 805 |
Similarly, if `P` has a form that contains `(T)`, then each parameter
|
| 806 |
-
type `Pᵢ` of the respective
|
| 807 |
-
with the corresponding parameter type `Aᵢ` of the corresponding
|
| 808 |
-
|
| 809 |
originated from deduction when taking the address of a function
|
| 810 |
template ([[temp.deduct.funcaddr]]) or when deducing template arguments
|
| 811 |
from a function declaration ([[temp.deduct.decl]]) and `Pᵢ` and `Aᵢ`
|
| 812 |
-
are parameters of the top-level
|
| 813 |
-
respectively, `Pᵢ` is adjusted if it is
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 819 |
|
| 820 |
``` cpp
|
| 821 |
template <class T> void f(T&&);
|
| 822 |
template <> void f(int&) { } // #1
|
| 823 |
template <> void f(int&&) { } // #2
|
|
@@ -825,32 +1041,40 @@ void g(int i) {
|
|
| 825 |
f(i); // calls f<int&>(int&), i.e., #1
|
| 826 |
f(0); // calls f<int>(int&&), i.e., #2
|
| 827 |
}
|
| 828 |
```
|
| 829 |
|
|
|
|
|
|
|
| 830 |
If the *parameter-declaration* corresponding to `Pᵢ` is a function
|
| 831 |
parameter pack, then the type of its *declarator-id* is compared with
|
| 832 |
-
each remaining parameter type in the
|
| 833 |
comparison deduces template arguments for subsequent positions in the
|
| 834 |
template parameter packs expanded by the function parameter pack. During
|
| 835 |
partial ordering ([[temp.deduct.partial]]), if `Aᵢ` was originally a
|
| 836 |
function parameter pack:
|
| 837 |
|
| 838 |
- if `P` does not contain a function parameter type corresponding to
|
| 839 |
`Aᵢ` then `Aᵢ` is ignored;
|
| 840 |
- otherwise, if `Pᵢ` is not a function parameter pack, template argument
|
| 841 |
deduction fails.
|
| 842 |
|
|
|
|
|
|
|
| 843 |
``` cpp
|
| 844 |
template<class T, class... U> void f(T*, U...) { } // #1
|
| 845 |
template<class T> void f(T) { } // #2
|
| 846 |
template void f(int*); // selects #1
|
| 847 |
```
|
| 848 |
|
|
|
|
|
|
|
| 849 |
These forms can be used in the same way as `T` is for further
|
| 850 |
composition of types.
|
| 851 |
|
|
|
|
|
|
|
| 852 |
``` cpp
|
| 853 |
X<int> (*)(char[6])
|
| 854 |
```
|
| 855 |
|
| 856 |
is of the form
|
|
@@ -865,22 +1089,67 @@ which is a variant of
|
|
| 865 |
type (*)(T)
|
| 866 |
```
|
| 867 |
|
| 868 |
where type is `X<int>` and `T` is `char[6]`.
|
| 869 |
|
|
|
|
|
|
|
| 870 |
Template arguments cannot be deduced from function arguments involving
|
| 871 |
constructs other than the ones specified above.
|
| 872 |
|
| 873 |
-
|
| 874 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 875 |
|
| 876 |
``` cpp
|
| 877 |
-
template<
|
| 878 |
-
|
| 879 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 880 |
```
|
| 881 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 882 |
Except for reference and pointer types, a major array bound is not part
|
| 883 |
of a function parameter type and cannot be deduced from an argument:
|
| 884 |
|
| 885 |
``` cpp
|
| 886 |
template<int i> void f1(int a[10][i]);
|
|
@@ -895,17 +1164,23 @@ void g() {
|
|
| 895 |
f2<10>(v); // OK
|
| 896 |
f3(v); // OK: i deduced to be 10
|
| 897 |
}
|
| 898 |
```
|
| 899 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 900 |
If, in the declaration of a function template with a non-type template
|
| 901 |
parameter, the non-type template parameter is used in a subexpression in
|
| 902 |
the function parameter list, the expression is a non-deduced context as
|
| 903 |
specified above.
|
| 904 |
|
|
|
|
|
|
|
| 905 |
``` cpp
|
| 906 |
-
template <int i> class A {
|
| 907 |
template <int i> void g(A<i+1>);
|
| 908 |
template <int i> void f(A<i>, A<i+1>);
|
| 909 |
void k() {
|
| 910 |
A<1> a1;
|
| 911 |
A<2> a2;
|
|
@@ -913,10 +1188,16 @@ void k() {
|
|
| 913 |
g<0>(a1); // OK
|
| 914 |
f(a1, a2); // OK
|
| 915 |
}
|
| 916 |
```
|
| 917 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 918 |
Template parameters do not participate in template argument deduction if
|
| 919 |
they are used only in non-deduced contexts. For example,
|
| 920 |
|
| 921 |
``` cpp
|
| 922 |
template<int i, typename T>
|
|
@@ -925,23 +1206,26 @@ T deduce(typename A<T>::X x, // T is not deduced here
|
|
| 925 |
typename B<i>::Y y); // i is not deduced here
|
| 926 |
A<int> a;
|
| 927 |
B<77> b;
|
| 928 |
|
| 929 |
int x = deduce<77>(a.xm, 62, b.ym);
|
| 930 |
-
// T is deduced to be int, a.xm must be convertible to
|
| 931 |
-
//
|
| 932 |
-
// i is explicitly specified to be 77, b.ym must be convertible
|
| 933 |
-
// to B<77>::Y
|
| 934 |
```
|
| 935 |
|
| 936 |
-
|
| 937 |
-
|
| 938 |
-
|
| 939 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 940 |
|
| 941 |
``` cpp
|
| 942 |
-
template<int i> class A {
|
| 943 |
template<short s> void f(A<s>);
|
| 944 |
void k1() {
|
| 945 |
A<1> a;
|
| 946 |
f(a); // error: deduction fails for conversion from int to short
|
| 947 |
f<1>(a); // OK
|
|
@@ -953,13 +1237,17 @@ void k2() {
|
|
| 953 |
B<1> b;
|
| 954 |
g(b); // OK: cv-qualifiers are ignored on template parameter types
|
| 955 |
}
|
| 956 |
```
|
| 957 |
|
|
|
|
|
|
|
| 958 |
A *template-argument* can be deduced from a function, pointer to
|
| 959 |
function, or pointer to member function type.
|
| 960 |
|
|
|
|
|
|
|
| 961 |
``` cpp
|
| 962 |
template<class T> void f(void(*)(T,int));
|
| 963 |
template<class T> void foo(T,int);
|
| 964 |
void g(int,int);
|
| 965 |
void g(char,int);
|
|
@@ -971,37 +1259,49 @@ int m() {
|
|
| 971 |
f(&h); // OK: void h(char,int) is a unique match
|
| 972 |
f(&foo); // error: type deduction fails because foo is a template
|
| 973 |
}
|
| 974 |
```
|
| 975 |
|
|
|
|
|
|
|
| 976 |
A template *type-parameter* cannot be deduced from the type of a
|
| 977 |
function default argument.
|
| 978 |
|
|
|
|
|
|
|
| 979 |
``` cpp
|
| 980 |
template <class T> void f(T = 5, T = 7);
|
| 981 |
void g() {
|
| 982 |
f(1); // OK: call f<int>(1,7)
|
| 983 |
f(); // error: cannot deduce T
|
| 984 |
f<int>(); // OK: call f<int>(5,7)
|
| 985 |
}
|
| 986 |
```
|
| 987 |
|
|
|
|
|
|
|
| 988 |
The *template-argument* corresponding to a template *template-parameter*
|
| 989 |
is deduced from the type of the *template-argument* of a class template
|
| 990 |
specialization used in the argument list of a function call.
|
| 991 |
|
|
|
|
|
|
|
| 992 |
``` cpp
|
| 993 |
template <template <class T> class X> struct A { };
|
| 994 |
template <template <class T> class X> void f(A<X>) { }
|
| 995 |
template<class T> struct B { };
|
| 996 |
A<B> ab;
|
| 997 |
f(ab); // calls f(A<B>)
|
| 998 |
```
|
| 999 |
|
| 1000 |
-
|
|
|
|
|
|
|
| 1001 |
[[temp.variadic]]) can deduce zero or more arguments for each parameter
|
| 1002 |
-
pack.
|
|
|
|
|
|
|
| 1003 |
|
| 1004 |
``` cpp
|
| 1005 |
template<class> struct X { };
|
| 1006 |
template<class R, class ... ArgTypes> struct X<R(int, ArgTypes ...)> { };
|
| 1007 |
template<class ... Types> struct Y { };
|
|
@@ -1017,10 +1317,12 @@ Y<> y1; // use primary template; Types is empty
|
|
| 1017 |
Y<int&, float&, double&> y2; // uses partial specialization; T is int&, Types contains float, double
|
| 1018 |
Y<int, float, double> y3; // uses primary template; Types contains int, float, double
|
| 1019 |
int fv = f(g); // OK; Types contains int, float
|
| 1020 |
```
|
| 1021 |
|
|
|
|
|
|
|
| 1022 |
#### Deducing template arguments from a function declaration <a id="temp.deduct.decl">[[temp.deduct.decl]]</a>
|
| 1023 |
|
| 1024 |
In a declaration whose *declarator-id* refers to a specialization of a
|
| 1025 |
function template, template argument deduction is performed to identify
|
| 1026 |
the specialization to which the declaration refers. Specifically, this
|
|
|
|
| 3 |
When a function template specialization is referenced, all of the
|
| 4 |
template arguments shall have values. The values can be explicitly
|
| 5 |
specified or, in some cases, be deduced from the use or obtained from
|
| 6 |
default *template-argument*s.
|
| 7 |
|
| 8 |
+
[*Example 1*:
|
| 9 |
+
|
| 10 |
``` cpp
|
| 11 |
void f(Array<dcomplex>& cv, Array<int>& ci) {
|
| 12 |
sort(cv); // calls sort(Array<dcomplex>&)
|
| 13 |
sort(ci); // calls sort(Array<int>&)
|
| 14 |
}
|
|
|
|
| 21 |
int i = convert<int>(d); // calls convert<int,double>(double)
|
| 22 |
int c = convert<char>(d); // calls convert<char,double>(double)
|
| 23 |
}
|
| 24 |
```
|
| 25 |
|
| 26 |
+
— *end example*]
|
| 27 |
+
|
| 28 |
When an explicit template argument list is specified, the template
|
| 29 |
arguments must be compatible with the template parameter list and must
|
| 30 |
result in a valid function type as described below; otherwise type
|
| 31 |
deduction fails. Specifically, the following steps are performed when
|
| 32 |
evaluating an explicitly specified template argument list with respect
|
|
|
|
| 43 |
[[temp.arg.nontype]], otherwise type deduction fails.
|
| 44 |
- The specified template argument values are substituted for the
|
| 45 |
corresponding template parameters as specified below.
|
| 46 |
|
| 47 |
After this substitution is performed, the function parameter type
|
| 48 |
+
adjustments described in [[dcl.fct]] are performed.
|
| 49 |
+
|
| 50 |
+
[*Example 2*: A parameter type of “`void (const int, int[5])`” becomes
|
| 51 |
+
“`void(*)(int,int*)`”. — *end example*]
|
| 52 |
+
|
| 53 |
+
[*Note 1*: A top-level qualifier in a function parameter declaration
|
| 54 |
+
does not affect the function type but still affects the type of the
|
| 55 |
+
function parameter variable within the function. — *end note*]
|
| 56 |
+
|
| 57 |
+
[*Example 3*:
|
| 58 |
|
| 59 |
``` cpp
|
| 60 |
template <class T> void f(T t);
|
| 61 |
template <class X> void g(const X x);
|
| 62 |
template <class Z> void h(Z, Z*);
|
|
|
|
| 77 |
// #5: function type is h(int, const int*)
|
| 78 |
h<const int>(1,0);
|
| 79 |
}
|
| 80 |
```
|
| 81 |
|
| 82 |
+
— *end example*]
|
| 83 |
+
|
| 84 |
+
[*Note 2*: `f<int>(1)` and `f<const int>(1)` call distinct functions
|
| 85 |
+
even though both of the functions called have the same function
|
| 86 |
+
type. — *end note*]
|
| 87 |
|
| 88 |
The resulting substituted and adjusted function type is used as the type
|
| 89 |
of the function template for template argument deduction. If a template
|
| 90 |
argument has not been deduced and its corresponding template parameter
|
| 91 |
has a default argument, the template argument is determined by
|
| 92 |
substituting the template arguments determined for preceding template
|
| 93 |
parameters into the default argument. If the substitution results in an
|
| 94 |
invalid type, as described above, type deduction fails.
|
| 95 |
|
| 96 |
+
[*Example 4*:
|
| 97 |
+
|
| 98 |
``` cpp
|
| 99 |
template <class T, class U = double>
|
| 100 |
void f(T t = 0, U u = 0);
|
| 101 |
|
| 102 |
void g() {
|
|
|
|
| 106 |
f<int>(); // f<int,double>(0,0)
|
| 107 |
f<int,char>(); // f<int,char>(0,0)
|
| 108 |
}
|
| 109 |
```
|
| 110 |
|
| 111 |
+
— *end example*]
|
| 112 |
+
|
| 113 |
When all template arguments have been deduced or obtained from default
|
| 114 |
template arguments, all uses of template parameters in the template
|
| 115 |
parameter list of the template and the function type are replaced with
|
| 116 |
the corresponding deduced or default argument values. If the
|
| 117 |
substitution results in an invalid type, as described above, type
|
|
|
|
| 131 |
expressions include not only constant expressions such as those that
|
| 132 |
appear in array bounds or as nontype template arguments but also general
|
| 133 |
expressions (i.e., non-constant expressions) inside `sizeof`,
|
| 134 |
`decltype`, and other contexts that allow non-constant expressions. The
|
| 135 |
substitution proceeds in lexical order and stops when a condition that
|
| 136 |
+
causes deduction to fail is encountered.
|
| 137 |
+
|
| 138 |
+
[*Note 3*: The equivalent substitution in exception specifications is
|
| 139 |
+
done only when the *noexcept-specifier* is instantiated, at which point
|
| 140 |
+
a program is ill-formed if the substitution results in an invalid type
|
| 141 |
+
or expression. — *end note*]
|
| 142 |
+
|
| 143 |
+
[*Example 5*:
|
| 144 |
|
| 145 |
``` cpp
|
| 146 |
template <class T> struct A { using X = typename T::X; };
|
| 147 |
template <class T> typename T::X f(typename A<T>::X);
|
| 148 |
template <class T> void f(...) { }
|
|
|
|
| 153 |
f<int>(0); // OK, substituting return type causes deduction to fail
|
| 154 |
g<int>(0); // error, substituting parameter type instantiates A<int>
|
| 155 |
}
|
| 156 |
```
|
| 157 |
|
| 158 |
+
— *end example*]
|
| 159 |
+
|
| 160 |
If a substitution results in an invalid type or expression, type
|
| 161 |
deduction fails. An invalid type or expression is one that would be
|
| 162 |
ill-formed, with a diagnostic required, if written using the substituted
|
| 163 |
+
arguments.
|
| 164 |
+
|
| 165 |
+
[*Note 4*: If no diagnostic is required, the program is still
|
| 166 |
+
ill-formed. Access checking is done as part of the substitution
|
| 167 |
+
process. — *end note*]
|
| 168 |
+
|
| 169 |
Only invalid types and expressions in the immediate context of the
|
| 170 |
function type and its template parameter types can result in a deduction
|
| 171 |
+
failure.
|
| 172 |
+
|
| 173 |
+
[*Note 5*: The substitution into types and expressions can result in
|
| 174 |
+
effects such as the instantiation of class template specializations
|
| 175 |
+
and/or function template specializations, the generation of
|
| 176 |
+
implicitly-defined functions, etc. Such effects are not in the
|
| 177 |
+
“immediate context” and can result in the program being
|
| 178 |
+
ill-formed. — *end note*]
|
| 179 |
+
|
| 180 |
+
[*Example 6*:
|
| 181 |
|
| 182 |
``` cpp
|
| 183 |
struct X { };
|
| 184 |
struct Y {
|
| 185 |
Y(X){}
|
|
|
|
| 190 |
|
| 191 |
X x1, x2;
|
| 192 |
X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
|
| 193 |
```
|
| 194 |
|
| 195 |
+
— *end example*]
|
| 196 |
+
|
| 197 |
+
[*Note 6*:
|
| 198 |
+
|
| 199 |
Type deduction may fail for the following reasons:
|
| 200 |
|
| 201 |
- Attempting to instantiate a pack expansion containing multiple
|
| 202 |
parameter packs of differing lengths.
|
| 203 |
- Attempting to create an array with an element type that is `void`, a
|
| 204 |
function type, a reference type, or an abstract class type, or
|
| 205 |
attempting to create an array with a size that is zero or negative.
|
| 206 |
+
\[*Example 7*:
|
| 207 |
``` cpp
|
| 208 |
template <class T> int f(T[5]);
|
| 209 |
int I = f<int>(0);
|
| 210 |
int j = f<void>(0); // invalid array
|
| 211 |
```
|
| 212 |
+
|
| 213 |
+
— *end example*]
|
| 214 |
- Attempting to use a type that is not a class or enumeration type in a
|
| 215 |
qualified name.
|
| 216 |
+
\[*Example 8*:
|
| 217 |
``` cpp
|
| 218 |
template <class T> int f(typename T::B*);
|
| 219 |
int i = f<int>(0);
|
| 220 |
```
|
| 221 |
+
|
| 222 |
+
— *end example*]
|
| 223 |
- Attempting to use a type in a *nested-name-specifier* of a
|
| 224 |
*qualified-id* when that type does not contain the specified member,
|
| 225 |
or
|
| 226 |
- the specified member is not a type where a type is required, or
|
| 227 |
- the specified member is not a template where a template is required,
|
| 228 |
or
|
| 229 |
- the specified member is not a non-type where a non-type is required.
|
| 230 |
|
| 231 |
+
\[*Example 9*:
|
| 232 |
``` cpp
|
| 233 |
template <int I> struct X { };
|
| 234 |
template <template <class T> class> struct Z { };
|
| 235 |
template <class T> void f(typename T::Y*){}
|
| 236 |
template <class T> void g(X<T::N>*){}
|
|
|
|
| 250 |
f<B>(0); // The Y member of B is not a type
|
| 251 |
g<C>(0); // The N member of C is not a non-type
|
| 252 |
h<D>(0); // The TT member of D is not a template
|
| 253 |
}
|
| 254 |
```
|
| 255 |
+
|
| 256 |
+
— *end example*]
|
| 257 |
- Attempting to create a pointer to reference type.
|
| 258 |
- Attempting to create a reference to `void`.
|
| 259 |
- Attempting to create “pointer to member of `T`” when `T` is not a
|
| 260 |
class type.
|
| 261 |
+
\[*Example 10*:
|
| 262 |
``` cpp
|
| 263 |
template <class T> int f(int T::*);
|
| 264 |
int i = f<int>(0);
|
| 265 |
```
|
| 266 |
+
|
| 267 |
+
— *end example*]
|
| 268 |
- Attempting to give an invalid type to a non-type template parameter.
|
| 269 |
+
\[*Example 11*:
|
| 270 |
``` cpp
|
| 271 |
template <class T, T> struct S {};
|
| 272 |
template <class T> int f(S<T, T()>*);
|
| 273 |
struct X {};
|
| 274 |
int i0 = f<X>(0);
|
| 275 |
```
|
| 276 |
+
|
| 277 |
+
— *end example*]
|
| 278 |
- Attempting to perform an invalid conversion in either a template
|
| 279 |
argument expression, or an expression used in the function
|
| 280 |
declaration.
|
| 281 |
+
\[*Example 12*:
|
| 282 |
``` cpp
|
| 283 |
template <class T, T*> int f(int);
|
| 284 |
int i2 = f<int,1>(0); // can't conv 1 to int*
|
| 285 |
```
|
| 286 |
+
|
| 287 |
+
— *end example*]
|
| 288 |
- Attempting to create a function type in which a parameter has a type
|
| 289 |
of `void`, or in which the return type is a function type or array
|
| 290 |
type.
|
| 291 |
- Attempting to create a function type in which a parameter type or the
|
| 292 |
return type is an abstract class type ([[class.abstract]]).
|
| 293 |
|
| 294 |
+
— *end note*]
|
| 295 |
+
|
| 296 |
+
[*Example 13*:
|
| 297 |
+
|
| 298 |
+
In the following example, assuming a `signed char` cannot represent the
|
| 299 |
+
value 1000, a narrowing conversion ([[dcl.init.list]]) would be
|
| 300 |
+
required to convert the *template-argument* of type `int` to
|
| 301 |
+
`signed char`, therefore substitution fails for the second template (
|
| 302 |
+
[[temp.arg.nontype]]).
|
| 303 |
|
| 304 |
``` cpp
|
| 305 |
template <int> int f(int);
|
| 306 |
template <signed char> int f(int);
|
| 307 |
+
int i1 = f<1000>(0); // OK
|
| 308 |
+
int i2 = f<1>(0); // ambiguous; not narrowing
|
| 309 |
```
|
| 310 |
|
| 311 |
+
— *end example*]
|
| 312 |
+
|
| 313 |
#### Deducing template arguments from a function call <a id="temp.deduct.call">[[temp.deduct.call]]</a>
|
| 314 |
|
| 315 |
Template argument deduction is done by comparing each function template
|
| 316 |
+
parameter type (call it `P`) that contains *template-parameter*s that
|
| 317 |
+
participate in template argument deduction with the type of the
|
| 318 |
+
corresponding argument of the call (call it `A`) as described below. If
|
| 319 |
+
removing references and cv-qualifiers from `P` gives
|
| 320 |
+
`std::initializer_list<P'>` or `P'[N]` for some `P'` and `N` and the
|
| 321 |
+
argument is a non-empty initializer list ([[dcl.init.list]]), then
|
| 322 |
+
deduction is performed instead for each element of the initializer list,
|
| 323 |
+
taking `P'` as a function template parameter type and the initializer
|
| 324 |
+
element as its argument, and in the `P'[N]` case, if `N` is a non-type
|
| 325 |
+
template parameter, `N` is deduced from the length of the initializer
|
| 326 |
+
list. Otherwise, an initializer list argument causes the parameter to be
|
| 327 |
+
considered a non-deduced context ([[temp.deduct.type]]).
|
| 328 |
+
|
| 329 |
+
[*Example 1*:
|
| 330 |
|
| 331 |
``` cpp
|
| 332 |
template<class T> void f(std::initializer_list<T>);
|
| 333 |
f({1,2,3}); // T deduced to int
|
| 334 |
f({1,"asdf"}); // error: T deduced to both int and const char*
|
| 335 |
|
| 336 |
template<class T> void g(T);
|
| 337 |
g({1,2,3}); // error: no argument deduced for T
|
| 338 |
+
|
| 339 |
+
template<class T, int N> void h(T const(&)[N]);
|
| 340 |
+
h({1,2,3}); // T deduced to int, N deduced to 3
|
| 341 |
+
|
| 342 |
+
template<class T> void j(T const(&)[3]);
|
| 343 |
+
j({42}); // T deduced to int, array bound not considered
|
| 344 |
+
|
| 345 |
+
struct Aggr { int i; int j; };
|
| 346 |
+
template<int N> void k(Aggr const(&)[N]);
|
| 347 |
+
k({1,2,3}); // error: deduction fails, no conversion from int to Aggr
|
| 348 |
+
k({{1},{2},{3}}); // OK, N deduced to 3
|
| 349 |
+
|
| 350 |
+
template<int M, int N> void m(int const(&)[M][N]);
|
| 351 |
+
m({{1,2},{3,4}}); // M and N both deduced to 2
|
| 352 |
+
|
| 353 |
+
template<class T, int N> void n(T const(&)[N], T);
|
| 354 |
+
n({{1},{2},{3}},Aggr()); // OK, T is Aggr, N is 3
|
| 355 |
```
|
| 356 |
|
| 357 |
+
— *end example*]
|
| 358 |
+
|
| 359 |
For a function parameter pack that occurs at the end of the
|
| 360 |
+
*parameter-declaration-list*, deduction is performed for each remaining
|
| 361 |
+
argument of the call, taking the type `P` of the *declarator-id* of the
|
| 362 |
+
function parameter pack as the corresponding function template parameter
|
| 363 |
+
type. Each deduction deduces template arguments for subsequent positions
|
| 364 |
+
in the template parameter packs expanded by the function parameter pack.
|
| 365 |
+
When a function parameter pack appears in a non-deduced context (
|
| 366 |
+
[[temp.deduct.type]]), the type of that parameter pack is never deduced.
|
| 367 |
+
|
| 368 |
+
[*Example 2*:
|
| 369 |
|
| 370 |
``` cpp
|
| 371 |
template<class ... Types> void f(Types& ...);
|
| 372 |
template<class T1, class ... Types> void g(T1, Types ...);
|
| 373 |
template<class T1, class ... Types> void g1(Types ..., T1);
|
|
|
|
| 376 |
const int z = x;
|
| 377 |
f(x, y, z); // Types is deduced to int, float, const int
|
| 378 |
g(x, y, z); // T1 is deduced to int; Types is deduced to float, int
|
| 379 |
g1(x, y, z); // error: Types is not deduced
|
| 380 |
g1<int, int, int>(x, y, z); // OK, no deduction occurs
|
|
|
|
| 381 |
}
|
| 382 |
```
|
| 383 |
|
| 384 |
+
— *end example*]
|
| 385 |
+
|
| 386 |
If `P` is not a reference type:
|
| 387 |
|
| 388 |
- If `A` is an array type, the pointer type produced by the
|
| 389 |
array-to-pointer standard conversion ([[conv.array]]) is used in
|
| 390 |
place of `A` for type deduction; otherwise,
|
| 391 |
- If `A` is a function type, the pointer type produced by the
|
| 392 |
function-to-pointer standard conversion ([[conv.func]]) is used in
|
| 393 |
place of `A` for type deduction; otherwise,
|
| 394 |
+
- If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s
|
| 395 |
type are ignored for type deduction.
|
| 396 |
|
| 397 |
+
If `P` is a cv-qualified type, the top-level cv-qualifiers of `P`’s type
|
| 398 |
are ignored for type deduction. If `P` is a reference type, the type
|
| 399 |
+
referred to by `P` is used for type deduction.
|
| 400 |
+
|
| 401 |
+
[*Example 3*:
|
| 402 |
+
|
| 403 |
+
``` cpp
|
| 404 |
+
template<class T> int f(const T&);
|
| 405 |
+
int n1 = f(5); // calls f<int>(const int&)
|
| 406 |
+
const int i = 0;
|
| 407 |
+
int n2 = f(i); // calls f<int>(const int&)
|
| 408 |
+
template <class T> int g(volatile T&);
|
| 409 |
+
int n3 = g(i); // calls g<const int>(const volatile int&)
|
| 410 |
+
```
|
| 411 |
+
|
| 412 |
+
— *end example*]
|
| 413 |
+
|
| 414 |
+
A *forwarding reference* is an rvalue reference to a cv-unqualified
|
| 415 |
+
template parameter that does not represent a template parameter of a
|
| 416 |
+
class template (during class template argument deduction (
|
| 417 |
+
[[over.match.class.deduct]])). If `P` is a forwarding reference and the
|
| 418 |
+
argument is an lvalue, the type “lvalue reference to `A`” is used in
|
| 419 |
+
place of `A` for type deduction.
|
| 420 |
+
|
| 421 |
+
[*Example 4*:
|
| 422 |
|
| 423 |
``` cpp
|
| 424 |
+
template <class T> int f(T&& heisenreference);
|
| 425 |
template <class T> int g(const T&&);
|
| 426 |
int i;
|
| 427 |
int n1 = f(i); // calls f<int&>(int&)
|
| 428 |
int n2 = f(0); // calls f<int>(int&&)
|
| 429 |
int n3 = g(i); // error: would call g<int>(const int&&), which
|
| 430 |
// would bind an rvalue reference to an lvalue
|
| 431 |
+
|
| 432 |
+
template <class T> struct A {
|
| 433 |
+
template <class U>
|
| 434 |
+
A(T&&, U&&, int*); // #1: T&& is not a forwarding reference.
|
| 435 |
+
// U&& is a forwarding reference.
|
| 436 |
+
A(T&&, int*); // #2
|
| 437 |
+
};
|
| 438 |
+
|
| 439 |
+
template <class T> A(T&&, int*) -> A<T>; // #3: T&& is a forwarding reference.
|
| 440 |
+
|
| 441 |
+
int *ip;
|
| 442 |
+
A a{i, 0, ip}; // error: cannot deduce from #1
|
| 443 |
+
A a0{0, 0, ip}; // uses #1 to deduce A<int> and #1 to initialize
|
| 444 |
+
A a2{i, ip}; // uses #3 to deduce A<int&> and #2 to initialize
|
| 445 |
```
|
| 446 |
|
| 447 |
+
— *end example*]
|
| 448 |
+
|
| 449 |
In general, the deduction process attempts to find template argument
|
| 450 |
values that will make the deduced `A` identical to `A` (after the type
|
| 451 |
`A` is transformed as described above). However, there are three cases
|
| 452 |
that allow a difference:
|
| 453 |
|
| 454 |
- If the original `P` is a reference type, the deduced `A` (i.e., the
|
| 455 |
type referred to by the reference) can be more cv-qualified than the
|
| 456 |
transformed `A`.
|
| 457 |
- The transformed `A` can be another pointer or pointer to member type
|
| 458 |
+
that can be converted to the deduced `A` via a function pointer
|
| 459 |
+
conversion ([[conv.fctptr]]) and/or qualification conversion (
|
| 460 |
+
[[conv.qual]]).
|
| 461 |
- If `P` is a class and `P` has the form *simple-template-id*, then the
|
| 462 |
transformed `A` can be a derived class of the deduced `A`. Likewise,
|
| 463 |
if `P` is a pointer to a class of the form *simple-template-id*, the
|
| 464 |
transformed `A` can be a pointer to a derived class pointed to by the
|
| 465 |
deduced `A`.
|
| 466 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
These alternatives are considered only if type deduction would otherwise
|
| 468 |
fail. If they yield more than one possible deduced `A`, the type
|
| 469 |
+
deduction fails.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 470 |
|
| 471 |
+
[*Note 1*: If a *template-parameter* is not used in any of the function
|
| 472 |
+
parameters of a function template, or is used only in a non-deduced
|
| 473 |
+
context, its corresponding *template-argument* cannot be deduced from a
|
| 474 |
+
function call and the *template-argument* must be explicitly
|
| 475 |
+
specified. — *end note*]
|
| 476 |
+
|
| 477 |
+
When `P` is a function type, function pointer type, or pointer to member
|
| 478 |
+
function type:
|
| 479 |
|
| 480 |
- If the argument is an overload set containing one or more function
|
| 481 |
templates, the parameter is treated as a non-deduced context.
|
| 482 |
- If the argument is an overload set (not containing function
|
| 483 |
templates), trial argument deduction is attempted using each of the
|
| 484 |
members of the set. If deduction succeeds for only one of the overload
|
| 485 |
set members, that member is used as the argument value for the
|
| 486 |
deduction. If deduction succeeds for more than one member of the
|
| 487 |
overload set the parameter is treated as a non-deduced context.
|
| 488 |
+
|
| 489 |
+
[*Example 5*:
|
| 490 |
+
|
| 491 |
``` cpp
|
| 492 |
+
// Only one function of an overload set matches the call so the function parameter is a deduced context.
|
|
|
|
| 493 |
template <class T> int f(T (*p)(T));
|
| 494 |
int g(int);
|
| 495 |
int g(char);
|
| 496 |
int i = f(g); // calls f(int (*)(int))
|
| 497 |
```
|
| 498 |
|
| 499 |
+
— *end example*]
|
| 500 |
+
|
| 501 |
+
[*Example 6*:
|
| 502 |
+
|
| 503 |
``` cpp
|
| 504 |
+
// Ambiguous deduction causes the second function parameter to be a non-deduced context.
|
|
|
|
| 505 |
template <class T> int f(T, T (*p)(T));
|
| 506 |
int g(int);
|
| 507 |
char g(char);
|
| 508 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 509 |
```
|
| 510 |
|
| 511 |
+
— *end example*]
|
| 512 |
+
|
| 513 |
+
[*Example 7*:
|
| 514 |
+
|
| 515 |
``` cpp
|
| 516 |
+
// The overload set contains a template, causing the second function parameter to be a non-deduced context.
|
|
|
|
| 517 |
template <class T> int f(T, T (*p)(T));
|
| 518 |
char g(char);
|
| 519 |
template <class T> T g(T);
|
| 520 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 521 |
```
|
| 522 |
|
| 523 |
+
— *end example*]
|
| 524 |
+
|
| 525 |
+
If deduction succeeds for all parameters that contain
|
| 526 |
+
*template-parameter*s that participate in template argument deduction,
|
| 527 |
+
and all template arguments are explicitly specified, deduced, or
|
| 528 |
+
obtained from default template arguments, remaining parameters are then
|
| 529 |
+
compared with the corresponding arguments. For each remaining parameter
|
| 530 |
+
`P` with a type that was non-dependent before substitution of any
|
| 531 |
+
explicitly-specified template arguments, if the corresponding argument
|
| 532 |
+
`A` cannot be implicitly converted to `P`, deduction fails.
|
| 533 |
+
|
| 534 |
+
[*Note 2*: Parameters with dependent types in which no
|
| 535 |
+
*template-parameter*s participate in template argument deduction, and
|
| 536 |
+
parameters that became non-dependent due to substitution of
|
| 537 |
+
explicitly-specified template arguments, will be checked during overload
|
| 538 |
+
resolution. — *end note*]
|
| 539 |
+
|
| 540 |
+
[*Example 8*:
|
| 541 |
+
|
| 542 |
+
``` cpp
|
| 543 |
+
template <class T> struct Z {
|
| 544 |
+
typedef typename T::x xx;
|
| 545 |
+
};
|
| 546 |
+
template <class T> typename Z<T>::xx f(void *, T); // #1
|
| 547 |
+
template <class T> void f(int, T); // #2
|
| 548 |
+
struct A {} a;
|
| 549 |
+
int main() {
|
| 550 |
+
f(1, a); // OK, deduction fails for #1 because there is no conversion from int to void*
|
| 551 |
+
}
|
| 552 |
+
```
|
| 553 |
+
|
| 554 |
+
— *end example*]
|
| 555 |
+
|
| 556 |
#### Deducing template arguments taking the address of a function template <a id="temp.deduct.funcaddr">[[temp.deduct.funcaddr]]</a>
|
| 557 |
|
| 558 |
Template arguments can be deduced from the type specified when taking
|
| 559 |
the address of an overloaded function ([[over.over]]). The function
|
| 560 |
template’s function type and the specified type are used as the types of
|
|
|
|
| 584 |
array-to-pointer standard conversion ([[conv.array]]) is used in
|
| 585 |
place of `P` for type deduction; otherwise,
|
| 586 |
- If `P` is a function type, the pointer type produced by the
|
| 587 |
function-to-pointer standard conversion ([[conv.func]]) is used in
|
| 588 |
place of `P` for type deduction; otherwise,
|
| 589 |
+
- If `P` is a cv-qualified type, the top-level cv-qualifiers of `P`’s
|
| 590 |
type are ignored for type deduction.
|
| 591 |
|
| 592 |
+
If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s type
|
| 593 |
are ignored for type deduction. If `A` is a reference type, the type
|
| 594 |
referred to by `A` is used for type deduction.
|
| 595 |
|
| 596 |
In general, the deduction process attempts to find template argument
|
| 597 |
values that will make the deduced `A` identical to `A`. However, there
|
| 598 |
+
are four cases that allow a difference:
|
| 599 |
|
| 600 |
- If the original `A` is a reference type, `A` can be more cv-qualified
|
| 601 |
than the deduced `A` (i.e., the type referred to by the reference)
|
| 602 |
+
- If the original `A` is a function pointer type, `A` can be “pointer to
|
| 603 |
+
function” even if the deduced `A` is “pointer to noexcept function”.
|
| 604 |
+
- If the original `A` is a pointer to member function type, `A` can be
|
| 605 |
+
“pointer to member of type function” even if the deduced `A` is
|
| 606 |
+
“pointer to member of type noexcept function”.
|
| 607 |
- The deduced `A` can be another pointer or pointer to member type that
|
| 608 |
can be converted to `A` via a qualification conversion.
|
| 609 |
|
| 610 |
These alternatives are considered only if type deduction would otherwise
|
| 611 |
fail. If they yield more than one possible deduced `A`, the type
|
|
|
|
| 617 |
|
| 618 |
If `A` is a type
|
| 619 |
|
| 620 |
and `P` is a type
|
| 621 |
|
| 622 |
+
then the cv-unqualified `T1` and `T2` are used as the types of `A` and
|
| 623 |
+
`P` respectively for type deduction.
|
| 624 |
+
|
| 625 |
+
[*Example 1*:
|
| 626 |
|
| 627 |
``` cpp
|
| 628 |
struct A {
|
| 629 |
template <class T> operator T***();
|
| 630 |
};
|
| 631 |
A a;
|
| 632 |
const int * const * const * p1 = a; // T is deduced as int, not const int
|
| 633 |
```
|
| 634 |
|
| 635 |
+
— *end example*]
|
| 636 |
+
|
| 637 |
#### Deducing template arguments during partial ordering <a id="temp.deduct.partial">[[temp.deduct.partial]]</a>
|
| 638 |
|
| 639 |
Template argument deduction is done by comparing certain types
|
| 640 |
associated with the two function templates being compared.
|
| 641 |
|
| 642 |
Two sets of types are used to determine the partial ordering. For each
|
| 643 |
of the templates involved there is the original function type and the
|
| 644 |
+
transformed function type.
|
| 645 |
+
|
| 646 |
+
[*Note 1*: The creation of the transformed type is described in
|
| 647 |
+
[[temp.func.order]]. — *end note*]
|
| 648 |
+
|
| 649 |
+
The deduction process uses the transformed type as the argument template
|
| 650 |
+
and the original type of the other template as the parameter template.
|
| 651 |
+
This process is done twice for each type involved in the partial
|
| 652 |
+
ordering comparison: once using the transformed template-1 as the
|
| 653 |
+
argument template and template-2 as the parameter template and again
|
| 654 |
+
using the transformed template-2 as the argument template and template-1
|
| 655 |
+
as the parameter template.
|
| 656 |
|
| 657 |
The types used to determine the ordering depend on the context in which
|
| 658 |
the partial ordering is done:
|
| 659 |
|
| 660 |
- In the context of a function call, the types used are those function
|
|
|
|
| 664 |
- In other contexts ([[temp.func.order]]) the function template’s
|
| 665 |
function type is used.
|
| 666 |
|
| 667 |
Each type nominated above from the parameter template and the
|
| 668 |
corresponding type from the argument template are used as the types of
|
| 669 |
+
`P` and `A`. If a particular `P` contains no *template-parameter*s that
|
| 670 |
+
participate in template argument deduction, that `P` is not used to
|
| 671 |
+
determine the ordering.
|
| 672 |
|
| 673 |
Before the partial ordering is done, certain transformations are
|
| 674 |
performed on the types used for partial ordering:
|
| 675 |
|
| 676 |
- If `P` is a reference type, `P` is replaced by the type referred to.
|
|
|
|
| 687 |
- If `P` is a cv-qualified type, `P` is replaced by the cv-unqualified
|
| 688 |
version of `P`.
|
| 689 |
- If `A` is a cv-qualified type, `A` is replaced by the cv-unqualified
|
| 690 |
version of `A`.
|
| 691 |
|
| 692 |
+
Using the resulting types `P` and `A`, the deduction is then done as
|
| 693 |
+
described in [[temp.deduct.type]]. If `P` is a function parameter pack,
|
| 694 |
+
the type `A` of each remaining parameter type of the argument template
|
| 695 |
+
is compared with the type `P` of the *declarator-id* of the function
|
| 696 |
+
parameter pack. Each comparison deduces template arguments for
|
| 697 |
+
subsequent positions in the template parameter packs expanded by the
|
| 698 |
+
function parameter pack. Similarly, if `A` was transformed from a
|
| 699 |
+
function parameter pack, it is compared with each remaining parameter
|
| 700 |
+
type of the parameter template. If deduction succeeds for a given type,
|
| 701 |
+
the type from the argument template is considered to be at least as
|
| 702 |
+
specialized as the type from the parameter template.
|
| 703 |
+
|
| 704 |
+
[*Example 1*:
|
| 705 |
|
| 706 |
``` cpp
|
| 707 |
template<class... Args> void f(Args... args); // #1
|
| 708 |
template<class T1, class... Args> void f(T1 a1, Args... args); // #2
|
| 709 |
template<class T1, class T2> void f(T1 a1, T2 a2); // #3
|
| 710 |
|
| 711 |
f(); // calls #1
|
| 712 |
f(1, 2, 3); // calls #2
|
| 713 |
+
f(1, 2); // calls #3; non-variadic template #3 is more specialized
|
| 714 |
+
// than the variadic templates #1 and #2
|
| 715 |
```
|
| 716 |
|
| 717 |
+
— *end example*]
|
| 718 |
+
|
| 719 |
If, for a given type, deduction succeeds in both directions (i.e., the
|
| 720 |
types are identical after the transformations above) and both `P` and
|
| 721 |
`A` were reference types (before being replaced with the type referred
|
| 722 |
to above):
|
| 723 |
|
| 724 |
- if the type from the argument template was an lvalue reference and the
|
| 725 |
+
type from the parameter template was not, the parameter type is not
|
| 726 |
+
considered to be at least as specialized as the argument type;
|
| 727 |
+
otherwise,
|
| 728 |
- if the type from the argument template is more cv-qualified than the
|
| 729 |
+
type from the parameter template (as described above), the parameter
|
| 730 |
+
type is not considered to be at least as specialized as the argument
|
| 731 |
+
type.
|
| 732 |
|
| 733 |
+
Function template `F` is *at least as specialized as* function template
|
| 734 |
+
`G` if, for each pair of types used to determine the ordering, the type
|
| 735 |
+
from `F` is at least as specialized as the type from `G`. `F` is *more
|
| 736 |
+
specialized than* `G` if `F` is at least as specialized as `G` and `G`
|
| 737 |
+
is not at least as specialized as `F`.
|
| 738 |
+
|
| 739 |
+
If, after considering the above, function template `F` is at least as
|
| 740 |
+
specialized as function template `G` and vice-versa, and if `G` has a
|
| 741 |
+
trailing parameter pack for which `F` does not have a corresponding
|
| 742 |
+
parameter, and if `F` does not have a trailing parameter pack, then `F`
|
| 743 |
+
is more specialized than `G`.
|
| 744 |
|
| 745 |
In most cases, all template parameters must have values in order for
|
| 746 |
deduction to succeed, but for partial ordering purposes a template
|
| 747 |
parameter may remain without a value provided it is not used in the
|
| 748 |
+
types being used for partial ordering.
|
| 749 |
+
|
| 750 |
+
[*Note 2*: A template parameter used in a non-deduced context is
|
| 751 |
+
considered used. — *end note*]
|
| 752 |
+
|
| 753 |
+
[*Example 2*:
|
| 754 |
|
| 755 |
``` cpp
|
| 756 |
template <class T> T f(int); // #1
|
| 757 |
template <class T, class U> T f(U); // #2
|
| 758 |
void g() {
|
| 759 |
f<int>(1); // calls #1
|
| 760 |
}
|
| 761 |
```
|
| 762 |
|
| 763 |
+
— *end example*]
|
| 764 |
+
|
| 765 |
+
[*Note 3*: Partial ordering of function templates containing template
|
| 766 |
+
parameter packs is independent of the number of deduced arguments for
|
| 767 |
+
those template parameter packs. — *end note*]
|
| 768 |
+
|
| 769 |
+
[*Example 3*:
|
| 770 |
|
| 771 |
``` cpp
|
| 772 |
template<class ...> struct Tuple { };
|
| 773 |
template<class ... Types> void g(Tuple<Types ...>); // #1
|
| 774 |
template<class T1, class ... Types> void g(Tuple<T1, Types ...>); // #2
|
|
|
|
| 778 |
g(Tuple<int, float>()); // calls #2
|
| 779 |
g(Tuple<int, float&>()); // calls #3
|
| 780 |
g(Tuple<int>()); // calls #3
|
| 781 |
```
|
| 782 |
|
| 783 |
+
— *end example*]
|
| 784 |
+
|
| 785 |
#### Deducing template arguments from a type <a id="temp.deduct.type">[[temp.deduct.type]]</a>
|
| 786 |
|
| 787 |
Template arguments can be deduced in several different contexts, but in
|
| 788 |
each case a type that is specified in terms of template parameters (call
|
| 789 |
it `P`) is compared with an actual type (call it `A`), and an attempt is
|
|
|
|
| 798 |
deduced template argument values are then combined. If type deduction
|
| 799 |
cannot be done for any `P/A` pair, or if for any pair the deduction
|
| 800 |
leads to more than one possible set of deduced values, or if different
|
| 801 |
pairs yield different deduced values, or if any template argument
|
| 802 |
remains neither deduced nor explicitly specified, template argument
|
| 803 |
+
deduction fails. The type of a type parameter is only deduced from an
|
| 804 |
+
array bound if it is not otherwise deduced.
|
| 805 |
|
| 806 |
A given type `P` can be composed from a number of other types,
|
| 807 |
templates, and non-type values:
|
| 808 |
|
| 809 |
- A function type includes the types of each of the function parameters
|
|
|
|
| 824 |
deduction, but instead uses the values of template arguments that were
|
| 825 |
either deduced elsewhere or explicitly specified. If a template
|
| 826 |
parameter is used only in non-deduced contexts and is not explicitly
|
| 827 |
specified, template argument deduction fails.
|
| 828 |
|
| 829 |
+
[*Note 1*: Under [[temp.deduct.call]] and [[temp.deduct.partial]], if
|
| 830 |
+
`P` contains no *template-parameter*s that appear in deduced contexts,
|
| 831 |
+
no deduction is done, so `P` and `A` need not have the same
|
| 832 |
+
form. — *end note*]
|
| 833 |
+
|
| 834 |
The non-deduced contexts are:
|
| 835 |
|
| 836 |
- The *nested-name-specifier* of a type that was specified using a
|
| 837 |
*qualified-id*.
|
| 838 |
- The *expression* of a *decltype-specifier*.
|
|
|
|
| 850 |
- no function matches the function parameter type, or
|
| 851 |
- the set of functions supplied as an argument contains one or more
|
| 852 |
function templates.
|
| 853 |
- A function parameter for which the associated argument is an
|
| 854 |
initializer list ([[dcl.init.list]]) but the parameter does not have
|
| 855 |
+
a type for which deduction from an initializer list is specified (
|
| 856 |
+
[[temp.deduct.call]]).
|
| 857 |
+
\[*Example 1*:
|
| 858 |
``` cpp
|
| 859 |
template<class T> void g(T);
|
| 860 |
g({1,2,3}); // error: no argument deduced for T
|
| 861 |
```
|
| 862 |
+
|
| 863 |
+
— *end example*]
|
| 864 |
- A function parameter pack that does not occur at the end of the
|
| 865 |
*parameter-declaration-list*.
|
| 866 |
|
| 867 |
When a type name is specified in a way that includes a non-deduced
|
| 868 |
context, all of the types that comprise that type name are also
|
| 869 |
non-deduced. However, a compound type can include both deduced and
|
| 870 |
+
non-deduced types.
|
| 871 |
+
|
| 872 |
+
[*Example 2*: If a type is specified as `A<T>::B<T2>`, both `T` and
|
| 873 |
`T2` are non-deduced. Likewise, if a type is specified as
|
| 874 |
`A<I+J>::X<T>`, `I`, `J`, and `T` are non-deduced. If a type is
|
| 875 |
specified as `void` `f(typename` `A<T>::B,` `A<T>)`, the `T` in
|
| 876 |
+
`A<T>::B` is non-deduced but the `T` in `A<T>` is
|
| 877 |
+
deduced. — *end example*]
|
| 878 |
+
|
| 879 |
+
[*Example 3*:
|
| 880 |
|
| 881 |
Here is an example in which different parameter/argument pairs produce
|
| 882 |
inconsistent template argument deductions:
|
| 883 |
|
| 884 |
``` cpp
|
| 885 |
+
template<class T> void f(T x, T y) { ... }
|
| 886 |
+
struct A { ... };
|
| 887 |
+
struct B : A { ... };
|
| 888 |
void g(A a, B b) {
|
| 889 |
f(a,b); // error: T could be A or B
|
| 890 |
f(b,a); // error: T could be A or B
|
| 891 |
f(a,a); // OK: T is A
|
| 892 |
f(b,b); // OK: T is B
|
|
|
|
| 937 |
f(d); // calls f(B<int>&)
|
| 938 |
f(d2); // calls f(B<int>&)
|
| 939 |
}
|
| 940 |
```
|
| 941 |
|
| 942 |
+
— *end example*]
|
| 943 |
+
|
| 944 |
A template type argument `T`, a template template argument `TT` or a
|
| 945 |
template non-type argument `i` can be deduced if `P` and `A` have one of
|
| 946 |
the following forms:
|
| 947 |
|
| 948 |
``` cpp
|
|
|
|
| 971 |
TT<T>
|
| 972 |
TT<i>
|
| 973 |
TT<>
|
| 974 |
```
|
| 975 |
|
| 976 |
+
where `(T)` represents a parameter-type-list ([[dcl.fct]]) where at
|
| 977 |
+
least one parameter type contains a `T`, and `()` represents a
|
| 978 |
+
parameter-type-list where no parameter type contains a `T`. Similarly,
|
| 979 |
`<T>` represents template argument lists where at least one argument
|
| 980 |
contains a `T`, `<i>` represents template argument lists where at least
|
| 981 |
one argument contains an `i` and `<>` represents template argument lists
|
| 982 |
where no argument contains a `T` or an `i`.
|
| 983 |
|
| 984 |
If `P` has a form that contains `<T>` or `<i>`, then each argument Pᵢ of
|
| 985 |
+
the respective template argument list of `P` is compared with the
|
| 986 |
corresponding argument Aᵢ of the corresponding template argument list of
|
| 987 |
`A`. If the template argument list of `P` contains a pack expansion that
|
| 988 |
is not the last template argument, the entire template argument list is
|
| 989 |
a non-deduced context. If `Pᵢ` is a pack expansion, then the pattern of
|
| 990 |
`Pᵢ` is compared with each remaining argument in the template argument
|
|
|
|
| 996 |
- if `P` does not contain a template argument corresponding to `Aᵢ` then
|
| 997 |
`Aᵢ` is ignored;
|
| 998 |
- otherwise, if `Pᵢ` is not a pack expansion, template argument
|
| 999 |
deduction fails.
|
| 1000 |
|
| 1001 |
+
[*Example 4*:
|
| 1002 |
+
|
| 1003 |
``` cpp
|
| 1004 |
template<class T1, class... Z> class S; // #1
|
| 1005 |
template<class T1, class... Z> class S<T1, const Z&...> { }; // #2
|
| 1006 |
template<class T1, class T2> class S<T1, const T2&> { }; // #3
|
| 1007 |
S<int, const int&> s; // both #2 and #3 match; #3 is more specialized
|
|
|
|
| 1010 |
template<class T1, class T2, class... U> struct A<T1, T2*, U...> { }; // #2
|
| 1011 |
template<class T1, class T2> struct A<T1, T2> { }; // #3
|
| 1012 |
template struct A<int, int*>; // selects #2
|
| 1013 |
```
|
| 1014 |
|
| 1015 |
+
— *end example*]
|
| 1016 |
+
|
| 1017 |
Similarly, if `P` has a form that contains `(T)`, then each parameter
|
| 1018 |
+
type `Pᵢ` of the respective parameter-type-list ([[dcl.fct]]) of `P` is
|
| 1019 |
+
compared with the corresponding parameter type `Aᵢ` of the corresponding
|
| 1020 |
+
parameter-type-list of `A`. If `P` and `A` are function types that
|
| 1021 |
originated from deduction when taking the address of a function
|
| 1022 |
template ([[temp.deduct.funcaddr]]) or when deducing template arguments
|
| 1023 |
from a function declaration ([[temp.deduct.decl]]) and `Pᵢ` and `Aᵢ`
|
| 1024 |
+
are parameters of the top-level parameter-type-list of `P` and `A`,
|
| 1025 |
+
respectively, `Pᵢ` is adjusted if it is a forwarding reference (
|
| 1026 |
+
[[temp.deduct.call]]) and `Aᵢ` is an lvalue reference, in which case the
|
| 1027 |
+
type of `Pᵢ` is changed to be the template parameter type (i.e., `T&&`
|
| 1028 |
+
is changed to simply `T`).
|
| 1029 |
+
|
| 1030 |
+
[*Note 2*: As a result, when `Pᵢ` is `T&&` and `Aᵢ` is `X&`, the
|
| 1031 |
+
adjusted `Pᵢ` will be `T`, causing `T` to be deduced as
|
| 1032 |
+
`X&`. — *end note*]
|
| 1033 |
+
|
| 1034 |
+
[*Example 5*:
|
| 1035 |
|
| 1036 |
``` cpp
|
| 1037 |
template <class T> void f(T&&);
|
| 1038 |
template <> void f(int&) { } // #1
|
| 1039 |
template <> void f(int&&) { } // #2
|
|
|
|
| 1041 |
f(i); // calls f<int&>(int&), i.e., #1
|
| 1042 |
f(0); // calls f<int>(int&&), i.e., #2
|
| 1043 |
}
|
| 1044 |
```
|
| 1045 |
|
| 1046 |
+
— *end example*]
|
| 1047 |
+
|
| 1048 |
If the *parameter-declaration* corresponding to `Pᵢ` is a function
|
| 1049 |
parameter pack, then the type of its *declarator-id* is compared with
|
| 1050 |
+
each remaining parameter type in the parameter-type-list of `A`. Each
|
| 1051 |
comparison deduces template arguments for subsequent positions in the
|
| 1052 |
template parameter packs expanded by the function parameter pack. During
|
| 1053 |
partial ordering ([[temp.deduct.partial]]), if `Aᵢ` was originally a
|
| 1054 |
function parameter pack:
|
| 1055 |
|
| 1056 |
- if `P` does not contain a function parameter type corresponding to
|
| 1057 |
`Aᵢ` then `Aᵢ` is ignored;
|
| 1058 |
- otherwise, if `Pᵢ` is not a function parameter pack, template argument
|
| 1059 |
deduction fails.
|
| 1060 |
|
| 1061 |
+
[*Example 6*:
|
| 1062 |
+
|
| 1063 |
``` cpp
|
| 1064 |
template<class T, class... U> void f(T*, U...) { } // #1
|
| 1065 |
template<class T> void f(T) { } // #2
|
| 1066 |
template void f(int*); // selects #1
|
| 1067 |
```
|
| 1068 |
|
| 1069 |
+
— *end example*]
|
| 1070 |
+
|
| 1071 |
These forms can be used in the same way as `T` is for further
|
| 1072 |
composition of types.
|
| 1073 |
|
| 1074 |
+
[*Example 7*:
|
| 1075 |
+
|
| 1076 |
``` cpp
|
| 1077 |
X<int> (*)(char[6])
|
| 1078 |
```
|
| 1079 |
|
| 1080 |
is of the form
|
|
|
|
| 1089 |
type (*)(T)
|
| 1090 |
```
|
| 1091 |
|
| 1092 |
where type is `X<int>` and `T` is `char[6]`.
|
| 1093 |
|
| 1094 |
+
— *end example*]
|
| 1095 |
+
|
| 1096 |
Template arguments cannot be deduced from function arguments involving
|
| 1097 |
constructs other than the ones specified above.
|
| 1098 |
|
| 1099 |
+
When the value of the argument corresponding to a non-type template
|
| 1100 |
+
parameter `P` that is declared with a dependent type is deduced from an
|
| 1101 |
+
expression, the template parameters in the type of `P` are deduced from
|
| 1102 |
+
the type of the value.
|
| 1103 |
+
|
| 1104 |
+
[*Example 8*:
|
| 1105 |
|
| 1106 |
``` cpp
|
| 1107 |
+
template<long n> struct A { };
|
| 1108 |
+
|
| 1109 |
+
template<typename T> struct C;
|
| 1110 |
+
template<typename T, T n> struct C<A<n>> {
|
| 1111 |
+
using Q = T;
|
| 1112 |
+
};
|
| 1113 |
+
|
| 1114 |
+
using R = long;
|
| 1115 |
+
using R = C<A<2>>::Q; // OK; T was deduced to long from the
|
| 1116 |
+
// template argument value in the type A<2>
|
| 1117 |
```
|
| 1118 |
|
| 1119 |
+
— *end example*]
|
| 1120 |
+
|
| 1121 |
+
The type of `N` in the type `T[N]` is `std::size_t`.
|
| 1122 |
+
|
| 1123 |
+
[*Example 9*:
|
| 1124 |
+
|
| 1125 |
+
``` cpp
|
| 1126 |
+
template<typename T> struct S;
|
| 1127 |
+
template<typename T, T n> struct S<int[n]> {
|
| 1128 |
+
using Q = T;
|
| 1129 |
+
};
|
| 1130 |
+
|
| 1131 |
+
using V = decltype(sizeof 0);
|
| 1132 |
+
using V = S<int[42]>::Q; // OK; T was deduced to std::size_t from the type int[42]
|
| 1133 |
+
```
|
| 1134 |
+
|
| 1135 |
+
— *end example*]
|
| 1136 |
+
|
| 1137 |
+
[*Example 10*:
|
| 1138 |
+
|
| 1139 |
+
``` cpp
|
| 1140 |
+
template<class T, T i> void f(int (&a)[i]);
|
| 1141 |
+
int v[10];
|
| 1142 |
+
void g() {
|
| 1143 |
+
f(v); // OK: T is std::size_t
|
| 1144 |
+
}
|
| 1145 |
+
```
|
| 1146 |
+
|
| 1147 |
+
— *end example*]
|
| 1148 |
+
|
| 1149 |
+
[*Note 3*:
|
| 1150 |
+
|
| 1151 |
Except for reference and pointer types, a major array bound is not part
|
| 1152 |
of a function parameter type and cannot be deduced from an argument:
|
| 1153 |
|
| 1154 |
``` cpp
|
| 1155 |
template<int i> void f1(int a[10][i]);
|
|
|
|
| 1164 |
f2<10>(v); // OK
|
| 1165 |
f3(v); // OK: i deduced to be 10
|
| 1166 |
}
|
| 1167 |
```
|
| 1168 |
|
| 1169 |
+
— *end note*]
|
| 1170 |
+
|
| 1171 |
+
[*Note 4*:
|
| 1172 |
+
|
| 1173 |
If, in the declaration of a function template with a non-type template
|
| 1174 |
parameter, the non-type template parameter is used in a subexpression in
|
| 1175 |
the function parameter list, the expression is a non-deduced context as
|
| 1176 |
specified above.
|
| 1177 |
|
| 1178 |
+
[*Example 11*:
|
| 1179 |
+
|
| 1180 |
``` cpp
|
| 1181 |
+
template <int i> class A { ... };
|
| 1182 |
template <int i> void g(A<i+1>);
|
| 1183 |
template <int i> void f(A<i>, A<i+1>);
|
| 1184 |
void k() {
|
| 1185 |
A<1> a1;
|
| 1186 |
A<2> a2;
|
|
|
|
| 1188 |
g<0>(a1); // OK
|
| 1189 |
f(a1, a2); // OK
|
| 1190 |
}
|
| 1191 |
```
|
| 1192 |
|
| 1193 |
+
— *end example*]
|
| 1194 |
+
|
| 1195 |
+
— *end note*]
|
| 1196 |
+
|
| 1197 |
+
[*Note 5*:
|
| 1198 |
+
|
| 1199 |
Template parameters do not participate in template argument deduction if
|
| 1200 |
they are used only in non-deduced contexts. For example,
|
| 1201 |
|
| 1202 |
``` cpp
|
| 1203 |
template<int i, typename T>
|
|
|
|
| 1206 |
typename B<i>::Y y); // i is not deduced here
|
| 1207 |
A<int> a;
|
| 1208 |
B<77> b;
|
| 1209 |
|
| 1210 |
int x = deduce<77>(a.xm, 62, b.ym);
|
| 1211 |
+
// T is deduced to be int, a.xm must be convertible to A<int>::X
|
| 1212 |
+
// i is explicitly specified to be 77, b.ym must be convertible to B<77>::Y
|
|
|
|
|
|
|
| 1213 |
```
|
| 1214 |
|
| 1215 |
+
— *end note*]
|
| 1216 |
+
|
| 1217 |
+
If `P` has a form that contains `<i>`, and if the type of `i` differs
|
| 1218 |
+
from the type of the corresponding template parameter of the template
|
| 1219 |
+
named by the enclosing *simple-template-id*, deduction fails. If `P` has
|
| 1220 |
+
a form that contains `[i]`, and if the type of `i` is not an integral
|
| 1221 |
+
type, deduction fails.[^8]
|
| 1222 |
+
|
| 1223 |
+
[*Example 12*:
|
| 1224 |
|
| 1225 |
``` cpp
|
| 1226 |
+
template<int i> class A { ... };
|
| 1227 |
template<short s> void f(A<s>);
|
| 1228 |
void k1() {
|
| 1229 |
A<1> a;
|
| 1230 |
f(a); // error: deduction fails for conversion from int to short
|
| 1231 |
f<1>(a); // OK
|
|
|
|
| 1237 |
B<1> b;
|
| 1238 |
g(b); // OK: cv-qualifiers are ignored on template parameter types
|
| 1239 |
}
|
| 1240 |
```
|
| 1241 |
|
| 1242 |
+
— *end example*]
|
| 1243 |
+
|
| 1244 |
A *template-argument* can be deduced from a function, pointer to
|
| 1245 |
function, or pointer to member function type.
|
| 1246 |
|
| 1247 |
+
[*Example 13*:
|
| 1248 |
+
|
| 1249 |
``` cpp
|
| 1250 |
template<class T> void f(void(*)(T,int));
|
| 1251 |
template<class T> void foo(T,int);
|
| 1252 |
void g(int,int);
|
| 1253 |
void g(char,int);
|
|
|
|
| 1259 |
f(&h); // OK: void h(char,int) is a unique match
|
| 1260 |
f(&foo); // error: type deduction fails because foo is a template
|
| 1261 |
}
|
| 1262 |
```
|
| 1263 |
|
| 1264 |
+
— *end example*]
|
| 1265 |
+
|
| 1266 |
A template *type-parameter* cannot be deduced from the type of a
|
| 1267 |
function default argument.
|
| 1268 |
|
| 1269 |
+
[*Example 14*:
|
| 1270 |
+
|
| 1271 |
``` cpp
|
| 1272 |
template <class T> void f(T = 5, T = 7);
|
| 1273 |
void g() {
|
| 1274 |
f(1); // OK: call f<int>(1,7)
|
| 1275 |
f(); // error: cannot deduce T
|
| 1276 |
f<int>(); // OK: call f<int>(5,7)
|
| 1277 |
}
|
| 1278 |
```
|
| 1279 |
|
| 1280 |
+
— *end example*]
|
| 1281 |
+
|
| 1282 |
The *template-argument* corresponding to a template *template-parameter*
|
| 1283 |
is deduced from the type of the *template-argument* of a class template
|
| 1284 |
specialization used in the argument list of a function call.
|
| 1285 |
|
| 1286 |
+
[*Example 15*:
|
| 1287 |
+
|
| 1288 |
``` cpp
|
| 1289 |
template <template <class T> class X> struct A { };
|
| 1290 |
template <template <class T> class X> void f(A<X>) { }
|
| 1291 |
template<class T> struct B { };
|
| 1292 |
A<B> ab;
|
| 1293 |
f(ab); // calls f(A<B>)
|
| 1294 |
```
|
| 1295 |
|
| 1296 |
+
— *end example*]
|
| 1297 |
+
|
| 1298 |
+
[*Note 6*: Template argument deduction involving parameter packs (
|
| 1299 |
[[temp.variadic]]) can deduce zero or more arguments for each parameter
|
| 1300 |
+
pack. — *end note*]
|
| 1301 |
+
|
| 1302 |
+
[*Example 16*:
|
| 1303 |
|
| 1304 |
``` cpp
|
| 1305 |
template<class> struct X { };
|
| 1306 |
template<class R, class ... ArgTypes> struct X<R(int, ArgTypes ...)> { };
|
| 1307 |
template<class ... Types> struct Y { };
|
|
|
|
| 1317 |
Y<int&, float&, double&> y2; // uses partial specialization; T is int&, Types contains float, double
|
| 1318 |
Y<int, float, double> y3; // uses primary template; Types contains int, float, double
|
| 1319 |
int fv = f(g); // OK; Types contains int, float
|
| 1320 |
```
|
| 1321 |
|
| 1322 |
+
— *end example*]
|
| 1323 |
+
|
| 1324 |
#### Deducing template arguments from a function declaration <a id="temp.deduct.decl">[[temp.deduct.decl]]</a>
|
| 1325 |
|
| 1326 |
In a declaration whose *declarator-id* refers to a specialization of a
|
| 1327 |
function template, template argument deduction is performed to identify
|
| 1328 |
the specialization to which the declaration refers. Specifically, this
|