- tmp/tmpe2ffr06d/{from.md → to.md} +527 -329
tmp/tmpe2ffr06d/{from.md → to.md}
RENAMED
|
@@ -9,10 +9,12 @@ specialization, see [[temp.deduct]]), or obtained from default template
|
|
| 9 |
arguments.
|
| 10 |
|
| 11 |
Each function template specialization instantiated from a template has
|
| 12 |
its own copy of any static variable.
|
| 13 |
|
|
|
|
|
|
|
| 14 |
``` cpp
|
| 15 |
template<class T> void f(T* p) {
|
| 16 |
static T s;
|
| 17 |
};
|
| 18 |
|
|
@@ -23,17 +25,21 @@ void g(int a, char* b) {
|
|
| 23 |
```
|
| 24 |
|
| 25 |
Here `f<int>(int*)` has a static variable `s` of type `int` and
|
| 26 |
`f<char*>(char**)` has a static variable `s` of type `char*`.
|
| 27 |
|
|
|
|
|
|
|
| 28 |
### Explicit template argument specification <a id="temp.arg.explicit">[[temp.arg.explicit]]</a>
|
| 29 |
|
| 30 |
Template arguments can be specified when referring to a function
|
| 31 |
template specialization by qualifying the function template name with
|
| 32 |
the list of *template-argument*s in the same way as *template-argument*s
|
| 33 |
are specified in uses of a class template specialization.
|
| 34 |
|
|
|
|
|
|
|
| 35 |
``` cpp
|
| 36 |
template<class T> void sort(Array<T>& v);
|
| 37 |
void f(Array<dcomplex>& cv, Array<int>& ci) {
|
| 38 |
sort<dcomplex>(cv); // sort(Array<dcomplex>&)
|
| 39 |
sort<int>(ci); // sort(Array<int>&)
|
|
@@ -49,10 +55,12 @@ void g(double d) {
|
|
| 49 |
int i = convert<int,double>(d); // int convert(double)
|
| 50 |
char c = convert<char,double>(d); // char convert(double)
|
| 51 |
}
|
| 52 |
```
|
| 53 |
|
|
|
|
|
|
|
| 54 |
A template argument list may be specified when referring to a
|
| 55 |
specialization of a function template
|
| 56 |
|
| 57 |
- when a function is called,
|
| 58 |
- when the address of a function is taken, when a function initializes a
|
|
@@ -72,26 +80,30 @@ deduction is done and fails, or in contexts where deduction is not done,
|
|
| 72 |
if a template argument list is specified and it, along with any default
|
| 73 |
template arguments, identifies a single function template
|
| 74 |
specialization, then the *template-id* is an lvalue for the function
|
| 75 |
template specialization.
|
| 76 |
|
|
|
|
|
|
|
| 77 |
``` cpp
|
| 78 |
template<class X, class Y> X f(Y);
|
| 79 |
template<class X, class Y, class ... Z> X g(Y);
|
| 80 |
void h() {
|
| 81 |
int i = f<int>(5.6); // Y is deduced to be double
|
| 82 |
int j = f(5.6); // ill-formed: X cannot be deduced
|
| 83 |
-
f<void>(f<int, bool>); // Y for outer f deduced to be
|
| 84 |
-
|
| 85 |
-
f<void>(f<int>); // ill-formed: f<int> does not denote a
|
| 86 |
-
// single function template specialization
|
| 87 |
int k = g<int>(5.6); // Y is deduced to be double, Z is deduced to an empty sequence
|
| 88 |
-
f<void>(g<int, bool>); // Y for outer f is deduced to be
|
| 89 |
-
//
|
| 90 |
}
|
| 91 |
```
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
An empty template argument list can be used to indicate that a given use
|
| 94 |
refers to a specialization of a function template even when a
|
| 95 |
non-template function ([[dcl.fct]]) is visible that would otherwise be
|
| 96 |
used. For example:
|
| 97 |
|
|
@@ -100,35 +112,43 @@ template <class T> int f(T); // #1
|
|
| 100 |
int f(int); // #2
|
| 101 |
int k = f(1); // uses #2
|
| 102 |
int l = f<>(1); // uses #1
|
| 103 |
```
|
| 104 |
|
|
|
|
|
|
|
| 105 |
Template arguments that are present shall be specified in the
|
| 106 |
declaration order of their corresponding *template-parameter*s. The
|
| 107 |
template argument list shall not specify more *template-argument*s than
|
| 108 |
there are corresponding *template-parameter*s unless one of the
|
| 109 |
*template-parameter*s is a template parameter pack.
|
| 110 |
|
|
|
|
|
|
|
| 111 |
``` cpp
|
| 112 |
template<class X, class Y, class Z> X f(Y,Z);
|
| 113 |
template<class ... Args> void f2();
|
| 114 |
void g() {
|
| 115 |
f<int,const char*,double>("aa",3.0);
|
| 116 |
f<int,const char*>("aa",3.0); // Z is deduced to be double
|
| 117 |
-
f<int>("aa",3.0); // Y is deduced to be const char*, and
|
| 118 |
-
// Z is deduced to be double
|
| 119 |
f("aa",3.0); // error: X cannot be deduced
|
| 120 |
f2<char, short, int, long>(); // OK
|
| 121 |
}
|
| 122 |
```
|
| 123 |
|
|
|
|
|
|
|
| 124 |
Implicit conversions (Clause [[conv]]) will be performed on a function
|
| 125 |
argument to convert it to the type of the corresponding function
|
| 126 |
parameter if the parameter type contains no *template-parameter*s that
|
| 127 |
-
participate in template argument deduction.
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
``` cpp
|
| 132 |
template<class T> void f(T);
|
| 133 |
|
| 134 |
class Complex {
|
|
@@ -138,15 +158,19 @@ class Complex {
|
|
| 138 |
void g() {
|
| 139 |
f<Complex>(1); // OK, means f<Complex>(Complex(1))
|
| 140 |
}
|
| 141 |
```
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
| 146 |
function name, there is no way to provide an explicit template argument
|
| 147 |
-
list for these function templates.
|
|
|
|
|
|
|
| 148 |
|
| 149 |
For simple function names, argument dependent lookup (
|
| 150 |
[[basic.lookup.argdep]]) applies even when the function name is not
|
| 151 |
visible within the scope of the call. This is because the call still has
|
| 152 |
the syntactic form of a function call ([[basic.lookup.unqual]]). But
|
|
@@ -156,10 +180,12 @@ template with that name visible at the point of the call. If no such
|
|
| 156 |
name is visible, the call is not syntactically well-formed and
|
| 157 |
argument-dependent lookup does not apply. If some such name is visible,
|
| 158 |
argument dependent lookup applies and additional function templates may
|
| 159 |
be found in other namespaces.
|
| 160 |
|
|
|
|
|
|
|
| 161 |
``` cpp
|
| 162 |
namespace A {
|
| 163 |
struct B { };
|
| 164 |
template<int X> void f(B);
|
| 165 |
}
|
|
@@ -167,37 +193,45 @@ namespace C {
|
|
| 167 |
template<class T> void f(T t);
|
| 168 |
}
|
| 169 |
void g(A::B b) {
|
| 170 |
f<3>(b); // ill-formed: not a function call
|
| 171 |
A::f<3>(b); // well-formed
|
| 172 |
-
C::f<3>(b);
|
| 173 |
-
// applies only to unqualified names
|
| 174 |
using C::f;
|
| 175 |
-
f<3>(b);
|
| 176 |
-
// A::f is found by argument dependent lookup
|
| 177 |
}
|
| 178 |
```
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
Template argument deduction can extend the sequence of template
|
| 181 |
arguments corresponding to a template parameter pack, even when the
|
| 182 |
sequence contains explicitly specified template arguments.
|
| 183 |
|
|
|
|
|
|
|
| 184 |
``` cpp
|
| 185 |
template<class ... Types> void f(Types ... values);
|
| 186 |
|
| 187 |
void g() {
|
| 188 |
f<int*, float*>(0, 0, 0); // Types is deduced to the sequence int*, float*, int
|
| 189 |
}
|
| 190 |
```
|
| 191 |
|
|
|
|
|
|
|
| 192 |
### Template argument deduction <a id="temp.deduct">[[temp.deduct]]</a>
|
| 193 |
|
| 194 |
When a function template specialization is referenced, all of the
|
| 195 |
template arguments shall have values. The values can be explicitly
|
| 196 |
specified or, in some cases, be deduced from the use or obtained from
|
| 197 |
default *template-argument*s.
|
| 198 |
|
|
|
|
|
|
|
| 199 |
``` cpp
|
| 200 |
void f(Array<dcomplex>& cv, Array<int>& ci) {
|
| 201 |
sort(cv); // calls sort(Array<dcomplex>&)
|
| 202 |
sort(ci); // calls sort(Array<int>&)
|
| 203 |
}
|
|
@@ -210,10 +244,12 @@ void g(double d) {
|
|
| 210 |
int i = convert<int>(d); // calls convert<int,double>(double)
|
| 211 |
int c = convert<char>(d); // calls convert<char,double>(double)
|
| 212 |
}
|
| 213 |
```
|
| 214 |
|
|
|
|
|
|
|
| 215 |
When an explicit template argument list is specified, the template
|
| 216 |
arguments must be compatible with the template parameter list and must
|
| 217 |
result in a valid function type as described below; otherwise type
|
| 218 |
deduction fails. Specifically, the following steps are performed when
|
| 219 |
evaluating an explicitly specified template argument list with respect
|
|
@@ -230,15 +266,20 @@ to a given function template:
|
|
| 230 |
[[temp.arg.nontype]], otherwise type deduction fails.
|
| 231 |
- The specified template argument values are substituted for the
|
| 232 |
corresponding template parameters as specified below.
|
| 233 |
|
| 234 |
After this substitution is performed, the function parameter type
|
| 235 |
-
adjustments described in [[dcl.fct]] are performed.
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
|
| 241 |
``` cpp
|
| 242 |
template <class T> void f(T t);
|
| 243 |
template <class X> void g(const X x);
|
| 244 |
template <class Z> void h(Z, Z*);
|
|
@@ -259,21 +300,26 @@ int main() {
|
|
| 259 |
// #5: function type is h(int, const int*)
|
| 260 |
h<const int>(1,0);
|
| 261 |
}
|
| 262 |
```
|
| 263 |
|
| 264 |
-
|
| 265 |
-
|
|
|
|
|
|
|
|
|
|
| 266 |
|
| 267 |
The resulting substituted and adjusted function type is used as the type
|
| 268 |
of the function template for template argument deduction. If a template
|
| 269 |
argument has not been deduced and its corresponding template parameter
|
| 270 |
has a default argument, the template argument is determined by
|
| 271 |
substituting the template arguments determined for preceding template
|
| 272 |
parameters into the default argument. If the substitution results in an
|
| 273 |
invalid type, as described above, type deduction fails.
|
| 274 |
|
|
|
|
|
|
|
| 275 |
``` cpp
|
| 276 |
template <class T, class U = double>
|
| 277 |
void f(T t = 0, U u = 0);
|
| 278 |
|
| 279 |
void g() {
|
|
@@ -283,10 +329,12 @@ void g() {
|
|
| 283 |
f<int>(); // f<int,double>(0,0)
|
| 284 |
f<int,char>(); // f<int,char>(0,0)
|
| 285 |
}
|
| 286 |
```
|
| 287 |
|
|
|
|
|
|
|
| 288 |
When all template arguments have been deduced or obtained from default
|
| 289 |
template arguments, all uses of template parameters in the template
|
| 290 |
parameter list of the template and the function type are replaced with
|
| 291 |
the corresponding deduced or default argument values. If the
|
| 292 |
substitution results in an invalid type, as described above, type
|
|
@@ -306,14 +354,18 @@ the function type and in template parameter declarations. The
|
|
| 306 |
expressions include not only constant expressions such as those that
|
| 307 |
appear in array bounds or as nontype template arguments but also general
|
| 308 |
expressions (i.e., non-constant expressions) inside `sizeof`,
|
| 309 |
`decltype`, and other contexts that allow non-constant expressions. The
|
| 310 |
substitution proceeds in lexical order and stops when a condition that
|
| 311 |
-
causes deduction to fail is encountered.
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
|
| 316 |
``` cpp
|
| 317 |
template <class T> struct A { using X = typename T::X; };
|
| 318 |
template <class T> typename T::X f(typename A<T>::X);
|
| 319 |
template <class T> void f(...) { }
|
|
@@ -324,22 +376,33 @@ void h() {
|
|
| 324 |
f<int>(0); // OK, substituting return type causes deduction to fail
|
| 325 |
g<int>(0); // error, substituting parameter type instantiates A<int>
|
| 326 |
}
|
| 327 |
```
|
| 328 |
|
|
|
|
|
|
|
| 329 |
If a substitution results in an invalid type or expression, type
|
| 330 |
deduction fails. An invalid type or expression is one that would be
|
| 331 |
ill-formed, with a diagnostic required, if written using the substituted
|
| 332 |
-
arguments.
|
| 333 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
Only invalid types and expressions in the immediate context of the
|
| 335 |
function type and its template parameter types can result in a deduction
|
| 336 |
-
failure.
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
|
| 342 |
``` cpp
|
| 343 |
struct X { };
|
| 344 |
struct Y {
|
| 345 |
Y(X){}
|
|
@@ -350,36 +413,47 @@ X f(Y, Y); // #2
|
|
| 350 |
|
| 351 |
X x1, x2;
|
| 352 |
X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
|
| 353 |
```
|
| 354 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
Type deduction may fail for the following reasons:
|
| 356 |
|
| 357 |
- Attempting to instantiate a pack expansion containing multiple
|
| 358 |
parameter packs of differing lengths.
|
| 359 |
- Attempting to create an array with an element type that is `void`, a
|
| 360 |
function type, a reference type, or an abstract class type, or
|
| 361 |
attempting to create an array with a size that is zero or negative.
|
|
|
|
| 362 |
``` cpp
|
| 363 |
template <class T> int f(T[5]);
|
| 364 |
int I = f<int>(0);
|
| 365 |
int j = f<void>(0); // invalid array
|
| 366 |
```
|
|
|
|
|
|
|
| 367 |
- Attempting to use a type that is not a class or enumeration type in a
|
| 368 |
qualified name.
|
|
|
|
| 369 |
``` cpp
|
| 370 |
template <class T> int f(typename T::B*);
|
| 371 |
int i = f<int>(0);
|
| 372 |
```
|
|
|
|
|
|
|
| 373 |
- Attempting to use a type in a *nested-name-specifier* of a
|
| 374 |
*qualified-id* when that type does not contain the specified member,
|
| 375 |
or
|
| 376 |
- the specified member is not a type where a type is required, or
|
| 377 |
- the specified member is not a template where a template is required,
|
| 378 |
or
|
| 379 |
- the specified member is not a non-type where a non-type is required.
|
| 380 |
|
|
|
|
| 381 |
``` cpp
|
| 382 |
template <int I> struct X { };
|
| 383 |
template <template <class T> class> struct Z { };
|
| 384 |
template <class T> void f(typename T::Y*){}
|
| 385 |
template <class T> void g(X<T::N>*){}
|
|
@@ -399,82 +473,124 @@ Type deduction may fail for the following reasons:
|
|
| 399 |
f<B>(0); // The Y member of B is not a type
|
| 400 |
g<C>(0); // The N member of C is not a non-type
|
| 401 |
h<D>(0); // The TT member of D is not a template
|
| 402 |
}
|
| 403 |
```
|
|
|
|
|
|
|
| 404 |
- Attempting to create a pointer to reference type.
|
| 405 |
- Attempting to create a reference to `void`.
|
| 406 |
- Attempting to create “pointer to member of `T`” when `T` is not a
|
| 407 |
class type.
|
|
|
|
| 408 |
``` cpp
|
| 409 |
template <class T> int f(int T::*);
|
| 410 |
int i = f<int>(0);
|
| 411 |
```
|
|
|
|
|
|
|
| 412 |
- Attempting to give an invalid type to a non-type template parameter.
|
|
|
|
| 413 |
``` cpp
|
| 414 |
template <class T, T> struct S {};
|
| 415 |
template <class T> int f(S<T, T()>*);
|
| 416 |
struct X {};
|
| 417 |
int i0 = f<X>(0);
|
| 418 |
```
|
|
|
|
|
|
|
| 419 |
- Attempting to perform an invalid conversion in either a template
|
| 420 |
argument expression, or an expression used in the function
|
| 421 |
declaration.
|
|
|
|
| 422 |
``` cpp
|
| 423 |
template <class T, T*> int f(int);
|
| 424 |
int i2 = f<int,1>(0); // can't conv 1 to int*
|
| 425 |
```
|
|
|
|
|
|
|
| 426 |
- Attempting to create a function type in which a parameter has a type
|
| 427 |
of `void`, or in which the return type is a function type or array
|
| 428 |
type.
|
| 429 |
- Attempting to create a function type in which a parameter type or the
|
| 430 |
return type is an abstract class type ([[class.abstract]]).
|
| 431 |
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
|
|
|
|
|
|
|
|
|
| 438 |
|
| 439 |
``` cpp
|
| 440 |
template <int> int f(int);
|
| 441 |
template <signed char> int f(int);
|
| 442 |
-
int i1 = f<
|
| 443 |
-
int i2 = f<
|
| 444 |
```
|
| 445 |
|
|
|
|
|
|
|
| 446 |
#### Deducing template arguments from a function call <a id="temp.deduct.call">[[temp.deduct.call]]</a>
|
| 447 |
|
| 448 |
Template argument deduction is done by comparing each function template
|
| 449 |
-
parameter type (call it `P`)
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
|
| 459 |
``` cpp
|
| 460 |
template<class T> void f(std::initializer_list<T>);
|
| 461 |
f({1,2,3}); // T deduced to int
|
| 462 |
f({1,"asdf"}); // error: T deduced to both int and const char*
|
| 463 |
|
| 464 |
template<class T> void g(T);
|
| 465 |
g({1,2,3}); // error: no argument deduced for T
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 466 |
```
|
| 467 |
|
|
|
|
|
|
|
| 468 |
For a function parameter pack that occurs at the end of the
|
| 469 |
-
*parameter-declaration-list*,
|
| 470 |
-
the call
|
| 471 |
-
function parameter pack
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
non-deduced context (
|
| 475 |
-
pack is never deduced.
|
|
|
|
|
|
|
| 476 |
|
| 477 |
``` cpp
|
| 478 |
template<class ... Types> void f(Types& ...);
|
| 479 |
template<class T1, class ... Types> void g(T1, Types ...);
|
| 480 |
template<class T1, class ... Types> void g1(Types ..., T1);
|
|
@@ -483,112 +599,185 @@ void h(int x, float& y) {
|
|
| 483 |
const int z = x;
|
| 484 |
f(x, y, z); // Types is deduced to int, float, const int
|
| 485 |
g(x, y, z); // T1 is deduced to int; Types is deduced to float, int
|
| 486 |
g1(x, y, z); // error: Types is not deduced
|
| 487 |
g1<int, int, int>(x, y, z); // OK, no deduction occurs
|
| 488 |
-
|
| 489 |
}
|
| 490 |
```
|
| 491 |
|
|
|
|
|
|
|
| 492 |
If `P` is not a reference type:
|
| 493 |
|
| 494 |
- If `A` is an array type, the pointer type produced by the
|
| 495 |
array-to-pointer standard conversion ([[conv.array]]) is used in
|
| 496 |
place of `A` for type deduction; otherwise,
|
| 497 |
- If `A` is a function type, the pointer type produced by the
|
| 498 |
function-to-pointer standard conversion ([[conv.func]]) is used in
|
| 499 |
place of `A` for type deduction; otherwise,
|
| 500 |
-
- If `A` is a cv-qualified type, the top
|
| 501 |
type are ignored for type deduction.
|
| 502 |
|
| 503 |
-
If `P` is a cv-qualified type, the top
|
| 504 |
are ignored for type deduction. If `P` is a reference type, the type
|
| 505 |
-
referred to by `P` is used for type deduction.
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 509 |
|
| 510 |
``` cpp
|
| 511 |
-
template <class T> int f(T&&);
|
| 512 |
template <class T> int g(const T&&);
|
| 513 |
int i;
|
| 514 |
int n1 = f(i); // calls f<int&>(int&)
|
| 515 |
int n2 = f(0); // calls f<int>(int&&)
|
| 516 |
int n3 = g(i); // error: would call g<int>(const int&&), which
|
| 517 |
// would bind an rvalue reference to an lvalue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 518 |
```
|
| 519 |
|
|
|
|
|
|
|
| 520 |
In general, the deduction process attempts to find template argument
|
| 521 |
values that will make the deduced `A` identical to `A` (after the type
|
| 522 |
`A` is transformed as described above). However, there are three cases
|
| 523 |
that allow a difference:
|
| 524 |
|
| 525 |
- If the original `P` is a reference type, the deduced `A` (i.e., the
|
| 526 |
type referred to by the reference) can be more cv-qualified than the
|
| 527 |
transformed `A`.
|
| 528 |
- The transformed `A` can be another pointer or pointer to member type
|
| 529 |
-
that can be converted to the deduced `A` via a
|
| 530 |
-
conversion ([[conv.
|
|
|
|
| 531 |
- If `P` is a class and `P` has the form *simple-template-id*, then the
|
| 532 |
transformed `A` can be a derived class of the deduced `A`. Likewise,
|
| 533 |
if `P` is a pointer to a class of the form *simple-template-id*, the
|
| 534 |
transformed `A` can be a pointer to a derived class pointed to by the
|
| 535 |
deduced `A`.
|
| 536 |
|
| 537 |
-
as specified in [[temp.arg.explicit]], implicit conversions will be
|
| 538 |
-
performed on a function argument to convert it to the type of the
|
| 539 |
-
corresponding function parameter if the parameter contains no
|
| 540 |
-
*template-parameter*s that participate in template argument deduction.
|
| 541 |
-
Such conversions are also allowed, in addition to the ones described in
|
| 542 |
-
the preceding list.
|
| 543 |
-
|
| 544 |
These alternatives are considered only if type deduction would otherwise
|
| 545 |
fail. If they yield more than one possible deduced `A`, the type
|
| 546 |
-
deduction fails.
|
| 547 |
-
function parameters of a function template, or is used only in a
|
| 548 |
-
non-deduced context, its corresponding *template-argument* cannot be
|
| 549 |
-
deduced from a function call and the *template-argument* must be
|
| 550 |
-
explicitly specified.
|
| 551 |
|
| 552 |
-
|
| 553 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 554 |
|
| 555 |
- If the argument is an overload set containing one or more function
|
| 556 |
templates, the parameter is treated as a non-deduced context.
|
| 557 |
- If the argument is an overload set (not containing function
|
| 558 |
templates), trial argument deduction is attempted using each of the
|
| 559 |
members of the set. If deduction succeeds for only one of the overload
|
| 560 |
set members, that member is used as the argument value for the
|
| 561 |
deduction. If deduction succeeds for more than one member of the
|
| 562 |
overload set the parameter is treated as a non-deduced context.
|
|
|
|
|
|
|
|
|
|
| 563 |
``` cpp
|
| 564 |
-
|
| 565 |
-
// parameter is a deduced context.
|
| 566 |
template <class T> int f(T (*p)(T));
|
| 567 |
int g(int);
|
| 568 |
int g(char);
|
| 569 |
int i = f(g); // calls f(int (*)(int))
|
| 570 |
```
|
| 571 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 572 |
``` cpp
|
| 573 |
-
|
| 574 |
-
// non-deduced context.
|
| 575 |
template <class T> int f(T, T (*p)(T));
|
| 576 |
int g(int);
|
| 577 |
char g(char);
|
| 578 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 579 |
```
|
| 580 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 581 |
``` cpp
|
| 582 |
-
|
| 583 |
-
// parameter to be a non-deduced context.
|
| 584 |
template <class T> int f(T, T (*p)(T));
|
| 585 |
char g(char);
|
| 586 |
template <class T> T g(T);
|
| 587 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 588 |
```
|
| 589 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 590 |
#### Deducing template arguments taking the address of a function template <a id="temp.deduct.funcaddr">[[temp.deduct.funcaddr]]</a>
|
| 591 |
|
| 592 |
Template arguments can be deduced from the type specified when taking
|
| 593 |
the address of an overloaded function ([[over.over]]). The function
|
| 594 |
template’s function type and the specified type are used as the types of
|
|
@@ -618,23 +807,28 @@ If `A` is not a reference type:
|
|
| 618 |
array-to-pointer standard conversion ([[conv.array]]) is used in
|
| 619 |
place of `P` for type deduction; otherwise,
|
| 620 |
- If `P` is a function type, the pointer type produced by the
|
| 621 |
function-to-pointer standard conversion ([[conv.func]]) is used in
|
| 622 |
place of `P` for type deduction; otherwise,
|
| 623 |
-
- If `P` is a cv-qualified type, the top
|
| 624 |
type are ignored for type deduction.
|
| 625 |
|
| 626 |
-
If `A` is a cv-qualified type, the top
|
| 627 |
are ignored for type deduction. If `A` is a reference type, the type
|
| 628 |
referred to by `A` is used for type deduction.
|
| 629 |
|
| 630 |
In general, the deduction process attempts to find template argument
|
| 631 |
values that will make the deduced `A` identical to `A`. However, there
|
| 632 |
-
are
|
| 633 |
|
| 634 |
- If the original `A` is a reference type, `A` can be more cv-qualified
|
| 635 |
than the deduced `A` (i.e., the type referred to by the reference)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 636 |
- The deduced `A` can be another pointer or pointer to member type that
|
| 637 |
can be converted to `A` via a qualification conversion.
|
| 638 |
|
| 639 |
These alternatives are considered only if type deduction would otherwise
|
| 640 |
fail. If they yield more than one possible deduced `A`, the type
|
|
@@ -646,36 +840,44 @@ process is used to determine the deduced template argument values:
|
|
| 646 |
|
| 647 |
If `A` is a type
|
| 648 |
|
| 649 |
and `P` is a type
|
| 650 |
|
| 651 |
-
|
| 652 |
-
respectively for type deduction.
|
|
|
|
|
|
|
| 653 |
|
| 654 |
``` cpp
|
| 655 |
struct A {
|
| 656 |
template <class T> operator T***();
|
| 657 |
};
|
| 658 |
A a;
|
| 659 |
const int * const * const * p1 = a; // T is deduced as int, not const int
|
| 660 |
```
|
| 661 |
|
|
|
|
|
|
|
| 662 |
#### Deducing template arguments during partial ordering <a id="temp.deduct.partial">[[temp.deduct.partial]]</a>
|
| 663 |
|
| 664 |
Template argument deduction is done by comparing certain types
|
| 665 |
associated with the two function templates being compared.
|
| 666 |
|
| 667 |
Two sets of types are used to determine the partial ordering. For each
|
| 668 |
of the templates involved there is the original function type and the
|
| 669 |
-
transformed function type.
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 677 |
|
| 678 |
The types used to determine the ordering depend on the context in which
|
| 679 |
the partial ordering is done:
|
| 680 |
|
| 681 |
- In the context of a function call, the types used are those function
|
|
@@ -685,11 +887,13 @@ the partial ordering is done:
|
|
| 685 |
- In other contexts ([[temp.func.order]]) the function template’s
|
| 686 |
function type is used.
|
| 687 |
|
| 688 |
Each type nominated above from the parameter template and the
|
| 689 |
corresponding type from the argument template are used as the types of
|
| 690 |
-
`P` and `A`.
|
|
|
|
|
|
|
| 691 |
|
| 692 |
Before the partial ordering is done, certain transformations are
|
| 693 |
performed on the types used for partial ordering:
|
| 694 |
|
| 695 |
- If `P` is a reference type, `P` is replaced by the type referred to.
|
|
@@ -706,70 +910,88 @@ Remove any top-level cv-qualifiers:
|
|
| 706 |
- If `P` is a cv-qualified type, `P` is replaced by the cv-unqualified
|
| 707 |
version of `P`.
|
| 708 |
- If `A` is a cv-qualified type, `A` is replaced by the cv-unqualified
|
| 709 |
version of `A`.
|
| 710 |
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
deduction succeeds for a given type,
|
| 720 |
-
is considered to be at least as
|
| 721 |
-
parameter template.
|
|
|
|
|
|
|
| 722 |
|
| 723 |
``` cpp
|
| 724 |
template<class... Args> void f(Args... args); // #1
|
| 725 |
template<class T1, class... Args> void f(T1 a1, Args... args); // #2
|
| 726 |
template<class T1, class T2> void f(T1 a1, T2 a2); // #3
|
| 727 |
|
| 728 |
f(); // calls #1
|
| 729 |
f(1, 2, 3); // calls #2
|
| 730 |
-
f(1, 2);
|
| 731 |
-
|
| 732 |
```
|
| 733 |
|
|
|
|
|
|
|
| 734 |
If, for a given type, deduction succeeds in both directions (i.e., the
|
| 735 |
types are identical after the transformations above) and both `P` and
|
| 736 |
`A` were reference types (before being replaced with the type referred
|
| 737 |
to above):
|
| 738 |
|
| 739 |
- if the type from the argument template was an lvalue reference and the
|
| 740 |
-
type from the parameter template was not, the
|
| 741 |
-
considered to be
|
|
|
|
| 742 |
- if the type from the argument template is more cv-qualified than the
|
| 743 |
-
type from the parameter template (as described above), the
|
| 744 |
-
type is considered to be
|
| 745 |
-
|
| 746 |
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 753 |
|
| 754 |
In most cases, all template parameters must have values in order for
|
| 755 |
deduction to succeed, but for partial ordering purposes a template
|
| 756 |
parameter may remain without a value provided it is not used in the
|
| 757 |
-
types being used for partial ordering.
|
| 758 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 759 |
|
| 760 |
``` cpp
|
| 761 |
template <class T> T f(int); // #1
|
| 762 |
template <class T, class U> T f(U); // #2
|
| 763 |
void g() {
|
| 764 |
f<int>(1); // calls #1
|
| 765 |
}
|
| 766 |
```
|
| 767 |
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 771 |
|
| 772 |
``` cpp
|
| 773 |
template<class ...> struct Tuple { };
|
| 774 |
template<class ... Types> void g(Tuple<Types ...>); // #1
|
| 775 |
template<class T1, class ... Types> void g(Tuple<T1, Types ...>); // #2
|
|
@@ -779,10 +1001,12 @@ g(Tuple<>()); // calls #1
|
|
| 779 |
g(Tuple<int, float>()); // calls #2
|
| 780 |
g(Tuple<int, float&>()); // calls #3
|
| 781 |
g(Tuple<int>()); // calls #3
|
| 782 |
```
|
| 783 |
|
|
|
|
|
|
|
| 784 |
#### Deducing template arguments from a type <a id="temp.deduct.type">[[temp.deduct.type]]</a>
|
| 785 |
|
| 786 |
Template arguments can be deduced in several different contexts, but in
|
| 787 |
each case a type that is specified in terms of template parameters (call
|
| 788 |
it `P`) is compared with an actual type (call it `A`), and an attempt is
|
|
@@ -797,11 +1021,12 @@ In some cases, the deduction is done using a single set of types `P` and
|
|
| 797 |
deduced template argument values are then combined. If type deduction
|
| 798 |
cannot be done for any `P/A` pair, or if for any pair the deduction
|
| 799 |
leads to more than one possible set of deduced values, or if different
|
| 800 |
pairs yield different deduced values, or if any template argument
|
| 801 |
remains neither deduced nor explicitly specified, template argument
|
| 802 |
-
deduction fails.
|
|
|
|
| 803 |
|
| 804 |
A given type `P` can be composed from a number of other types,
|
| 805 |
templates, and non-type values:
|
| 806 |
|
| 807 |
- A function type includes the types of each of the function parameters
|
|
@@ -822,10 +1047,15 @@ In certain contexts, however, the value does not participate in type
|
|
| 822 |
deduction, but instead uses the values of template arguments that were
|
| 823 |
either deduced elsewhere or explicitly specified. If a template
|
| 824 |
parameter is used only in non-deduced contexts and is not explicitly
|
| 825 |
specified, template argument deduction fails.
|
| 826 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 827 |
The non-deduced contexts are:
|
| 828 |
|
| 829 |
- The *nested-name-specifier* of a type that was specified using a
|
| 830 |
*qualified-id*.
|
| 831 |
- The *expression* of a *decltype-specifier*.
|
|
@@ -843,35 +1073,43 @@ The non-deduced contexts are:
|
|
| 843 |
- no function matches the function parameter type, or
|
| 844 |
- the set of functions supplied as an argument contains one or more
|
| 845 |
function templates.
|
| 846 |
- A function parameter for which the associated argument is an
|
| 847 |
initializer list ([[dcl.init.list]]) but the parameter does not have
|
| 848 |
-
|
| 849 |
-
|
|
|
|
| 850 |
``` cpp
|
| 851 |
template<class T> void g(T);
|
| 852 |
g({1,2,3}); // error: no argument deduced for T
|
| 853 |
```
|
|
|
|
|
|
|
| 854 |
- A function parameter pack that does not occur at the end of the
|
| 855 |
*parameter-declaration-list*.
|
| 856 |
|
| 857 |
When a type name is specified in a way that includes a non-deduced
|
| 858 |
context, all of the types that comprise that type name are also
|
| 859 |
non-deduced. However, a compound type can include both deduced and
|
| 860 |
-
non-deduced types.
|
|
|
|
|
|
|
| 861 |
`T2` are non-deduced. Likewise, if a type is specified as
|
| 862 |
`A<I+J>::X<T>`, `I`, `J`, and `T` are non-deduced. If a type is
|
| 863 |
specified as `void` `f(typename` `A<T>::B,` `A<T>)`, the `T` in
|
| 864 |
-
`A<T>::B` is non-deduced but the `T` in `A<T>` is
|
|
|
|
|
|
|
|
|
|
| 865 |
|
| 866 |
Here is an example in which different parameter/argument pairs produce
|
| 867 |
inconsistent template argument deductions:
|
| 868 |
|
| 869 |
``` cpp
|
| 870 |
-
template<class T> void f(T x, T y) {
|
| 871 |
-
struct A {
|
| 872 |
-
struct B : A {
|
| 873 |
void g(A a, B b) {
|
| 874 |
f(a,b); // error: T could be A or B
|
| 875 |
f(b,a); // error: T could be A or B
|
| 876 |
f(a,a); // OK: T is A
|
| 877 |
f(b,b); // OK: T is B
|
|
@@ -922,10 +1160,12 @@ void t() {
|
|
| 922 |
f(d); // calls f(B<int>&)
|
| 923 |
f(d2); // calls f(B<int>&)
|
| 924 |
}
|
| 925 |
```
|
| 926 |
|
|
|
|
|
|
|
| 927 |
A template type argument `T`, a template template argument `TT` or a
|
| 928 |
template non-type argument `i` can be deduced if `P` and `A` have one of
|
| 929 |
the following forms:
|
| 930 |
|
| 931 |
``` cpp
|
|
@@ -954,20 +1194,20 @@ template-name<i> (where template-name refers to a class template)
|
|
| 954 |
TT<T>
|
| 955 |
TT<i>
|
| 956 |
TT<>
|
| 957 |
```
|
| 958 |
|
| 959 |
-
where `(T)` represents a
|
| 960 |
-
parameter type contains a `T`, and `()` represents a
|
| 961 |
-
|
| 962 |
`<T>` represents template argument lists where at least one argument
|
| 963 |
contains a `T`, `<i>` represents template argument lists where at least
|
| 964 |
one argument contains an `i` and `<>` represents template argument lists
|
| 965 |
where no argument contains a `T` or an `i`.
|
| 966 |
|
| 967 |
If `P` has a form that contains `<T>` or `<i>`, then each argument Pᵢ of
|
| 968 |
-
the respective template argument list `P` is compared with the
|
| 969 |
corresponding argument Aᵢ of the corresponding template argument list of
|
| 970 |
`A`. If the template argument list of `P` contains a pack expansion that
|
| 971 |
is not the last template argument, the entire template argument list is
|
| 972 |
a non-deduced context. If `Pᵢ` is a pack expansion, then the pattern of
|
| 973 |
`Pᵢ` is compared with each remaining argument in the template argument
|
|
@@ -979,10 +1219,12 @@ pack expansion:
|
|
| 979 |
- if `P` does not contain a template argument corresponding to `Aᵢ` then
|
| 980 |
`Aᵢ` is ignored;
|
| 981 |
- otherwise, if `Pᵢ` is not a pack expansion, template argument
|
| 982 |
deduction fails.
|
| 983 |
|
|
|
|
|
|
|
| 984 |
``` cpp
|
| 985 |
template<class T1, class... Z> class S; // #1
|
| 986 |
template<class T1, class... Z> class S<T1, const Z&...> { }; // #2
|
| 987 |
template<class T1, class T2> class S<T1, const T2&> { }; // #3
|
| 988 |
S<int, const int&> s; // both #2 and #3 match; #3 is more specialized
|
|
@@ -991,24 +1233,30 @@ template<class T, class... U> struct A { }; // #1
|
|
| 991 |
template<class T1, class T2, class... U> struct A<T1, T2*, U...> { }; // #2
|
| 992 |
template<class T1, class T2> struct A<T1, T2> { }; // #3
|
| 993 |
template struct A<int, int*>; // selects #2
|
| 994 |
```
|
| 995 |
|
|
|
|
|
|
|
| 996 |
Similarly, if `P` has a form that contains `(T)`, then each parameter
|
| 997 |
-
type `Pᵢ` of the respective
|
| 998 |
-
with the corresponding parameter type `Aᵢ` of the corresponding
|
| 999 |
-
|
| 1000 |
originated from deduction when taking the address of a function
|
| 1001 |
template ([[temp.deduct.funcaddr]]) or when deducing template arguments
|
| 1002 |
from a function declaration ([[temp.deduct.decl]]) and `Pᵢ` and `Aᵢ`
|
| 1003 |
-
are parameters of the top-level
|
| 1004 |
-
respectively, `Pᵢ` is adjusted if it is
|
| 1005 |
-
|
| 1006 |
-
|
| 1007 |
-
|
| 1008 |
-
|
| 1009 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1010 |
|
| 1011 |
``` cpp
|
| 1012 |
template <class T> void f(T&&);
|
| 1013 |
template <> void f(int&) { } // #1
|
| 1014 |
template <> void f(int&&) { } // #2
|
|
@@ -1016,32 +1264,40 @@ void g(int i) {
|
|
| 1016 |
f(i); // calls f<int&>(int&), i.e., #1
|
| 1017 |
f(0); // calls f<int>(int&&), i.e., #2
|
| 1018 |
}
|
| 1019 |
```
|
| 1020 |
|
|
|
|
|
|
|
| 1021 |
If the *parameter-declaration* corresponding to `Pᵢ` is a function
|
| 1022 |
parameter pack, then the type of its *declarator-id* is compared with
|
| 1023 |
-
each remaining parameter type in the
|
| 1024 |
comparison deduces template arguments for subsequent positions in the
|
| 1025 |
template parameter packs expanded by the function parameter pack. During
|
| 1026 |
partial ordering ([[temp.deduct.partial]]), if `Aᵢ` was originally a
|
| 1027 |
function parameter pack:
|
| 1028 |
|
| 1029 |
- if `P` does not contain a function parameter type corresponding to
|
| 1030 |
`Aᵢ` then `Aᵢ` is ignored;
|
| 1031 |
- otherwise, if `Pᵢ` is not a function parameter pack, template argument
|
| 1032 |
deduction fails.
|
| 1033 |
|
|
|
|
|
|
|
| 1034 |
``` cpp
|
| 1035 |
template<class T, class... U> void f(T*, U...) { } // #1
|
| 1036 |
template<class T> void f(T) { } // #2
|
| 1037 |
template void f(int*); // selects #1
|
| 1038 |
```
|
| 1039 |
|
|
|
|
|
|
|
| 1040 |
These forms can be used in the same way as `T` is for further
|
| 1041 |
composition of types.
|
| 1042 |
|
|
|
|
|
|
|
| 1043 |
``` cpp
|
| 1044 |
X<int> (*)(char[6])
|
| 1045 |
```
|
| 1046 |
|
| 1047 |
is of the form
|
|
@@ -1056,22 +1312,67 @@ which is a variant of
|
|
| 1056 |
type (*)(T)
|
| 1057 |
```
|
| 1058 |
|
| 1059 |
where type is `X<int>` and `T` is `char[6]`.
|
| 1060 |
|
|
|
|
|
|
|
| 1061 |
Template arguments cannot be deduced from function arguments involving
|
| 1062 |
constructs other than the ones specified above.
|
| 1063 |
|
| 1064 |
-
|
| 1065 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1066 |
|
| 1067 |
``` cpp
|
| 1068 |
-
template<
|
| 1069 |
-
|
| 1070 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1071 |
```
|
| 1072 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1073 |
Except for reference and pointer types, a major array bound is not part
|
| 1074 |
of a function parameter type and cannot be deduced from an argument:
|
| 1075 |
|
| 1076 |
``` cpp
|
| 1077 |
template<int i> void f1(int a[10][i]);
|
|
@@ -1086,17 +1387,23 @@ void g() {
|
|
| 1086 |
f2<10>(v); // OK
|
| 1087 |
f3(v); // OK: i deduced to be 10
|
| 1088 |
}
|
| 1089 |
```
|
| 1090 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1091 |
If, in the declaration of a function template with a non-type template
|
| 1092 |
parameter, the non-type template parameter is used in a subexpression in
|
| 1093 |
the function parameter list, the expression is a non-deduced context as
|
| 1094 |
specified above.
|
| 1095 |
|
|
|
|
|
|
|
| 1096 |
``` cpp
|
| 1097 |
-
template <int i> class A {
|
| 1098 |
template <int i> void g(A<i+1>);
|
| 1099 |
template <int i> void f(A<i>, A<i+1>);
|
| 1100 |
void k() {
|
| 1101 |
A<1> a1;
|
| 1102 |
A<2> a2;
|
|
@@ -1104,10 +1411,16 @@ void k() {
|
|
| 1104 |
g<0>(a1); // OK
|
| 1105 |
f(a1, a2); // OK
|
| 1106 |
}
|
| 1107 |
```
|
| 1108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1109 |
Template parameters do not participate in template argument deduction if
|
| 1110 |
they are used only in non-deduced contexts. For example,
|
| 1111 |
|
| 1112 |
``` cpp
|
| 1113 |
template<int i, typename T>
|
|
@@ -1116,23 +1429,26 @@ T deduce(typename A<T>::X x, // T is not deduced here
|
|
| 1116 |
typename B<i>::Y y); // i is not deduced here
|
| 1117 |
A<int> a;
|
| 1118 |
B<77> b;
|
| 1119 |
|
| 1120 |
int x = deduce<77>(a.xm, 62, b.ym);
|
| 1121 |
-
// T is deduced to be int, a.xm must be convertible to
|
| 1122 |
-
//
|
| 1123 |
-
// i is explicitly specified to be 77, b.ym must be convertible
|
| 1124 |
-
// to B<77>::Y
|
| 1125 |
```
|
| 1126 |
|
| 1127 |
-
|
| 1128 |
-
|
| 1129 |
-
|
| 1130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1131 |
|
| 1132 |
``` cpp
|
| 1133 |
-
template<int i> class A {
|
| 1134 |
template<short s> void f(A<s>);
|
| 1135 |
void k1() {
|
| 1136 |
A<1> a;
|
| 1137 |
f(a); // error: deduction fails for conversion from int to short
|
| 1138 |
f<1>(a); // OK
|
|
@@ -1144,13 +1460,17 @@ void k2() {
|
|
| 1144 |
B<1> b;
|
| 1145 |
g(b); // OK: cv-qualifiers are ignored on template parameter types
|
| 1146 |
}
|
| 1147 |
```
|
| 1148 |
|
|
|
|
|
|
|
| 1149 |
A *template-argument* can be deduced from a function, pointer to
|
| 1150 |
function, or pointer to member function type.
|
| 1151 |
|
|
|
|
|
|
|
| 1152 |
``` cpp
|
| 1153 |
template<class T> void f(void(*)(T,int));
|
| 1154 |
template<class T> void foo(T,int);
|
| 1155 |
void g(int,int);
|
| 1156 |
void g(char,int);
|
|
@@ -1162,37 +1482,49 @@ int m() {
|
|
| 1162 |
f(&h); // OK: void h(char,int) is a unique match
|
| 1163 |
f(&foo); // error: type deduction fails because foo is a template
|
| 1164 |
}
|
| 1165 |
```
|
| 1166 |
|
|
|
|
|
|
|
| 1167 |
A template *type-parameter* cannot be deduced from the type of a
|
| 1168 |
function default argument.
|
| 1169 |
|
|
|
|
|
|
|
| 1170 |
``` cpp
|
| 1171 |
template <class T> void f(T = 5, T = 7);
|
| 1172 |
void g() {
|
| 1173 |
f(1); // OK: call f<int>(1,7)
|
| 1174 |
f(); // error: cannot deduce T
|
| 1175 |
f<int>(); // OK: call f<int>(5,7)
|
| 1176 |
}
|
| 1177 |
```
|
| 1178 |
|
|
|
|
|
|
|
| 1179 |
The *template-argument* corresponding to a template *template-parameter*
|
| 1180 |
is deduced from the type of the *template-argument* of a class template
|
| 1181 |
specialization used in the argument list of a function call.
|
| 1182 |
|
|
|
|
|
|
|
| 1183 |
``` cpp
|
| 1184 |
template <template <class T> class X> struct A { };
|
| 1185 |
template <template <class T> class X> void f(A<X>) { }
|
| 1186 |
template<class T> struct B { };
|
| 1187 |
A<B> ab;
|
| 1188 |
f(ab); // calls f(A<B>)
|
| 1189 |
```
|
| 1190 |
|
| 1191 |
-
|
|
|
|
|
|
|
| 1192 |
[[temp.variadic]]) can deduce zero or more arguments for each parameter
|
| 1193 |
-
pack.
|
|
|
|
|
|
|
| 1194 |
|
| 1195 |
``` cpp
|
| 1196 |
template<class> struct X { };
|
| 1197 |
template<class R, class ... ArgTypes> struct X<R(int, ArgTypes ...)> { };
|
| 1198 |
template<class ... Types> struct Y { };
|
|
@@ -1208,10 +1540,12 @@ Y<> y1; // use primary template; Types is empty
|
|
| 1208 |
Y<int&, float&, double&> y2; // uses partial specialization; T is int&, Types contains float, double
|
| 1209 |
Y<int, float, double> y3; // uses primary template; Types contains int, float, double
|
| 1210 |
int fv = f(g); // OK; Types contains int, float
|
| 1211 |
```
|
| 1212 |
|
|
|
|
|
|
|
| 1213 |
#### Deducing template arguments from a function declaration <a id="temp.deduct.decl">[[temp.deduct.decl]]</a>
|
| 1214 |
|
| 1215 |
In a declaration whose *declarator-id* refers to a specialization of a
|
| 1216 |
function template, template argument deduction is performed to identify
|
| 1217 |
the specialization to which the declaration refers. Specifically, this
|
|
@@ -1244,18 +1578,21 @@ instantiate a function template specialization that can be invoked with
|
|
| 1244 |
the call arguments. For each function template, if the argument
|
| 1245 |
deduction and checking succeeds, the *template-argument*s (deduced
|
| 1246 |
and/or explicit) are used to synthesize the declaration of a single
|
| 1247 |
function template specialization which is added to the candidate
|
| 1248 |
functions set to be used in overload resolution. If, for a given
|
| 1249 |
-
function template, argument deduction fails
|
|
|
|
| 1250 |
to the set of candidate functions for that template. The complete set of
|
| 1251 |
candidate functions includes all the synthesized declarations and all of
|
| 1252 |
the non-template overloaded functions of the same name. The synthesized
|
| 1253 |
declarations are treated like any other functions in the remainder of
|
| 1254 |
overload resolution, except as explicitly noted in
|
| 1255 |
[[over.match.best]].[^9]
|
| 1256 |
|
|
|
|
|
|
|
| 1257 |
``` cpp
|
| 1258 |
template<class T> T max(T a, T b) { return a>b?a:b; }
|
| 1259 |
|
| 1260 |
void f(int a, int b, char c, char d) {
|
| 1261 |
int m1 = max(a,b); // max(int a, int b)
|
|
@@ -1272,24 +1609,32 @@ int max(int,int);
|
|
| 1272 |
|
| 1273 |
to the example above would resolve the third call, by providing a
|
| 1274 |
function that could be called for `max(a,c)` after using the standard
|
| 1275 |
conversion of `char` to `int` for `c`.
|
| 1276 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1277 |
Here is an example involving conversions on a function argument involved
|
| 1278 |
in *template-argument* deduction:
|
| 1279 |
|
| 1280 |
``` cpp
|
| 1281 |
-
template<class T> struct B {
|
| 1282 |
-
template<class T> struct D : public B<T> {
|
| 1283 |
template<class T> void f(B<T>&);
|
| 1284 |
|
| 1285 |
void g(B<int>& bi, D<int>& di) {
|
| 1286 |
f(bi); // f(bi)
|
| 1287 |
f(di); // f((B<int>&)di)
|
| 1288 |
}
|
| 1289 |
```
|
| 1290 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1291 |
Here is an example involving conversions on a function argument not
|
| 1292 |
involved in *template-parameter* deduction:
|
| 1293 |
|
| 1294 |
``` cpp
|
| 1295 |
template<class T> void f(T*,int); // #1
|
|
@@ -1302,15 +1647,19 @@ void h(int* pi, int i, char c) {
|
|
| 1302 |
f(i,c); // #2: f<int>(i,c);
|
| 1303 |
f(i,i); // #2: f<int>(i,char(i))
|
| 1304 |
}
|
| 1305 |
```
|
| 1306 |
|
|
|
|
|
|
|
| 1307 |
Only the signature of a function template specialization is needed to
|
| 1308 |
enter the specialization in a set of candidate functions. Therefore only
|
| 1309 |
the function template declaration is needed to resolve a call for which
|
| 1310 |
a template specialization is a candidate.
|
| 1311 |
|
|
|
|
|
|
|
| 1312 |
``` cpp
|
| 1313 |
template<class T> void f(T); // declaration
|
| 1314 |
|
| 1315 |
void g() {
|
| 1316 |
f("Annemarie"); // call of f<const char*>
|
|
@@ -1320,158 +1669,7 @@ void g() {
|
|
| 1320 |
The call of `f` is well-formed even if the template `f` is only declared
|
| 1321 |
and not defined at the point of the call. The program will be ill-formed
|
| 1322 |
unless a specialization for `f<const char*>`, either implicitly or
|
| 1323 |
explicitly generated, is present in some translation unit.
|
| 1324 |
|
| 1325 |
-
|
| 1326 |
-
[basic.def.odr]: basic.md#basic.def.odr
|
| 1327 |
-
[basic.link]: basic.md#basic.link
|
| 1328 |
-
[basic.lookup]: basic.md#basic.lookup
|
| 1329 |
-
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
| 1330 |
-
[basic.lookup.classref]: basic.md#basic.lookup.classref
|
| 1331 |
-
[basic.lookup.qual]: basic.md#basic.lookup.qual
|
| 1332 |
-
[basic.lookup.unqual]: basic.md#basic.lookup.unqual
|
| 1333 |
-
[basic.scope]: basic.md#basic.scope
|
| 1334 |
-
[basic.scope.hiding]: basic.md#basic.scope.hiding
|
| 1335 |
-
[basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
|
| 1336 |
-
[basic.types]: basic.md#basic.types
|
| 1337 |
-
[class]: class.md#class
|
| 1338 |
-
[class.abstract]: class.md#class.abstract
|
| 1339 |
-
[class.access]: class.md#class.access
|
| 1340 |
-
[class.base.init]: special.md#class.base.init
|
| 1341 |
-
[class.derived]: class.md#class.derived
|
| 1342 |
-
[class.dtor]: special.md#class.dtor
|
| 1343 |
-
[class.friend]: class.md#class.friend
|
| 1344 |
-
[class.mem]: class.md#class.mem
|
| 1345 |
-
[class.member.lookup]: class.md#class.member.lookup
|
| 1346 |
-
[class.qual]: basic.md#class.qual
|
| 1347 |
-
[conv]: conv.md#conv
|
| 1348 |
-
[conv.array]: conv.md#conv.array
|
| 1349 |
-
[conv.func]: conv.md#conv.func
|
| 1350 |
-
[conv.integral]: conv.md#conv.integral
|
| 1351 |
-
[conv.mem]: conv.md#conv.mem
|
| 1352 |
-
[conv.ptr]: conv.md#conv.ptr
|
| 1353 |
-
[conv.qual]: conv.md#conv.qual
|
| 1354 |
-
[dcl.align]: dcl.md#dcl.align
|
| 1355 |
-
[dcl.attr.grammar]: dcl.md#dcl.attr.grammar
|
| 1356 |
-
[dcl.dcl]: dcl.md#dcl.dcl
|
| 1357 |
-
[dcl.fct]: dcl.md#dcl.fct
|
| 1358 |
-
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 1359 |
-
[dcl.init]: dcl.md#dcl.init
|
| 1360 |
-
[dcl.init.list]: dcl.md#dcl.init.list
|
| 1361 |
-
[dcl.meaning]: dcl.md#dcl.meaning
|
| 1362 |
-
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
| 1363 |
-
[dcl.type.elab]: dcl.md#dcl.type.elab
|
| 1364 |
-
[except.spec]: except.md#except.spec
|
| 1365 |
-
[expr.const]: expr.md#expr.const
|
| 1366 |
-
[expr.new]: expr.md#expr.new
|
| 1367 |
-
[expr.prim.lambda]: expr.md#expr.prim.lambda
|
| 1368 |
-
[expr.ref]: expr.md#expr.ref
|
| 1369 |
-
[expr.sizeof]: expr.md#expr.sizeof
|
| 1370 |
-
[expr.unary.op]: expr.md#expr.unary.op
|
| 1371 |
-
[intro.defs]: intro.md#intro.defs
|
| 1372 |
-
[lex.string]: lex.md#lex.string
|
| 1373 |
-
[namespace.def]: dcl.md#namespace.def
|
| 1374 |
-
[namespace.memdef]: dcl.md#namespace.memdef
|
| 1375 |
-
[over.ics.rank]: over.md#over.ics.rank
|
| 1376 |
-
[over.match.best]: over.md#over.match.best
|
| 1377 |
-
[over.match.conv]: over.md#over.match.conv
|
| 1378 |
-
[over.match.ref]: over.md#over.match.ref
|
| 1379 |
-
[over.over]: over.md#over.over
|
| 1380 |
-
[special]: special.md#special
|
| 1381 |
-
[support.types]: language.md#support.types
|
| 1382 |
-
[temp]: #temp
|
| 1383 |
-
[temp.alias]: #temp.alias
|
| 1384 |
-
[temp.arg]: #temp.arg
|
| 1385 |
-
[temp.arg.explicit]: #temp.arg.explicit
|
| 1386 |
-
[temp.arg.nontype]: #temp.arg.nontype
|
| 1387 |
-
[temp.arg.template]: #temp.arg.template
|
| 1388 |
-
[temp.arg.type]: #temp.arg.type
|
| 1389 |
-
[temp.class]: #temp.class
|
| 1390 |
-
[temp.class.order]: #temp.class.order
|
| 1391 |
-
[temp.class.spec]: #temp.class.spec
|
| 1392 |
-
[temp.class.spec.match]: #temp.class.spec.match
|
| 1393 |
-
[temp.class.spec.mfunc]: #temp.class.spec.mfunc
|
| 1394 |
-
[temp.decls]: #temp.decls
|
| 1395 |
-
[temp.deduct]: #temp.deduct
|
| 1396 |
-
[temp.deduct.call]: #temp.deduct.call
|
| 1397 |
-
[temp.deduct.conv]: #temp.deduct.conv
|
| 1398 |
-
[temp.deduct.decl]: #temp.deduct.decl
|
| 1399 |
-
[temp.deduct.funcaddr]: #temp.deduct.funcaddr
|
| 1400 |
-
[temp.deduct.partial]: #temp.deduct.partial
|
| 1401 |
-
[temp.deduct.type]: #temp.deduct.type
|
| 1402 |
-
[temp.dep]: #temp.dep
|
| 1403 |
-
[temp.dep.candidate]: #temp.dep.candidate
|
| 1404 |
-
[temp.dep.constexpr]: #temp.dep.constexpr
|
| 1405 |
-
[temp.dep.expr]: #temp.dep.expr
|
| 1406 |
-
[temp.dep.res]: #temp.dep.res
|
| 1407 |
-
[temp.dep.temp]: #temp.dep.temp
|
| 1408 |
-
[temp.dep.type]: #temp.dep.type
|
| 1409 |
-
[temp.expl.spec]: #temp.expl.spec
|
| 1410 |
-
[temp.explicit]: #temp.explicit
|
| 1411 |
-
[temp.fct]: #temp.fct
|
| 1412 |
-
[temp.fct.spec]: #temp.fct.spec
|
| 1413 |
-
[temp.friend]: #temp.friend
|
| 1414 |
-
[temp.func.order]: #temp.func.order
|
| 1415 |
-
[temp.inject]: #temp.inject
|
| 1416 |
-
[temp.inst]: #temp.inst
|
| 1417 |
-
[temp.local]: #temp.local
|
| 1418 |
-
[temp.mem]: #temp.mem
|
| 1419 |
-
[temp.mem.class]: #temp.mem.class
|
| 1420 |
-
[temp.mem.enum]: #temp.mem.enum
|
| 1421 |
-
[temp.mem.func]: #temp.mem.func
|
| 1422 |
-
[temp.names]: #temp.names
|
| 1423 |
-
[temp.nondep]: #temp.nondep
|
| 1424 |
-
[temp.over]: #temp.over
|
| 1425 |
-
[temp.over.link]: #temp.over.link
|
| 1426 |
-
[temp.param]: #temp.param
|
| 1427 |
-
[temp.point]: #temp.point
|
| 1428 |
-
[temp.res]: #temp.res
|
| 1429 |
-
[temp.spec]: #temp.spec
|
| 1430 |
-
[temp.static]: #temp.static
|
| 1431 |
-
[temp.type]: #temp.type
|
| 1432 |
-
[temp.variadic]: #temp.variadic
|
| 1433 |
|
| 1434 |
-
[^1]: Since template *template-parameter*s and template
|
| 1435 |
-
*template-argument*s are treated as types for descriptive purposes,
|
| 1436 |
-
the terms *non-type parameter* and *non-type argument* are used to
|
| 1437 |
-
refer to non-type, non-template parameters and arguments.
|
| 1438 |
-
|
| 1439 |
-
[^2]: A `>` that encloses the *type-id* of a `dynamic_cast`,
|
| 1440 |
-
`static_cast`, `reinterpret_cast` or `const_cast`, or which encloses
|
| 1441 |
-
the *template-argument*s of a subsequent *template-id*, is
|
| 1442 |
-
considered nested for the purpose of this description.
|
| 1443 |
-
|
| 1444 |
-
[^3]: There is no such ambiguity in a default *template-argument*
|
| 1445 |
-
because the form of the *template-parameter* determines the
|
| 1446 |
-
allowable forms of the *template-argument*.
|
| 1447 |
-
|
| 1448 |
-
[^4]: There is no way in which they could be used.
|
| 1449 |
-
|
| 1450 |
-
[^5]: That is, declarations of non-template functions do not merely
|
| 1451 |
-
guide overload resolution of function template specializations with
|
| 1452 |
-
the same name. If such a non-template function is odr-used (
|
| 1453 |
-
[[basic.def.odr]]) in a program, it must be defined; it will not be
|
| 1454 |
-
implicitly instantiated using the function template definition.
|
| 1455 |
-
|
| 1456 |
-
[^6]: Friend declarations do not introduce new names into any scope,
|
| 1457 |
-
either when the template is declared or when it is instantiated.
|
| 1458 |
-
|
| 1459 |
-
[^7]: Default arguments are not considered to be arguments in this
|
| 1460 |
-
context; they only become arguments after a function has been
|
| 1461 |
-
selected.
|
| 1462 |
-
|
| 1463 |
-
[^8]: Although the *template-argument* corresponding to a
|
| 1464 |
-
*template-parameter* of type `bool` may be deduced from an array
|
| 1465 |
-
bound, the resulting value will always be `true` because the array
|
| 1466 |
-
bound will be non-zero.
|
| 1467 |
-
|
| 1468 |
-
[^9]: The parameters of function template specializations contain no
|
| 1469 |
-
template parameter types. The set of conversions allowed on deduced
|
| 1470 |
-
arguments is limited, because the argument deduction process
|
| 1471 |
-
produces function templates with parameters that either match the
|
| 1472 |
-
call arguments exactly or differ only in ways that can be bridged by
|
| 1473 |
-
the allowed limited conversions. Non-deduced arguments allow the
|
| 1474 |
-
full range of conversions. Note also that [[over.match.best]]
|
| 1475 |
-
specifies that a non-template function will be given preference over
|
| 1476 |
-
a template specialization if the two functions are otherwise equally
|
| 1477 |
-
good candidates for an overload match.
|
|
|
|
| 9 |
arguments.
|
| 10 |
|
| 11 |
Each function template specialization instantiated from a template has
|
| 12 |
its own copy of any static variable.
|
| 13 |
|
| 14 |
+
[*Example 1*:
|
| 15 |
+
|
| 16 |
``` cpp
|
| 17 |
template<class T> void f(T* p) {
|
| 18 |
static T s;
|
| 19 |
};
|
| 20 |
|
|
|
|
| 25 |
```
|
| 26 |
|
| 27 |
Here `f<int>(int*)` has a static variable `s` of type `int` and
|
| 28 |
`f<char*>(char**)` has a static variable `s` of type `char*`.
|
| 29 |
|
| 30 |
+
— *end example*]
|
| 31 |
+
|
| 32 |
### Explicit template argument specification <a id="temp.arg.explicit">[[temp.arg.explicit]]</a>
|
| 33 |
|
| 34 |
Template arguments can be specified when referring to a function
|
| 35 |
template specialization by qualifying the function template name with
|
| 36 |
the list of *template-argument*s in the same way as *template-argument*s
|
| 37 |
are specified in uses of a class template specialization.
|
| 38 |
|
| 39 |
+
[*Example 1*:
|
| 40 |
+
|
| 41 |
``` cpp
|
| 42 |
template<class T> void sort(Array<T>& v);
|
| 43 |
void f(Array<dcomplex>& cv, Array<int>& ci) {
|
| 44 |
sort<dcomplex>(cv); // sort(Array<dcomplex>&)
|
| 45 |
sort<int>(ci); // sort(Array<int>&)
|
|
|
|
| 55 |
int i = convert<int,double>(d); // int convert(double)
|
| 56 |
char c = convert<char,double>(d); // char convert(double)
|
| 57 |
}
|
| 58 |
```
|
| 59 |
|
| 60 |
+
— *end example*]
|
| 61 |
+
|
| 62 |
A template argument list may be specified when referring to a
|
| 63 |
specialization of a function template
|
| 64 |
|
| 65 |
- when a function is called,
|
| 66 |
- when the address of a function is taken, when a function initializes a
|
|
|
|
| 80 |
if a template argument list is specified and it, along with any default
|
| 81 |
template arguments, identifies a single function template
|
| 82 |
specialization, then the *template-id* is an lvalue for the function
|
| 83 |
template specialization.
|
| 84 |
|
| 85 |
+
[*Example 2*:
|
| 86 |
+
|
| 87 |
``` cpp
|
| 88 |
template<class X, class Y> X f(Y);
|
| 89 |
template<class X, class Y, class ... Z> X g(Y);
|
| 90 |
void h() {
|
| 91 |
int i = f<int>(5.6); // Y is deduced to be double
|
| 92 |
int j = f(5.6); // ill-formed: X cannot be deduced
|
| 93 |
+
f<void>(f<int, bool>); // Y for outer f deduced to be int (*)(bool)
|
| 94 |
+
f<void>(f<int>); // ill-formed: f<int> does not denote a single function template specialization
|
|
|
|
|
|
|
| 95 |
int k = g<int>(5.6); // Y is deduced to be double, Z is deduced to an empty sequence
|
| 96 |
+
f<void>(g<int, bool>); // Y for outer f is deduced to be int (*)(bool),
|
| 97 |
+
// Z is deduced to an empty sequence
|
| 98 |
}
|
| 99 |
```
|
| 100 |
|
| 101 |
+
— *end example*]
|
| 102 |
+
|
| 103 |
+
[*Note 1*:
|
| 104 |
+
|
| 105 |
An empty template argument list can be used to indicate that a given use
|
| 106 |
refers to a specialization of a function template even when a
|
| 107 |
non-template function ([[dcl.fct]]) is visible that would otherwise be
|
| 108 |
used. For example:
|
| 109 |
|
|
|
|
| 112 |
int f(int); // #2
|
| 113 |
int k = f(1); // uses #2
|
| 114 |
int l = f<>(1); // uses #1
|
| 115 |
```
|
| 116 |
|
| 117 |
+
— *end note*]
|
| 118 |
+
|
| 119 |
Template arguments that are present shall be specified in the
|
| 120 |
declaration order of their corresponding *template-parameter*s. The
|
| 121 |
template argument list shall not specify more *template-argument*s than
|
| 122 |
there are corresponding *template-parameter*s unless one of the
|
| 123 |
*template-parameter*s is a template parameter pack.
|
| 124 |
|
| 125 |
+
[*Example 3*:
|
| 126 |
+
|
| 127 |
``` cpp
|
| 128 |
template<class X, class Y, class Z> X f(Y,Z);
|
| 129 |
template<class ... Args> void f2();
|
| 130 |
void g() {
|
| 131 |
f<int,const char*,double>("aa",3.0);
|
| 132 |
f<int,const char*>("aa",3.0); // Z is deduced to be double
|
| 133 |
+
f<int>("aa",3.0); // Y is deduced to be const char*, and Z is deduced to be double
|
|
|
|
| 134 |
f("aa",3.0); // error: X cannot be deduced
|
| 135 |
f2<char, short, int, long>(); // OK
|
| 136 |
}
|
| 137 |
```
|
| 138 |
|
| 139 |
+
— *end example*]
|
| 140 |
+
|
| 141 |
Implicit conversions (Clause [[conv]]) will be performed on a function
|
| 142 |
argument to convert it to the type of the corresponding function
|
| 143 |
parameter if the parameter type contains no *template-parameter*s that
|
| 144 |
+
participate in template argument deduction.
|
| 145 |
+
|
| 146 |
+
[*Note 2*:
|
| 147 |
+
|
| 148 |
+
Template parameters do not participate in template argument deduction if
|
| 149 |
+
they are explicitly specified. For example,
|
| 150 |
|
| 151 |
``` cpp
|
| 152 |
template<class T> void f(T);
|
| 153 |
|
| 154 |
class Complex {
|
|
|
|
| 158 |
void g() {
|
| 159 |
f<Complex>(1); // OK, means f<Complex>(Complex(1))
|
| 160 |
}
|
| 161 |
```
|
| 162 |
|
| 163 |
+
— *end note*]
|
| 164 |
+
|
| 165 |
+
[*Note 3*: Because the explicit template argument list follows the
|
| 166 |
+
function template name, and because conversion member function templates
|
| 167 |
+
and constructor member function templates are called without using a
|
| 168 |
function name, there is no way to provide an explicit template argument
|
| 169 |
+
list for these function templates. — *end note*]
|
| 170 |
+
|
| 171 |
+
[*Note 4*:
|
| 172 |
|
| 173 |
For simple function names, argument dependent lookup (
|
| 174 |
[[basic.lookup.argdep]]) applies even when the function name is not
|
| 175 |
visible within the scope of the call. This is because the call still has
|
| 176 |
the syntactic form of a function call ([[basic.lookup.unqual]]). But
|
|
|
|
| 180 |
name is visible, the call is not syntactically well-formed and
|
| 181 |
argument-dependent lookup does not apply. If some such name is visible,
|
| 182 |
argument dependent lookup applies and additional function templates may
|
| 183 |
be found in other namespaces.
|
| 184 |
|
| 185 |
+
[*Example 4*:
|
| 186 |
+
|
| 187 |
``` cpp
|
| 188 |
namespace A {
|
| 189 |
struct B { };
|
| 190 |
template<int X> void f(B);
|
| 191 |
}
|
|
|
|
| 193 |
template<class T> void f(T t);
|
| 194 |
}
|
| 195 |
void g(A::B b) {
|
| 196 |
f<3>(b); // ill-formed: not a function call
|
| 197 |
A::f<3>(b); // well-formed
|
| 198 |
+
C::f<3>(b); // ill-formed; argument dependent lookup applies only to unqualified names
|
|
|
|
| 199 |
using C::f;
|
| 200 |
+
f<3>(b); // well-formed because C::f is visible; then A::f is found by argument dependent lookup
|
|
|
|
| 201 |
}
|
| 202 |
```
|
| 203 |
|
| 204 |
+
— *end example*]
|
| 205 |
+
|
| 206 |
+
— *end note*]
|
| 207 |
+
|
| 208 |
Template argument deduction can extend the sequence of template
|
| 209 |
arguments corresponding to a template parameter pack, even when the
|
| 210 |
sequence contains explicitly specified template arguments.
|
| 211 |
|
| 212 |
+
[*Example 5*:
|
| 213 |
+
|
| 214 |
``` cpp
|
| 215 |
template<class ... Types> void f(Types ... values);
|
| 216 |
|
| 217 |
void g() {
|
| 218 |
f<int*, float*>(0, 0, 0); // Types is deduced to the sequence int*, float*, int
|
| 219 |
}
|
| 220 |
```
|
| 221 |
|
| 222 |
+
— *end example*]
|
| 223 |
+
|
| 224 |
### Template argument deduction <a id="temp.deduct">[[temp.deduct]]</a>
|
| 225 |
|
| 226 |
When a function template specialization is referenced, all of the
|
| 227 |
template arguments shall have values. The values can be explicitly
|
| 228 |
specified or, in some cases, be deduced from the use or obtained from
|
| 229 |
default *template-argument*s.
|
| 230 |
|
| 231 |
+
[*Example 1*:
|
| 232 |
+
|
| 233 |
``` cpp
|
| 234 |
void f(Array<dcomplex>& cv, Array<int>& ci) {
|
| 235 |
sort(cv); // calls sort(Array<dcomplex>&)
|
| 236 |
sort(ci); // calls sort(Array<int>&)
|
| 237 |
}
|
|
|
|
| 244 |
int i = convert<int>(d); // calls convert<int,double>(double)
|
| 245 |
int c = convert<char>(d); // calls convert<char,double>(double)
|
| 246 |
}
|
| 247 |
```
|
| 248 |
|
| 249 |
+
— *end example*]
|
| 250 |
+
|
| 251 |
When an explicit template argument list is specified, the template
|
| 252 |
arguments must be compatible with the template parameter list and must
|
| 253 |
result in a valid function type as described below; otherwise type
|
| 254 |
deduction fails. Specifically, the following steps are performed when
|
| 255 |
evaluating an explicitly specified template argument list with respect
|
|
|
|
| 266 |
[[temp.arg.nontype]], otherwise type deduction fails.
|
| 267 |
- The specified template argument values are substituted for the
|
| 268 |
corresponding template parameters as specified below.
|
| 269 |
|
| 270 |
After this substitution is performed, the function parameter type
|
| 271 |
+
adjustments described in [[dcl.fct]] are performed.
|
| 272 |
+
|
| 273 |
+
[*Example 2*: A parameter type of “`void (const int, int[5])`” becomes
|
| 274 |
+
“`void(*)(int,int*)`”. — *end example*]
|
| 275 |
+
|
| 276 |
+
[*Note 1*: A top-level qualifier in a function parameter declaration
|
| 277 |
+
does not affect the function type but still affects the type of the
|
| 278 |
+
function parameter variable within the function. — *end note*]
|
| 279 |
+
|
| 280 |
+
[*Example 3*:
|
| 281 |
|
| 282 |
``` cpp
|
| 283 |
template <class T> void f(T t);
|
| 284 |
template <class X> void g(const X x);
|
| 285 |
template <class Z> void h(Z, Z*);
|
|
|
|
| 300 |
// #5: function type is h(int, const int*)
|
| 301 |
h<const int>(1,0);
|
| 302 |
}
|
| 303 |
```
|
| 304 |
|
| 305 |
+
— *end example*]
|
| 306 |
+
|
| 307 |
+
[*Note 2*: `f<int>(1)` and `f<const int>(1)` call distinct functions
|
| 308 |
+
even though both of the functions called have the same function
|
| 309 |
+
type. — *end note*]
|
| 310 |
|
| 311 |
The resulting substituted and adjusted function type is used as the type
|
| 312 |
of the function template for template argument deduction. If a template
|
| 313 |
argument has not been deduced and its corresponding template parameter
|
| 314 |
has a default argument, the template argument is determined by
|
| 315 |
substituting the template arguments determined for preceding template
|
| 316 |
parameters into the default argument. If the substitution results in an
|
| 317 |
invalid type, as described above, type deduction fails.
|
| 318 |
|
| 319 |
+
[*Example 4*:
|
| 320 |
+
|
| 321 |
``` cpp
|
| 322 |
template <class T, class U = double>
|
| 323 |
void f(T t = 0, U u = 0);
|
| 324 |
|
| 325 |
void g() {
|
|
|
|
| 329 |
f<int>(); // f<int,double>(0,0)
|
| 330 |
f<int,char>(); // f<int,char>(0,0)
|
| 331 |
}
|
| 332 |
```
|
| 333 |
|
| 334 |
+
— *end example*]
|
| 335 |
+
|
| 336 |
When all template arguments have been deduced or obtained from default
|
| 337 |
template arguments, all uses of template parameters in the template
|
| 338 |
parameter list of the template and the function type are replaced with
|
| 339 |
the corresponding deduced or default argument values. If the
|
| 340 |
substitution results in an invalid type, as described above, type
|
|
|
|
| 354 |
expressions include not only constant expressions such as those that
|
| 355 |
appear in array bounds or as nontype template arguments but also general
|
| 356 |
expressions (i.e., non-constant expressions) inside `sizeof`,
|
| 357 |
`decltype`, and other contexts that allow non-constant expressions. The
|
| 358 |
substitution proceeds in lexical order and stops when a condition that
|
| 359 |
+
causes deduction to fail is encountered.
|
| 360 |
+
|
| 361 |
+
[*Note 3*: The equivalent substitution in exception specifications is
|
| 362 |
+
done only when the *noexcept-specifier* is instantiated, at which point
|
| 363 |
+
a program is ill-formed if the substitution results in an invalid type
|
| 364 |
+
or expression. — *end note*]
|
| 365 |
+
|
| 366 |
+
[*Example 5*:
|
| 367 |
|
| 368 |
``` cpp
|
| 369 |
template <class T> struct A { using X = typename T::X; };
|
| 370 |
template <class T> typename T::X f(typename A<T>::X);
|
| 371 |
template <class T> void f(...) { }
|
|
|
|
| 376 |
f<int>(0); // OK, substituting return type causes deduction to fail
|
| 377 |
g<int>(0); // error, substituting parameter type instantiates A<int>
|
| 378 |
}
|
| 379 |
```
|
| 380 |
|
| 381 |
+
— *end example*]
|
| 382 |
+
|
| 383 |
If a substitution results in an invalid type or expression, type
|
| 384 |
deduction fails. An invalid type or expression is one that would be
|
| 385 |
ill-formed, with a diagnostic required, if written using the substituted
|
| 386 |
+
arguments.
|
| 387 |
+
|
| 388 |
+
[*Note 4*: If no diagnostic is required, the program is still
|
| 389 |
+
ill-formed. Access checking is done as part of the substitution
|
| 390 |
+
process. — *end note*]
|
| 391 |
+
|
| 392 |
Only invalid types and expressions in the immediate context of the
|
| 393 |
function type and its template parameter types can result in a deduction
|
| 394 |
+
failure.
|
| 395 |
+
|
| 396 |
+
[*Note 5*: The substitution into types and expressions can result in
|
| 397 |
+
effects such as the instantiation of class template specializations
|
| 398 |
+
and/or function template specializations, the generation of
|
| 399 |
+
implicitly-defined functions, etc. Such effects are not in the
|
| 400 |
+
“immediate context” and can result in the program being
|
| 401 |
+
ill-formed. — *end note*]
|
| 402 |
+
|
| 403 |
+
[*Example 6*:
|
| 404 |
|
| 405 |
``` cpp
|
| 406 |
struct X { };
|
| 407 |
struct Y {
|
| 408 |
Y(X){}
|
|
|
|
| 413 |
|
| 414 |
X x1, x2;
|
| 415 |
X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
|
| 416 |
```
|
| 417 |
|
| 418 |
+
— *end example*]
|
| 419 |
+
|
| 420 |
+
[*Note 6*:
|
| 421 |
+
|
| 422 |
Type deduction may fail for the following reasons:
|
| 423 |
|
| 424 |
- Attempting to instantiate a pack expansion containing multiple
|
| 425 |
parameter packs of differing lengths.
|
| 426 |
- Attempting to create an array with an element type that is `void`, a
|
| 427 |
function type, a reference type, or an abstract class type, or
|
| 428 |
attempting to create an array with a size that is zero or negative.
|
| 429 |
+
\[*Example 7*:
|
| 430 |
``` cpp
|
| 431 |
template <class T> int f(T[5]);
|
| 432 |
int I = f<int>(0);
|
| 433 |
int j = f<void>(0); // invalid array
|
| 434 |
```
|
| 435 |
+
|
| 436 |
+
— *end example*]
|
| 437 |
- Attempting to use a type that is not a class or enumeration type in a
|
| 438 |
qualified name.
|
| 439 |
+
\[*Example 8*:
|
| 440 |
``` cpp
|
| 441 |
template <class T> int f(typename T::B*);
|
| 442 |
int i = f<int>(0);
|
| 443 |
```
|
| 444 |
+
|
| 445 |
+
— *end example*]
|
| 446 |
- Attempting to use a type in a *nested-name-specifier* of a
|
| 447 |
*qualified-id* when that type does not contain the specified member,
|
| 448 |
or
|
| 449 |
- the specified member is not a type where a type is required, or
|
| 450 |
- the specified member is not a template where a template is required,
|
| 451 |
or
|
| 452 |
- the specified member is not a non-type where a non-type is required.
|
| 453 |
|
| 454 |
+
\[*Example 9*:
|
| 455 |
``` cpp
|
| 456 |
template <int I> struct X { };
|
| 457 |
template <template <class T> class> struct Z { };
|
| 458 |
template <class T> void f(typename T::Y*){}
|
| 459 |
template <class T> void g(X<T::N>*){}
|
|
|
|
| 473 |
f<B>(0); // The Y member of B is not a type
|
| 474 |
g<C>(0); // The N member of C is not a non-type
|
| 475 |
h<D>(0); // The TT member of D is not a template
|
| 476 |
}
|
| 477 |
```
|
| 478 |
+
|
| 479 |
+
— *end example*]
|
| 480 |
- Attempting to create a pointer to reference type.
|
| 481 |
- Attempting to create a reference to `void`.
|
| 482 |
- Attempting to create “pointer to member of `T`” when `T` is not a
|
| 483 |
class type.
|
| 484 |
+
\[*Example 10*:
|
| 485 |
``` cpp
|
| 486 |
template <class T> int f(int T::*);
|
| 487 |
int i = f<int>(0);
|
| 488 |
```
|
| 489 |
+
|
| 490 |
+
— *end example*]
|
| 491 |
- Attempting to give an invalid type to a non-type template parameter.
|
| 492 |
+
\[*Example 11*:
|
| 493 |
``` cpp
|
| 494 |
template <class T, T> struct S {};
|
| 495 |
template <class T> int f(S<T, T()>*);
|
| 496 |
struct X {};
|
| 497 |
int i0 = f<X>(0);
|
| 498 |
```
|
| 499 |
+
|
| 500 |
+
— *end example*]
|
| 501 |
- Attempting to perform an invalid conversion in either a template
|
| 502 |
argument expression, or an expression used in the function
|
| 503 |
declaration.
|
| 504 |
+
\[*Example 12*:
|
| 505 |
``` cpp
|
| 506 |
template <class T, T*> int f(int);
|
| 507 |
int i2 = f<int,1>(0); // can't conv 1 to int*
|
| 508 |
```
|
| 509 |
+
|
| 510 |
+
— *end example*]
|
| 511 |
- Attempting to create a function type in which a parameter has a type
|
| 512 |
of `void`, or in which the return type is a function type or array
|
| 513 |
type.
|
| 514 |
- Attempting to create a function type in which a parameter type or the
|
| 515 |
return type is an abstract class type ([[class.abstract]]).
|
| 516 |
|
| 517 |
+
— *end note*]
|
| 518 |
+
|
| 519 |
+
[*Example 13*:
|
| 520 |
+
|
| 521 |
+
In the following example, assuming a `signed char` cannot represent the
|
| 522 |
+
value 1000, a narrowing conversion ([[dcl.init.list]]) would be
|
| 523 |
+
required to convert the *template-argument* of type `int` to
|
| 524 |
+
`signed char`, therefore substitution fails for the second template (
|
| 525 |
+
[[temp.arg.nontype]]).
|
| 526 |
|
| 527 |
``` cpp
|
| 528 |
template <int> int f(int);
|
| 529 |
template <signed char> int f(int);
|
| 530 |
+
int i1 = f<1000>(0); // OK
|
| 531 |
+
int i2 = f<1>(0); // ambiguous; not narrowing
|
| 532 |
```
|
| 533 |
|
| 534 |
+
— *end example*]
|
| 535 |
+
|
| 536 |
#### Deducing template arguments from a function call <a id="temp.deduct.call">[[temp.deduct.call]]</a>
|
| 537 |
|
| 538 |
Template argument deduction is done by comparing each function template
|
| 539 |
+
parameter type (call it `P`) that contains *template-parameter*s that
|
| 540 |
+
participate in template argument deduction with the type of the
|
| 541 |
+
corresponding argument of the call (call it `A`) as described below. If
|
| 542 |
+
removing references and cv-qualifiers from `P` gives
|
| 543 |
+
`std::initializer_list<P'>` or `P'[N]` for some `P'` and `N` and the
|
| 544 |
+
argument is a non-empty initializer list ([[dcl.init.list]]), then
|
| 545 |
+
deduction is performed instead for each element of the initializer list,
|
| 546 |
+
taking `P'` as a function template parameter type and the initializer
|
| 547 |
+
element as its argument, and in the `P'[N]` case, if `N` is a non-type
|
| 548 |
+
template parameter, `N` is deduced from the length of the initializer
|
| 549 |
+
list. Otherwise, an initializer list argument causes the parameter to be
|
| 550 |
+
considered a non-deduced context ([[temp.deduct.type]]).
|
| 551 |
+
|
| 552 |
+
[*Example 1*:
|
| 553 |
|
| 554 |
``` cpp
|
| 555 |
template<class T> void f(std::initializer_list<T>);
|
| 556 |
f({1,2,3}); // T deduced to int
|
| 557 |
f({1,"asdf"}); // error: T deduced to both int and const char*
|
| 558 |
|
| 559 |
template<class T> void g(T);
|
| 560 |
g({1,2,3}); // error: no argument deduced for T
|
| 561 |
+
|
| 562 |
+
template<class T, int N> void h(T const(&)[N]);
|
| 563 |
+
h({1,2,3}); // T deduced to int, N deduced to 3
|
| 564 |
+
|
| 565 |
+
template<class T> void j(T const(&)[3]);
|
| 566 |
+
j({42}); // T deduced to int, array bound not considered
|
| 567 |
+
|
| 568 |
+
struct Aggr { int i; int j; };
|
| 569 |
+
template<int N> void k(Aggr const(&)[N]);
|
| 570 |
+
k({1,2,3}); // error: deduction fails, no conversion from int to Aggr
|
| 571 |
+
k({{1},{2},{3}}); // OK, N deduced to 3
|
| 572 |
+
|
| 573 |
+
template<int M, int N> void m(int const(&)[M][N]);
|
| 574 |
+
m({{1,2},{3,4}}); // M and N both deduced to 2
|
| 575 |
+
|
| 576 |
+
template<class T, int N> void n(T const(&)[N], T);
|
| 577 |
+
n({{1},{2},{3}},Aggr()); // OK, T is Aggr, N is 3
|
| 578 |
```
|
| 579 |
|
| 580 |
+
— *end example*]
|
| 581 |
+
|
| 582 |
For a function parameter pack that occurs at the end of the
|
| 583 |
+
*parameter-declaration-list*, deduction is performed for each remaining
|
| 584 |
+
argument of the call, taking the type `P` of the *declarator-id* of the
|
| 585 |
+
function parameter pack as the corresponding function template parameter
|
| 586 |
+
type. Each deduction deduces template arguments for subsequent positions
|
| 587 |
+
in the template parameter packs expanded by the function parameter pack.
|
| 588 |
+
When a function parameter pack appears in a non-deduced context (
|
| 589 |
+
[[temp.deduct.type]]), the type of that parameter pack is never deduced.
|
| 590 |
+
|
| 591 |
+
[*Example 2*:
|
| 592 |
|
| 593 |
``` cpp
|
| 594 |
template<class ... Types> void f(Types& ...);
|
| 595 |
template<class T1, class ... Types> void g(T1, Types ...);
|
| 596 |
template<class T1, class ... Types> void g1(Types ..., T1);
|
|
|
|
| 599 |
const int z = x;
|
| 600 |
f(x, y, z); // Types is deduced to int, float, const int
|
| 601 |
g(x, y, z); // T1 is deduced to int; Types is deduced to float, int
|
| 602 |
g1(x, y, z); // error: Types is not deduced
|
| 603 |
g1<int, int, int>(x, y, z); // OK, no deduction occurs
|
|
|
|
| 604 |
}
|
| 605 |
```
|
| 606 |
|
| 607 |
+
— *end example*]
|
| 608 |
+
|
| 609 |
If `P` is not a reference type:
|
| 610 |
|
| 611 |
- If `A` is an array type, the pointer type produced by the
|
| 612 |
array-to-pointer standard conversion ([[conv.array]]) is used in
|
| 613 |
place of `A` for type deduction; otherwise,
|
| 614 |
- If `A` is a function type, the pointer type produced by the
|
| 615 |
function-to-pointer standard conversion ([[conv.func]]) is used in
|
| 616 |
place of `A` for type deduction; otherwise,
|
| 617 |
+
- If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s
|
| 618 |
type are ignored for type deduction.
|
| 619 |
|
| 620 |
+
If `P` is a cv-qualified type, the top-level cv-qualifiers of `P`’s type
|
| 621 |
are ignored for type deduction. If `P` is a reference type, the type
|
| 622 |
+
referred to by `P` is used for type deduction.
|
| 623 |
+
|
| 624 |
+
[*Example 3*:
|
| 625 |
+
|
| 626 |
+
``` cpp
|
| 627 |
+
template<class T> int f(const T&);
|
| 628 |
+
int n1 = f(5); // calls f<int>(const int&)
|
| 629 |
+
const int i = 0;
|
| 630 |
+
int n2 = f(i); // calls f<int>(const int&)
|
| 631 |
+
template <class T> int g(volatile T&);
|
| 632 |
+
int n3 = g(i); // calls g<const int>(const volatile int&)
|
| 633 |
+
```
|
| 634 |
+
|
| 635 |
+
— *end example*]
|
| 636 |
+
|
| 637 |
+
A *forwarding reference* is an rvalue reference to a cv-unqualified
|
| 638 |
+
template parameter that does not represent a template parameter of a
|
| 639 |
+
class template (during class template argument deduction (
|
| 640 |
+
[[over.match.class.deduct]])). If `P` is a forwarding reference and the
|
| 641 |
+
argument is an lvalue, the type “lvalue reference to `A`” is used in
|
| 642 |
+
place of `A` for type deduction.
|
| 643 |
+
|
| 644 |
+
[*Example 4*:
|
| 645 |
|
| 646 |
``` cpp
|
| 647 |
+
template <class T> int f(T&& heisenreference);
|
| 648 |
template <class T> int g(const T&&);
|
| 649 |
int i;
|
| 650 |
int n1 = f(i); // calls f<int&>(int&)
|
| 651 |
int n2 = f(0); // calls f<int>(int&&)
|
| 652 |
int n3 = g(i); // error: would call g<int>(const int&&), which
|
| 653 |
// would bind an rvalue reference to an lvalue
|
| 654 |
+
|
| 655 |
+
template <class T> struct A {
|
| 656 |
+
template <class U>
|
| 657 |
+
A(T&&, U&&, int*); // #1: T&& is not a forwarding reference.
|
| 658 |
+
// U&& is a forwarding reference.
|
| 659 |
+
A(T&&, int*); // #2
|
| 660 |
+
};
|
| 661 |
+
|
| 662 |
+
template <class T> A(T&&, int*) -> A<T>; // #3: T&& is a forwarding reference.
|
| 663 |
+
|
| 664 |
+
int *ip;
|
| 665 |
+
A a{i, 0, ip}; // error: cannot deduce from #1
|
| 666 |
+
A a0{0, 0, ip}; // uses #1 to deduce A<int> and #1 to initialize
|
| 667 |
+
A a2{i, ip}; // uses #3 to deduce A<int&> and #2 to initialize
|
| 668 |
```
|
| 669 |
|
| 670 |
+
— *end example*]
|
| 671 |
+
|
| 672 |
In general, the deduction process attempts to find template argument
|
| 673 |
values that will make the deduced `A` identical to `A` (after the type
|
| 674 |
`A` is transformed as described above). However, there are three cases
|
| 675 |
that allow a difference:
|
| 676 |
|
| 677 |
- If the original `P` is a reference type, the deduced `A` (i.e., the
|
| 678 |
type referred to by the reference) can be more cv-qualified than the
|
| 679 |
transformed `A`.
|
| 680 |
- The transformed `A` can be another pointer or pointer to member type
|
| 681 |
+
that can be converted to the deduced `A` via a function pointer
|
| 682 |
+
conversion ([[conv.fctptr]]) and/or qualification conversion (
|
| 683 |
+
[[conv.qual]]).
|
| 684 |
- If `P` is a class and `P` has the form *simple-template-id*, then the
|
| 685 |
transformed `A` can be a derived class of the deduced `A`. Likewise,
|
| 686 |
if `P` is a pointer to a class of the form *simple-template-id*, the
|
| 687 |
transformed `A` can be a pointer to a derived class pointed to by the
|
| 688 |
deduced `A`.
|
| 689 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 690 |
These alternatives are considered only if type deduction would otherwise
|
| 691 |
fail. If they yield more than one possible deduced `A`, the type
|
| 692 |
+
deduction fails.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 693 |
|
| 694 |
+
[*Note 1*: If a *template-parameter* is not used in any of the function
|
| 695 |
+
parameters of a function template, or is used only in a non-deduced
|
| 696 |
+
context, its corresponding *template-argument* cannot be deduced from a
|
| 697 |
+
function call and the *template-argument* must be explicitly
|
| 698 |
+
specified. — *end note*]
|
| 699 |
+
|
| 700 |
+
When `P` is a function type, function pointer type, or pointer to member
|
| 701 |
+
function type:
|
| 702 |
|
| 703 |
- If the argument is an overload set containing one or more function
|
| 704 |
templates, the parameter is treated as a non-deduced context.
|
| 705 |
- If the argument is an overload set (not containing function
|
| 706 |
templates), trial argument deduction is attempted using each of the
|
| 707 |
members of the set. If deduction succeeds for only one of the overload
|
| 708 |
set members, that member is used as the argument value for the
|
| 709 |
deduction. If deduction succeeds for more than one member of the
|
| 710 |
overload set the parameter is treated as a non-deduced context.
|
| 711 |
+
|
| 712 |
+
[*Example 5*:
|
| 713 |
+
|
| 714 |
``` cpp
|
| 715 |
+
// Only one function of an overload set matches the call so the function parameter is a deduced context.
|
|
|
|
| 716 |
template <class T> int f(T (*p)(T));
|
| 717 |
int g(int);
|
| 718 |
int g(char);
|
| 719 |
int i = f(g); // calls f(int (*)(int))
|
| 720 |
```
|
| 721 |
|
| 722 |
+
— *end example*]
|
| 723 |
+
|
| 724 |
+
[*Example 6*:
|
| 725 |
+
|
| 726 |
``` cpp
|
| 727 |
+
// Ambiguous deduction causes the second function parameter to be a non-deduced context.
|
|
|
|
| 728 |
template <class T> int f(T, T (*p)(T));
|
| 729 |
int g(int);
|
| 730 |
char g(char);
|
| 731 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 732 |
```
|
| 733 |
|
| 734 |
+
— *end example*]
|
| 735 |
+
|
| 736 |
+
[*Example 7*:
|
| 737 |
+
|
| 738 |
``` cpp
|
| 739 |
+
// The overload set contains a template, causing the second function parameter to be a non-deduced context.
|
|
|
|
| 740 |
template <class T> int f(T, T (*p)(T));
|
| 741 |
char g(char);
|
| 742 |
template <class T> T g(T);
|
| 743 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 744 |
```
|
| 745 |
|
| 746 |
+
— *end example*]
|
| 747 |
+
|
| 748 |
+
If deduction succeeds for all parameters that contain
|
| 749 |
+
*template-parameter*s that participate in template argument deduction,
|
| 750 |
+
and all template arguments are explicitly specified, deduced, or
|
| 751 |
+
obtained from default template arguments, remaining parameters are then
|
| 752 |
+
compared with the corresponding arguments. For each remaining parameter
|
| 753 |
+
`P` with a type that was non-dependent before substitution of any
|
| 754 |
+
explicitly-specified template arguments, if the corresponding argument
|
| 755 |
+
`A` cannot be implicitly converted to `P`, deduction fails.
|
| 756 |
+
|
| 757 |
+
[*Note 2*: Parameters with dependent types in which no
|
| 758 |
+
*template-parameter*s participate in template argument deduction, and
|
| 759 |
+
parameters that became non-dependent due to substitution of
|
| 760 |
+
explicitly-specified template arguments, will be checked during overload
|
| 761 |
+
resolution. — *end note*]
|
| 762 |
+
|
| 763 |
+
[*Example 8*:
|
| 764 |
+
|
| 765 |
+
``` cpp
|
| 766 |
+
template <class T> struct Z {
|
| 767 |
+
typedef typename T::x xx;
|
| 768 |
+
};
|
| 769 |
+
template <class T> typename Z<T>::xx f(void *, T); // #1
|
| 770 |
+
template <class T> void f(int, T); // #2
|
| 771 |
+
struct A {} a;
|
| 772 |
+
int main() {
|
| 773 |
+
f(1, a); // OK, deduction fails for #1 because there is no conversion from int to void*
|
| 774 |
+
}
|
| 775 |
+
```
|
| 776 |
+
|
| 777 |
+
— *end example*]
|
| 778 |
+
|
| 779 |
#### Deducing template arguments taking the address of a function template <a id="temp.deduct.funcaddr">[[temp.deduct.funcaddr]]</a>
|
| 780 |
|
| 781 |
Template arguments can be deduced from the type specified when taking
|
| 782 |
the address of an overloaded function ([[over.over]]). The function
|
| 783 |
template’s function type and the specified type are used as the types of
|
|
|
|
| 807 |
array-to-pointer standard conversion ([[conv.array]]) is used in
|
| 808 |
place of `P` for type deduction; otherwise,
|
| 809 |
- If `P` is a function type, the pointer type produced by the
|
| 810 |
function-to-pointer standard conversion ([[conv.func]]) is used in
|
| 811 |
place of `P` for type deduction; otherwise,
|
| 812 |
+
- If `P` is a cv-qualified type, the top-level cv-qualifiers of `P`’s
|
| 813 |
type are ignored for type deduction.
|
| 814 |
|
| 815 |
+
If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s type
|
| 816 |
are ignored for type deduction. If `A` is a reference type, the type
|
| 817 |
referred to by `A` is used for type deduction.
|
| 818 |
|
| 819 |
In general, the deduction process attempts to find template argument
|
| 820 |
values that will make the deduced `A` identical to `A`. However, there
|
| 821 |
+
are four cases that allow a difference:
|
| 822 |
|
| 823 |
- If the original `A` is a reference type, `A` can be more cv-qualified
|
| 824 |
than the deduced `A` (i.e., the type referred to by the reference)
|
| 825 |
+
- If the original `A` is a function pointer type, `A` can be “pointer to
|
| 826 |
+
function” even if the deduced `A` is “pointer to noexcept function”.
|
| 827 |
+
- If the original `A` is a pointer to member function type, `A` can be
|
| 828 |
+
“pointer to member of type function” even if the deduced `A` is
|
| 829 |
+
“pointer to member of type noexcept function”.
|
| 830 |
- The deduced `A` can be another pointer or pointer to member type that
|
| 831 |
can be converted to `A` via a qualification conversion.
|
| 832 |
|
| 833 |
These alternatives are considered only if type deduction would otherwise
|
| 834 |
fail. If they yield more than one possible deduced `A`, the type
|
|
|
|
| 840 |
|
| 841 |
If `A` is a type
|
| 842 |
|
| 843 |
and `P` is a type
|
| 844 |
|
| 845 |
+
then the cv-unqualified `T1` and `T2` are used as the types of `A` and
|
| 846 |
+
`P` respectively for type deduction.
|
| 847 |
+
|
| 848 |
+
[*Example 1*:
|
| 849 |
|
| 850 |
``` cpp
|
| 851 |
struct A {
|
| 852 |
template <class T> operator T***();
|
| 853 |
};
|
| 854 |
A a;
|
| 855 |
const int * const * const * p1 = a; // T is deduced as int, not const int
|
| 856 |
```
|
| 857 |
|
| 858 |
+
— *end example*]
|
| 859 |
+
|
| 860 |
#### Deducing template arguments during partial ordering <a id="temp.deduct.partial">[[temp.deduct.partial]]</a>
|
| 861 |
|
| 862 |
Template argument deduction is done by comparing certain types
|
| 863 |
associated with the two function templates being compared.
|
| 864 |
|
| 865 |
Two sets of types are used to determine the partial ordering. For each
|
| 866 |
of the templates involved there is the original function type and the
|
| 867 |
+
transformed function type.
|
| 868 |
+
|
| 869 |
+
[*Note 1*: The creation of the transformed type is described in
|
| 870 |
+
[[temp.func.order]]. — *end note*]
|
| 871 |
+
|
| 872 |
+
The deduction process uses the transformed type as the argument template
|
| 873 |
+
and the original type of the other template as the parameter template.
|
| 874 |
+
This process is done twice for each type involved in the partial
|
| 875 |
+
ordering comparison: once using the transformed template-1 as the
|
| 876 |
+
argument template and template-2 as the parameter template and again
|
| 877 |
+
using the transformed template-2 as the argument template and template-1
|
| 878 |
+
as the parameter template.
|
| 879 |
|
| 880 |
The types used to determine the ordering depend on the context in which
|
| 881 |
the partial ordering is done:
|
| 882 |
|
| 883 |
- In the context of a function call, the types used are those function
|
|
|
|
| 887 |
- In other contexts ([[temp.func.order]]) the function template’s
|
| 888 |
function type is used.
|
| 889 |
|
| 890 |
Each type nominated above from the parameter template and the
|
| 891 |
corresponding type from the argument template are used as the types of
|
| 892 |
+
`P` and `A`. If a particular `P` contains no *template-parameter*s that
|
| 893 |
+
participate in template argument deduction, that `P` is not used to
|
| 894 |
+
determine the ordering.
|
| 895 |
|
| 896 |
Before the partial ordering is done, certain transformations are
|
| 897 |
performed on the types used for partial ordering:
|
| 898 |
|
| 899 |
- If `P` is a reference type, `P` is replaced by the type referred to.
|
|
|
|
| 910 |
- If `P` is a cv-qualified type, `P` is replaced by the cv-unqualified
|
| 911 |
version of `P`.
|
| 912 |
- If `A` is a cv-qualified type, `A` is replaced by the cv-unqualified
|
| 913 |
version of `A`.
|
| 914 |
|
| 915 |
+
Using the resulting types `P` and `A`, the deduction is then done as
|
| 916 |
+
described in [[temp.deduct.type]]. If `P` is a function parameter pack,
|
| 917 |
+
the type `A` of each remaining parameter type of the argument template
|
| 918 |
+
is compared with the type `P` of the *declarator-id* of the function
|
| 919 |
+
parameter pack. Each comparison deduces template arguments for
|
| 920 |
+
subsequent positions in the template parameter packs expanded by the
|
| 921 |
+
function parameter pack. Similarly, if `A` was transformed from a
|
| 922 |
+
function parameter pack, it is compared with each remaining parameter
|
| 923 |
+
type of the parameter template. If deduction succeeds for a given type,
|
| 924 |
+
the type from the argument template is considered to be at least as
|
| 925 |
+
specialized as the type from the parameter template.
|
| 926 |
+
|
| 927 |
+
[*Example 1*:
|
| 928 |
|
| 929 |
``` cpp
|
| 930 |
template<class... Args> void f(Args... args); // #1
|
| 931 |
template<class T1, class... Args> void f(T1 a1, Args... args); // #2
|
| 932 |
template<class T1, class T2> void f(T1 a1, T2 a2); // #3
|
| 933 |
|
| 934 |
f(); // calls #1
|
| 935 |
f(1, 2, 3); // calls #2
|
| 936 |
+
f(1, 2); // calls #3; non-variadic template #3 is more specialized
|
| 937 |
+
// than the variadic templates #1 and #2
|
| 938 |
```
|
| 939 |
|
| 940 |
+
— *end example*]
|
| 941 |
+
|
| 942 |
If, for a given type, deduction succeeds in both directions (i.e., the
|
| 943 |
types are identical after the transformations above) and both `P` and
|
| 944 |
`A` were reference types (before being replaced with the type referred
|
| 945 |
to above):
|
| 946 |
|
| 947 |
- if the type from the argument template was an lvalue reference and the
|
| 948 |
+
type from the parameter template was not, the parameter type is not
|
| 949 |
+
considered to be at least as specialized as the argument type;
|
| 950 |
+
otherwise,
|
| 951 |
- if the type from the argument template is more cv-qualified than the
|
| 952 |
+
type from the parameter template (as described above), the parameter
|
| 953 |
+
type is not considered to be at least as specialized as the argument
|
| 954 |
+
type.
|
| 955 |
|
| 956 |
+
Function template `F` is *at least as specialized as* function template
|
| 957 |
+
`G` if, for each pair of types used to determine the ordering, the type
|
| 958 |
+
from `F` is at least as specialized as the type from `G`. `F` is *more
|
| 959 |
+
specialized than* `G` if `F` is at least as specialized as `G` and `G`
|
| 960 |
+
is not at least as specialized as `F`.
|
| 961 |
+
|
| 962 |
+
If, after considering the above, function template `F` is at least as
|
| 963 |
+
specialized as function template `G` and vice-versa, and if `G` has a
|
| 964 |
+
trailing parameter pack for which `F` does not have a corresponding
|
| 965 |
+
parameter, and if `F` does not have a trailing parameter pack, then `F`
|
| 966 |
+
is more specialized than `G`.
|
| 967 |
|
| 968 |
In most cases, all template parameters must have values in order for
|
| 969 |
deduction to succeed, but for partial ordering purposes a template
|
| 970 |
parameter may remain without a value provided it is not used in the
|
| 971 |
+
types being used for partial ordering.
|
| 972 |
+
|
| 973 |
+
[*Note 2*: A template parameter used in a non-deduced context is
|
| 974 |
+
considered used. — *end note*]
|
| 975 |
+
|
| 976 |
+
[*Example 2*:
|
| 977 |
|
| 978 |
``` cpp
|
| 979 |
template <class T> T f(int); // #1
|
| 980 |
template <class T, class U> T f(U); // #2
|
| 981 |
void g() {
|
| 982 |
f<int>(1); // calls #1
|
| 983 |
}
|
| 984 |
```
|
| 985 |
|
| 986 |
+
— *end example*]
|
| 987 |
+
|
| 988 |
+
[*Note 3*: Partial ordering of function templates containing template
|
| 989 |
+
parameter packs is independent of the number of deduced arguments for
|
| 990 |
+
those template parameter packs. — *end note*]
|
| 991 |
+
|
| 992 |
+
[*Example 3*:
|
| 993 |
|
| 994 |
``` cpp
|
| 995 |
template<class ...> struct Tuple { };
|
| 996 |
template<class ... Types> void g(Tuple<Types ...>); // #1
|
| 997 |
template<class T1, class ... Types> void g(Tuple<T1, Types ...>); // #2
|
|
|
|
| 1001 |
g(Tuple<int, float>()); // calls #2
|
| 1002 |
g(Tuple<int, float&>()); // calls #3
|
| 1003 |
g(Tuple<int>()); // calls #3
|
| 1004 |
```
|
| 1005 |
|
| 1006 |
+
— *end example*]
|
| 1007 |
+
|
| 1008 |
#### Deducing template arguments from a type <a id="temp.deduct.type">[[temp.deduct.type]]</a>
|
| 1009 |
|
| 1010 |
Template arguments can be deduced in several different contexts, but in
|
| 1011 |
each case a type that is specified in terms of template parameters (call
|
| 1012 |
it `P`) is compared with an actual type (call it `A`), and an attempt is
|
|
|
|
| 1021 |
deduced template argument values are then combined. If type deduction
|
| 1022 |
cannot be done for any `P/A` pair, or if for any pair the deduction
|
| 1023 |
leads to more than one possible set of deduced values, or if different
|
| 1024 |
pairs yield different deduced values, or if any template argument
|
| 1025 |
remains neither deduced nor explicitly specified, template argument
|
| 1026 |
+
deduction fails. The type of a type parameter is only deduced from an
|
| 1027 |
+
array bound if it is not otherwise deduced.
|
| 1028 |
|
| 1029 |
A given type `P` can be composed from a number of other types,
|
| 1030 |
templates, and non-type values:
|
| 1031 |
|
| 1032 |
- A function type includes the types of each of the function parameters
|
|
|
|
| 1047 |
deduction, but instead uses the values of template arguments that were
|
| 1048 |
either deduced elsewhere or explicitly specified. If a template
|
| 1049 |
parameter is used only in non-deduced contexts and is not explicitly
|
| 1050 |
specified, template argument deduction fails.
|
| 1051 |
|
| 1052 |
+
[*Note 1*: Under [[temp.deduct.call]] and [[temp.deduct.partial]], if
|
| 1053 |
+
`P` contains no *template-parameter*s that appear in deduced contexts,
|
| 1054 |
+
no deduction is done, so `P` and `A` need not have the same
|
| 1055 |
+
form. — *end note*]
|
| 1056 |
+
|
| 1057 |
The non-deduced contexts are:
|
| 1058 |
|
| 1059 |
- The *nested-name-specifier* of a type that was specified using a
|
| 1060 |
*qualified-id*.
|
| 1061 |
- The *expression* of a *decltype-specifier*.
|
|
|
|
| 1073 |
- no function matches the function parameter type, or
|
| 1074 |
- the set of functions supplied as an argument contains one or more
|
| 1075 |
function templates.
|
| 1076 |
- A function parameter for which the associated argument is an
|
| 1077 |
initializer list ([[dcl.init.list]]) but the parameter does not have
|
| 1078 |
+
a type for which deduction from an initializer list is specified (
|
| 1079 |
+
[[temp.deduct.call]]).
|
| 1080 |
+
\[*Example 1*:
|
| 1081 |
``` cpp
|
| 1082 |
template<class T> void g(T);
|
| 1083 |
g({1,2,3}); // error: no argument deduced for T
|
| 1084 |
```
|
| 1085 |
+
|
| 1086 |
+
— *end example*]
|
| 1087 |
- A function parameter pack that does not occur at the end of the
|
| 1088 |
*parameter-declaration-list*.
|
| 1089 |
|
| 1090 |
When a type name is specified in a way that includes a non-deduced
|
| 1091 |
context, all of the types that comprise that type name are also
|
| 1092 |
non-deduced. However, a compound type can include both deduced and
|
| 1093 |
+
non-deduced types.
|
| 1094 |
+
|
| 1095 |
+
[*Example 2*: If a type is specified as `A<T>::B<T2>`, both `T` and
|
| 1096 |
`T2` are non-deduced. Likewise, if a type is specified as
|
| 1097 |
`A<I+J>::X<T>`, `I`, `J`, and `T` are non-deduced. If a type is
|
| 1098 |
specified as `void` `f(typename` `A<T>::B,` `A<T>)`, the `T` in
|
| 1099 |
+
`A<T>::B` is non-deduced but the `T` in `A<T>` is
|
| 1100 |
+
deduced. — *end example*]
|
| 1101 |
+
|
| 1102 |
+
[*Example 3*:
|
| 1103 |
|
| 1104 |
Here is an example in which different parameter/argument pairs produce
|
| 1105 |
inconsistent template argument deductions:
|
| 1106 |
|
| 1107 |
``` cpp
|
| 1108 |
+
template<class T> void f(T x, T y) { ... }
|
| 1109 |
+
struct A { ... };
|
| 1110 |
+
struct B : A { ... };
|
| 1111 |
void g(A a, B b) {
|
| 1112 |
f(a,b); // error: T could be A or B
|
| 1113 |
f(b,a); // error: T could be A or B
|
| 1114 |
f(a,a); // OK: T is A
|
| 1115 |
f(b,b); // OK: T is B
|
|
|
|
| 1160 |
f(d); // calls f(B<int>&)
|
| 1161 |
f(d2); // calls f(B<int>&)
|
| 1162 |
}
|
| 1163 |
```
|
| 1164 |
|
| 1165 |
+
— *end example*]
|
| 1166 |
+
|
| 1167 |
A template type argument `T`, a template template argument `TT` or a
|
| 1168 |
template non-type argument `i` can be deduced if `P` and `A` have one of
|
| 1169 |
the following forms:
|
| 1170 |
|
| 1171 |
``` cpp
|
|
|
|
| 1194 |
TT<T>
|
| 1195 |
TT<i>
|
| 1196 |
TT<>
|
| 1197 |
```
|
| 1198 |
|
| 1199 |
+
where `(T)` represents a parameter-type-list ([[dcl.fct]]) where at
|
| 1200 |
+
least one parameter type contains a `T`, and `()` represents a
|
| 1201 |
+
parameter-type-list where no parameter type contains a `T`. Similarly,
|
| 1202 |
`<T>` represents template argument lists where at least one argument
|
| 1203 |
contains a `T`, `<i>` represents template argument lists where at least
|
| 1204 |
one argument contains an `i` and `<>` represents template argument lists
|
| 1205 |
where no argument contains a `T` or an `i`.
|
| 1206 |
|
| 1207 |
If `P` has a form that contains `<T>` or `<i>`, then each argument Pᵢ of
|
| 1208 |
+
the respective template argument list of `P` is compared with the
|
| 1209 |
corresponding argument Aᵢ of the corresponding template argument list of
|
| 1210 |
`A`. If the template argument list of `P` contains a pack expansion that
|
| 1211 |
is not the last template argument, the entire template argument list is
|
| 1212 |
a non-deduced context. If `Pᵢ` is a pack expansion, then the pattern of
|
| 1213 |
`Pᵢ` is compared with each remaining argument in the template argument
|
|
|
|
| 1219 |
- if `P` does not contain a template argument corresponding to `Aᵢ` then
|
| 1220 |
`Aᵢ` is ignored;
|
| 1221 |
- otherwise, if `Pᵢ` is not a pack expansion, template argument
|
| 1222 |
deduction fails.
|
| 1223 |
|
| 1224 |
+
[*Example 4*:
|
| 1225 |
+
|
| 1226 |
``` cpp
|
| 1227 |
template<class T1, class... Z> class S; // #1
|
| 1228 |
template<class T1, class... Z> class S<T1, const Z&...> { }; // #2
|
| 1229 |
template<class T1, class T2> class S<T1, const T2&> { }; // #3
|
| 1230 |
S<int, const int&> s; // both #2 and #3 match; #3 is more specialized
|
|
|
|
| 1233 |
template<class T1, class T2, class... U> struct A<T1, T2*, U...> { }; // #2
|
| 1234 |
template<class T1, class T2> struct A<T1, T2> { }; // #3
|
| 1235 |
template struct A<int, int*>; // selects #2
|
| 1236 |
```
|
| 1237 |
|
| 1238 |
+
— *end example*]
|
| 1239 |
+
|
| 1240 |
Similarly, if `P` has a form that contains `(T)`, then each parameter
|
| 1241 |
+
type `Pᵢ` of the respective parameter-type-list ([[dcl.fct]]) of `P` is
|
| 1242 |
+
compared with the corresponding parameter type `Aᵢ` of the corresponding
|
| 1243 |
+
parameter-type-list of `A`. If `P` and `A` are function types that
|
| 1244 |
originated from deduction when taking the address of a function
|
| 1245 |
template ([[temp.deduct.funcaddr]]) or when deducing template arguments
|
| 1246 |
from a function declaration ([[temp.deduct.decl]]) and `Pᵢ` and `Aᵢ`
|
| 1247 |
+
are parameters of the top-level parameter-type-list of `P` and `A`,
|
| 1248 |
+
respectively, `Pᵢ` is adjusted if it is a forwarding reference (
|
| 1249 |
+
[[temp.deduct.call]]) and `Aᵢ` is an lvalue reference, in which case the
|
| 1250 |
+
type of `Pᵢ` is changed to be the template parameter type (i.e., `T&&`
|
| 1251 |
+
is changed to simply `T`).
|
| 1252 |
+
|
| 1253 |
+
[*Note 2*: As a result, when `Pᵢ` is `T&&` and `Aᵢ` is `X&`, the
|
| 1254 |
+
adjusted `Pᵢ` will be `T`, causing `T` to be deduced as
|
| 1255 |
+
`X&`. — *end note*]
|
| 1256 |
+
|
| 1257 |
+
[*Example 5*:
|
| 1258 |
|
| 1259 |
``` cpp
|
| 1260 |
template <class T> void f(T&&);
|
| 1261 |
template <> void f(int&) { } // #1
|
| 1262 |
template <> void f(int&&) { } // #2
|
|
|
|
| 1264 |
f(i); // calls f<int&>(int&), i.e., #1
|
| 1265 |
f(0); // calls f<int>(int&&), i.e., #2
|
| 1266 |
}
|
| 1267 |
```
|
| 1268 |
|
| 1269 |
+
— *end example*]
|
| 1270 |
+
|
| 1271 |
If the *parameter-declaration* corresponding to `Pᵢ` is a function
|
| 1272 |
parameter pack, then the type of its *declarator-id* is compared with
|
| 1273 |
+
each remaining parameter type in the parameter-type-list of `A`. Each
|
| 1274 |
comparison deduces template arguments for subsequent positions in the
|
| 1275 |
template parameter packs expanded by the function parameter pack. During
|
| 1276 |
partial ordering ([[temp.deduct.partial]]), if `Aᵢ` was originally a
|
| 1277 |
function parameter pack:
|
| 1278 |
|
| 1279 |
- if `P` does not contain a function parameter type corresponding to
|
| 1280 |
`Aᵢ` then `Aᵢ` is ignored;
|
| 1281 |
- otherwise, if `Pᵢ` is not a function parameter pack, template argument
|
| 1282 |
deduction fails.
|
| 1283 |
|
| 1284 |
+
[*Example 6*:
|
| 1285 |
+
|
| 1286 |
``` cpp
|
| 1287 |
template<class T, class... U> void f(T*, U...) { } // #1
|
| 1288 |
template<class T> void f(T) { } // #2
|
| 1289 |
template void f(int*); // selects #1
|
| 1290 |
```
|
| 1291 |
|
| 1292 |
+
— *end example*]
|
| 1293 |
+
|
| 1294 |
These forms can be used in the same way as `T` is for further
|
| 1295 |
composition of types.
|
| 1296 |
|
| 1297 |
+
[*Example 7*:
|
| 1298 |
+
|
| 1299 |
``` cpp
|
| 1300 |
X<int> (*)(char[6])
|
| 1301 |
```
|
| 1302 |
|
| 1303 |
is of the form
|
|
|
|
| 1312 |
type (*)(T)
|
| 1313 |
```
|
| 1314 |
|
| 1315 |
where type is `X<int>` and `T` is `char[6]`.
|
| 1316 |
|
| 1317 |
+
— *end example*]
|
| 1318 |
+
|
| 1319 |
Template arguments cannot be deduced from function arguments involving
|
| 1320 |
constructs other than the ones specified above.
|
| 1321 |
|
| 1322 |
+
When the value of the argument corresponding to a non-type template
|
| 1323 |
+
parameter `P` that is declared with a dependent type is deduced from an
|
| 1324 |
+
expression, the template parameters in the type of `P` are deduced from
|
| 1325 |
+
the type of the value.
|
| 1326 |
+
|
| 1327 |
+
[*Example 8*:
|
| 1328 |
|
| 1329 |
``` cpp
|
| 1330 |
+
template<long n> struct A { };
|
| 1331 |
+
|
| 1332 |
+
template<typename T> struct C;
|
| 1333 |
+
template<typename T, T n> struct C<A<n>> {
|
| 1334 |
+
using Q = T;
|
| 1335 |
+
};
|
| 1336 |
+
|
| 1337 |
+
using R = long;
|
| 1338 |
+
using R = C<A<2>>::Q; // OK; T was deduced to long from the
|
| 1339 |
+
// template argument value in the type A<2>
|
| 1340 |
```
|
| 1341 |
|
| 1342 |
+
— *end example*]
|
| 1343 |
+
|
| 1344 |
+
The type of `N` in the type `T[N]` is `std::size_t`.
|
| 1345 |
+
|
| 1346 |
+
[*Example 9*:
|
| 1347 |
+
|
| 1348 |
+
``` cpp
|
| 1349 |
+
template<typename T> struct S;
|
| 1350 |
+
template<typename T, T n> struct S<int[n]> {
|
| 1351 |
+
using Q = T;
|
| 1352 |
+
};
|
| 1353 |
+
|
| 1354 |
+
using V = decltype(sizeof 0);
|
| 1355 |
+
using V = S<int[42]>::Q; // OK; T was deduced to std::size_t from the type int[42]
|
| 1356 |
+
```
|
| 1357 |
+
|
| 1358 |
+
— *end example*]
|
| 1359 |
+
|
| 1360 |
+
[*Example 10*:
|
| 1361 |
+
|
| 1362 |
+
``` cpp
|
| 1363 |
+
template<class T, T i> void f(int (&a)[i]);
|
| 1364 |
+
int v[10];
|
| 1365 |
+
void g() {
|
| 1366 |
+
f(v); // OK: T is std::size_t
|
| 1367 |
+
}
|
| 1368 |
+
```
|
| 1369 |
+
|
| 1370 |
+
— *end example*]
|
| 1371 |
+
|
| 1372 |
+
[*Note 3*:
|
| 1373 |
+
|
| 1374 |
Except for reference and pointer types, a major array bound is not part
|
| 1375 |
of a function parameter type and cannot be deduced from an argument:
|
| 1376 |
|
| 1377 |
``` cpp
|
| 1378 |
template<int i> void f1(int a[10][i]);
|
|
|
|
| 1387 |
f2<10>(v); // OK
|
| 1388 |
f3(v); // OK: i deduced to be 10
|
| 1389 |
}
|
| 1390 |
```
|
| 1391 |
|
| 1392 |
+
— *end note*]
|
| 1393 |
+
|
| 1394 |
+
[*Note 4*:
|
| 1395 |
+
|
| 1396 |
If, in the declaration of a function template with a non-type template
|
| 1397 |
parameter, the non-type template parameter is used in a subexpression in
|
| 1398 |
the function parameter list, the expression is a non-deduced context as
|
| 1399 |
specified above.
|
| 1400 |
|
| 1401 |
+
[*Example 11*:
|
| 1402 |
+
|
| 1403 |
``` cpp
|
| 1404 |
+
template <int i> class A { ... };
|
| 1405 |
template <int i> void g(A<i+1>);
|
| 1406 |
template <int i> void f(A<i>, A<i+1>);
|
| 1407 |
void k() {
|
| 1408 |
A<1> a1;
|
| 1409 |
A<2> a2;
|
|
|
|
| 1411 |
g<0>(a1); // OK
|
| 1412 |
f(a1, a2); // OK
|
| 1413 |
}
|
| 1414 |
```
|
| 1415 |
|
| 1416 |
+
— *end example*]
|
| 1417 |
+
|
| 1418 |
+
— *end note*]
|
| 1419 |
+
|
| 1420 |
+
[*Note 5*:
|
| 1421 |
+
|
| 1422 |
Template parameters do not participate in template argument deduction if
|
| 1423 |
they are used only in non-deduced contexts. For example,
|
| 1424 |
|
| 1425 |
``` cpp
|
| 1426 |
template<int i, typename T>
|
|
|
|
| 1429 |
typename B<i>::Y y); // i is not deduced here
|
| 1430 |
A<int> a;
|
| 1431 |
B<77> b;
|
| 1432 |
|
| 1433 |
int x = deduce<77>(a.xm, 62, b.ym);
|
| 1434 |
+
// T is deduced to be int, a.xm must be convertible to A<int>::X
|
| 1435 |
+
// i is explicitly specified to be 77, b.ym must be convertible to B<77>::Y
|
|
|
|
|
|
|
| 1436 |
```
|
| 1437 |
|
| 1438 |
+
— *end note*]
|
| 1439 |
+
|
| 1440 |
+
If `P` has a form that contains `<i>`, and if the type of `i` differs
|
| 1441 |
+
from the type of the corresponding template parameter of the template
|
| 1442 |
+
named by the enclosing *simple-template-id*, deduction fails. If `P` has
|
| 1443 |
+
a form that contains `[i]`, and if the type of `i` is not an integral
|
| 1444 |
+
type, deduction fails.[^8]
|
| 1445 |
+
|
| 1446 |
+
[*Example 12*:
|
| 1447 |
|
| 1448 |
``` cpp
|
| 1449 |
+
template<int i> class A { ... };
|
| 1450 |
template<short s> void f(A<s>);
|
| 1451 |
void k1() {
|
| 1452 |
A<1> a;
|
| 1453 |
f(a); // error: deduction fails for conversion from int to short
|
| 1454 |
f<1>(a); // OK
|
|
|
|
| 1460 |
B<1> b;
|
| 1461 |
g(b); // OK: cv-qualifiers are ignored on template parameter types
|
| 1462 |
}
|
| 1463 |
```
|
| 1464 |
|
| 1465 |
+
— *end example*]
|
| 1466 |
+
|
| 1467 |
A *template-argument* can be deduced from a function, pointer to
|
| 1468 |
function, or pointer to member function type.
|
| 1469 |
|
| 1470 |
+
[*Example 13*:
|
| 1471 |
+
|
| 1472 |
``` cpp
|
| 1473 |
template<class T> void f(void(*)(T,int));
|
| 1474 |
template<class T> void foo(T,int);
|
| 1475 |
void g(int,int);
|
| 1476 |
void g(char,int);
|
|
|
|
| 1482 |
f(&h); // OK: void h(char,int) is a unique match
|
| 1483 |
f(&foo); // error: type deduction fails because foo is a template
|
| 1484 |
}
|
| 1485 |
```
|
| 1486 |
|
| 1487 |
+
— *end example*]
|
| 1488 |
+
|
| 1489 |
A template *type-parameter* cannot be deduced from the type of a
|
| 1490 |
function default argument.
|
| 1491 |
|
| 1492 |
+
[*Example 14*:
|
| 1493 |
+
|
| 1494 |
``` cpp
|
| 1495 |
template <class T> void f(T = 5, T = 7);
|
| 1496 |
void g() {
|
| 1497 |
f(1); // OK: call f<int>(1,7)
|
| 1498 |
f(); // error: cannot deduce T
|
| 1499 |
f<int>(); // OK: call f<int>(5,7)
|
| 1500 |
}
|
| 1501 |
```
|
| 1502 |
|
| 1503 |
+
— *end example*]
|
| 1504 |
+
|
| 1505 |
The *template-argument* corresponding to a template *template-parameter*
|
| 1506 |
is deduced from the type of the *template-argument* of a class template
|
| 1507 |
specialization used in the argument list of a function call.
|
| 1508 |
|
| 1509 |
+
[*Example 15*:
|
| 1510 |
+
|
| 1511 |
``` cpp
|
| 1512 |
template <template <class T> class X> struct A { };
|
| 1513 |
template <template <class T> class X> void f(A<X>) { }
|
| 1514 |
template<class T> struct B { };
|
| 1515 |
A<B> ab;
|
| 1516 |
f(ab); // calls f(A<B>)
|
| 1517 |
```
|
| 1518 |
|
| 1519 |
+
— *end example*]
|
| 1520 |
+
|
| 1521 |
+
[*Note 6*: Template argument deduction involving parameter packs (
|
| 1522 |
[[temp.variadic]]) can deduce zero or more arguments for each parameter
|
| 1523 |
+
pack. — *end note*]
|
| 1524 |
+
|
| 1525 |
+
[*Example 16*:
|
| 1526 |
|
| 1527 |
``` cpp
|
| 1528 |
template<class> struct X { };
|
| 1529 |
template<class R, class ... ArgTypes> struct X<R(int, ArgTypes ...)> { };
|
| 1530 |
template<class ... Types> struct Y { };
|
|
|
|
| 1540 |
Y<int&, float&, double&> y2; // uses partial specialization; T is int&, Types contains float, double
|
| 1541 |
Y<int, float, double> y3; // uses primary template; Types contains int, float, double
|
| 1542 |
int fv = f(g); // OK; Types contains int, float
|
| 1543 |
```
|
| 1544 |
|
| 1545 |
+
— *end example*]
|
| 1546 |
+
|
| 1547 |
#### Deducing template arguments from a function declaration <a id="temp.deduct.decl">[[temp.deduct.decl]]</a>
|
| 1548 |
|
| 1549 |
In a declaration whose *declarator-id* refers to a specialization of a
|
| 1550 |
function template, template argument deduction is performed to identify
|
| 1551 |
the specialization to which the declaration refers. Specifically, this
|
|
|
|
| 1578 |
the call arguments. For each function template, if the argument
|
| 1579 |
deduction and checking succeeds, the *template-argument*s (deduced
|
| 1580 |
and/or explicit) are used to synthesize the declaration of a single
|
| 1581 |
function template specialization which is added to the candidate
|
| 1582 |
functions set to be used in overload resolution. If, for a given
|
| 1583 |
+
function template, argument deduction fails or the synthesized function
|
| 1584 |
+
template specialization would be ill-formed, no such function is added
|
| 1585 |
to the set of candidate functions for that template. The complete set of
|
| 1586 |
candidate functions includes all the synthesized declarations and all of
|
| 1587 |
the non-template overloaded functions of the same name. The synthesized
|
| 1588 |
declarations are treated like any other functions in the remainder of
|
| 1589 |
overload resolution, except as explicitly noted in
|
| 1590 |
[[over.match.best]].[^9]
|
| 1591 |
|
| 1592 |
+
[*Example 1*:
|
| 1593 |
+
|
| 1594 |
``` cpp
|
| 1595 |
template<class T> T max(T a, T b) { return a>b?a:b; }
|
| 1596 |
|
| 1597 |
void f(int a, int b, char c, char d) {
|
| 1598 |
int m1 = max(a,b); // max(int a, int b)
|
|
|
|
| 1609 |
|
| 1610 |
to the example above would resolve the third call, by providing a
|
| 1611 |
function that could be called for `max(a,c)` after using the standard
|
| 1612 |
conversion of `char` to `int` for `c`.
|
| 1613 |
|
| 1614 |
+
— *end example*]
|
| 1615 |
+
|
| 1616 |
+
[*Example 2*:
|
| 1617 |
+
|
| 1618 |
Here is an example involving conversions on a function argument involved
|
| 1619 |
in *template-argument* deduction:
|
| 1620 |
|
| 1621 |
``` cpp
|
| 1622 |
+
template<class T> struct B { ... };
|
| 1623 |
+
template<class T> struct D : public B<T> { ... };
|
| 1624 |
template<class T> void f(B<T>&);
|
| 1625 |
|
| 1626 |
void g(B<int>& bi, D<int>& di) {
|
| 1627 |
f(bi); // f(bi)
|
| 1628 |
f(di); // f((B<int>&)di)
|
| 1629 |
}
|
| 1630 |
```
|
| 1631 |
|
| 1632 |
+
— *end example*]
|
| 1633 |
+
|
| 1634 |
+
[*Example 3*:
|
| 1635 |
+
|
| 1636 |
Here is an example involving conversions on a function argument not
|
| 1637 |
involved in *template-parameter* deduction:
|
| 1638 |
|
| 1639 |
``` cpp
|
| 1640 |
template<class T> void f(T*,int); // #1
|
|
|
|
| 1647 |
f(i,c); // #2: f<int>(i,c);
|
| 1648 |
f(i,i); // #2: f<int>(i,char(i))
|
| 1649 |
}
|
| 1650 |
```
|
| 1651 |
|
| 1652 |
+
— *end example*]
|
| 1653 |
+
|
| 1654 |
Only the signature of a function template specialization is needed to
|
| 1655 |
enter the specialization in a set of candidate functions. Therefore only
|
| 1656 |
the function template declaration is needed to resolve a call for which
|
| 1657 |
a template specialization is a candidate.
|
| 1658 |
|
| 1659 |
+
[*Example 4*:
|
| 1660 |
+
|
| 1661 |
``` cpp
|
| 1662 |
template<class T> void f(T); // declaration
|
| 1663 |
|
| 1664 |
void g() {
|
| 1665 |
f("Annemarie"); // call of f<const char*>
|
|
|
|
| 1669 |
The call of `f` is well-formed even if the template `f` is only declared
|
| 1670 |
and not defined at the point of the call. The program will be ill-formed
|
| 1671 |
unless a specialization for `f<const char*>`, either implicitly or
|
| 1672 |
explicitly generated, is present in some translation unit.
|
| 1673 |
|
| 1674 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1675 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|