- tmp/tmp63e99m4b/{from.md → to.md} +2113 -933
tmp/tmp63e99m4b/{from.md → to.md}
RENAMED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
# Templates <a id="temp">[[temp]]</a>
|
| 2 |
|
| 3 |
-
A *template* defines a family of classes
|
| 4 |
-
family of types.
|
| 5 |
|
| 6 |
``` bnf
|
| 7 |
template-declaration:
|
| 8 |
'template <' template-parameter-list '>' declaration
|
| 9 |
```
|
|
@@ -12,28 +12,32 @@ template-declaration:
|
|
| 12 |
template-parameter-list:
|
| 13 |
template-parameter
|
| 14 |
template-parameter-list ',' template-parameter
|
| 15 |
```
|
| 16 |
|
| 17 |
-
The `>` token following the
|
| 18 |
-
`>{>}` token by
|
|
|
|
| 19 |
|
| 20 |
The *declaration* in a *template-declaration* shall
|
| 21 |
|
| 22 |
- declare or define a function, a class, or a variable, or
|
| 23 |
- define a member function, a member class, a member enumeration, or a
|
| 24 |
static data member of a class template or of a class nested within a
|
| 25 |
class template, or
|
| 26 |
- define a member template of a class or class template, or
|
|
|
|
| 27 |
- be an *alias-declaration*.
|
| 28 |
|
| 29 |
A *template-declaration* is a *declaration*. A *template-declaration* is
|
| 30 |
also a definition if its *declaration* defines a function, a class, a
|
| 31 |
variable, or a static data member. A declaration introduced by a
|
| 32 |
template declaration of a variable is a *variable template*. A variable
|
| 33 |
template at class scope is a *static data member template*.
|
| 34 |
|
|
|
|
|
|
|
| 35 |
``` cpp
|
| 36 |
template<class T>
|
| 37 |
constexpr T pi = T(3.1415926535897932385L);
|
| 38 |
template<class T>
|
| 39 |
T circular_area(T r) {
|
|
@@ -49,47 +53,65 @@ struct matrix_constants {
|
|
| 49 |
template<class T>
|
| 50 |
constexpr pauli<T> sigma3 = { { 1, 0 }, { 0, -1 } };
|
| 51 |
};
|
| 52 |
```
|
| 53 |
|
|
|
|
|
|
|
| 54 |
A *template-declaration* can appear only as a namespace scope or class
|
| 55 |
scope declaration. In a function template declaration, the last
|
| 56 |
-
component of the *declarator-id* shall not be a *template-id*.
|
| 57 |
-
|
| 58 |
-
*
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
| 62 |
|
| 63 |
In a *template-declaration*, explicit specialization, or explicit
|
| 64 |
instantiation the *init-declarator-list* in the declaration shall
|
| 65 |
contain at most one declarator. When such a declaration is used to
|
| 66 |
declare a class template, no declarator is permitted.
|
| 67 |
|
| 68 |
-
A template name has linkage ([[basic.link]]).
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
definitions for the purpose
|
| 80 |
-
and must also obey the
|
|
|
|
| 81 |
|
| 82 |
A class template shall not have the same name as any other template,
|
| 83 |
class, function, variable, enumeration, enumerator, namespace, or type
|
| 84 |
-
in the same scope ([[basic.scope]]), except as specified in
|
| 85 |
-
[[temp.class.spec]]
|
| 86 |
either by non-template functions ([[dcl.fct]]) with the same name or by
|
| 87 |
other function templates with the same name ([[temp.over]]), a template
|
| 88 |
name declared in namespace scope or in class scope shall be unique in
|
| 89 |
that scope.
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
A function template, member function of a class template, variable
|
| 92 |
template, or static data member of a class template shall be defined in
|
| 93 |
every translation unit in which it is implicitly instantiated (
|
| 94 |
[[temp.inst]]) unless the corresponding specialization is explicitly
|
| 95 |
instantiated ([[temp.explicit]]) in some translation unit; no
|
|
@@ -105,50 +127,37 @@ template-parameter:
|
|
| 105 |
parameter-declaration
|
| 106 |
```
|
| 107 |
|
| 108 |
``` bnf
|
| 109 |
type-parameter:
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
'
|
| 113 |
-
'
|
| 114 |
-
'template <' template-parameter-list '> class' '...'ₒₚₜ identifierₒₚₜ
|
| 115 |
-
'template <' template-parameter-list '> class' identifierₒₚₜ '=' id-expression
|
| 116 |
```
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
There is no semantic difference between `class` and `typename` in a
|
| 122 |
-
*
|
| 123 |
template type parameter. `typename` followed by a *qualified-id* denotes
|
| 124 |
-
the type in a non-type [^1] *parameter-declaration*. A
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
parameter may be a class template. For example,
|
| 128 |
|
| 129 |
-
|
| 130 |
-
template<class T> class myarray { /* ... */ };
|
| 131 |
-
|
| 132 |
-
template<class K, class V, template<class T> class C = myarray>
|
| 133 |
-
class Map {
|
| 134 |
-
C<K> key;
|
| 135 |
-
C<V> value;
|
| 136 |
-
};
|
| 137 |
-
```
|
| 138 |
-
|
| 139 |
-
A *type-parameter* whose identifier does not follow an ellipsis defines
|
| 140 |
-
its *identifier* to be a *typedef-name* (if declared with `class` or
|
| 141 |
-
`typename`) or *template-name* (if declared with `template`) in the
|
| 142 |
-
scope of the template declaration. Because of the name lookup rules, a
|
| 143 |
-
*template-parameter* that could be interpreted as either a non-type
|
| 144 |
-
*template-parameter* or a *type-parameter* (because its *identifier* is
|
| 145 |
-
the name of an already existing class) is taken as a *type-parameter*.
|
| 146 |
-
For example,
|
| 147 |
|
| 148 |
``` cpp
|
| 149 |
-
class T {
|
| 150 |
int i;
|
| 151 |
|
| 152 |
template<class T, T i> void f(T t) {
|
| 153 |
T t1 = i; // template-parameters T and i
|
| 154 |
::T t2 = ::i; // global namespace members T and i
|
|
@@ -156,30 +165,63 @@ template<class T, T i> void f(T t) {
|
|
| 156 |
```
|
| 157 |
|
| 158 |
Here, the template `f` has a *type-parameter* called `T`, rather than an
|
| 159 |
unnamed non-type *template-parameter* of class `T`.
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
A non-type *template-parameter* shall have one of the following
|
| 162 |
-
(optionally
|
| 163 |
|
| 164 |
- integral or enumeration type,
|
| 165 |
- pointer to object or pointer to function,
|
| 166 |
- lvalue reference to object or lvalue reference to function,
|
| 167 |
- pointer to member,
|
| 168 |
-
- `std::nullptr_t`
|
|
|
|
| 169 |
|
| 170 |
-
Other types are disallowed either explicitly below or
|
| 171 |
-
rules governing the form of *template-argument*s (
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
| 174 |
|
| 175 |
A non-type non-reference *template-parameter* is a prvalue. It shall not
|
| 176 |
be assigned to or in any other way have its value changed. A non-type
|
| 177 |
non-reference *template-parameter* cannot have its address taken. When a
|
| 178 |
non-type non-reference *template-parameter* is used as an initializer
|
| 179 |
for a reference, a temporary is always used.
|
| 180 |
|
|
|
|
|
|
|
| 181 |
``` cpp
|
| 182 |
template<const X& x, int i> void f() {
|
| 183 |
i++; // error: change of template-parameter value
|
| 184 |
|
| 185 |
&x; // OK
|
|
@@ -188,34 +230,43 @@ template<const X& x, int i> void f() {
|
|
| 188 |
int& ri = i; // error: non-const reference bound to temporary
|
| 189 |
const int& cri = i; // OK: const reference bound to temporary
|
| 190 |
}
|
| 191 |
```
|
| 192 |
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
``` cpp
|
| 197 |
template<double d> class X; // error
|
| 198 |
template<double* pd> class Y; // OK
|
| 199 |
template<double& rd> class Z; // OK
|
| 200 |
```
|
| 201 |
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
``` cpp
|
| 207 |
-
template<int* a> struct R {
|
| 208 |
-
template<int b[5]> struct S {
|
| 209 |
int p;
|
| 210 |
R<&p> w; // OK
|
| 211 |
S<&p> x; // OK due to parameter adjustment
|
| 212 |
int v[5];
|
| 213 |
R<v> y; // OK due to implicit argument conversion
|
| 214 |
S<v> z; // OK due to both adjustment and conversion
|
| 215 |
```
|
| 216 |
|
|
|
|
|
|
|
| 217 |
A *default template-argument* is a *template-argument* ([[temp.arg]])
|
| 218 |
specified after `=` in a *template-parameter*. A default
|
| 219 |
*template-argument* may be specified for any kind of
|
| 220 |
*template-parameter* (type, non-type, template) that is not a template
|
| 221 |
parameter pack ([[temp.variadic]]). A default *template-argument* may
|
|
@@ -226,16 +277,17 @@ member’s class. A default *template-argument* shall not be specified in
|
|
| 226 |
a friend class template declaration. If a friend function template
|
| 227 |
declaration specifies a default *template-argument*, that declaration
|
| 228 |
shall be a definition and shall be the only declaration of the function
|
| 229 |
template in the translation unit.
|
| 230 |
|
| 231 |
-
The set of default *template-argument*s available for use
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
scope in the same way default function arguments are (
|
| 235 |
[[dcl.fct.default]]).
|
| 236 |
|
|
|
|
|
|
|
| 237 |
``` cpp
|
| 238 |
template<class T1, class T2 = int> class A;
|
| 239 |
template<class T1 = int, class T2> class A;
|
| 240 |
```
|
| 241 |
|
|
@@ -243,53 +295,73 @@ is equivalent to
|
|
| 243 |
|
| 244 |
``` cpp
|
| 245 |
template<class T1 = int, class T2 = int> class A;
|
| 246 |
```
|
| 247 |
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
parameter
|
| 256 |
-
template
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
``` cpp
|
| 259 |
template<class T1 = int, class T2> class B; // error
|
| 260 |
|
| 261 |
// U can be neither deduced from the parameter-type-list nor specified
|
| 262 |
template<class... T, class... U> void f() { } // error
|
| 263 |
template<class... T, class U> void g() { } // error
|
| 264 |
```
|
| 265 |
|
|
|
|
|
|
|
| 266 |
A *template-parameter* shall not be given default arguments by two
|
| 267 |
different declarations in the same scope.
|
| 268 |
|
|
|
|
|
|
|
| 269 |
``` cpp
|
| 270 |
template<class T = int> class X;
|
| 271 |
-
template<class T = int> class X {
|
| 272 |
```
|
| 273 |
|
|
|
|
|
|
|
| 274 |
When parsing a default *template-argument* for a non-type
|
| 275 |
*template-parameter*, the first non-nested `>` is taken as the end of
|
| 276 |
the *template-parameter-list* rather than a greater-than operator.
|
| 277 |
|
|
|
|
|
|
|
| 278 |
``` cpp
|
| 279 |
template<int i = 3 > 4 > // syntax error
|
| 280 |
-
class X {
|
| 281 |
|
| 282 |
template<int i = (3 > 4) > // OK
|
| 283 |
-
class Y {
|
| 284 |
```
|
| 285 |
|
|
|
|
|
|
|
| 286 |
A *template-parameter* of a template *template-parameter* is permitted
|
| 287 |
to have a default *template-argument*. When such default arguments are
|
| 288 |
specified, they apply to the template *template-parameter* in the scope
|
| 289 |
of the template *template-parameter*.
|
| 290 |
|
|
|
|
|
|
|
| 291 |
``` cpp
|
| 292 |
template <class T = float> struct B {};
|
| 293 |
template <template <class TT = float> class T> struct A {
|
| 294 |
inline void f();
|
| 295 |
inline void g();
|
|
@@ -300,10 +372,12 @@ template <template <class TT> class T> void A<T>::f() {
|
|
| 300 |
template <template <class TT = char> class T> void A<T>::g() {
|
| 301 |
T<> t; // OK - T<char>
|
| 302 |
}
|
| 303 |
```
|
| 304 |
|
|
|
|
|
|
|
| 305 |
If a *template-parameter* is a *type-parameter* with an ellipsis prior
|
| 306 |
to its optional *identifier* or is a *parameter-declaration* that
|
| 307 |
declares a parameter pack ([[dcl.fct]]), then the *template-parameter*
|
| 308 |
is a template parameter pack ([[temp.variadic]]). A template parameter
|
| 309 |
pack that is a *parameter-declaration* whose type contains one or more
|
|
@@ -312,10 +386,12 @@ parameter pack that is a *type-parameter* with a
|
|
| 312 |
*template-parameter-list* containing one or more unexpanded parameter
|
| 313 |
packs is a pack expansion. A template parameter pack that is a pack
|
| 314 |
expansion shall not expand a parameter pack declared in the same
|
| 315 |
*template-parameter-list*.
|
| 316 |
|
|
|
|
|
|
|
| 317 |
``` cpp
|
| 318 |
template <class... Types> class Tuple; // Types is a template type parameter pack
|
| 319 |
// but not a pack expansion
|
| 320 |
template <class T, int... Dims> struct multi_array; // Dims is a non-type template parameter pack
|
| 321 |
// but not a pack expansion
|
|
@@ -325,10 +401,12 @@ template<class... T> struct value_holder {
|
|
| 325 |
};
|
| 326 |
template<class... T, T... Values> struct static_array;// error: Values expands template type parameter
|
| 327 |
// pack T within the same template parameter list
|
| 328 |
```
|
| 329 |
|
|
|
|
|
|
|
| 330 |
## Names of template specializations <a id="temp.names">[[temp.names]]</a>
|
| 331 |
|
| 332 |
A template specialization ([[temp.spec]]) can be referred to by a
|
| 333 |
*template-id*:
|
| 334 |
|
|
@@ -360,13 +438,13 @@ template-argument:
|
|
| 360 |
constant-expression
|
| 361 |
type-id
|
| 362 |
id-expression
|
| 363 |
```
|
| 364 |
|
| 365 |
-
The name lookup rules ([[basic.lookup]]) are used to
|
| 366 |
-
of a name with a template declaration; that is, to
|
| 367 |
-
*template-name*.
|
| 368 |
|
| 369 |
For a *template-name* to be explicitly qualified by the template
|
| 370 |
arguments, the name must be known to refer to a template.
|
| 371 |
|
| 372 |
After name lookup ([[basic.lookup]]) finds that a name is a
|
|
@@ -376,34 +454,49 @@ of which is a function template, if this is followed by a `<`, the `<`
|
|
| 376 |
is always taken as the delimiter of a *template-argument-list* and never
|
| 377 |
as the less-than operator. When parsing a *template-argument-list*, the
|
| 378 |
first non-nested `>`[^2] is taken as the ending delimiter rather than a
|
| 379 |
greater-than operator. Similarly, the first non-nested `>{>}` is treated
|
| 380 |
as two consecutive but distinct `>` tokens, the first of which is taken
|
| 381 |
-
as the end of the and completes the
|
| 382 |
-
|
| 383 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
|
| 385 |
``` cpp
|
| 386 |
-
template<int i> class X {
|
| 387 |
|
| 388 |
X< 1>2 > x1; // syntax error
|
| 389 |
X<(1>2)> x2; // OK
|
| 390 |
|
| 391 |
-
template<class T> class Y {
|
| 392 |
Y<X<1>> x3; // OK, same as Y<X<1> > x3;
|
| 393 |
Y<X<6>>1>> x4; // syntax error
|
| 394 |
Y<X<(6>>1)>> x5; // OK
|
| 395 |
```
|
| 396 |
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 405 |
|
| 406 |
``` cpp
|
| 407 |
struct X {
|
| 408 |
template<std::size_t> X* alloc();
|
| 409 |
template<std::size_t> static X* adjust();
|
|
@@ -414,18 +507,25 @@ template<class T> void f(T* p) {
|
|
| 414 |
T::adjust<100>(); // ill-formed: < means less than
|
| 415 |
T::template adjust<100>(); // OK: < starts template argument list
|
| 416 |
}
|
| 417 |
```
|
| 418 |
|
|
|
|
|
|
|
| 419 |
A name prefixed by the keyword `template` shall be a *template-id* or
|
| 420 |
-
the name shall refer to a class template
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
in
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 427 |
|
| 428 |
``` cpp
|
| 429 |
template <class T> struct A {
|
| 430 |
void f(int);
|
| 431 |
template <class U> void f(U);
|
|
@@ -444,10 +544,12 @@ template <class T> struct B {
|
|
| 444 |
// OK: T::template C names a class template:
|
| 445 |
template <class T, template <class X> class TT = T::template C> struct D { };
|
| 446 |
D<B<int> > db;
|
| 447 |
```
|
| 448 |
|
|
|
|
|
|
|
| 449 |
A *simple-template-id* that names a class template specialization is a
|
| 450 |
*class-name* (Clause [[class]]).
|
| 451 |
|
| 452 |
A *template-id* that names an alias template specialization is a
|
| 453 |
*type-name*.
|
|
@@ -461,10 +563,12 @@ match the type and form specified for the corresponding parameter
|
|
| 461 |
declared by the template in its *template-parameter-list*. When the
|
| 462 |
parameter declared by the template is a template parameter pack (
|
| 463 |
[[temp.variadic]]), it will correspond to zero or more
|
| 464 |
*template-argument*s.
|
| 465 |
|
|
|
|
|
|
|
| 466 |
``` cpp
|
| 467 |
template<class T> class Array {
|
| 468 |
T* v;
|
| 469 |
int sz;
|
| 470 |
public:
|
|
@@ -472,119 +576,159 @@ public:
|
|
| 472 |
T& operator[](int);
|
| 473 |
T& elem(int i) { return v[i]; }
|
| 474 |
};
|
| 475 |
|
| 476 |
Array<int> v1(20);
|
| 477 |
-
typedef std::complex<double> dcomplex; // std::complex is a standard
|
| 478 |
-
// library template
|
| 479 |
Array<dcomplex> v2(30);
|
| 480 |
Array<dcomplex> v3(40);
|
| 481 |
|
| 482 |
void bar() {
|
| 483 |
v1[3] = 7;
|
| 484 |
v2[3] = v3.elem(4) = dcomplex(7,8);
|
| 485 |
}
|
| 486 |
```
|
| 487 |
|
|
|
|
|
|
|
| 488 |
In a *template-argument*, an ambiguity between a *type-id* and an
|
| 489 |
expression is resolved to a *type-id*, regardless of the form of the
|
| 490 |
corresponding *template-parameter*.[^3]
|
| 491 |
|
|
|
|
|
|
|
| 492 |
``` cpp
|
| 493 |
template<class T> void f();
|
| 494 |
template<int I> void f();
|
| 495 |
|
| 496 |
void g() {
|
| 497 |
f<int()>(); // int() is a type-id: call the first f()
|
| 498 |
}
|
| 499 |
```
|
| 500 |
|
|
|
|
|
|
|
| 501 |
The name of a *template-argument* shall be accessible at the point where
|
| 502 |
-
it is used as a *template-argument*.
|
| 503 |
-
|
| 504 |
-
*
|
| 505 |
-
|
| 506 |
-
|
|
|
|
|
|
|
|
|
|
| 507 |
|
| 508 |
``` cpp
|
| 509 |
template<class T> class X {
|
| 510 |
static T t;
|
| 511 |
};
|
| 512 |
|
| 513 |
class Y {
|
| 514 |
private:
|
| 515 |
-
struct S {
|
| 516 |
X<S> x; // OK: S is accessible
|
| 517 |
// X<Y::S> has a static member of type Y::S
|
| 518 |
// OK: even though Y::S is private
|
| 519 |
};
|
| 520 |
|
| 521 |
X<Y::S> y; // error: S not accessible
|
| 522 |
```
|
| 523 |
|
|
|
|
|
|
|
| 524 |
For a *template-argument* that is a class type or a class template, the
|
| 525 |
template definition has no special access rights to the members of the
|
| 526 |
*template-argument*.
|
| 527 |
|
|
|
|
|
|
|
| 528 |
``` cpp
|
| 529 |
template <template <class TT> class T> class A {
|
| 530 |
typename T<int>::S s;
|
| 531 |
};
|
| 532 |
|
| 533 |
template <class U> class B {
|
| 534 |
private:
|
| 535 |
-
struct S {
|
| 536 |
};
|
| 537 |
|
| 538 |
A<B> b; // ill-formed: A has no access to B::S
|
| 539 |
```
|
| 540 |
|
|
|
|
|
|
|
| 541 |
When template argument packs or default *template-argument*s are used, a
|
| 542 |
*template-argument* list can be empty. In that case the empty `<>`
|
| 543 |
-
brackets shall still be used as the *template-argument-list
|
|
|
|
|
|
|
| 544 |
|
| 545 |
``` cpp
|
| 546 |
template<class T = char> class String;
|
| 547 |
String<>* p; // OK: String<char>
|
| 548 |
String* q; // syntax error
|
| 549 |
template<class ... Elements> class Tuple;
|
| 550 |
Tuple<>* t; // OK: Elements is empty
|
| 551 |
Tuple* u; // syntax error
|
| 552 |
```
|
| 553 |
|
|
|
|
|
|
|
| 554 |
An explicit destructor call ([[class.dtor]]) for an object that has a
|
| 555 |
type that is a class template specialization may explicitly specify the
|
| 556 |
*template-argument*s.
|
| 557 |
|
|
|
|
|
|
|
| 558 |
``` cpp
|
| 559 |
template<class T> struct A {
|
| 560 |
~A();
|
| 561 |
};
|
| 562 |
void f(A<int>* p, A<int>* q) {
|
| 563 |
p->A<int>::~A(); // OK: destructor call
|
| 564 |
q->A<int>::~A<int>(); // OK: destructor call
|
| 565 |
}
|
| 566 |
```
|
| 567 |
|
|
|
|
|
|
|
| 568 |
If the use of a *template-argument* gives rise to an ill-formed
|
| 569 |
construct in the instantiation of a template specialization, the program
|
| 570 |
is ill-formed.
|
| 571 |
|
| 572 |
When the template in a *template-id* is an overloaded function template,
|
| 573 |
both non-template functions in the overload set and function templates
|
| 574 |
in the overload set for which the *template-argument*s do not match the
|
| 575 |
*template-parameter*s are ignored. If none of the function templates
|
| 576 |
have matching *template-parameter*s, the program is ill-formed.
|
| 577 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 578 |
A *template-argument* followed by an ellipsis is a pack expansion (
|
| 579 |
[[temp.variadic]]).
|
| 580 |
|
| 581 |
### Template type arguments <a id="temp.arg.type">[[temp.arg.type]]</a>
|
| 582 |
|
| 583 |
A *template-argument* for a *template-parameter* which is a type shall
|
| 584 |
be a *type-id*.
|
| 585 |
|
|
|
|
|
|
|
| 586 |
``` cpp
|
| 587 |
template <class T> class X { };
|
| 588 |
template <class T> void f(T t) { }
|
| 589 |
struct { } unnamed_obj;
|
| 590 |
|
|
@@ -600,161 +744,135 @@ void f() {
|
|
| 600 |
f(unnamed_obj); // OK
|
| 601 |
f(b); // OK
|
| 602 |
}
|
| 603 |
```
|
| 604 |
|
| 605 |
-
|
| 606 |
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
syntactic form of a function declarator to have function type, the
|
| 610 |
-
program is ill-formed.
|
| 611 |
-
|
| 612 |
-
``` cpp
|
| 613 |
-
template<class T> struct A {
|
| 614 |
-
static T t;
|
| 615 |
-
};
|
| 616 |
-
typedef int function();
|
| 617 |
-
A<function> a; // ill-formed: would declare A<function>::t
|
| 618 |
-
// as a static member function
|
| 619 |
-
```
|
| 620 |
|
| 621 |
### Template non-type arguments <a id="temp.arg.nontype">[[temp.arg.nontype]]</a>
|
| 622 |
|
| 623 |
-
|
| 624 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 625 |
|
| 626 |
-
- for a non-type *template-parameter*
|
| 627 |
converted constant expression ([[expr.const]]) of the type of the
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 648 |
*template-argument*.
|
| 649 |
|
|
|
|
|
|
|
| 650 |
``` cpp
|
| 651 |
template<class T, const char* p> class X {
|
| 652 |
-
|
| 653 |
};
|
| 654 |
|
| 655 |
X<int, "Studebaker"> x1; // error: string literal as template-argument
|
| 656 |
|
| 657 |
const char p[] = "Vivisectionist";
|
| 658 |
X<int,p> x2; // OK
|
| 659 |
```
|
| 660 |
|
| 661 |
-
|
| 662 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 663 |
|
| 664 |
``` cpp
|
| 665 |
template<int* p> class X { };
|
| 666 |
|
| 667 |
int a[10];
|
| 668 |
struct S { int m; static int s; } s;
|
| 669 |
|
| 670 |
X<&a[2]> x3; // error: address of array element
|
| 671 |
X<&s.m> x4; // error: address of non-static member
|
| 672 |
-
X<&s.s> x5; //
|
| 673 |
X<&S::s> x6; // OK: address of static member
|
| 674 |
```
|
| 675 |
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 679 |
|
| 680 |
``` cpp
|
| 681 |
-
template<const int& CRI> struct B {
|
| 682 |
|
| 683 |
B<1> b2; // error: temporary would be required for template argument
|
| 684 |
|
| 685 |
int c = 1;
|
| 686 |
B<c> b1; // OK
|
| 687 |
```
|
| 688 |
|
| 689 |
-
|
| 690 |
-
non-type *template-argument*. If a non-type *template-argument* cannot
|
| 691 |
-
be converted to the type of the corresponding *template-parameter* then
|
| 692 |
-
the program is ill-formed.
|
| 693 |
|
| 694 |
-
|
| 695 |
-
conversions permitted in a converted constant expression (
|
| 696 |
-
[[expr.const]]) are applied.
|
| 697 |
-
- for a non-type *template-parameter* of type pointer to object,
|
| 698 |
-
qualification conversions ([[conv.qual]]) and the array-to-pointer
|
| 699 |
-
conversion ([[conv.array]]) are applied; if the *template-argument*
|
| 700 |
-
is of type `std::nullptr_t`, the null pointer conversion (
|
| 701 |
-
[[conv.ptr]]) is applied. In particular, neither the null pointer
|
| 702 |
-
conversion for a zero-valued integer literal ([[conv.ptr]]) nor the
|
| 703 |
-
derived-to-base conversion ([[conv.ptr]]) are applied. Although `0`
|
| 704 |
-
is a valid *template-argument* for a non-type *template-parameter* of
|
| 705 |
-
integral type, it is not a valid *template-argument* for a non-type
|
| 706 |
-
*template-parameter* of pointer type. However, both `(int*)0` and
|
| 707 |
-
`nullptr` are valid *template-argument*s for a non-type
|
| 708 |
-
*template-parameter* of type “pointer to int.”
|
| 709 |
-
- For a non-type *template-parameter* of type reference to object, no
|
| 710 |
-
conversions apply. The type referred to by the reference may be more
|
| 711 |
-
cv-qualified than the (otherwise identical) type of the
|
| 712 |
-
*template-argument*. The *template-parameter* is bound directly to the
|
| 713 |
-
*template-argument*, which shall be an lvalue.
|
| 714 |
-
- For a non-type *template-parameter* of type pointer to function, the
|
| 715 |
-
function-to-pointer conversion ([[conv.func]]) is applied; if the
|
| 716 |
-
*template-argument* is of type `std::nullptr_t`, the null pointer
|
| 717 |
-
conversion ([[conv.ptr]]) is applied. If the *template-argument*
|
| 718 |
-
represents a set of overloaded functions (or a pointer to such), the
|
| 719 |
-
matching function is selected from the set ([[over.over]]).
|
| 720 |
-
- For a non-type *template-parameter* of type reference to function, no
|
| 721 |
-
conversions apply. If the *template-argument* represents a set of
|
| 722 |
-
overloaded functions, the matching function is selected from the set (
|
| 723 |
-
[[over.over]]).
|
| 724 |
-
- For a non-type *template-parameter* of type pointer to member
|
| 725 |
-
function, if the *template-argument* is of type `std::nullptr_t`, the
|
| 726 |
-
null member pointer conversion ([[conv.mem]]) is applied; otherwise,
|
| 727 |
-
no conversions apply. If the *template-argument* represents a set of
|
| 728 |
-
overloaded member functions, the matching member function is selected
|
| 729 |
-
from the set ([[over.over]]).
|
| 730 |
-
- For a non-type *template-parameter* of type pointer to data member,
|
| 731 |
-
qualification conversions ([[conv.qual]]) are applied; if the
|
| 732 |
-
*template-argument* is of type `std::nullptr_t`, the null member
|
| 733 |
-
pointer conversion ([[conv.mem]]) is applied.
|
| 734 |
-
|
| 735 |
-
``` cpp
|
| 736 |
-
template<const int* pci> struct X { /* ... */ };
|
| 737 |
-
int ai[10];
|
| 738 |
-
X<ai> xi; // array to pointer and qualification conversions
|
| 739 |
-
|
| 740 |
-
struct Y { /* ... */ };
|
| 741 |
-
template<const Y& b> struct Z { /* ... */ };
|
| 742 |
-
Y y;
|
| 743 |
-
Z<y> z; // no conversion, but note extra cv-qualification
|
| 744 |
-
|
| 745 |
-
template<int (&pa)[5]> struct W { /* ... */ };
|
| 746 |
-
int b[5];
|
| 747 |
-
W<b> w; // no conversion
|
| 748 |
-
|
| 749 |
-
void f(char);
|
| 750 |
-
void f(int);
|
| 751 |
-
|
| 752 |
-
template<void (*pf)(int)> struct A { /* ... */ };
|
| 753 |
-
|
| 754 |
-
A<&f> a; // selects f(int)
|
| 755 |
-
```
|
| 756 |
|
| 757 |
### Template template arguments <a id="temp.arg.template">[[temp.arg.template]]</a>
|
| 758 |
|
| 759 |
A *template-argument* for a template *template-parameter* shall be the
|
| 760 |
name of a class template or an alias template, expressed as
|
|
@@ -767,11 +885,13 @@ that of the template template parameter.
|
|
| 767 |
Any partial specializations ([[temp.class.spec]]) associated with the
|
| 768 |
primary class template or primary variable template are considered when
|
| 769 |
a specialization based on the template *template-parameter* is
|
| 770 |
instantiated. If a specialization is not visible at the point of
|
| 771 |
instantiation, and it would have been selected had it been visible, the
|
| 772 |
-
program is ill-formed
|
|
|
|
|
|
|
| 773 |
|
| 774 |
``` cpp
|
| 775 |
template<class T> class A { // primary template
|
| 776 |
int x;
|
| 777 |
};
|
|
@@ -780,49 +900,56 @@ template<class T> class A<T*> { // partial specialization
|
|
| 780 |
};
|
| 781 |
template<template<class U> class V> class C {
|
| 782 |
V<int> y;
|
| 783 |
V<int*> z;
|
| 784 |
};
|
| 785 |
-
C<A> c;
|
| 786 |
-
|
| 787 |
-
// V<int*> within C<A> uses the partial specialization,
|
| 788 |
-
// so c.z.x has type long
|
| 789 |
```
|
| 790 |
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
*template-
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
parameters
|
| 797 |
-
|
| 798 |
-
|
| 799 |
-
*template-parameter*s,
|
| 800 |
-
|
| 801 |
-
template
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
template
|
|
|
|
|
|
|
|
|
|
|
|
|
| 806 |
|
| 807 |
``` cpp
|
| 808 |
-
template<class T> class A {
|
| 809 |
-
template<class T, class U = T> class B {
|
| 810 |
-
template
|
| 811 |
-
|
| 812 |
-
template<template<class> class P> class X {
|
| 813 |
-
template<template<class ...> class Q> class Y {
|
|
|
|
| 814 |
|
| 815 |
X<A> xa; // OK
|
| 816 |
-
X<B> xb; //
|
| 817 |
-
X<C> xc; //
|
| 818 |
-
|
| 819 |
Y<A> ya; // OK
|
| 820 |
Y<B> yb; // OK
|
| 821 |
Y<C> yc; // OK
|
|
|
|
| 822 |
```
|
| 823 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 824 |
``` cpp
|
| 825 |
template <class T> struct eval;
|
| 826 |
|
| 827 |
template <template <class, class...> class TT, class T1, class... Rest>
|
| 828 |
struct eval<TT<T1, Rest...>> { };
|
|
@@ -838,40 +965,66 @@ eval<B<int, float>> eB; // OK: matches partial specialization of eval
|
|
| 838 |
eval<C<17>> eC; // error: C does not match TT in partial specialization
|
| 839 |
eval<D<int, 17>> eD; // error: D does not match TT in partial specialization
|
| 840 |
eval<E<int, float>> eE; // error: E does not match TT in partial specialization
|
| 841 |
```
|
| 842 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 843 |
## Type equivalence <a id="temp.type">[[temp.type]]</a>
|
| 844 |
|
| 845 |
Two *template-id*s refer to the same class, function, or variable if
|
| 846 |
|
| 847 |
- their *template-name*s, *operator-function-id*s, or
|
| 848 |
*literal-operator-id*s refer to the same template and
|
| 849 |
- their corresponding type *template-argument*s are the same type and
|
| 850 |
- their corresponding non-type template arguments of integral or
|
| 851 |
enumeration type have identical values and
|
| 852 |
- their corresponding non-type *template-argument*s of pointer type
|
| 853 |
-
refer to the same
|
| 854 |
-
|
| 855 |
- their corresponding non-type *template-argument*s of pointer-to-member
|
| 856 |
type refer to the same class member or are both the null member
|
| 857 |
pointer value and
|
| 858 |
- their corresponding non-type *template-argument*s of reference type
|
| 859 |
-
refer to the same
|
| 860 |
- their corresponding template *template-argument*s refer to the same
|
| 861 |
template.
|
| 862 |
|
|
|
|
|
|
|
| 863 |
``` cpp
|
| 864 |
-
template<class E, int size> class buffer {
|
| 865 |
buffer<char,2*512> x;
|
| 866 |
buffer<char,1024> y;
|
| 867 |
```
|
| 868 |
|
| 869 |
declares `x` and `y` to be of the same type, and
|
| 870 |
|
| 871 |
``` cpp
|
| 872 |
-
template<class T, void(*err_fct)()> class list {
|
| 873 |
list<int,&error_handler1> x1;
|
| 874 |
list<int,&error_handler2> x2;
|
| 875 |
list<int,&error_handler2> x3;
|
| 876 |
list<char,&error_handler2> x4;
|
| 877 |
```
|
|
@@ -887,49 +1040,61 @@ X<Y<int> > y;
|
|
| 887 |
X<Z<int> > z;
|
| 888 |
```
|
| 889 |
|
| 890 |
declares `y` and `z` to be of the same type.
|
| 891 |
|
| 892 |
-
|
| 893 |
-
|
| 894 |
-
|
| 895 |
-
|
| 896 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
| 897 |
|
| 898 |
## Template declarations <a id="temp.decls">[[temp.decls]]</a>
|
| 899 |
|
| 900 |
A *template-id*, that is, the *template-name* followed by a
|
| 901 |
*template-argument-list* shall not be specified in the declaration of a
|
| 902 |
primary template declaration.
|
| 903 |
|
|
|
|
|
|
|
| 904 |
``` cpp
|
| 905 |
template<class T1, class T2, int I> class A<T1, T2, I> { }; // error
|
| 906 |
template<class T1, int I> void sort<T1, I>(T1 data[I]); // error
|
| 907 |
```
|
| 908 |
|
| 909 |
-
|
| 910 |
-
|
|
|
|
|
|
|
| 911 |
|
| 912 |
For purposes of name lookup and instantiation, default arguments and
|
| 913 |
-
*
|
| 914 |
-
|
| 915 |
-
|
| 916 |
-
|
| 917 |
-
|
| 918 |
-
|
|
|
|
| 919 |
|
| 920 |
Because an *alias-declaration* cannot declare a *template-id*, it is not
|
| 921 |
possible to partially or explicitly specialize an alias template.
|
| 922 |
|
| 923 |
### Class templates <a id="temp.class">[[temp.class]]</a>
|
| 924 |
|
| 925 |
-
A class
|
| 926 |
-
set of related types.
|
| 927 |
-
common definition for list of `int`, list of `float`, and list of
|
| 928 |
-
pointers to `Shape`s.
|
| 929 |
|
| 930 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 931 |
|
| 932 |
``` cpp
|
| 933 |
template<class T> class Array {
|
| 934 |
T* v;
|
| 935 |
int sz;
|
|
@@ -938,14 +1103,16 @@ public:
|
|
| 938 |
T& operator[](int);
|
| 939 |
T& elem(int i) { return v[i]; }
|
| 940 |
};
|
| 941 |
```
|
| 942 |
|
| 943 |
-
The prefix `template
|
| 944 |
-
declared and that a *type-name* `T`
|
| 945 |
other words, `Array` is a parameterized type with `T` as its parameter.
|
| 946 |
|
|
|
|
|
|
|
| 947 |
When a member function, a member class, a member enumeration, a static
|
| 948 |
data member or a member template of a class template is defined outside
|
| 949 |
of the class template definition, the member definition is defined as a
|
| 950 |
template definition in which the *template-parameter*s are those of the
|
| 951 |
class template. The names of the template parameters used in the
|
|
@@ -954,10 +1121,12 @@ names used in the class template definition. The template argument list
|
|
| 954 |
following the class template name in the member definition shall name
|
| 955 |
the parameters in the same order as the one used in the template
|
| 956 |
parameter list of the member. Each template parameter pack shall be
|
| 957 |
expanded with an ellipsis in the template argument list.
|
| 958 |
|
|
|
|
|
|
|
| 959 |
``` cpp
|
| 960 |
template<class T1, class T2> struct A {
|
| 961 |
void f1();
|
| 962 |
void f2();
|
| 963 |
};
|
|
@@ -974,20 +1143,24 @@ template<class ... Types> struct B {
|
|
| 974 |
|
| 975 |
template<class ... Types> void B<Types ...>::f3() { } // OK
|
| 976 |
template<class ... Types> void B<Types>::f4() { } // error
|
| 977 |
```
|
| 978 |
|
|
|
|
|
|
|
| 979 |
In a redeclaration, partial specialization, explicit specialization or
|
| 980 |
explicit instantiation of a class template, the *class-key* shall agree
|
| 981 |
in kind with the original class template declaration (
|
| 982 |
[[dcl.type.elab]]).
|
| 983 |
|
| 984 |
#### Member functions of class templates <a id="temp.mem.func">[[temp.mem.func]]</a>
|
| 985 |
|
| 986 |
A member function of a class template may be defined outside of the
|
| 987 |
class template definition in which it is declared.
|
| 988 |
|
|
|
|
|
|
|
| 989 |
``` cpp
|
| 990 |
template<class T> class Array {
|
| 991 |
T* v;
|
| 992 |
int sz;
|
| 993 |
public:
|
|
@@ -1005,46 +1178,60 @@ template<class T> T& Array<T>::operator[](int i) {
|
|
| 1005 |
if (i<0 || sz<=i) error("Array: range error");
|
| 1006 |
return v[i];
|
| 1007 |
}
|
| 1008 |
```
|
| 1009 |
|
|
|
|
|
|
|
| 1010 |
The *template-argument*s for a member function of a class template are
|
| 1011 |
determined by the *template-argument*s of the type of the object for
|
| 1012 |
-
which the member function is called.
|
| 1013 |
-
|
| 1014 |
-
|
|
|
|
|
|
|
|
|
|
| 1015 |
|
| 1016 |
``` cpp
|
| 1017 |
Array<int> v1(20);
|
| 1018 |
Array<dcomplex> v2(30);
|
| 1019 |
|
| 1020 |
v1[3] = 7; // Array<int>::operator[]()
|
| 1021 |
v2[3] = dcomplex(7,8); // Array<dcomplex>::operator[]()
|
| 1022 |
```
|
| 1023 |
|
|
|
|
|
|
|
| 1024 |
#### Member classes of class templates <a id="temp.mem.class">[[temp.mem.class]]</a>
|
| 1025 |
|
| 1026 |
A member class of a class template may be defined outside the class
|
| 1027 |
-
template definition in which it is declared.
|
| 1028 |
-
|
| 1029 |
-
[
|
|
|
|
|
|
|
|
|
|
| 1030 |
|
| 1031 |
``` cpp
|
| 1032 |
template<class T> struct A {
|
| 1033 |
class B;
|
| 1034 |
};
|
| 1035 |
A<int>::B* b1; // OK: requires A to be defined but not A::B
|
| 1036 |
template<class T> class A<T>::B { };
|
| 1037 |
A<int>::B b2; // OK: requires A::B to be defined
|
| 1038 |
```
|
| 1039 |
|
|
|
|
|
|
|
| 1040 |
#### Static data members of class templates <a id="temp.static">[[temp.static]]</a>
|
| 1041 |
|
| 1042 |
A definition for a static data member or static data member template may
|
| 1043 |
be provided in a namespace scope enclosing the definition of the static
|
| 1044 |
member’s class template.
|
| 1045 |
|
|
|
|
|
|
|
| 1046 |
``` cpp
|
| 1047 |
template<class T> class X {
|
| 1048 |
static T s;
|
| 1049 |
};
|
| 1050 |
template<class T> T X<T>::s = 0;
|
|
@@ -1056,64 +1243,80 @@ struct limits {
|
|
| 1056 |
|
| 1057 |
template<class T>
|
| 1058 |
const T limits::min = { }; // definition
|
| 1059 |
```
|
| 1060 |
|
|
|
|
|
|
|
| 1061 |
An explicit specialization of a static data member declared as an array
|
| 1062 |
of unknown bound can have a different bound from its definition, if any.
|
| 1063 |
|
|
|
|
|
|
|
| 1064 |
``` cpp
|
| 1065 |
template <class T> struct A {
|
| 1066 |
static int i[];
|
| 1067 |
};
|
| 1068 |
template <class T> int A<T>::i[4]; // 4 elements
|
| 1069 |
template <> int A<int>::i[] = { 1 }; // OK: 1 element
|
| 1070 |
```
|
| 1071 |
|
|
|
|
|
|
|
| 1072 |
#### Enumeration members of class templates <a id="temp.mem.enum">[[temp.mem.enum]]</a>
|
| 1073 |
|
| 1074 |
An enumeration member of a class template may be defined outside the
|
| 1075 |
class template definition.
|
| 1076 |
|
|
|
|
|
|
|
| 1077 |
``` cpp
|
| 1078 |
template<class T> struct A {
|
| 1079 |
enum E : T;
|
| 1080 |
};
|
| 1081 |
A<int> a;
|
| 1082 |
template<class T> enum A<T>::E : T { e1, e2 };
|
| 1083 |
A<int>::E e = A<int>::e1;
|
| 1084 |
```
|
| 1085 |
|
|
|
|
|
|
|
| 1086 |
### Member templates <a id="temp.mem">[[temp.mem]]</a>
|
| 1087 |
|
| 1088 |
A template can be declared within a class or class template; such a
|
| 1089 |
template is called a member template. A member template can be defined
|
| 1090 |
within or outside its class definition or class template definition. A
|
| 1091 |
member template of a class template that is defined outside of its class
|
| 1092 |
template definition shall be specified with the *template-parameter*s of
|
| 1093 |
the class template followed by the *template-parameter*s of the member
|
| 1094 |
template.
|
| 1095 |
|
|
|
|
|
|
|
| 1096 |
``` cpp
|
| 1097 |
template<class T> struct string {
|
| 1098 |
template<class T2> int compare(const T2&);
|
| 1099 |
-
template<class T2> string(const string<T2>& s) {
|
| 1100 |
};
|
| 1101 |
|
| 1102 |
template<class T> template<class T2> int string<T>::compare(const T2& s) {
|
| 1103 |
}
|
| 1104 |
```
|
| 1105 |
|
|
|
|
|
|
|
| 1106 |
A local class of non-closure type shall not have member templates.
|
| 1107 |
Access control rules (Clause [[class.access]]) apply to member template
|
| 1108 |
names. A destructor shall not be a member template. A non-template
|
| 1109 |
member function ([[dcl.fct]]) with a given name and type and a member
|
| 1110 |
function template of the same name, which could be used to generate a
|
| 1111 |
specialization of the same type, can both be declared in a class. When
|
| 1112 |
both exist, a use of that name and type refers to the non-template
|
| 1113 |
member unless an explicit template argument list is supplied.
|
| 1114 |
|
|
|
|
|
|
|
| 1115 |
``` cpp
|
| 1116 |
template <class T> struct A {
|
| 1117 |
void f(int);
|
| 1118 |
template <class T2> void f(T2);
|
| 1119 |
};
|
|
@@ -1127,38 +1330,49 @@ int main() {
|
|
| 1127 |
ac.f('c'); // template
|
| 1128 |
ac.f<>(1); // template
|
| 1129 |
}
|
| 1130 |
```
|
| 1131 |
|
|
|
|
|
|
|
| 1132 |
A member function template shall not be virtual.
|
| 1133 |
|
|
|
|
|
|
|
| 1134 |
``` cpp
|
| 1135 |
template <class T> struct AA {
|
| 1136 |
template <class C> virtual void g(C); // error
|
| 1137 |
virtual void f(); // OK
|
| 1138 |
};
|
| 1139 |
```
|
| 1140 |
|
|
|
|
|
|
|
| 1141 |
A specialization of a member function template does not override a
|
| 1142 |
virtual function from a base class.
|
| 1143 |
|
|
|
|
|
|
|
| 1144 |
``` cpp
|
| 1145 |
class B {
|
| 1146 |
virtual void f(int);
|
| 1147 |
};
|
| 1148 |
|
| 1149 |
class D : public B {
|
| 1150 |
template <class T> void f(T); // does not override B::f(int)
|
| 1151 |
-
void f(int i) { f<>(i); } // overriding function that calls
|
| 1152 |
-
// the template instantiation
|
| 1153 |
};
|
| 1154 |
```
|
| 1155 |
|
|
|
|
|
|
|
| 1156 |
A specialization of a conversion function template is referenced in the
|
| 1157 |
same way as a non-template conversion function that converts to the same
|
| 1158 |
type.
|
| 1159 |
|
|
|
|
|
|
|
| 1160 |
``` cpp
|
| 1161 |
struct A {
|
| 1162 |
template <class T> operator T*();
|
| 1163 |
};
|
| 1164 |
template <class T> A::operator T*(){ return 0; }
|
|
@@ -1166,20 +1380,21 @@ template <> A::operator char*(){ return 0; } // specialization
|
|
| 1166 |
template A::operator void*(); // explicit instantiation
|
| 1167 |
|
| 1168 |
int main() {
|
| 1169 |
A a;
|
| 1170 |
int* ip;
|
| 1171 |
-
ip = a.operator int*(); // explicit call to template operator
|
| 1172 |
-
// A::operator int*()
|
| 1173 |
}
|
| 1174 |
```
|
| 1175 |
|
| 1176 |
-
|
| 1177 |
-
|
| 1178 |
-
|
|
|
|
|
|
|
| 1179 |
function name, there is no way to provide an explicit template argument
|
| 1180 |
-
list for these function templates.
|
| 1181 |
|
| 1182 |
A specialization of a conversion function template is not found by name
|
| 1183 |
lookup. Instead, any conversion function templates visible in the
|
| 1184 |
context of the use are considered. For each such operator, if argument
|
| 1185 |
deduction succeeds ([[temp.deduct.conv]]), the resulting specialization
|
|
@@ -1196,30 +1411,38 @@ non-template conversion functions.
|
|
| 1196 |
### Variadic templates <a id="temp.variadic">[[temp.variadic]]</a>
|
| 1197 |
|
| 1198 |
A *template parameter pack* is a template parameter that accepts zero or
|
| 1199 |
more template arguments.
|
| 1200 |
|
|
|
|
|
|
|
| 1201 |
``` cpp
|
| 1202 |
template<class ... Types> struct Tuple { };
|
| 1203 |
|
| 1204 |
Tuple<> t0; // Types contains no arguments
|
| 1205 |
Tuple<int> t1; // Types contains one argument: int
|
| 1206 |
Tuple<int, float> t2; // Types contains two arguments: int and float
|
| 1207 |
Tuple<0> error; // error: 0 is not a type
|
| 1208 |
```
|
| 1209 |
|
|
|
|
|
|
|
| 1210 |
A *function parameter pack* is a function parameter that accepts zero or
|
| 1211 |
more function arguments.
|
| 1212 |
|
|
|
|
|
|
|
| 1213 |
``` cpp
|
| 1214 |
template<class ... Types> void f(Types ... args);
|
| 1215 |
|
| 1216 |
f(); // OK: args contains no arguments
|
| 1217 |
f(1); // OK: args contains one argument: int
|
| 1218 |
f(2, 1.0); // OK: args contains two arguments: int and double
|
| 1219 |
```
|
| 1220 |
|
|
|
|
|
|
|
| 1221 |
A *parameter pack* is either a template parameter pack or a function
|
| 1222 |
parameter pack.
|
| 1223 |
|
| 1224 |
A *pack expansion* consists of a *pattern* and an ellipsis, the
|
| 1225 |
instantiation of which produces zero or more instantiations of the
|
|
@@ -1227,10 +1450,12 @@ pattern in a list (described below). The form of the pattern depends on
|
|
| 1227 |
the context in which the expansion occurs. Pack expansions can occur in
|
| 1228 |
the following contexts:
|
| 1229 |
|
| 1230 |
- In a function parameter pack ([[dcl.fct]]); the pattern is the
|
| 1231 |
*parameter-declaration* without the ellipsis.
|
|
|
|
|
|
|
| 1232 |
- In a template parameter pack that is a pack expansion (
|
| 1233 |
[[temp.param]]):
|
| 1234 |
- if the template parameter pack is a *parameter-declaration*; the
|
| 1235 |
pattern is the *parameter-declaration* without the ellipsis;
|
| 1236 |
- if the template parameter pack is a *type-parameter* with a
|
|
@@ -1243,43 +1468,49 @@ the following contexts:
|
|
| 1243 |
- In a *mem-initializer-list* ([[class.base.init]]) for a
|
| 1244 |
*mem-initializer* whose *mem-initializer-id* denotes a base class; the
|
| 1245 |
pattern is the *mem-initializer*.
|
| 1246 |
- In a *template-argument-list* ([[temp.arg]]); the pattern is a
|
| 1247 |
*template-argument*.
|
| 1248 |
-
- In a *dynamic-exception-specification* ([[except.spec]]); the pattern
|
| 1249 |
-
is a *type-id*.
|
| 1250 |
- In an *attribute-list* ([[dcl.attr.grammar]]); the pattern is an
|
| 1251 |
*attribute*.
|
| 1252 |
- In an *alignment-specifier* ([[dcl.align]]); the pattern is the
|
| 1253 |
*alignment-specifier* without the ellipsis.
|
| 1254 |
- In a *capture-list* ([[expr.prim.lambda]]); the pattern is a
|
| 1255 |
*capture*.
|
| 1256 |
- In a `sizeof...` expression ([[expr.sizeof]]); the pattern is an
|
| 1257 |
*identifier*.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1258 |
|
| 1259 |
For the purpose of determining whether a parameter pack satisfies a rule
|
| 1260 |
regarding entities other than parameter packs, the parameter pack is
|
| 1261 |
considered to be the entity that would result from an instantiation of
|
| 1262 |
the pattern in which it appears.
|
| 1263 |
|
| 1264 |
-
``` cpp
|
| 1265 |
-
template<class ... Types> void f(Types ... rest);
|
| 1266 |
-
template<class ... Types> void g(Types ... rest) {
|
| 1267 |
-
f(&rest ...); // ``&rest ...'' is a pack expansion; ``&rest'' is its pattern
|
| 1268 |
-
}
|
| 1269 |
-
```
|
| 1270 |
-
|
| 1271 |
A parameter pack whose name appears within the pattern of a pack
|
| 1272 |
expansion is expanded by that pack expansion. An appearance of the name
|
| 1273 |
of a parameter pack is only expanded by the innermost enclosing pack
|
| 1274 |
expansion. The pattern of a pack expansion shall name one or more
|
| 1275 |
parameter packs that are not expanded by a nested pack expansion; such
|
| 1276 |
-
parameter packs are called *unexpanded
|
| 1277 |
All of the parameter packs expanded by a pack expansion shall have the
|
| 1278 |
same number of arguments specified. An appearance of a name of a
|
| 1279 |
parameter pack that is not expanded is ill-formed.
|
| 1280 |
|
|
|
|
|
|
|
| 1281 |
``` cpp
|
| 1282 |
template<typename...> struct Tuple {};
|
| 1283 |
template<typename T1, typename T2> struct Pair {};
|
| 1284 |
|
| 1285 |
template<class ... Args1> struct zip {
|
|
@@ -1296,38 +1527,46 @@ typedef zip<short>::with<unsigned short, unsigned>::type T2;
|
|
| 1296 |
template<class ... Args>
|
| 1297 |
void g(Args ... args) { // OK: Args is expanded by the function parameter pack args
|
| 1298 |
f(const_cast<const Args*>(&args)...); // OK: ``Args'' and ``args'' are expanded
|
| 1299 |
f(5 ...); // error: pattern does not contain any parameter packs
|
| 1300 |
f(args); // error: parameter pack ``args'' is not expanded
|
| 1301 |
-
f(h(args ...) + args ...);
|
| 1302 |
-
|
| 1303 |
}
|
| 1304 |
```
|
| 1305 |
|
| 1306 |
-
|
| 1307 |
-
|
| 1308 |
-
|
| 1309 |
-
|
| 1310 |
-
|
| 1311 |
-
|
|
|
|
|
|
|
| 1312 |
instantiation, is interpreted as follows:
|
| 1313 |
|
| 1314 |
- if the pack is a template parameter pack, the element is a template
|
| 1315 |
parameter ([[temp.param]]) of the corresponding kind (type or
|
| 1316 |
non-type) designating the type or value from the template argument;
|
| 1317 |
otherwise,
|
| 1318 |
- if the pack is a function parameter pack, the element is an
|
| 1319 |
*id-expression* designating the function parameter that resulted from
|
| 1320 |
the instantiation of the pattern where the pack is declared.
|
| 1321 |
|
| 1322 |
-
All of the Eᵢ become elements in the enclosing list.
|
| 1323 |
-
|
| 1324 |
-
*
|
| 1325 |
-
|
| 1326 |
-
|
| 1327 |
-
|
| 1328 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1329 |
|
| 1330 |
``` cpp
|
| 1331 |
template<class... T> struct X : T... { };
|
| 1332 |
template<class... T> void f(T... values) {
|
| 1333 |
X<T...> x(values...);
|
|
@@ -1335,14 +1574,60 @@ template<class... T> void f(T... values) {
|
|
| 1335 |
|
| 1336 |
template void f<>(); // OK: X<> has no base classes
|
| 1337 |
// x is a variable of type X<> that is value-initialized
|
| 1338 |
```
|
| 1339 |
|
|
|
|
|
|
|
| 1340 |
The instantiation of a `sizeof...` expression ([[expr.sizeof]])
|
| 1341 |
produces an integral constant containing the number of elements in the
|
| 1342 |
parameter pack it expands.
|
| 1343 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1344 |
### Friends <a id="temp.friend">[[temp.friend]]</a>
|
| 1345 |
|
| 1346 |
A friend of a class or class template can be a function template or
|
| 1347 |
class template, a specialization of a function template or class
|
| 1348 |
template, or a non-template function or class. For a friend function
|
|
@@ -1359,10 +1644,12 @@ declaration that is not a template declaration:
|
|
| 1359 |
declaration refers to the deduced specialization of that function
|
| 1360 |
template ([[temp.deduct.decl]]), otherwise,
|
| 1361 |
- the name shall be an *unqualified-id* that declares (or redeclares) a
|
| 1362 |
non-template function.
|
| 1363 |
|
|
|
|
|
|
|
| 1364 |
``` cpp
|
| 1365 |
template<class T> class task;
|
| 1366 |
template<class T> task<T>* preempt(task<T>*);
|
| 1367 |
|
| 1368 |
template<class T> class task {
|
|
@@ -1387,54 +1674,61 @@ function template `preempt` as a friend; and each specialization of the
|
|
| 1387 |
`task` class template has all specializations of the function template
|
| 1388 |
`func` as friends. Similarly, each specialization of the `task` class
|
| 1389 |
template has the class template specialization `task<int>` as a friend,
|
| 1390 |
and has all specializations of the class template `frd` as friends.
|
| 1391 |
|
|
|
|
|
|
|
| 1392 |
A friend template may be declared within a class or class template. A
|
| 1393 |
friend function template may be defined within a class or class
|
| 1394 |
template, but a friend class template may not be defined in a class or
|
| 1395 |
class template. In these cases, all specializations of the friend class
|
| 1396 |
or friend function template are friends of the class or class template
|
| 1397 |
granting friendship.
|
| 1398 |
|
|
|
|
|
|
|
| 1399 |
``` cpp
|
| 1400 |
class A {
|
| 1401 |
template<class T> friend class B; // OK
|
| 1402 |
-
template<class T> friend void f(T){
|
| 1403 |
};
|
| 1404 |
```
|
| 1405 |
|
|
|
|
|
|
|
| 1406 |
A template friend declaration specifies that all specializations of that
|
| 1407 |
template, whether they are implicitly instantiated ([[temp.inst]]),
|
| 1408 |
partially specialized ([[temp.class.spec]]) or explicitly specialized (
|
| 1409 |
[[temp.expl.spec]]), are friends of the class containing the template
|
| 1410 |
friend declaration.
|
| 1411 |
|
|
|
|
|
|
|
| 1412 |
``` cpp
|
| 1413 |
class X {
|
| 1414 |
template<class T> friend struct A;
|
| 1415 |
class Y { };
|
| 1416 |
};
|
| 1417 |
|
| 1418 |
template<class T> struct A { X::Y ab; }; // OK
|
| 1419 |
template<class T> struct A<T*> { X::Y ab; }; // OK
|
| 1420 |
```
|
| 1421 |
|
| 1422 |
-
|
| 1423 |
-
template, the function is instantiated when the function is odr-used (
|
| 1424 |
-
[[basic.def.odr]]). The same restrictions on multiple declarations and
|
| 1425 |
-
definitions that apply to non-template function declarations and
|
| 1426 |
-
definitions also apply to these implicit definitions.
|
| 1427 |
|
| 1428 |
A member of a class template may be declared to be a friend of a
|
| 1429 |
non-template class. In this case, the corresponding member of every
|
| 1430 |
-
specialization of the class template
|
| 1431 |
-
|
| 1432 |
-
|
| 1433 |
-
|
| 1434 |
-
the
|
| 1435 |
-
|
|
|
|
|
|
|
|
|
|
| 1436 |
|
| 1437 |
``` cpp
|
| 1438 |
template<class T> struct A {
|
| 1439 |
struct B { };
|
| 1440 |
void f();
|
|
@@ -1458,31 +1752,37 @@ class C {
|
|
| 1458 |
template<class T> friend void A<T>::D::g(); // does not grant friendship to A<int>::D::g()
|
| 1459 |
// because A<int>::D is not a specialization of A<T>::D
|
| 1460 |
};
|
| 1461 |
```
|
| 1462 |
|
| 1463 |
-
|
| 1464 |
-
|
|
|
|
|
|
|
| 1465 |
|
| 1466 |
A friend template shall not be declared in a local class.
|
| 1467 |
|
| 1468 |
Friend declarations shall not declare partial specializations.
|
| 1469 |
|
|
|
|
|
|
|
| 1470 |
``` cpp
|
| 1471 |
template<class T> class A { };
|
| 1472 |
class X {
|
| 1473 |
template<class T> friend class A<T*>; // error
|
| 1474 |
};
|
| 1475 |
```
|
| 1476 |
|
|
|
|
|
|
|
| 1477 |
When a friend declaration refers to a specialization of a function
|
| 1478 |
template, the function parameter declarations shall not include default
|
| 1479 |
arguments, nor shall the inline specifier be used in such a declaration.
|
| 1480 |
|
| 1481 |
### Class template partial specializations <a id="temp.class.spec">[[temp.class.spec]]</a>
|
| 1482 |
|
| 1483 |
-
A *primary
|
| 1484 |
template name is an identifier. A template declaration in which the
|
| 1485 |
class template name is a *simple-template-id* is a *partial
|
| 1486 |
specialization* of the class template named in the *simple-template-id*.
|
| 1487 |
A partial specialization of a class template provides an alternative
|
| 1488 |
definition of the template that is used instead of the primary
|
|
@@ -1497,62 +1797,81 @@ required.
|
|
| 1497 |
|
| 1498 |
Each class template partial specialization is a distinct template and
|
| 1499 |
definitions shall be provided for the members of a template partial
|
| 1500 |
specialization ([[temp.class.spec.mfunc]]).
|
| 1501 |
|
|
|
|
|
|
|
| 1502 |
``` cpp
|
| 1503 |
-
template<class T1, class T2, int I> class A { };
|
| 1504 |
-
template<class T, int I> class A<T, T*, I> { };
|
| 1505 |
-
template<class T1, class T2, int I> class A<T1*, T2, I> { };
|
| 1506 |
-
template<class T> class A<int, T*, 5> { };
|
| 1507 |
-
template<class T1, class T2, int I> class A<T1, T2*, I> { };
|
| 1508 |
```
|
| 1509 |
|
| 1510 |
The first declaration declares the primary (unspecialized) class
|
| 1511 |
template. The second and subsequent declarations declare partial
|
| 1512 |
specializations of the primary template.
|
| 1513 |
|
|
|
|
|
|
|
| 1514 |
The template parameters are specified in the angle bracket enclosed list
|
| 1515 |
that immediately follows the keyword `template`. For partial
|
| 1516 |
specializations, the template argument list is explicitly written
|
| 1517 |
immediately following the class template name. For primary templates,
|
| 1518 |
this list is implicitly described by the template parameter list.
|
| 1519 |
Specifically, the order of the template arguments is the sequence in
|
| 1520 |
-
which they appear in the template parameter list.
|
| 1521 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1522 |
The template argument list shall not be specified in the primary
|
| 1523 |
template declaration. For example,
|
| 1524 |
|
| 1525 |
``` cpp
|
| 1526 |
-
template<class T1, class T2, int I>
|
|
|
|
| 1527 |
```
|
| 1528 |
|
| 1529 |
-
|
| 1530 |
-
|
| 1531 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1532 |
|
| 1533 |
``` cpp
|
| 1534 |
template<class T> struct A {
|
| 1535 |
struct C {
|
| 1536 |
template<class T2> struct B { };
|
|
|
|
| 1537 |
};
|
| 1538 |
};
|
| 1539 |
|
| 1540 |
// partial specialization of A<T>::C::B<T2>
|
| 1541 |
template<class T> template<class T2>
|
| 1542 |
-
struct A<T>::C::B<T2*> { };
|
| 1543 |
|
| 1544 |
-
A<short>::C::B<int*> absip;
|
| 1545 |
```
|
| 1546 |
|
|
|
|
|
|
|
| 1547 |
Partial specialization declarations themselves are not found by name
|
| 1548 |
lookup. Rather, when the primary template name is used, any
|
| 1549 |
previously-declared partial specializations of the primary template are
|
| 1550 |
also considered. One consequence is that a *using-declaration* which
|
| 1551 |
refers to a class template does not restrict the set of partial
|
| 1552 |
specializations which may be found through the *using-declaration*.
|
| 1553 |
|
|
|
|
|
|
|
| 1554 |
``` cpp
|
| 1555 |
namespace N {
|
| 1556 |
template<class T1, class T2> class A { }; // primary template
|
| 1557 |
}
|
| 1558 |
|
|
@@ -1560,43 +1879,36 @@ using N::A; // refers to the primary template
|
|
| 1560 |
|
| 1561 |
namespace N {
|
| 1562 |
template<class T> class A<T, T*> { }; // partial specialization
|
| 1563 |
}
|
| 1564 |
|
| 1565 |
-
A<int,int*> a;
|
| 1566 |
-
|
| 1567 |
```
|
| 1568 |
|
|
|
|
|
|
|
| 1569 |
A non-type argument is non-specialized if it is the name of a non-type
|
| 1570 |
parameter. All other non-type arguments are specialized.
|
| 1571 |
|
| 1572 |
Within the argument list of a class template partial specialization, the
|
| 1573 |
following restrictions apply:
|
| 1574 |
|
| 1575 |
-
- A partially specialized non-type argument expression shall not involve
|
| 1576 |
-
a template parameter of the partial specialization except when the
|
| 1577 |
-
argument expression is a simple *identifier*.
|
| 1578 |
-
``` cpp
|
| 1579 |
-
template <int I, int J> struct A {};
|
| 1580 |
-
template <int I> struct A<I+5, I*2> {}; // error
|
| 1581 |
-
|
| 1582 |
-
template <int I, int J> struct B {};
|
| 1583 |
-
template <int I> struct B<I, I> {}; // OK
|
| 1584 |
-
```
|
| 1585 |
- The type of a template parameter corresponding to a specialized
|
| 1586 |
non-type argument shall not be dependent on a parameter of the
|
| 1587 |
specialization.
|
|
|
|
| 1588 |
``` cpp
|
| 1589 |
template <class T, T t> struct C {};
|
| 1590 |
template <class T> struct C<T, 1>; // error
|
| 1591 |
|
| 1592 |
template< int X, int (*array_ptr)[X] > class A {};
|
| 1593 |
int array[5];
|
| 1594 |
template< int X > class A<X,&array> { }; // error
|
| 1595 |
```
|
| 1596 |
-
|
| 1597 |
-
|
| 1598 |
- The specialization shall be more specialized than the primary
|
| 1599 |
template ([[temp.class.order]]).
|
| 1600 |
- The template parameter list of a specialization shall not contain
|
| 1601 |
default template argument values.[^4]
|
| 1602 |
- An argument shall not contain an unexpanded parameter pack. If an
|
|
@@ -1625,57 +1937,93 @@ argument lists of the partial specializations.
|
|
| 1625 |
|
| 1626 |
A partial specialization matches a given actual template argument list
|
| 1627 |
if the template arguments of the partial specialization can be deduced
|
| 1628 |
from the actual template argument list ([[temp.deduct]]).
|
| 1629 |
|
|
|
|
|
|
|
| 1630 |
``` cpp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1631 |
A<int, int, 1> a1; // uses #1
|
| 1632 |
A<int, int*, 1> a2; // uses #2, T is int, I is 1
|
| 1633 |
A<int, char*, 5> a3; // uses #4, T is char
|
| 1634 |
A<int, char*, 1> a4; // uses #5, T1 is int, T2 is char, I is 1
|
| 1635 |
A<int*, int*, 2> a5; // ambiguous: matches #3 and #5
|
| 1636 |
```
|
| 1637 |
|
| 1638 |
-
|
| 1639 |
-
|
| 1640 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1641 |
|
| 1642 |
In a type name that refers to a class template specialization, (e.g.,
|
| 1643 |
`A<int, int, 1>`) the argument list shall match the template parameter
|
| 1644 |
list of the primary template. The template arguments of a specialization
|
| 1645 |
are deduced from the arguments of the primary template.
|
| 1646 |
|
| 1647 |
#### Partial ordering of class template specializations <a id="temp.class.order">[[temp.class.order]]</a>
|
| 1648 |
|
| 1649 |
-
For two class template partial specializations, the first is
|
| 1650 |
-
specialized
|
| 1651 |
-
function templates, the first function template is
|
| 1652 |
-
|
| 1653 |
-
|
| 1654 |
|
| 1655 |
-
- the
|
| 1656 |
-
|
| 1657 |
-
|
| 1658 |
-
the
|
| 1659 |
-
|
| 1660 |
-
|
| 1661 |
-
|
| 1662 |
-
|
|
|
|
| 1663 |
|
| 1664 |
``` cpp
|
| 1665 |
template<int I, int J, class T> class X { };
|
| 1666 |
template<int I, int J> class X<I, J, int> { }; // #1
|
| 1667 |
template<int I> class X<I, I, int> { }; // #2
|
| 1668 |
|
| 1669 |
-
template<int
|
| 1670 |
-
template<int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1671 |
```
|
| 1672 |
|
| 1673 |
-
|
| 1674 |
-
|
| 1675 |
-
|
| 1676 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1677 |
|
| 1678 |
#### Members of class template specializations <a id="temp.class.spec.mfunc">[[temp.class.spec.mfunc]]</a>
|
| 1679 |
|
| 1680 |
The template parameter list of a member of a class template partial
|
| 1681 |
specialization shall match the template parameter list of the class
|
|
@@ -1690,16 +2038,19 @@ definitions of members of the primary template are never used as
|
|
| 1690 |
definitions for members of a class template partial specialization. An
|
| 1691 |
explicit specialization of a member of a class template partial
|
| 1692 |
specialization is declared in the same way as an explicit specialization
|
| 1693 |
of the primary template.
|
| 1694 |
|
|
|
|
|
|
|
| 1695 |
``` cpp
|
| 1696 |
-
// primary template
|
| 1697 |
template<class T, int I> struct A {
|
| 1698 |
void f();
|
| 1699 |
};
|
| 1700 |
|
|
|
|
| 1701 |
template<class T, int I> void A<T,I>::f() { }
|
| 1702 |
|
| 1703 |
// class template partial specialization
|
| 1704 |
template<class T> struct A<T,2> {
|
| 1705 |
void f();
|
|
@@ -1715,19 +2066,18 @@ template<> void A<char,2>::h() { }
|
|
| 1715 |
|
| 1716 |
int main() {
|
| 1717 |
A<char,0> a0;
|
| 1718 |
A<char,2> a2;
|
| 1719 |
a0.f(); // OK, uses definition of primary template's member
|
| 1720 |
-
a2.g();
|
| 1721 |
-
|
| 1722 |
-
a2.
|
| 1723 |
-
// explicit specialization's member
|
| 1724 |
-
a2.f(); // ill-formed, no definition of f for A<T,2>
|
| 1725 |
-
// the primary template is not used here
|
| 1726 |
}
|
| 1727 |
```
|
| 1728 |
|
|
|
|
|
|
|
| 1729 |
If a member template of a class template is partially specialized, the
|
| 1730 |
member template partial specializations are member templates of the
|
| 1731 |
enclosing class template; if the enclosing class template is
|
| 1732 |
instantiated ([[temp.inst]], [[temp.explicit]]), a declaration for
|
| 1733 |
every member template partial specialization is also instantiated as
|
|
@@ -1739,10 +2089,12 @@ specialization of the enclosing class template. If a partial
|
|
| 1739 |
specialization of the member template is explicitly specialized for a
|
| 1740 |
given (implicit) specialization of the enclosing class template, the
|
| 1741 |
primary member template and its other partial specializations are still
|
| 1742 |
considered for this specialization of the enclosing class template.
|
| 1743 |
|
|
|
|
|
|
|
| 1744 |
``` cpp
|
| 1745 |
template<class T> struct A {
|
| 1746 |
template<class T2> struct B {}; // #1
|
| 1747 |
template<class T2> struct B<T2*> {}; // #2
|
| 1748 |
};
|
|
@@ -1752,20 +2104,27 @@ template<> template<class T2> struct A<short>::B {}; // #3
|
|
| 1752 |
A<char>::B<int*> abcip; // uses #2
|
| 1753 |
A<short>::B<int*> absip; // uses #3
|
| 1754 |
A<char>::B<int> abci; // uses #1
|
| 1755 |
```
|
| 1756 |
|
|
|
|
|
|
|
| 1757 |
### Function templates <a id="temp.fct">[[temp.fct]]</a>
|
| 1758 |
|
| 1759 |
-
A function template defines an unbounded set of related functions.
|
| 1760 |
-
|
|
|
|
|
|
|
|
|
|
| 1761 |
|
| 1762 |
``` cpp
|
| 1763 |
template<class T> class Array { };
|
| 1764 |
template<class T> void sort(Array<T>&);
|
| 1765 |
```
|
| 1766 |
|
|
|
|
|
|
|
| 1767 |
A function template can be overloaded with other function templates and
|
| 1768 |
with non-template functions ([[dcl.fct]]). A non-template function is
|
| 1769 |
not related to a function template (i.e., it is never considered to be a
|
| 1770 |
specialization), even if it has the same name and type as a potentially
|
| 1771 |
generated function template specialization.[^5]
|
|
@@ -1773,76 +2132,92 @@ generated function template specialization.[^5]
|
|
| 1773 |
#### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
|
| 1774 |
|
| 1775 |
It is possible to overload function templates so that two different
|
| 1776 |
function template specializations have the same type.
|
| 1777 |
|
|
|
|
|
|
|
| 1778 |
``` cpp
|
| 1779 |
-
//
|
| 1780 |
template<class T>
|
| 1781 |
void f(T*);
|
| 1782 |
void g(int* p) {
|
| 1783 |
f(p); // calls f<int>(int*)
|
| 1784 |
}
|
| 1785 |
```
|
| 1786 |
|
| 1787 |
``` cpp
|
| 1788 |
-
//
|
| 1789 |
template<class T>
|
| 1790 |
void f(T);
|
| 1791 |
void h(int* p) {
|
| 1792 |
f(p); // calls f<int*>(int*)
|
| 1793 |
}
|
| 1794 |
```
|
| 1795 |
|
| 1796 |
-
|
| 1797 |
-
|
| 1798 |
-
|
| 1799 |
-
|
| 1800 |
-
|
| 1801 |
-
|
| 1802 |
-
|
| 1803 |
-
|
| 1804 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1805 |
|
| 1806 |
``` cpp
|
| 1807 |
template<class T> void f();
|
| 1808 |
template<int I> void f(); // OK: overloads the first template
|
| 1809 |
// distinguishable with an explicit template argument list
|
| 1810 |
```
|
| 1811 |
|
|
|
|
|
|
|
| 1812 |
When an expression that references a template parameter is used in the
|
| 1813 |
function parameter list or the return type in the declaration of a
|
| 1814 |
function template, the expression that references the template parameter
|
| 1815 |
is part of the signature of the function template. This is necessary to
|
| 1816 |
permit a declaration of a function template in one translation unit to
|
| 1817 |
be linked with another declaration of the function template in another
|
| 1818 |
translation unit and, conversely, to ensure that function templates that
|
| 1819 |
are intended to be distinct are not linked with one another.
|
| 1820 |
|
|
|
|
|
|
|
| 1821 |
``` cpp
|
| 1822 |
template <int I, int J> A<I+J> f(A<I>, A<J>); // #1
|
| 1823 |
template <int K, int L> A<K+L> f(A<K>, A<L>); // same as #1
|
| 1824 |
template <int I, int J> A<I-J> f(A<I>, A<J>); // different from #1
|
| 1825 |
```
|
| 1826 |
|
| 1827 |
-
|
| 1828 |
-
|
| 1829 |
-
|
| 1830 |
-
|
|
|
|
|
|
|
| 1831 |
|
| 1832 |
Two expressions involving template parameters are considered
|
| 1833 |
*equivalent* if two function definitions containing the expressions
|
| 1834 |
-
would satisfy the one
|
| 1835 |
the tokens used to name the template parameters may differ as long as a
|
| 1836 |
token used to name a template parameter in one expression is replaced by
|
| 1837 |
another token that names the same template parameter in the other
|
| 1838 |
expression. For determining whether two dependent names ([[temp.dep]])
|
| 1839 |
are equivalent, only the name itself is considered, not the result of
|
| 1840 |
name lookup in the context of the template. If multiple declarations of
|
| 1841 |
the same function template differ in the result of this name lookup, the
|
| 1842 |
result for the first declaration is used.
|
| 1843 |
|
|
|
|
|
|
|
| 1844 |
``` cpp
|
| 1845 |
template <int I, int J> void f(A<I+J>); // #1
|
| 1846 |
template <int K, int L> void f(A<K+L>); // same as #1
|
| 1847 |
|
| 1848 |
template <class T> decltype(g(T())) h();
|
|
@@ -1851,10 +2226,12 @@ template <class T> decltype(g(T())) h() // redeclaration of h() uses the
|
|
| 1851 |
{ return g(T()); } // ...although the lookup here does find g(int)
|
| 1852 |
int i = h<int>(); // template argument substitution fails; g(int)
|
| 1853 |
// was not in scope at the first declaration of h()
|
| 1854 |
```
|
| 1855 |
|
|
|
|
|
|
|
| 1856 |
Two expressions involving template parameters that are not equivalent
|
| 1857 |
are *functionally equivalent* if, for any given set of template
|
| 1858 |
arguments, the evaluation of the expression results in the same value.
|
| 1859 |
|
| 1860 |
Two function templates are *equivalent* if they are declared in the same
|
|
@@ -1865,11 +2242,13 @@ parameters. Two function templates are *functionally equivalent* if they
|
|
| 1865 |
are equivalent except that one or more expressions that involve template
|
| 1866 |
parameters in the return types and parameter lists are functionally
|
| 1867 |
equivalent using the rules described above to compare expressions
|
| 1868 |
involving template parameters. If a program contains declarations of
|
| 1869 |
function templates that are functionally equivalent but not equivalent,
|
| 1870 |
-
the program is ill-formed
|
|
|
|
|
|
|
| 1871 |
|
| 1872 |
This rule guarantees that equivalent declarations will be linked with
|
| 1873 |
one another, while not requiring implementations to use heroic efforts
|
| 1874 |
to guarantee that functionally equivalent declarations will be treated
|
| 1875 |
as distinct. For example, the last two declarations are functionally
|
|
@@ -1887,10 +2266,12 @@ template <int I> void f(A<I>, A<I+11>);
|
|
| 1887 |
// Ill-formed, no diagnostic required
|
| 1888 |
template <int I> void f(A<I>, A<I+10>);
|
| 1889 |
template <int I> void f(A<I>, A<I+1+2+3+4>);
|
| 1890 |
```
|
| 1891 |
|
|
|
|
|
|
|
| 1892 |
#### Partial ordering of function templates <a id="temp.func.order">[[temp.func.order]]</a>
|
| 1893 |
|
| 1894 |
If a function template is overloaded, the use of a function template
|
| 1895 |
specialization might be ambiguous because template argument deduction (
|
| 1896 |
[[temp.deduct]]) may associate the function template specialization with
|
|
@@ -1918,20 +2299,29 @@ specialized template is the one chosen by the partial ordering process.
|
|
| 1918 |
|
| 1919 |
To produce the transformed template, for each type, non-type, or
|
| 1920 |
template template parameter (including template parameter packs (
|
| 1921 |
[[temp.variadic]]) thereof) synthesize a unique type, value, or class
|
| 1922 |
template respectively and substitute it for each occurrence of that
|
| 1923 |
-
parameter in the function type of the template.
|
| 1924 |
-
|
| 1925 |
-
|
| 1926 |
-
|
| 1927 |
-
|
| 1928 |
-
|
| 1929 |
-
|
| 1930 |
-
|
| 1931 |
-
|
| 1932 |
-
of
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1933 |
|
| 1934 |
``` cpp
|
| 1935 |
struct A { };
|
| 1936 |
template<class T> struct B {
|
| 1937 |
template<class R> int operator*(R&); // #1
|
|
@@ -1947,14 +2337,18 @@ int main() {
|
|
| 1947 |
B<A> b;
|
| 1948 |
b * a; // calls #1a
|
| 1949 |
}
|
| 1950 |
```
|
| 1951 |
|
|
|
|
|
|
|
| 1952 |
Using the transformed function template’s function type, perform type
|
| 1953 |
deduction against the other template as described in
|
| 1954 |
[[temp.deduct.partial]].
|
| 1955 |
|
|
|
|
|
|
|
| 1956 |
``` cpp
|
| 1957 |
template<class T> struct A { A(); };
|
| 1958 |
|
| 1959 |
template<class T> void f(T);
|
| 1960 |
template<class T> void f(T*);
|
|
@@ -1968,23 +2362,29 @@ template<class T> void h(A<T>&);
|
|
| 1968 |
|
| 1969 |
void m() {
|
| 1970 |
const int* p;
|
| 1971 |
f(p); // f(const T*) is more specialized than f(T) or f(T*)
|
| 1972 |
float x;
|
| 1973 |
-
g(x); //
|
| 1974 |
A<int> z;
|
| 1975 |
h(z); // overload resolution selects h(A<T>&)
|
| 1976 |
const A<int> z2;
|
| 1977 |
h(z2); // h(const T&) is called because h(A<T>&) is not callable
|
| 1978 |
}
|
| 1979 |
```
|
| 1980 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1981 |
Since partial ordering in a call context considers only parameters for
|
| 1982 |
which there are explicit call arguments, some parameters are ignored
|
| 1983 |
(namely, function parameter packs, parameters with default arguments,
|
| 1984 |
and ellipsis parameters).
|
| 1985 |
|
|
|
|
|
|
|
| 1986 |
``` cpp
|
| 1987 |
template<class T> void f(T); // #1
|
| 1988 |
template<class T> void f(T*, int=1); // #2
|
| 1989 |
template<class T> void g(T); // #3
|
| 1990 |
template<class T> void g(T*, ...); // #4
|
|
@@ -1996,10 +2396,14 @@ int main() {
|
|
| 1996 |
f(ip); // calls #2
|
| 1997 |
g(ip); // calls #4
|
| 1998 |
}
|
| 1999 |
```
|
| 2000 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2001 |
``` cpp
|
| 2002 |
template<class T, class U> struct A { };
|
| 2003 |
|
| 2004 |
template<class T, class U> void f(U, A<U, T>* p = 0); // #1
|
| 2005 |
template< class U> void f(U, A<U, U>* p = 0); // #2
|
|
@@ -2011,10 +2415,14 @@ void h() {
|
|
| 2011 |
f<int>(42); // error: ambiguous
|
| 2012 |
g(42); // error: ambiguous
|
| 2013 |
}
|
| 2014 |
```
|
| 2015 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2016 |
``` cpp
|
| 2017 |
template<class T, class... U> void f(T, U...); // #1
|
| 2018 |
template<class T > void f(T); // #2
|
| 2019 |
template<class T, class... U> void g(T*, U...); // #3
|
| 2020 |
template<class T > void g(T); // #4
|
|
@@ -2023,34 +2431,42 @@ void h(int i) {
|
|
| 2023 |
f(&i); // error: ambiguous
|
| 2024 |
g(&i); // OK: calls #3
|
| 2025 |
}
|
| 2026 |
```
|
| 2027 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2028 |
### Alias templates <a id="temp.alias">[[temp.alias]]</a>
|
| 2029 |
|
| 2030 |
A *template-declaration* in which the *declaration* is an
|
| 2031 |
*alias-declaration* (Clause [[dcl.dcl]]) declares the *identifier* to
|
| 2032 |
-
be
|
| 2033 |
types. The name of the alias template is a *template-name*.
|
| 2034 |
|
| 2035 |
When a *template-id* refers to the specialization of an alias template,
|
| 2036 |
it is equivalent to the associated type obtained by substitution of its
|
| 2037 |
*template-argument*s for the *template-parameter*s in the *type-id* of
|
| 2038 |
-
the alias template.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2039 |
|
| 2040 |
``` cpp
|
| 2041 |
-
template<class T> struct Alloc {
|
| 2042 |
template<class T> using Vec = vector<T, Alloc<T>>;
|
| 2043 |
Vec<int> v; // same as vector<int, Alloc<int>{> v;}
|
| 2044 |
|
| 2045 |
template<class T>
|
| 2046 |
void process(Vec<T>& v)
|
| 2047 |
-
{
|
| 2048 |
|
| 2049 |
template<class T>
|
| 2050 |
void process(vector<T, Alloc<T>>& w)
|
| 2051 |
-
{
|
| 2052 |
|
| 2053 |
template<template<class> class TT>
|
| 2054 |
void f(TT<int>);
|
| 2055 |
|
| 2056 |
f(v); // error: Vec not deduced
|
|
@@ -2058,24 +2474,43 @@ f(v); // error: Vec not deduced
|
|
| 2058 |
template<template<class,class> class TT>
|
| 2059 |
void g(TT<int, Alloc<int>>);
|
| 2060 |
g(v); // OK: TT = vector
|
| 2061 |
```
|
| 2062 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2063 |
The *type-id* in an alias template declaration shall not refer to the
|
| 2064 |
alias template being declared. The type produced by an alias template
|
| 2065 |
specialization shall not directly or indirectly make use of that
|
| 2066 |
specialization.
|
| 2067 |
|
|
|
|
|
|
|
| 2068 |
``` cpp
|
| 2069 |
template <class T> struct A;
|
| 2070 |
template <class T> using B = typename A<T>::U;
|
| 2071 |
template <class T> struct A {
|
| 2072 |
typedef B<T> U;
|
| 2073 |
};
|
| 2074 |
B<short> b; // error: instantiation of B<short> uses own type via A<short>::U
|
| 2075 |
```
|
| 2076 |
|
|
|
|
|
|
|
| 2077 |
## Name resolution <a id="temp.res">[[temp.res]]</a>
|
| 2078 |
|
| 2079 |
Three kinds of names can be used within a template definition:
|
| 2080 |
|
| 2081 |
- The name of the template itself, and names declared within the
|
|
@@ -2086,10 +2521,12 @@ Three kinds of names can be used within a template definition:
|
|
| 2086 |
A name used in a template declaration or definition and that is
|
| 2087 |
dependent on a *template-parameter* is assumed not to name a type unless
|
| 2088 |
the applicable name lookup finds a type name or the name is qualified by
|
| 2089 |
the keyword `typename`.
|
| 2090 |
|
|
|
|
|
|
|
| 2091 |
``` cpp
|
| 2092 |
// no B declared here
|
| 2093 |
|
| 2094 |
class X;
|
| 2095 |
|
|
@@ -2103,36 +2540,39 @@ template<class T> class Y {
|
|
| 2103 |
Z* a4; // declare pointer to Z
|
| 2104 |
typedef typename T::A TA;
|
| 2105 |
TA* a5; // declare pointer to T's A
|
| 2106 |
typename T::A* a6; // declare pointer to T's A
|
| 2107 |
T::A* a7; // T::A is not a type name:
|
| 2108 |
-
//
|
| 2109 |
-
// no visible declaration of a7
|
| 2110 |
B* a8; // B is not a type name:
|
| 2111 |
-
//
|
| 2112 |
-
// no visible declarations of B and a8
|
| 2113 |
}
|
| 2114 |
};
|
| 2115 |
```
|
| 2116 |
|
| 2117 |
-
|
| 2118 |
-
|
| 2119 |
-
|
| 2120 |
-
|
| 2121 |
-
|
|
|
|
|
|
|
|
|
|
| 2122 |
|
| 2123 |
``` bnf
|
| 2124 |
typename-specifier:
|
| 2125 |
'typename' nested-name-specifier identifier
|
| 2126 |
'typename' nested-name-specifier 'template'ₒₚₜ simple-template-id
|
| 2127 |
```
|
| 2128 |
|
| 2129 |
If a specialization of a template is instantiated for a set of
|
| 2130 |
*template-argument*s such that the *qualified-id* prefixed by `typename`
|
| 2131 |
-
does not denote a type, the specialization is
|
| 2132 |
-
qualified name lookup ([[basic.lookup.qual]]) is
|
| 2133 |
-
*qualified-id* even in the presence of `typename`.
|
|
|
|
|
|
|
| 2134 |
|
| 2135 |
``` cpp
|
| 2136 |
struct A {
|
| 2137 |
struct X { };
|
| 2138 |
int X;
|
|
@@ -2149,25 +2589,31 @@ void foo() {
|
|
| 2149 |
f(b); // OK: T::X refers to B::X
|
| 2150 |
f(a); // error: T::X refers to the data member A::X not the struct A::X
|
| 2151 |
}
|
| 2152 |
```
|
| 2153 |
|
| 2154 |
-
|
| 2155 |
-
|
|
|
|
|
|
|
| 2156 |
assumed to name a type, without the use of the `typename` keyword. In a
|
| 2157 |
*nested-name-specifier* that immediately contains a
|
| 2158 |
*nested-name-specifier* that depends on a template parameter, the
|
| 2159 |
*identifier* or *simple-template-id* is implicitly assumed to name a
|
| 2160 |
-
type, without the use of the `typename` keyword.
|
| 2161 |
-
|
|
|
|
|
|
|
| 2162 |
|
| 2163 |
If, for a given set of template arguments, a specialization of a
|
| 2164 |
template is instantiated that refers to a *qualified-id* that denotes a
|
| 2165 |
-
type, and the *qualified-id* refers to a member of
|
| 2166 |
-
specialization, the *qualified-id* shall either be prefixed
|
| 2167 |
-
`typename` or shall be used in a context in which it implicitly names
|
| 2168 |
-
type as described above.
|
|
|
|
|
|
|
| 2169 |
|
| 2170 |
``` cpp
|
| 2171 |
template <class T> void f(int i) {
|
| 2172 |
T::x * i; // T::x must not be a type
|
| 2173 |
}
|
|
@@ -2184,73 +2630,121 @@ int main() {
|
|
| 2184 |
f<Bar>(1); // OK
|
| 2185 |
f<Foo>(1); // error: Foo::x is a type
|
| 2186 |
}
|
| 2187 |
```
|
| 2188 |
|
|
|
|
|
|
|
| 2189 |
Within the definition of a class template or within the definition of a
|
| 2190 |
member of a class template following the *declarator-id*, the keyword
|
| 2191 |
`typename` is not required when referring to the name of a previously
|
| 2192 |
-
declared member of the class template that declares a type
|
| 2193 |
-
|
| 2194 |
-
|
| 2195 |
-
[
|
| 2196 |
-
[[basic.lookup.
|
| 2197 |
-
current instantiation ([[temp.dep.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2198 |
|
| 2199 |
``` cpp
|
| 2200 |
template<class T> struct A {
|
| 2201 |
typedef int B;
|
| 2202 |
B b; // OK, no typename required
|
| 2203 |
};
|
| 2204 |
```
|
| 2205 |
|
|
|
|
|
|
|
| 2206 |
Knowing which names are type names allows the syntax of every template
|
| 2207 |
-
to be checked.
|
| 2208 |
-
|
| 2209 |
-
|
| 2210 |
-
|
| 2211 |
-
|
| 2212 |
-
|
| 2213 |
-
|
| 2214 |
-
|
| 2215 |
-
|
| 2216 |
-
|
| 2217 |
-
|
| 2218 |
-
|
| 2219 |
-
|
| 2220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2221 |
|
| 2222 |
``` cpp
|
| 2223 |
int j;
|
| 2224 |
template<class T> class X {
|
| 2225 |
void f(T t, int i, char* p) {
|
| 2226 |
-
t = i; // diagnosed if X::f is instantiated
|
| 2227 |
-
|
| 2228 |
-
p =
|
| 2229 |
-
// not instantiated
|
| 2230 |
-
p = j; // may be diagnosed even if X::f is
|
| 2231 |
-
// not instantiated
|
| 2232 |
}
|
| 2233 |
void g(T t) {
|
| 2234 |
-
+; // may be diagnosed even if X::g is
|
| 2235 |
-
// not instantiated
|
| 2236 |
}
|
| 2237 |
};
|
| 2238 |
|
| 2239 |
template<class... T> struct A {
|
| 2240 |
void operator++(int, T... t); // error: too many parameters
|
| 2241 |
};
|
| 2242 |
template<class... T> union X : T... { }; // error: union with base class
|
| 2243 |
template<class... T> struct A : T..., T... { }; // error: duplicate base class
|
| 2244 |
```
|
| 2245 |
|
|
|
|
|
|
|
| 2246 |
When looking for the declaration of a name used in a template
|
| 2247 |
definition, the usual lookup rules ([[basic.lookup.unqual]],
|
| 2248 |
[[basic.lookup.argdep]]) are used for non-dependent names. The lookup of
|
| 2249 |
names dependent on the template parameters is postponed until the actual
|
| 2250 |
template argument is known ([[temp.dep]]).
|
| 2251 |
|
|
|
|
|
|
|
| 2252 |
``` cpp
|
| 2253 |
#include <iostream>
|
| 2254 |
using namespace std;
|
| 2255 |
|
| 2256 |
template<class T> class Set {
|
|
@@ -2274,43 +2768,47 @@ the actual *template-argument*s are known. For example, even though the
|
|
| 2274 |
name `operator<<` is known within the definition of `printall()` and a
|
| 2275 |
declaration of it can be found in `<iostream>`, the actual declaration
|
| 2276 |
of `operator<<` needed to print `p[i]` cannot be known until it is known
|
| 2277 |
what type `T` is ([[temp.dep]]).
|
| 2278 |
|
|
|
|
|
|
|
| 2279 |
If a name does not depend on a *template-parameter* (as defined in
|
| 2280 |
[[temp.dep]]), a declaration (or set of declarations) for that name
|
| 2281 |
shall be in scope at the point where the name appears in the template
|
| 2282 |
definition; the name is bound to the declaration (or declarations) found
|
| 2283 |
at that point and this binding is not affected by declarations that are
|
| 2284 |
visible at the point of instantiation.
|
| 2285 |
|
|
|
|
|
|
|
| 2286 |
``` cpp
|
| 2287 |
void f(char);
|
| 2288 |
|
| 2289 |
template<class T> void g(T t) {
|
| 2290 |
f(1); // f(char)
|
| 2291 |
f(T(1)); // dependent
|
| 2292 |
f(t); // dependent
|
| 2293 |
-
dd++; // not dependent
|
| 2294 |
-
// error: declaration for dd not found
|
| 2295 |
}
|
| 2296 |
|
| 2297 |
enum E { e };
|
| 2298 |
void f(E);
|
| 2299 |
|
| 2300 |
double dd;
|
| 2301 |
void h() {
|
| 2302 |
-
g(e); // will cause one call of f(char) followed
|
| 2303 |
-
// by two calls of f(E)
|
| 2304 |
g('a'); // will cause three calls of f(char)
|
| 2305 |
}
|
| 2306 |
```
|
| 2307 |
|
| 2308 |
-
|
| 2309 |
-
|
| 2310 |
-
|
| 2311 |
-
|
|
|
|
|
|
|
| 2312 |
|
| 2313 |
### Locally declared names <a id="temp.local">[[temp.local]]</a>
|
| 2314 |
|
| 2315 |
Like normal (non-template) classes, class templates have an
|
| 2316 |
injected-class-name (Clause [[class]]). The injected-class-name can be
|
|
@@ -2326,10 +2824,12 @@ Within the scope of a class template specialization or partial
|
|
| 2326 |
specialization, when the injected-class-name is used as a *type-name*,
|
| 2327 |
it is equivalent to the *template-name* followed by the
|
| 2328 |
*template-argument*s of the class template specialization or partial
|
| 2329 |
specialization enclosed in `<>`.
|
| 2330 |
|
|
|
|
|
|
|
| 2331 |
``` cpp
|
| 2332 |
template<template<class> class T> class A { };
|
| 2333 |
template<class T> class Y;
|
| 2334 |
template<> class Y<int> {
|
| 2335 |
Y* p; // meaning Y<int>
|
|
@@ -2339,14 +2839,18 @@ template<> class Y<int> {
|
|
| 2339 |
template<class> friend class Y; // meaning ::Y
|
| 2340 |
};
|
| 2341 |
};
|
| 2342 |
```
|
| 2343 |
|
|
|
|
|
|
|
| 2344 |
The injected-class-name of a class template or class template
|
| 2345 |
specialization can be used either as a *template-name* or a *type-name*
|
| 2346 |
wherever it is in scope.
|
| 2347 |
|
|
|
|
|
|
|
| 2348 |
``` cpp
|
| 2349 |
template <class T> struct Base {
|
| 2350 |
Base* p;
|
| 2351 |
};
|
| 2352 |
|
|
@@ -2356,43 +2860,55 @@ template <class T> struct Derived: public Base<T> {
|
|
| 2356 |
|
| 2357 |
template<class T, template<class> class U = T::template Base> struct Third { };
|
| 2358 |
Third<Base<int> > t; // OK: default argument uses injected-class-name as a template
|
| 2359 |
```
|
| 2360 |
|
|
|
|
|
|
|
| 2361 |
A lookup that finds an injected-class-name ([[class.member.lookup]])
|
| 2362 |
can result in an ambiguity in certain cases (for example, if it is found
|
| 2363 |
in more than one base class). If all of the injected-class-names that
|
| 2364 |
are found refer to specializations of the same class template, and if
|
| 2365 |
the name is used as a *template-name*, the reference refers to the class
|
| 2366 |
template itself and not a specialization thereof, and is not ambiguous.
|
| 2367 |
|
|
|
|
|
|
|
| 2368 |
``` cpp
|
| 2369 |
template <class T> struct Base { };
|
| 2370 |
template <class T> struct Derived: Base<int>, Base<char> {
|
| 2371 |
typename Derived::Base b; // error: ambiguous
|
| 2372 |
typename Derived::Base<double> d; // OK
|
| 2373 |
};
|
| 2374 |
```
|
| 2375 |
|
|
|
|
|
|
|
| 2376 |
When the normal name of the template (i.e., the name from the enclosing
|
| 2377 |
scope, not the injected-class-name) is used, it always refers to the
|
| 2378 |
class template itself and not a specialization of the template.
|
| 2379 |
|
|
|
|
|
|
|
| 2380 |
``` cpp
|
| 2381 |
template<class T> class X {
|
| 2382 |
X* p; // meaning X<T>
|
| 2383 |
X<T>* p2;
|
| 2384 |
X<int>* p3;
|
| 2385 |
::X* p4; // error: missing template argument list
|
| 2386 |
// ::X does not refer to the injected-class-name
|
| 2387 |
};
|
| 2388 |
```
|
| 2389 |
|
|
|
|
|
|
|
| 2390 |
A *template-parameter* shall not be redeclared within its scope
|
| 2391 |
(including nested scopes). A *template-parameter* shall not have the
|
| 2392 |
same name as the template name.
|
| 2393 |
|
|
|
|
|
|
|
| 2394 |
``` cpp
|
| 2395 |
template<class T, int i> class Y {
|
| 2396 |
int T; // error: template-parameter redeclared
|
| 2397 |
void f() {
|
| 2398 |
char T; // error: template-parameter redeclared
|
|
@@ -2400,19 +2916,23 @@ template<class T, int i> class Y {
|
|
| 2400 |
};
|
| 2401 |
|
| 2402 |
template<class X> class X; // error: template-parameter redeclared
|
| 2403 |
```
|
| 2404 |
|
|
|
|
|
|
|
| 2405 |
In the definition of a member of a class template that appears outside
|
| 2406 |
of the class template definition, the name of a member of the class
|
| 2407 |
template hides the name of a *template-parameter* of any enclosing class
|
| 2408 |
templates (but not a *template-parameter* of the member if the member is
|
| 2409 |
a class or function template).
|
| 2410 |
|
|
|
|
|
|
|
| 2411 |
``` cpp
|
| 2412 |
template<class T> struct A {
|
| 2413 |
-
struct B {
|
| 2414 |
typedef void C;
|
| 2415 |
void f();
|
| 2416 |
template<class U> void g(U);
|
| 2417 |
};
|
| 2418 |
|
|
@@ -2424,14 +2944,18 @@ template<class B> template<class C> void A<B>::g(C) {
|
|
| 2424 |
B b; // A's B, not the template parameter
|
| 2425 |
C c; // the template parameter C, not A's C
|
| 2426 |
}
|
| 2427 |
```
|
| 2428 |
|
|
|
|
|
|
|
| 2429 |
In the definition of a member of a class template that appears outside
|
| 2430 |
of the namespace containing the class template definition, the name of a
|
| 2431 |
*template-parameter* hides the name of a member of this namespace.
|
| 2432 |
|
|
|
|
|
|
|
| 2433 |
``` cpp
|
| 2434 |
namespace N {
|
| 2435 |
class C { };
|
| 2436 |
template<class T> class B {
|
| 2437 |
void f(T);
|
|
@@ -2440,58 +2964,67 @@ namespace N {
|
|
| 2440 |
template<class C> void N::B<C>::f(C) {
|
| 2441 |
C b; // C is the template parameter, not N::C
|
| 2442 |
}
|
| 2443 |
```
|
| 2444 |
|
|
|
|
|
|
|
| 2445 |
In the definition of a class template or in the definition of a member
|
| 2446 |
of such a template that appears outside of the template definition, for
|
| 2447 |
-
each base class
|
| 2448 |
-
|
| 2449 |
-
|
| 2450 |
-
|
| 2451 |
-
|
|
|
|
| 2452 |
|
| 2453 |
``` cpp
|
| 2454 |
struct A {
|
| 2455 |
-
struct B {
|
| 2456 |
int a;
|
| 2457 |
int Y;
|
| 2458 |
};
|
| 2459 |
|
| 2460 |
template<class B, class a> struct X : A {
|
| 2461 |
B b; // A's B
|
| 2462 |
a b; // error: A's a isn't a type name
|
| 2463 |
};
|
| 2464 |
```
|
| 2465 |
|
|
|
|
|
|
|
| 2466 |
### Dependent names <a id="temp.dep">[[temp.dep]]</a>
|
| 2467 |
|
| 2468 |
Inside a template, some constructs have semantics which may differ from
|
| 2469 |
one instantiation to another. Such a construct *depends* on the template
|
| 2470 |
parameters. In particular, types and expressions may depend on the type
|
| 2471 |
and/or value of template parameters (as determined by the template
|
| 2472 |
arguments) and this determines the context for name lookup for certain
|
| 2473 |
-
names.
|
| 2474 |
-
parameter) or *value-dependent* (
|
| 2475 |
-
|
|
|
|
|
|
|
| 2476 |
|
| 2477 |
where the *postfix-expression* is an *unqualified-id*, the
|
| 2478 |
*unqualified-id* denotes a *dependent name* if
|
| 2479 |
|
| 2480 |
- any of the expressions in the *expression-list* is a pack expansion (
|
| 2481 |
[[temp.variadic]]),
|
| 2482 |
-
- any of the expressions
|
| 2483 |
-
|
| 2484 |
-
-
|
| 2485 |
-
|
| 2486 |
|
| 2487 |
If an operand of an operator is a type-dependent expression, the
|
| 2488 |
operator also denotes a dependent name. Such names are unbound and are
|
| 2489 |
looked up at the point of the template instantiation ([[temp.point]])
|
| 2490 |
in both the context of the template definition and the context of the
|
| 2491 |
point of instantiation.
|
| 2492 |
|
|
|
|
|
|
|
| 2493 |
``` cpp
|
| 2494 |
template<class T> struct X : B<T> {
|
| 2495 |
typename T::A* pa;
|
| 2496 |
void f(B<T>* pb) {
|
| 2497 |
static int i = B<T>::i;
|
|
@@ -2501,15 +3034,18 @@ template<class T> struct X : B<T> {
|
|
| 2501 |
```
|
| 2502 |
|
| 2503 |
the base class name `B<T>`, the type name `T::A`, the names `B<T>::i`
|
| 2504 |
and `pb->j` explicitly depend on the *template-parameter*.
|
| 2505 |
|
| 2506 |
-
|
| 2507 |
-
|
| 2508 |
-
|
| 2509 |
-
|
| 2510 |
-
member
|
|
|
|
|
|
|
|
|
|
| 2511 |
|
| 2512 |
``` cpp
|
| 2513 |
typedef double A;
|
| 2514 |
template<class T> class B {
|
| 2515 |
typedef int A;
|
|
@@ -2521,21 +3057,25 @@ template<class T> struct X : B<T> {
|
|
| 2521 |
|
| 2522 |
The type name `A` in the definition of `X<T>` binds to the typedef name
|
| 2523 |
defined in the global namespace scope, not to the typedef name defined
|
| 2524 |
in the base class `B<T>`.
|
| 2525 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2526 |
``` cpp
|
| 2527 |
struct A {
|
| 2528 |
-
struct B {
|
| 2529 |
int a;
|
| 2530 |
int Y;
|
| 2531 |
};
|
| 2532 |
|
| 2533 |
int a;
|
| 2534 |
|
| 2535 |
template<class T> struct Y : T {
|
| 2536 |
-
struct B {
|
| 2537 |
B b; // The B defined in Y
|
| 2538 |
void f(int i) { a = i; } // ::a
|
| 2539 |
Y* p; // Y<T>
|
| 2540 |
};
|
| 2541 |
|
|
@@ -2543,10 +3083,12 @@ Y<A> ya;
|
|
| 2543 |
```
|
| 2544 |
|
| 2545 |
The members `A::B`, `A::a`, and `A::Y` of the template argument `A` do
|
| 2546 |
not affect the binding of names in `Y<A>`.
|
| 2547 |
|
|
|
|
|
|
|
| 2548 |
#### Dependent types <a id="temp.dep.type">[[temp.dep.type]]</a>
|
| 2549 |
|
| 2550 |
A name refers to the *current instantiation* if it is
|
| 2551 |
|
| 2552 |
- in the definition of a class template, a nested class of a class
|
|
@@ -2581,21 +3123,22 @@ can be used in place of that template parameter in a reference to the
|
|
| 2581 |
current instantiation. In the case of a non-type template argument, the
|
| 2582 |
argument must have been given the value of the template parameter and
|
| 2583 |
not an expression in which the template parameter appears as a
|
| 2584 |
subexpression.
|
| 2585 |
|
|
|
|
|
|
|
| 2586 |
``` cpp
|
| 2587 |
template <class T> class A {
|
| 2588 |
A* p1; // A is the current instantiation
|
| 2589 |
A<T>* p2; // A<T> is the current instantiation
|
| 2590 |
A<T*> p3; // A<T*> is not the current instantiation
|
| 2591 |
::A<T>* p4; // ::A<T> is the current instantiation
|
| 2592 |
class B {
|
| 2593 |
B* p1; // B is the current instantiation
|
| 2594 |
A<T>::B* p2; // A<T>::B is the current instantiation
|
| 2595 |
-
typename A<T*>::B* p3; // A<T*>::B is not the
|
| 2596 |
-
// current instantiation
|
| 2597 |
};
|
| 2598 |
};
|
| 2599 |
|
| 2600 |
template <class T> class A<T*> {
|
| 2601 |
A<T*>* p1; // A<T*> is the current instantiation
|
|
@@ -2613,30 +3156,64 @@ template <class T1, class T2, int I> struct B {
|
|
| 2613 |
B<my_T1, T2, my_I2>* b4; // not the current instantiation
|
| 2614 |
B<my_T1, T2, my_I3>* b5; // refers to the current instantiation
|
| 2615 |
};
|
| 2616 |
```
|
| 2617 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2618 |
A name is a *member of the current instantiation* if it is
|
| 2619 |
|
| 2620 |
- An unqualified name that, when looked up, refers to at least one
|
| 2621 |
member of a class that is the current instantiation or a non-dependent
|
| 2622 |
-
base class thereof. This can only occur when looking up a
|
| 2623 |
-
scope enclosed by the definition of a class
|
|
|
|
| 2624 |
- A *qualified-id* in which the *nested-name-specifier* refers to the
|
| 2625 |
current instantiation and that, when looked up, refers to at least one
|
| 2626 |
member of a class that is the current instantiation or a non-dependent
|
| 2627 |
-
base class thereof.
|
| 2628 |
-
instantiation has any dependent base classes, then the
|
| 2629 |
-
is a member of an unknown specialization; see
|
|
|
|
| 2630 |
- An *id-expression* denoting the member in a class member access
|
| 2631 |
expression ([[expr.ref]]) for which the type of the object expression
|
| 2632 |
is the current instantiation, and the *id-expression*, when looked
|
| 2633 |
up ([[basic.lookup.classref]]), refers to at least one member of a
|
| 2634 |
class that is the current instantiation or a non-dependent base class
|
| 2635 |
-
thereof.
|
| 2636 |
-
any dependent base classes, then the *id-expression*
|
| 2637 |
-
unknown specialization; see below.
|
|
|
|
|
|
|
| 2638 |
|
| 2639 |
``` cpp
|
| 2640 |
template <class T> class A {
|
| 2641 |
static const int i = 5;
|
| 2642 |
int n1[i]; // i refers to a member of the current instantiation
|
|
@@ -2648,10 +3225,12 @@ template <class T> class A {
|
|
| 2648 |
template <class T> int A<T>::f() {
|
| 2649 |
return i; // i refers to a member of the current instantiation
|
| 2650 |
}
|
| 2651 |
```
|
| 2652 |
|
|
|
|
|
|
|
| 2653 |
A name is a *dependent member of the current instantiation* if it is a
|
| 2654 |
member of the current instantiation that, when looked up, refers to at
|
| 2655 |
least one member of a class that is the current instantiation.
|
| 2656 |
|
| 2657 |
A name is a *member of an unknown specialization* if it is
|
|
@@ -2682,10 +3261,12 @@ access expression for which the type of the object expression is the
|
|
| 2682 |
current instantiation does not refer to a member of the current
|
| 2683 |
instantiation or a member of an unknown specialization, the program is
|
| 2684 |
ill-formed even if the template containing the member access expression
|
| 2685 |
is not instantiated; no diagnostic required.
|
| 2686 |
|
|
|
|
|
|
|
| 2687 |
``` cpp
|
| 2688 |
template<class T> class A {
|
| 2689 |
typedef int type;
|
| 2690 |
void f() {
|
| 2691 |
A<T>::type i; // OK: refers to a member of the current instantiation
|
|
@@ -2693,42 +3274,68 @@ template<class T> class A {
|
|
| 2693 |
// a member of an unknown specialization
|
| 2694 |
}
|
| 2695 |
};
|
| 2696 |
```
|
| 2697 |
|
|
|
|
|
|
|
| 2698 |
If, for a given set of template arguments, a specialization of a
|
| 2699 |
template is instantiated that refers to a member of the current
|
| 2700 |
instantiation with a *qualified-id* or class member access expression,
|
| 2701 |
the name in the *qualified-id* or class member access expression is
|
| 2702 |
looked up in the template instantiation context. If the result of this
|
| 2703 |
lookup differs from the result of name lookup in the template definition
|
| 2704 |
-
context, name lookup is ambiguous.
|
| 2705 |
-
|
| 2706 |
-
|
| 2707 |
-
|
| 2708 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2709 |
|
| 2710 |
A type is dependent if it is
|
| 2711 |
|
| 2712 |
- a template parameter,
|
| 2713 |
- a member of an unknown specialization,
|
| 2714 |
- a nested class or enumeration that is a dependent member of the
|
| 2715 |
current instantiation,
|
| 2716 |
- a cv-qualified type where the cv-unqualified type is dependent,
|
| 2717 |
- a compound type constructed from any dependent type,
|
| 2718 |
-
- an array type
|
| 2719 |
-
|
|
|
|
| 2720 |
- a *simple-template-id* in which either the template name is a template
|
| 2721 |
parameter or any of the template arguments is a dependent type or an
|
| 2722 |
-
expression that is type-dependent or value-dependent
|
|
|
|
|
|
|
|
|
|
| 2723 |
- denoted by `decltype(`*expression*`)`, where *expression* is
|
| 2724 |
type-dependent ([[temp.dep.expr]]).
|
| 2725 |
|
| 2726 |
-
Because typedefs do not introduce new types, but instead
|
| 2727 |
-
other types, a name that refers to a typedef that is a
|
| 2728 |
-
current instantiation is dependent only if the type
|
| 2729 |
-
dependent.
|
| 2730 |
|
| 2731 |
#### Type-dependent expressions <a id="temp.dep.expr">[[temp.dep.expr]]</a>
|
| 2732 |
|
| 2733 |
Except as described below, an expression is type-dependent if any
|
| 2734 |
subexpression is type-dependent.
|
|
@@ -2740,14 +3347,22 @@ dependent ([[temp.dep.type]]).
|
|
| 2740 |
|
| 2741 |
An *id-expression* is type-dependent if it contains
|
| 2742 |
|
| 2743 |
- an *identifier* associated by name lookup with one or more
|
| 2744 |
declarations declared with a dependent type,
|
|
|
|
|
|
|
|
|
|
| 2745 |
- an *identifier* associated by name lookup with one or more
|
| 2746 |
declarations of member functions of the current instantiation declared
|
| 2747 |
-
with a return type that contains a placeholder type
|
| 2748 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2749 |
- a *template-id* that is dependent,
|
| 2750 |
- a *conversion-function-id* that specifies a dependent type, or
|
| 2751 |
- a *nested-name-specifier* or a *qualified-id* that names a member of
|
| 2752 |
an unknown specialization;
|
| 2753 |
|
|
@@ -2759,34 +3374,41 @@ type-dependent only if the type specified by the *type-id*,
|
|
| 2759 |
subexpression is type-dependent:
|
| 2760 |
|
| 2761 |
Expressions of the following forms are never type-dependent (because the
|
| 2762 |
type of the expression cannot be dependent):
|
| 2763 |
|
| 2764 |
-
For the standard library macro `offsetof`, see
|
|
|
|
| 2765 |
|
| 2766 |
A class member access expression ([[expr.ref]]) is type-dependent if
|
| 2767 |
the expression refers to a member of the current instantiation and the
|
| 2768 |
type of the referenced member is dependent, or the class member access
|
| 2769 |
-
expression refers to a member of an unknown specialization.
|
| 2770 |
-
|
| 2771 |
-
|
| 2772 |
-
|
| 2773 |
-
|
| 2774 |
-
dependent
|
| 2775 |
-
|
| 2776 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2777 |
|
| 2778 |
#### Value-dependent expressions <a id="temp.dep.constexpr">[[temp.dep.constexpr]]</a>
|
| 2779 |
|
| 2780 |
-
Except as described below,
|
| 2781 |
-
|
|
|
|
| 2782 |
|
| 2783 |
An *id-expression* is value-dependent if:
|
| 2784 |
|
| 2785 |
-
- it is
|
| 2786 |
- it is the name of a non-type template parameter,
|
| 2787 |
-
- it names a member of an unknown specialization,
|
| 2788 |
- it names a static data member that is a dependent member of the
|
| 2789 |
current instantiation and is not initialized in a *member-declarator*,
|
| 2790 |
- it names a static member function that is a dependent member of the
|
| 2791 |
current instantiation, or
|
| 2792 |
- it is a constant with literal type and is initialized with an
|
|
@@ -2794,21 +3416,26 @@ An *id-expression* is value-dependent if:
|
|
| 2794 |
|
| 2795 |
Expressions of the following form are value-dependent if the
|
| 2796 |
*unary-expression* or *expression* is type-dependent or the *type-id* is
|
| 2797 |
dependent:
|
| 2798 |
|
| 2799 |
-
For the standard library macro `offsetof`, see
|
|
|
|
| 2800 |
|
| 2801 |
Expressions of the following form are value-dependent if either the
|
| 2802 |
*type-id* or *simple-type-specifier* is dependent or the *expression* or
|
| 2803 |
*cast-expression* is value-dependent:
|
| 2804 |
|
| 2805 |
Expressions of the following form are value-dependent:
|
| 2806 |
|
| 2807 |
An expression of the form `&`*qualified-id* where the *qualified-id*
|
| 2808 |
names a dependent member of the current instantiation is
|
| 2809 |
-
value-dependent.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2810 |
|
| 2811 |
#### Dependent template arguments <a id="temp.dep.temp">[[temp.dep.temp]]</a>
|
| 2812 |
|
| 2813 |
A type *template-argument* is dependent if the type it specifies is
|
| 2814 |
dependent.
|
|
@@ -2828,28 +3455,30 @@ an unknown specialization.
|
|
| 2828 |
### Non-dependent names <a id="temp.nondep">[[temp.nondep]]</a>
|
| 2829 |
|
| 2830 |
Non-dependent names used in a template definition are found using the
|
| 2831 |
usual name lookup and bound at the point they are used.
|
| 2832 |
|
|
|
|
|
|
|
| 2833 |
``` cpp
|
| 2834 |
void g(double);
|
| 2835 |
void h();
|
| 2836 |
|
| 2837 |
template<class T> class Z {
|
| 2838 |
public:
|
| 2839 |
void f() {
|
| 2840 |
g(1); // calls g(double)
|
| 2841 |
-
h++; // ill-formed: cannot increment function;
|
| 2842 |
-
//
|
| 2843 |
-
// at the point of instantiation
|
| 2844 |
}
|
| 2845 |
};
|
| 2846 |
|
| 2847 |
-
void g(int); // not in scope at the point of the template
|
| 2848 |
-
// definition, not considered for the call g(1)
|
| 2849 |
```
|
| 2850 |
|
|
|
|
|
|
|
| 2851 |
### Dependent name resolution <a id="temp.dep.res">[[temp.dep.res]]</a>
|
| 2852 |
|
| 2853 |
In resolving dependent names, names from the following sources are
|
| 2854 |
considered:
|
| 2855 |
|
|
@@ -2876,20 +3505,19 @@ If a function template or member function of a class template is called
|
|
| 2876 |
in a way which uses the definition of a default argument of that
|
| 2877 |
function template or member function, the point of instantiation of the
|
| 2878 |
default argument is the point of instantiation of the function template
|
| 2879 |
or member function specialization.
|
| 2880 |
|
| 2881 |
-
For
|
| 2882 |
-
|
| 2883 |
-
*
|
| 2884 |
-
|
| 2885 |
-
|
| 2886 |
-
*
|
| 2887 |
-
|
| 2888 |
-
|
| 2889 |
-
|
| 2890 |
-
*exception-specification*.
|
| 2891 |
|
| 2892 |
For a class template specialization, a class member template
|
| 2893 |
specialization, or a specialization for a class member of a class
|
| 2894 |
template, if the specialization is implicitly instantiated because it is
|
| 2895 |
referenced from within another template specialization, if the context
|
|
@@ -2922,11 +3550,11 @@ specialization that has a point of instantiation within the translation
|
|
| 2922 |
unit, the end of the translation unit is also considered a point of
|
| 2923 |
instantiation. A specialization for a class template has at most one
|
| 2924 |
point of instantiation within a translation unit. A specialization for
|
| 2925 |
any template may have points of instantiation in multiple translation
|
| 2926 |
units. If two different points of instantiation give a template
|
| 2927 |
-
specialization different meanings according to the one
|
| 2928 |
[[basic.def.odr]]), the program is ill-formed, no diagnostic required.
|
| 2929 |
|
| 2930 |
#### Candidate functions <a id="temp.dep.candidate">[[temp.dep.candidate]]</a>
|
| 2931 |
|
| 2932 |
For a function call where the *postfix-expression* is a dependent name,
|
|
@@ -2959,25 +3587,28 @@ As with non-template classes, the names of namespace-scope friend
|
|
| 2959 |
functions of a class template specialization are not visible during an
|
| 2960 |
ordinary lookup unless explicitly declared at namespace scope (
|
| 2961 |
[[class.friend]]). Such names may be found under the rules for
|
| 2962 |
associated classes ([[basic.lookup.argdep]]).[^6]
|
| 2963 |
|
|
|
|
|
|
|
| 2964 |
``` cpp
|
| 2965 |
template<typename T> struct number {
|
| 2966 |
number(int);
|
| 2967 |
friend number gcd(number x, number y) { return 0; };
|
| 2968 |
};
|
| 2969 |
|
| 2970 |
void g() {
|
| 2971 |
number<double> a(3), b(4);
|
| 2972 |
-
a = gcd(a,b); // finds gcd because number<double> is an
|
| 2973 |
-
//
|
| 2974 |
-
// in its namespace (global scope)
|
| 2975 |
b = gcd(3,4); // ill-formed; gcd is not visible
|
| 2976 |
}
|
| 2977 |
```
|
| 2978 |
|
|
|
|
|
|
|
| 2979 |
## Template instantiation and specialization <a id="temp.spec">[[temp.spec]]</a>
|
| 2980 |
|
| 2981 |
The act of instantiating a function, a class, a member of a class
|
| 2982 |
template or a member template is referred to as *template
|
| 2983 |
instantiation*.
|
|
@@ -3001,10 +3632,12 @@ class template or a class member template, the name of the class that is
|
|
| 3001 |
explicitly specialized shall be a *simple-template-id*. In the explicit
|
| 3002 |
specialization declaration for a function template or a member function
|
| 3003 |
template, the name of the function or member function explicitly
|
| 3004 |
specialized may be a *template-id*.
|
| 3005 |
|
|
|
|
|
|
|
| 3006 |
``` cpp
|
| 3007 |
template<class T = int> struct A {
|
| 3008 |
static int x;
|
| 3009 |
};
|
| 3010 |
template<class U> void g(U) { }
|
|
@@ -3020,10 +3653,12 @@ template<class T = int> struct B {
|
|
| 3020 |
static int x;
|
| 3021 |
};
|
| 3022 |
template<> int B<>::x = 1; // specialize for T == int
|
| 3023 |
```
|
| 3024 |
|
|
|
|
|
|
|
| 3025 |
An instantiated template specialization can be either implicitly
|
| 3026 |
instantiated ([[temp.inst]]) for a given argument list or be explicitly
|
| 3027 |
instantiated ([[temp.explicit]]). A specialization is a class,
|
| 3028 |
function, or class member that is either instantiated or explicitly
|
| 3029 |
specialized ([[temp.expl.spec]]).
|
|
@@ -3041,10 +3676,12 @@ For a given template and a given set of *template-argument*s,
|
|
| 3041 |
An implementation is not required to diagnose a violation of this rule.
|
| 3042 |
|
| 3043 |
Each class template specialization instantiated from a template has its
|
| 3044 |
own copy of any static members.
|
| 3045 |
|
|
|
|
|
|
|
| 3046 |
``` cpp
|
| 3047 |
template<class T> class X {
|
| 3048 |
static T s;
|
| 3049 |
};
|
| 3050 |
template<class T> T X<T>::s = 0;
|
|
@@ -3055,28 +3692,98 @@ X<char*> bb;
|
|
| 3055 |
`X<int>`
|
| 3056 |
|
| 3057 |
has a static member `s` of type `int` and `X<char*>` has a static member
|
| 3058 |
`s` of type `char*`.
|
| 3059 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3060 |
### Implicit instantiation <a id="temp.inst">[[temp.inst]]</a>
|
| 3061 |
|
| 3062 |
Unless a class template specialization has been explicitly
|
| 3063 |
instantiated ([[temp.explicit]]) or explicitly specialized (
|
| 3064 |
[[temp.expl.spec]]), the class template specialization is implicitly
|
| 3065 |
instantiated when the specialization is referenced in a context that
|
| 3066 |
requires a completely-defined object type or when the completeness of
|
| 3067 |
-
the class type affects the semantics of the program.
|
| 3068 |
-
|
| 3069 |
-
|
| 3070 |
-
|
| 3071 |
-
|
| 3072 |
-
|
| 3073 |
-
|
| 3074 |
-
|
| 3075 |
-
|
| 3076 |
-
|
| 3077 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3078 |
|
| 3079 |
``` cpp
|
| 3080 |
template<class T, class U>
|
| 3081 |
struct Outer {
|
| 3082 |
template<class X, class Y> struct Inner;
|
|
@@ -3092,28 +3799,43 @@ Outer<int, int> outer; // error at #2
|
|
| 3092 |
defined but noted as being associated with a definition in
|
| 3093 |
`Outer<T, U>`.) \#2 is also a redeclaration of \#1a. It is noted as
|
| 3094 |
associated with a definition, so it is an invalid redeclaration of the
|
| 3095 |
same partial specialization.
|
| 3096 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3097 |
Unless a member of a class template or a member template has been
|
| 3098 |
explicitly instantiated or explicitly specialized, the specialization of
|
| 3099 |
the member is implicitly instantiated when the specialization is
|
| 3100 |
referenced in a context that requires the member definition to exist; in
|
| 3101 |
-
particular, the initialization (and any associated side
|
| 3102 |
static data member does not occur unless the static data member is
|
| 3103 |
itself used in a way that requires the definition of the static data
|
| 3104 |
member to exist.
|
| 3105 |
|
| 3106 |
Unless a function template specialization has been explicitly
|
| 3107 |
instantiated or explicitly specialized, the function template
|
| 3108 |
specialization is implicitly instantiated when the specialization is
|
| 3109 |
-
referenced in a context that requires a function definition to exist.
|
| 3110 |
-
|
| 3111 |
-
|
| 3112 |
-
|
| 3113 |
-
template
|
| 3114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3115 |
|
| 3116 |
``` cpp
|
| 3117 |
template<class T> struct Z {
|
| 3118 |
void f();
|
| 3119 |
void g();
|
|
@@ -3131,47 +3853,25 @@ void h() {
|
|
| 3131 |
```
|
| 3132 |
|
| 3133 |
Nothing in this example requires `class` `Z<double>`, `Z<int>::g()`, or
|
| 3134 |
`Z<char>::f()` to be implicitly instantiated.
|
| 3135 |
|
|
|
|
|
|
|
| 3136 |
Unless a variable template specialization has been explicitly
|
| 3137 |
instantiated or explicitly specialized, the variable template
|
| 3138 |
specialization is implicitly instantiated when the specialization is
|
| 3139 |
used. A default template argument for a variable template is implicitly
|
| 3140 |
instantiated when the variable template is referenced in a context that
|
| 3141 |
requires the value of the default argument.
|
| 3142 |
|
| 3143 |
-
|
| 3144 |
-
|
| 3145 |
-
or if the completeness of the class type might affect the semantics of
|
| 3146 |
-
the program. In particular, if the semantics of an expression depend on
|
| 3147 |
-
the member or base class lists of a class template specialization, the
|
| 3148 |
-
class template specialization is implicitly generated. For instance,
|
| 3149 |
-
deleting a pointer to class type depends on whether or not the class
|
| 3150 |
-
declares a destructor, and conversion between pointer to class types
|
| 3151 |
-
depends on the inheritance relationship between the two classes
|
| 3152 |
-
involved.
|
| 3153 |
-
|
| 3154 |
-
``` cpp
|
| 3155 |
-
template<class T> class B { /* ... */ };
|
| 3156 |
-
template<class T> class D : public B<T> { /* ... */ };
|
| 3157 |
-
|
| 3158 |
-
void f(void*);
|
| 3159 |
-
void f(B<int>*);
|
| 3160 |
-
|
| 3161 |
-
void g(D<int>* p, D<char>* pp, D<double>* ppp) {
|
| 3162 |
-
f(p); // instantiation of D<int> required: call f(B<int>*)
|
| 3163 |
-
B<char>* q = pp; // instantiation of D<char> required:
|
| 3164 |
-
// convert D<char>* to B<char>*
|
| 3165 |
-
delete ppp; // instantiation of D<double> required
|
| 3166 |
-
}
|
| 3167 |
-
```
|
| 3168 |
-
|
| 3169 |
-
If the overload resolution process can determine the correct function to
|
| 3170 |
-
call without instantiating a class template definition, it is
|
| 3171 |
unspecified whether that instantiation actually takes place.
|
| 3172 |
|
|
|
|
|
|
|
| 3173 |
``` cpp
|
| 3174 |
template <class T> struct S {
|
| 3175 |
operator int();
|
| 3176 |
};
|
| 3177 |
|
|
@@ -3183,31 +3883,21 @@ void g(S<int>& sr) {
|
|
| 3183 |
f(sr); // instantiation of S<int> allowed but not required
|
| 3184 |
// instantiation of S<float> allowed but not required
|
| 3185 |
};
|
| 3186 |
```
|
| 3187 |
|
| 3188 |
-
|
| 3189 |
-
required and the template is declared but not defined, the program is
|
| 3190 |
-
ill-formed.
|
| 3191 |
-
|
| 3192 |
-
``` cpp
|
| 3193 |
-
template<class T> class X;
|
| 3194 |
-
|
| 3195 |
-
X<char> ch; // error: definition of X required
|
| 3196 |
-
```
|
| 3197 |
-
|
| 3198 |
-
The implicit instantiation of a class template does not cause any static
|
| 3199 |
-
data members of that class to be implicitly instantiated.
|
| 3200 |
|
| 3201 |
If a function template or a member function template specialization is
|
| 3202 |
used in a way that involves overload resolution, a declaration of the
|
| 3203 |
specialization is implicitly instantiated ([[temp.over]]).
|
| 3204 |
|
| 3205 |
An implementation shall not implicitly instantiate a function template,
|
| 3206 |
a variable template, a member template, a non-virtual member function, a
|
| 3207 |
-
member class,
|
| 3208 |
-
|
|
|
|
| 3209 |
implementation implicitly instantiates a virtual member function of a
|
| 3210 |
class template if the virtual member function would not otherwise be
|
| 3211 |
instantiated. The use of a template specialization in a default argument
|
| 3212 |
shall not cause the template to be implicitly instantiated except that a
|
| 3213 |
class template may be instantiated where its complete type is needed to
|
|
@@ -3220,10 +3910,12 @@ specializations are placed in the namespace where the template is
|
|
| 3220 |
defined. Implicitly instantiated specializations for members of a class
|
| 3221 |
template are placed in the namespace where the enclosing class template
|
| 3222 |
is defined. Implicitly instantiated member templates are placed in the
|
| 3223 |
namespace where the enclosing class or class template is defined.
|
| 3224 |
|
|
|
|
|
|
|
| 3225 |
``` cpp
|
| 3226 |
namespace N {
|
| 3227 |
template<class T> class List {
|
| 3228 |
public:
|
| 3229 |
T* get();
|
|
@@ -3243,25 +3935,29 @@ void g(Map<const char*,int>& m) {
|
|
| 3243 |
|
| 3244 |
a call of `lt.get()` from `Map<const char*,int>::get()` would place
|
| 3245 |
`List<int>::get()` in the namespace `N` rather than in the global
|
| 3246 |
namespace.
|
| 3247 |
|
|
|
|
|
|
|
| 3248 |
If a function template `f` is called in a way that requires a default
|
| 3249 |
argument to be used, the dependent names are looked up, the semantics
|
| 3250 |
constraints are checked, and the instantiation of any template used in
|
| 3251 |
the default argument is done as if the default argument had been an
|
| 3252 |
initializer used in a function template specialization with the same
|
| 3253 |
scope, the same template parameters and the same access as that of the
|
| 3254 |
function template `f` used at that point, except that the scope in which
|
| 3255 |
-
a closure type is declared ([[expr.prim.lambda]]) – and
|
| 3256 |
-
associated namespaces – remain as determined from the
|
| 3257 |
-
definition for the default argument. This analysis is
|
| 3258 |
-
argument instantiation*. The instantiated default
|
| 3259 |
-
as the argument of `f`.
|
| 3260 |
|
| 3261 |
Each default argument is instantiated independently.
|
| 3262 |
|
|
|
|
|
|
|
| 3263 |
``` cpp
|
| 3264 |
template<class T> void f(T x, T y = ydef(T()), T z = zdef(T()));
|
| 3265 |
|
| 3266 |
class A { };
|
| 3267 |
|
|
@@ -3272,36 +3968,42 @@ void g(A a, A b, A c) {
|
|
| 3272 |
f(a, b); // default argument z = zdef(T()) instantiated
|
| 3273 |
f(a); // ill-formed; ydef is not declared
|
| 3274 |
}
|
| 3275 |
```
|
| 3276 |
|
| 3277 |
-
|
| 3278 |
-
|
| 3279 |
-
|
|
|
|
|
|
|
| 3280 |
needed but has not yet been instantiated, the dependent names are looked
|
| 3281 |
up, the semantics constraints are checked, and the instantiation of any
|
| 3282 |
-
template used in the *
|
| 3283 |
-
|
| 3284 |
-
|
| 3285 |
|
| 3286 |
-
[[temp.point]] defines the point of instantiation of a
|
| 3287 |
-
specialization.
|
| 3288 |
|
| 3289 |
-
There is an implementation-defined quantity that specifies the limit
|
| 3290 |
-
the total depth of recursive instantiations, which
|
| 3291 |
-
than one template. The result of an infinite
|
| 3292 |
-
is undefined.
|
|
|
|
|
|
|
| 3293 |
|
| 3294 |
``` cpp
|
| 3295 |
template<class T> class X {
|
| 3296 |
X<T>* p; // OK
|
| 3297 |
X<T*> a; // implicit generation of X<T> requires
|
| 3298 |
// the implicit instantiation of X<T*> which requires
|
| 3299 |
-
// the implicit instantiation of X<T**> which
|
| 3300 |
};
|
| 3301 |
```
|
| 3302 |
|
|
|
|
|
|
|
| 3303 |
### Explicit instantiation <a id="temp.explicit">[[temp.explicit]]</a>
|
| 3304 |
|
| 3305 |
A class, function, variable, or member template specialization can be
|
| 3306 |
explicitly instantiated from its template. A member function, member
|
| 3307 |
class or static data member of a class template can be explicitly
|
|
@@ -3324,39 +4026,49 @@ instantiation declaration begins with the `extern` keyword.
|
|
| 3324 |
If the explicit instantiation is for a class or member class, the
|
| 3325 |
*elaborated-type-specifier* in the *declaration* shall include a
|
| 3326 |
*simple-template-id*. If the explicit instantiation is for a function or
|
| 3327 |
member function, the *unqualified-id* in the *declaration* shall be
|
| 3328 |
either a *template-id* or, where all template arguments can be deduced,
|
| 3329 |
-
a *template-name* or *operator-function-id*.
|
| 3330 |
-
|
| 3331 |
-
*
|
| 3332 |
-
|
| 3333 |
-
template
|
| 3334 |
-
|
| 3335 |
-
|
| 3336 |
-
|
| 3337 |
-
|
| 3338 |
-
|
| 3339 |
-
|
| 3340 |
-
|
| 3341 |
-
|
| 3342 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3343 |
|
| 3344 |
``` cpp
|
| 3345 |
template<class T> class Array { void mf(); };
|
| 3346 |
template class Array<char>;
|
| 3347 |
template void Array<int>::mf();
|
| 3348 |
|
| 3349 |
-
template<class T> void sort(Array<T>& v) {
|
| 3350 |
template void sort(Array<char>&); // argument is deduced here
|
| 3351 |
|
| 3352 |
namespace N {
|
| 3353 |
template<class T> void f(T&) { }
|
| 3354 |
}
|
| 3355 |
template void N::f<int>(int&);
|
| 3356 |
```
|
| 3357 |
|
|
|
|
|
|
|
| 3358 |
A declaration of a function template, a variable template, a member
|
| 3359 |
function or static data member of a class template, or a member function
|
| 3360 |
template of a class or class template shall precede an explicit
|
| 3361 |
instantiation of that entity. A definition of a class template, a member
|
| 3362 |
class of a class template, or a member class template of a class or
|
|
@@ -3380,97 +4092,114 @@ template specialization is placed in the namespace in which the template
|
|
| 3380 |
is defined. An explicit instantiation for a member of a class template
|
| 3381 |
is placed in the namespace where the enclosing class template is
|
| 3382 |
defined. An explicit instantiation for a member template is placed in
|
| 3383 |
the namespace where the enclosing class or class template is defined.
|
| 3384 |
|
|
|
|
|
|
|
| 3385 |
``` cpp
|
| 3386 |
namespace N {
|
| 3387 |
template<class T> class Y { void mf() { } };
|
| 3388 |
}
|
| 3389 |
|
| 3390 |
-
template class Y<int>;
|
| 3391 |
-
// in the global namespace
|
| 3392 |
|
| 3393 |
using N::Y;
|
| 3394 |
-
template class Y<int>;
|
| 3395 |
-
// namespace of the template
|
| 3396 |
|
| 3397 |
template class N::Y<char*>; // OK: explicit instantiation in namespace N
|
| 3398 |
-
template void N::Y<double>::mf(); // OK: explicit instantiation
|
| 3399 |
-
// in namespace N
|
| 3400 |
```
|
| 3401 |
|
|
|
|
|
|
|
| 3402 |
A trailing *template-argument* can be left unspecified in an explicit
|
| 3403 |
instantiation of a function template specialization or of a member
|
| 3404 |
function template specialization provided it can be deduced from the
|
| 3405 |
type of a function parameter ([[temp.deduct]]).
|
| 3406 |
|
|
|
|
|
|
|
| 3407 |
``` cpp
|
| 3408 |
-
template<class T> class Array {
|
| 3409 |
-
template<class T> void sort(Array<T>& v) {
|
| 3410 |
|
| 3411 |
-
// instantiate sort(Array<int>&) - template-argument deduced
|
| 3412 |
template void sort<>(Array<int>&);
|
| 3413 |
```
|
| 3414 |
|
|
|
|
|
|
|
| 3415 |
An explicit instantiation that names a class template specialization is
|
| 3416 |
also an explicit instantiation of the same kind (declaration or
|
| 3417 |
definition) of each of its members (not including members inherited from
|
| 3418 |
base classes and members that are templates) that has not been
|
| 3419 |
previously explicitly specialized in the translation unit containing the
|
| 3420 |
-
explicit instantiation, except as described below.
|
| 3421 |
-
|
| 3422 |
-
|
|
|
|
| 3423 |
|
| 3424 |
An explicit instantiation definition that names a class template
|
| 3425 |
specialization explicitly instantiates the class template specialization
|
| 3426 |
and is an explicit instantiation definition of only those members that
|
| 3427 |
have been defined at the point of instantiation.
|
| 3428 |
|
| 3429 |
-
Except for inline functions, declarations with types
|
| 3430 |
-
initializer or return value ([[dcl.spec.auto]]),
|
| 3431 |
-
literal types, variables of reference types, and
|
| 3432 |
-
specializations, explicit instantiation declarations have
|
| 3433 |
-
suppressing the implicit instantiation of the entity to
|
| 3434 |
-
|
| 3435 |
-
|
| 3436 |
-
|
| 3437 |
-
|
| 3438 |
-
|
|
|
|
|
|
|
| 3439 |
|
| 3440 |
If an entity is the subject of both an explicit instantiation
|
| 3441 |
declaration and an explicit instantiation definition in the same
|
| 3442 |
translation unit, the definition shall follow the declaration. An entity
|
| 3443 |
that is the subject of an explicit instantiation declaration and that is
|
| 3444 |
also used in a way that would otherwise cause an implicit
|
| 3445 |
instantiation ([[temp.inst]]) in the translation unit shall be the
|
| 3446 |
subject of an explicit instantiation definition somewhere in the
|
| 3447 |
program; otherwise the program is ill-formed, no diagnostic required.
|
| 3448 |
-
|
| 3449 |
-
|
| 3450 |
-
|
| 3451 |
-
|
| 3452 |
-
|
| 3453 |
-
|
| 3454 |
-
|
|
|
|
|
|
|
|
|
|
| 3455 |
|
| 3456 |
The usual access checking rules do not apply to names used to specify
|
| 3457 |
-
explicit instantiations.
|
| 3458 |
-
|
| 3459 |
-
|
| 3460 |
-
|
| 3461 |
-
|
|
|
|
|
|
|
| 3462 |
|
| 3463 |
An explicit instantiation does not constitute a use of a default
|
| 3464 |
argument, so default argument instantiation is not done.
|
| 3465 |
|
|
|
|
|
|
|
| 3466 |
``` cpp
|
| 3467 |
char* p = 0;
|
| 3468 |
template<class T> T g(T x = &p) { return x; }
|
| 3469 |
template int g<int>(int); // OK even though &p isn't an int.
|
| 3470 |
```
|
| 3471 |
|
|
|
|
|
|
|
| 3472 |
### Explicit specialization <a id="temp.expl.spec">[[temp.expl.spec]]</a>
|
| 3473 |
|
| 3474 |
An explicit specialization of any of the following:
|
| 3475 |
|
| 3476 |
- function template
|
|
@@ -3488,17 +4217,19 @@ can be declared by a declaration introduced by `template<>`; that is:
|
|
| 3488 |
``` bnf
|
| 3489 |
explicit-specialization:
|
| 3490 |
'template < >' declaration
|
| 3491 |
```
|
| 3492 |
|
|
|
|
|
|
|
| 3493 |
``` cpp
|
| 3494 |
template<class T> class stream;
|
| 3495 |
|
| 3496 |
-
template<> class stream<char> {
|
| 3497 |
|
| 3498 |
-
template<class T> class Array {
|
| 3499 |
-
template<class T> void sort(Array<T>& v) {
|
| 3500 |
|
| 3501 |
template<> void sort<char*>(Array<char*>&);
|
| 3502 |
```
|
| 3503 |
|
| 3504 |
Given these declarations, `stream<char>` will be used as the definition
|
|
@@ -3506,34 +4237,39 @@ of streams of `char`s; other streams will be handled by class template
|
|
| 3506 |
specializations instantiated from the class template. Similarly,
|
| 3507 |
`sort<char*>` will be used as the sort function for arguments of type
|
| 3508 |
`Array<char*>`; other `Array` types will be sorted by functions
|
| 3509 |
generated from the template.
|
| 3510 |
|
| 3511 |
-
|
| 3512 |
-
|
| 3513 |
-
|
| 3514 |
-
|
| 3515 |
-
[[
|
| 3516 |
-
a declaration may also be a definition. If the declaration is not a
|
| 3517 |
-
definition, the specialization may be defined later (
|
| 3518 |
-
[[namespace.memdef]]).
|
| 3519 |
|
| 3520 |
A declaration of a function template, class template, or variable
|
| 3521 |
template being explicitly specialized shall precede the declaration of
|
| 3522 |
-
the explicit specialization.
|
| 3523 |
-
|
| 3524 |
-
|
| 3525 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3526 |
|
| 3527 |
``` cpp
|
| 3528 |
-
template<> class X<int> {
|
| 3529 |
|
| 3530 |
template<class T> class X;
|
| 3531 |
|
| 3532 |
-
template<> class X<char*> {
|
| 3533 |
```
|
| 3534 |
|
|
|
|
|
|
|
| 3535 |
A member function, a member function template, a member class, a member
|
| 3536 |
enumeration, a member class template, a static data member, or a static
|
| 3537 |
data member template of a class template may be explicitly specialized
|
| 3538 |
for a class specialization that is implicitly instantiated; in this
|
| 3539 |
case, the definition of the class template shall precede the explicit
|
|
@@ -3556,10 +4292,12 @@ manner as members of normal classes, and not using the `template<>`
|
|
| 3556 |
syntax. The same is true when defining a member of an explicitly
|
| 3557 |
specialized member class. However, `template<>` is used in defining a
|
| 3558 |
member of an explicitly specialized member class template that is
|
| 3559 |
specialized as a class template.
|
| 3560 |
|
|
|
|
|
|
|
| 3561 |
``` cpp
|
| 3562 |
template<class T> struct A {
|
| 3563 |
struct B { };
|
| 3564 |
template<class U> struct C { };
|
| 3565 |
};
|
|
@@ -3571,40 +4309,40 @@ template<> struct A<int> {
|
|
| 3571 |
void h() {
|
| 3572 |
A<int> a;
|
| 3573 |
a.f(16); // A<int>::f must be defined somewhere
|
| 3574 |
}
|
| 3575 |
|
| 3576 |
-
// template<> not used for a member of an
|
| 3577 |
-
|
| 3578 |
-
void A<int>::f(int) { /* ... */ }
|
| 3579 |
|
| 3580 |
template<> struct A<char>::B {
|
| 3581 |
void f();
|
| 3582 |
};
|
| 3583 |
-
// template<> also not used when defining a member of
|
| 3584 |
-
|
| 3585 |
-
void A<char>::B::f() { /* ... */ }
|
| 3586 |
|
| 3587 |
template<> template<class U> struct A<char>::C {
|
| 3588 |
void f();
|
| 3589 |
};
|
| 3590 |
-
// template<> is used when defining a member of an explicitly
|
| 3591 |
-
// specialized
|
| 3592 |
template<>
|
| 3593 |
-
template<class U> void A<char>::C<U>::f() {
|
| 3594 |
|
| 3595 |
template<> struct A<short>::B {
|
| 3596 |
void f();
|
| 3597 |
};
|
| 3598 |
-
template<> void A<short>::B::f() {
|
| 3599 |
|
| 3600 |
template<> template<class U> struct A<short>::C {
|
| 3601 |
void f();
|
| 3602 |
};
|
| 3603 |
-
template<class U> void A<short>::C<U>::f() {
|
| 3604 |
```
|
| 3605 |
|
|
|
|
|
|
|
| 3606 |
If a template, a member template or a member of a class template is
|
| 3607 |
explicitly specialized then that specialization shall be declared before
|
| 3608 |
the first use of that specialization that would cause an implicit
|
| 3609 |
instantiation to take place, in every translation unit in which such a
|
| 3610 |
use occurs; no diagnostic is required. If the program does not provide a
|
|
@@ -3613,22 +4351,22 @@ is used in a way that would cause an implicit instantiation to take
|
|
| 3613 |
place or the member is a virtual member function, the program is
|
| 3614 |
ill-formed, no diagnostic required. An implicit instantiation is never
|
| 3615 |
generated for an explicit specialization that is declared but not
|
| 3616 |
defined.
|
| 3617 |
|
|
|
|
|
|
|
| 3618 |
``` cpp
|
| 3619 |
class String { };
|
| 3620 |
-
template<class T> class Array {
|
| 3621 |
-
template<class T> void sort(Array<T>& v) {
|
| 3622 |
|
| 3623 |
void f(Array<String>& v) {
|
| 3624 |
-
sort(v); // use primary template
|
| 3625 |
-
// sort(Array<T>&), T is String
|
| 3626 |
}
|
| 3627 |
|
| 3628 |
-
template<> void sort<String>(Array<String>& v);
|
| 3629 |
-
// after use of primary template
|
| 3630 |
template<> void sort<>(Array<char*>& v); // OK: sort<char*> not yet used
|
| 3631 |
template<class T> struct A {
|
| 3632 |
enum E : T;
|
| 3633 |
enum class S : T;
|
| 3634 |
};
|
|
@@ -3639,10 +4377,12 @@ template<class T> enum class A<T>::S : T { sT };
|
|
| 3639 |
template<> enum A<char>::E : char { echar }; // ill-formed, A<char>::E was instantiated
|
| 3640 |
// when A<char> was instantiated
|
| 3641 |
template<> enum class A<char>::S : char { schar }; // OK
|
| 3642 |
```
|
| 3643 |
|
|
|
|
|
|
|
| 3644 |
The placement of explicit specialization declarations for function
|
| 3645 |
templates, class templates, variable templates, member functions of
|
| 3646 |
class templates, static data members of class templates, member classes
|
| 3647 |
of class templates, member enumerations of class templates, member class
|
| 3648 |
templates of class templates, member function templates of class
|
|
@@ -3661,87 +4401,108 @@ When writing a specialization, be careful about its location; or to make
|
|
| 3661 |
it compile will be such a trial as to kindle its self-immolation.
|
| 3662 |
|
| 3663 |
A template explicit specialization is in the scope of the namespace in
|
| 3664 |
which the template was defined.
|
| 3665 |
|
|
|
|
|
|
|
| 3666 |
``` cpp
|
| 3667 |
namespace N {
|
| 3668 |
-
template<class T> class X {
|
| 3669 |
-
template<class T> class Y {
|
| 3670 |
|
| 3671 |
-
template<> class X<int> {
|
| 3672 |
-
|
| 3673 |
-
template<> class Y<double>; // forward declare intent to
|
| 3674 |
-
// specialize for double
|
| 3675 |
}
|
| 3676 |
|
| 3677 |
-
template<> class N::Y<double> {
|
| 3678 |
-
|
| 3679 |
-
template<> class N::Y<short> { /* ... */ }; // OK: specialization
|
| 3680 |
-
// in enclosing namespace
|
| 3681 |
```
|
| 3682 |
|
|
|
|
|
|
|
| 3683 |
A *simple-template-id* that names a class template explicit
|
| 3684 |
specialization that has been declared but not defined can be used
|
| 3685 |
exactly like the names of other incompletely-defined classes (
|
| 3686 |
[[basic.types]]).
|
| 3687 |
|
|
|
|
|
|
|
| 3688 |
``` cpp
|
| 3689 |
template<class T> class X; // X is a class template
|
| 3690 |
template<> class X<int>;
|
| 3691 |
|
| 3692 |
X<int>* p; // OK: pointer to declared class X<int>
|
| 3693 |
X<int> x; // error: object of incomplete class X<int>
|
| 3694 |
```
|
| 3695 |
|
|
|
|
|
|
|
| 3696 |
A trailing *template-argument* can be left unspecified in the
|
| 3697 |
*template-id* naming an explicit function template specialization
|
| 3698 |
provided it can be deduced from the function argument type.
|
| 3699 |
|
|
|
|
|
|
|
| 3700 |
``` cpp
|
| 3701 |
-
template<class T> class Array {
|
| 3702 |
template<class T> void sort(Array<T>& v);
|
| 3703 |
|
| 3704 |
// explicit specialization for sort(Array<int>&)
|
| 3705 |
// with deduced template-argument of type int
|
| 3706 |
template<> void sort(Array<int>&);
|
| 3707 |
```
|
| 3708 |
|
|
|
|
|
|
|
| 3709 |
A function with the same name as a template and a type that exactly
|
| 3710 |
matches that of a template specialization is not an explicit
|
| 3711 |
specialization ([[temp.fct]]).
|
| 3712 |
|
| 3713 |
-
An explicit specialization of a function template is inline
|
| 3714 |
-
is declared with the `inline` specifier or defined as
|
| 3715 |
-
independently of whether its function
|
|
|
|
|
|
|
|
|
|
| 3716 |
|
| 3717 |
``` cpp
|
| 3718 |
-
template<class T> void f(T) {
|
| 3719 |
-
template<class T> inline T g(T) {
|
| 3720 |
|
| 3721 |
-
template<> inline void f<>(int) {
|
| 3722 |
-
template<> int g<>(int) {
|
| 3723 |
```
|
| 3724 |
|
|
|
|
|
|
|
| 3725 |
An explicit specialization of a static data member of a template or an
|
| 3726 |
explicit specialization of a static data member template is a definition
|
| 3727 |
if the declaration includes an initializer; otherwise, it is a
|
| 3728 |
-
declaration.
|
| 3729 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3730 |
|
| 3731 |
``` cpp
|
| 3732 |
template<> X Q<int>::x; // declaration
|
| 3733 |
template<> X Q<int>::x (); // error: declares a function
|
| 3734 |
template<> X Q<int>::x { }; // definition
|
| 3735 |
```
|
| 3736 |
|
|
|
|
|
|
|
| 3737 |
A member or a member template of a class template may be explicitly
|
| 3738 |
specialized for a given implicit instantiation of the class template,
|
| 3739 |
even if the member or member template is defined in the class template
|
| 3740 |
definition. An explicit specialization of a member or member template is
|
| 3741 |
specified using the syntax for explicit specialization.
|
| 3742 |
|
|
|
|
|
|
|
| 3743 |
``` cpp
|
| 3744 |
template<class T> struct A {
|
| 3745 |
void f(T);
|
| 3746 |
template<class X1> void g1(T, X1);
|
| 3747 |
template<class X2> void g2(T, X2);
|
|
@@ -3765,37 +4526,45 @@ template<> template<>
|
|
| 3765 |
|
| 3766 |
// member specialization even if defined in class definition
|
| 3767 |
template<> void A<int>::h(int) { }
|
| 3768 |
```
|
| 3769 |
|
|
|
|
|
|
|
| 3770 |
A member or a member template may be nested within many enclosing class
|
| 3771 |
templates. In an explicit specialization for such a member, the member
|
| 3772 |
declaration shall be preceded by a `template<>` for each enclosing class
|
| 3773 |
template that is explicitly specialized.
|
| 3774 |
|
|
|
|
|
|
|
| 3775 |
``` cpp
|
| 3776 |
template<class T1> class A {
|
| 3777 |
template<class T2> class B {
|
| 3778 |
void mf();
|
| 3779 |
};
|
| 3780 |
};
|
| 3781 |
template<> template<> class A<int>::B<double>;
|
| 3782 |
template<> template<> void A<char>::B<char>::mf();
|
| 3783 |
```
|
| 3784 |
|
|
|
|
|
|
|
| 3785 |
In an explicit specialization declaration for a member of a class
|
| 3786 |
template or a member template that appears in namespace scope, the
|
| 3787 |
member template and some of its enclosing class templates may remain
|
| 3788 |
unspecialized, except that the declaration shall not explicitly
|
| 3789 |
specialize a class member template if its enclosing class templates are
|
| 3790 |
not explicitly specialized as well. In such explicit specialization
|
| 3791 |
declaration, the keyword `template` followed by a
|
| 3792 |
*template-parameter-list* shall be provided instead of the `template<>`
|
| 3793 |
preceding the explicit specialization declaration of the member. The
|
| 3794 |
-
types of the *template-
|
| 3795 |
shall be the same as those specified in the primary template definition.
|
| 3796 |
|
|
|
|
|
|
|
| 3797 |
``` cpp
|
| 3798 |
template <class T1> class A {
|
| 3799 |
template<class T2> class B {
|
| 3800 |
template<class T3> void mf1(T3);
|
| 3801 |
void mf2();
|
|
@@ -3810,10 +4579,12 @@ template <> template <> template<class T>
|
|
| 3810 |
template <class Y> template <>
|
| 3811 |
void A<Y>::B<double>::mf2() { } // ill-formed; B<double> is specialized but
|
| 3812 |
// its enclosing class template A is not
|
| 3813 |
```
|
| 3814 |
|
|
|
|
|
|
|
| 3815 |
A specialization of a member function template, member class template,
|
| 3816 |
or static data member template of a non-specialized class template is
|
| 3817 |
itself a template.
|
| 3818 |
|
| 3819 |
An explicit specialization declaration shall not be a friend
|
|
@@ -3824,14 +4595,14 @@ definition for one of the following explicit specializations:
|
|
| 3824 |
|
| 3825 |
- the explicit specialization of a function template;
|
| 3826 |
- the explicit specialization of a member function template;
|
| 3827 |
- the explicit specialization of a member function of a class template
|
| 3828 |
where the class template specialization to which the member function
|
| 3829 |
-
specialization belongs is implicitly instantiated.
|
| 3830 |
-
arguments may be specified in the declaration or definition
|
| 3831 |
-
member function of a class template specialization that is
|
| 3832 |
-
specialized.
|
| 3833 |
|
| 3834 |
## Function template specializations <a id="temp.fct.spec">[[temp.fct.spec]]</a>
|
| 3835 |
|
| 3836 |
A function instantiated from a function template is called a function
|
| 3837 |
template specialization; so is an explicit specialization of a function
|
|
@@ -3842,10 +4613,12 @@ specialization, see [[temp.deduct]]), or obtained from default template
|
|
| 3842 |
arguments.
|
| 3843 |
|
| 3844 |
Each function template specialization instantiated from a template has
|
| 3845 |
its own copy of any static variable.
|
| 3846 |
|
|
|
|
|
|
|
| 3847 |
``` cpp
|
| 3848 |
template<class T> void f(T* p) {
|
| 3849 |
static T s;
|
| 3850 |
};
|
| 3851 |
|
|
@@ -3856,17 +4629,21 @@ void g(int a, char* b) {
|
|
| 3856 |
```
|
| 3857 |
|
| 3858 |
Here `f<int>(int*)` has a static variable `s` of type `int` and
|
| 3859 |
`f<char*>(char**)` has a static variable `s` of type `char*`.
|
| 3860 |
|
|
|
|
|
|
|
| 3861 |
### Explicit template argument specification <a id="temp.arg.explicit">[[temp.arg.explicit]]</a>
|
| 3862 |
|
| 3863 |
Template arguments can be specified when referring to a function
|
| 3864 |
template specialization by qualifying the function template name with
|
| 3865 |
the list of *template-argument*s in the same way as *template-argument*s
|
| 3866 |
are specified in uses of a class template specialization.
|
| 3867 |
|
|
|
|
|
|
|
| 3868 |
``` cpp
|
| 3869 |
template<class T> void sort(Array<T>& v);
|
| 3870 |
void f(Array<dcomplex>& cv, Array<int>& ci) {
|
| 3871 |
sort<dcomplex>(cv); // sort(Array<dcomplex>&)
|
| 3872 |
sort<int>(ci); // sort(Array<int>&)
|
|
@@ -3882,10 +4659,12 @@ void g(double d) {
|
|
| 3882 |
int i = convert<int,double>(d); // int convert(double)
|
| 3883 |
char c = convert<char,double>(d); // char convert(double)
|
| 3884 |
}
|
| 3885 |
```
|
| 3886 |
|
|
|
|
|
|
|
| 3887 |
A template argument list may be specified when referring to a
|
| 3888 |
specialization of a function template
|
| 3889 |
|
| 3890 |
- when a function is called,
|
| 3891 |
- when the address of a function is taken, when a function initializes a
|
|
@@ -3905,26 +4684,30 @@ deduction is done and fails, or in contexts where deduction is not done,
|
|
| 3905 |
if a template argument list is specified and it, along with any default
|
| 3906 |
template arguments, identifies a single function template
|
| 3907 |
specialization, then the *template-id* is an lvalue for the function
|
| 3908 |
template specialization.
|
| 3909 |
|
|
|
|
|
|
|
| 3910 |
``` cpp
|
| 3911 |
template<class X, class Y> X f(Y);
|
| 3912 |
template<class X, class Y, class ... Z> X g(Y);
|
| 3913 |
void h() {
|
| 3914 |
int i = f<int>(5.6); // Y is deduced to be double
|
| 3915 |
int j = f(5.6); // ill-formed: X cannot be deduced
|
| 3916 |
-
f<void>(f<int, bool>); // Y for outer f deduced to be
|
| 3917 |
-
|
| 3918 |
-
f<void>(f<int>); // ill-formed: f<int> does not denote a
|
| 3919 |
-
// single function template specialization
|
| 3920 |
int k = g<int>(5.6); // Y is deduced to be double, Z is deduced to an empty sequence
|
| 3921 |
-
f<void>(g<int, bool>); // Y for outer f is deduced to be
|
| 3922 |
-
//
|
| 3923 |
}
|
| 3924 |
```
|
| 3925 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3926 |
An empty template argument list can be used to indicate that a given use
|
| 3927 |
refers to a specialization of a function template even when a
|
| 3928 |
non-template function ([[dcl.fct]]) is visible that would otherwise be
|
| 3929 |
used. For example:
|
| 3930 |
|
|
@@ -3933,35 +4716,43 @@ template <class T> int f(T); // #1
|
|
| 3933 |
int f(int); // #2
|
| 3934 |
int k = f(1); // uses #2
|
| 3935 |
int l = f<>(1); // uses #1
|
| 3936 |
```
|
| 3937 |
|
|
|
|
|
|
|
| 3938 |
Template arguments that are present shall be specified in the
|
| 3939 |
declaration order of their corresponding *template-parameter*s. The
|
| 3940 |
template argument list shall not specify more *template-argument*s than
|
| 3941 |
there are corresponding *template-parameter*s unless one of the
|
| 3942 |
*template-parameter*s is a template parameter pack.
|
| 3943 |
|
|
|
|
|
|
|
| 3944 |
``` cpp
|
| 3945 |
template<class X, class Y, class Z> X f(Y,Z);
|
| 3946 |
template<class ... Args> void f2();
|
| 3947 |
void g() {
|
| 3948 |
f<int,const char*,double>("aa",3.0);
|
| 3949 |
f<int,const char*>("aa",3.0); // Z is deduced to be double
|
| 3950 |
-
f<int>("aa",3.0); // Y is deduced to be const char*, and
|
| 3951 |
-
// Z is deduced to be double
|
| 3952 |
f("aa",3.0); // error: X cannot be deduced
|
| 3953 |
f2<char, short, int, long>(); // OK
|
| 3954 |
}
|
| 3955 |
```
|
| 3956 |
|
|
|
|
|
|
|
| 3957 |
Implicit conversions (Clause [[conv]]) will be performed on a function
|
| 3958 |
argument to convert it to the type of the corresponding function
|
| 3959 |
parameter if the parameter type contains no *template-parameter*s that
|
| 3960 |
-
participate in template argument deduction.
|
| 3961 |
-
|
| 3962 |
-
|
|
|
|
|
|
|
|
|
|
| 3963 |
|
| 3964 |
``` cpp
|
| 3965 |
template<class T> void f(T);
|
| 3966 |
|
| 3967 |
class Complex {
|
|
@@ -3971,15 +4762,19 @@ class Complex {
|
|
| 3971 |
void g() {
|
| 3972 |
f<Complex>(1); // OK, means f<Complex>(Complex(1))
|
| 3973 |
}
|
| 3974 |
```
|
| 3975 |
|
| 3976 |
-
|
| 3977 |
-
|
| 3978 |
-
|
|
|
|
|
|
|
| 3979 |
function name, there is no way to provide an explicit template argument
|
| 3980 |
-
list for these function templates.
|
|
|
|
|
|
|
| 3981 |
|
| 3982 |
For simple function names, argument dependent lookup (
|
| 3983 |
[[basic.lookup.argdep]]) applies even when the function name is not
|
| 3984 |
visible within the scope of the call. This is because the call still has
|
| 3985 |
the syntactic form of a function call ([[basic.lookup.unqual]]). But
|
|
@@ -3989,10 +4784,12 @@ template with that name visible at the point of the call. If no such
|
|
| 3989 |
name is visible, the call is not syntactically well-formed and
|
| 3990 |
argument-dependent lookup does not apply. If some such name is visible,
|
| 3991 |
argument dependent lookup applies and additional function templates may
|
| 3992 |
be found in other namespaces.
|
| 3993 |
|
|
|
|
|
|
|
| 3994 |
``` cpp
|
| 3995 |
namespace A {
|
| 3996 |
struct B { };
|
| 3997 |
template<int X> void f(B);
|
| 3998 |
}
|
|
@@ -4000,37 +4797,45 @@ namespace C {
|
|
| 4000 |
template<class T> void f(T t);
|
| 4001 |
}
|
| 4002 |
void g(A::B b) {
|
| 4003 |
f<3>(b); // ill-formed: not a function call
|
| 4004 |
A::f<3>(b); // well-formed
|
| 4005 |
-
C::f<3>(b);
|
| 4006 |
-
// applies only to unqualified names
|
| 4007 |
using C::f;
|
| 4008 |
-
f<3>(b);
|
| 4009 |
-
// A::f is found by argument dependent lookup
|
| 4010 |
}
|
| 4011 |
```
|
| 4012 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4013 |
Template argument deduction can extend the sequence of template
|
| 4014 |
arguments corresponding to a template parameter pack, even when the
|
| 4015 |
sequence contains explicitly specified template arguments.
|
| 4016 |
|
|
|
|
|
|
|
| 4017 |
``` cpp
|
| 4018 |
template<class ... Types> void f(Types ... values);
|
| 4019 |
|
| 4020 |
void g() {
|
| 4021 |
f<int*, float*>(0, 0, 0); // Types is deduced to the sequence int*, float*, int
|
| 4022 |
}
|
| 4023 |
```
|
| 4024 |
|
|
|
|
|
|
|
| 4025 |
### Template argument deduction <a id="temp.deduct">[[temp.deduct]]</a>
|
| 4026 |
|
| 4027 |
When a function template specialization is referenced, all of the
|
| 4028 |
template arguments shall have values. The values can be explicitly
|
| 4029 |
specified or, in some cases, be deduced from the use or obtained from
|
| 4030 |
default *template-argument*s.
|
| 4031 |
|
|
|
|
|
|
|
| 4032 |
``` cpp
|
| 4033 |
void f(Array<dcomplex>& cv, Array<int>& ci) {
|
| 4034 |
sort(cv); // calls sort(Array<dcomplex>&)
|
| 4035 |
sort(ci); // calls sort(Array<int>&)
|
| 4036 |
}
|
|
@@ -4043,10 +4848,12 @@ void g(double d) {
|
|
| 4043 |
int i = convert<int>(d); // calls convert<int,double>(double)
|
| 4044 |
int c = convert<char>(d); // calls convert<char,double>(double)
|
| 4045 |
}
|
| 4046 |
```
|
| 4047 |
|
|
|
|
|
|
|
| 4048 |
When an explicit template argument list is specified, the template
|
| 4049 |
arguments must be compatible with the template parameter list and must
|
| 4050 |
result in a valid function type as described below; otherwise type
|
| 4051 |
deduction fails. Specifically, the following steps are performed when
|
| 4052 |
evaluating an explicitly specified template argument list with respect
|
|
@@ -4063,15 +4870,20 @@ to a given function template:
|
|
| 4063 |
[[temp.arg.nontype]], otherwise type deduction fails.
|
| 4064 |
- The specified template argument values are substituted for the
|
| 4065 |
corresponding template parameters as specified below.
|
| 4066 |
|
| 4067 |
After this substitution is performed, the function parameter type
|
| 4068 |
-
adjustments described in [[dcl.fct]] are performed.
|
| 4069 |
-
|
| 4070 |
-
|
| 4071 |
-
|
| 4072 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4073 |
|
| 4074 |
``` cpp
|
| 4075 |
template <class T> void f(T t);
|
| 4076 |
template <class X> void g(const X x);
|
| 4077 |
template <class Z> void h(Z, Z*);
|
|
@@ -4092,21 +4904,26 @@ int main() {
|
|
| 4092 |
// #5: function type is h(int, const int*)
|
| 4093 |
h<const int>(1,0);
|
| 4094 |
}
|
| 4095 |
```
|
| 4096 |
|
| 4097 |
-
|
| 4098 |
-
|
|
|
|
|
|
|
|
|
|
| 4099 |
|
| 4100 |
The resulting substituted and adjusted function type is used as the type
|
| 4101 |
of the function template for template argument deduction. If a template
|
| 4102 |
argument has not been deduced and its corresponding template parameter
|
| 4103 |
has a default argument, the template argument is determined by
|
| 4104 |
substituting the template arguments determined for preceding template
|
| 4105 |
parameters into the default argument. If the substitution results in an
|
| 4106 |
invalid type, as described above, type deduction fails.
|
| 4107 |
|
|
|
|
|
|
|
| 4108 |
``` cpp
|
| 4109 |
template <class T, class U = double>
|
| 4110 |
void f(T t = 0, U u = 0);
|
| 4111 |
|
| 4112 |
void g() {
|
|
@@ -4116,10 +4933,12 @@ void g() {
|
|
| 4116 |
f<int>(); // f<int,double>(0,0)
|
| 4117 |
f<int,char>(); // f<int,char>(0,0)
|
| 4118 |
}
|
| 4119 |
```
|
| 4120 |
|
|
|
|
|
|
|
| 4121 |
When all template arguments have been deduced or obtained from default
|
| 4122 |
template arguments, all uses of template parameters in the template
|
| 4123 |
parameter list of the template and the function type are replaced with
|
| 4124 |
the corresponding deduced or default argument values. If the
|
| 4125 |
substitution results in an invalid type, as described above, type
|
|
@@ -4139,14 +4958,18 @@ the function type and in template parameter declarations. The
|
|
| 4139 |
expressions include not only constant expressions such as those that
|
| 4140 |
appear in array bounds or as nontype template arguments but also general
|
| 4141 |
expressions (i.e., non-constant expressions) inside `sizeof`,
|
| 4142 |
`decltype`, and other contexts that allow non-constant expressions. The
|
| 4143 |
substitution proceeds in lexical order and stops when a condition that
|
| 4144 |
-
causes deduction to fail is encountered.
|
| 4145 |
-
|
| 4146 |
-
|
| 4147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4148 |
|
| 4149 |
``` cpp
|
| 4150 |
template <class T> struct A { using X = typename T::X; };
|
| 4151 |
template <class T> typename T::X f(typename A<T>::X);
|
| 4152 |
template <class T> void f(...) { }
|
|
@@ -4157,22 +4980,33 @@ void h() {
|
|
| 4157 |
f<int>(0); // OK, substituting return type causes deduction to fail
|
| 4158 |
g<int>(0); // error, substituting parameter type instantiates A<int>
|
| 4159 |
}
|
| 4160 |
```
|
| 4161 |
|
|
|
|
|
|
|
| 4162 |
If a substitution results in an invalid type or expression, type
|
| 4163 |
deduction fails. An invalid type or expression is one that would be
|
| 4164 |
ill-formed, with a diagnostic required, if written using the substituted
|
| 4165 |
-
arguments.
|
| 4166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4167 |
Only invalid types and expressions in the immediate context of the
|
| 4168 |
function type and its template parameter types can result in a deduction
|
| 4169 |
-
failure.
|
| 4170 |
-
|
| 4171 |
-
|
| 4172 |
-
|
| 4173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4174 |
|
| 4175 |
``` cpp
|
| 4176 |
struct X { };
|
| 4177 |
struct Y {
|
| 4178 |
Y(X){}
|
|
@@ -4183,36 +5017,47 @@ X f(Y, Y); // #2
|
|
| 4183 |
|
| 4184 |
X x1, x2;
|
| 4185 |
X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
|
| 4186 |
```
|
| 4187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4188 |
Type deduction may fail for the following reasons:
|
| 4189 |
|
| 4190 |
- Attempting to instantiate a pack expansion containing multiple
|
| 4191 |
parameter packs of differing lengths.
|
| 4192 |
- Attempting to create an array with an element type that is `void`, a
|
| 4193 |
function type, a reference type, or an abstract class type, or
|
| 4194 |
attempting to create an array with a size that is zero or negative.
|
|
|
|
| 4195 |
``` cpp
|
| 4196 |
template <class T> int f(T[5]);
|
| 4197 |
int I = f<int>(0);
|
| 4198 |
int j = f<void>(0); // invalid array
|
| 4199 |
```
|
|
|
|
|
|
|
| 4200 |
- Attempting to use a type that is not a class or enumeration type in a
|
| 4201 |
qualified name.
|
|
|
|
| 4202 |
``` cpp
|
| 4203 |
template <class T> int f(typename T::B*);
|
| 4204 |
int i = f<int>(0);
|
| 4205 |
```
|
|
|
|
|
|
|
| 4206 |
- Attempting to use a type in a *nested-name-specifier* of a
|
| 4207 |
*qualified-id* when that type does not contain the specified member,
|
| 4208 |
or
|
| 4209 |
- the specified member is not a type where a type is required, or
|
| 4210 |
- the specified member is not a template where a template is required,
|
| 4211 |
or
|
| 4212 |
- the specified member is not a non-type where a non-type is required.
|
| 4213 |
|
|
|
|
| 4214 |
``` cpp
|
| 4215 |
template <int I> struct X { };
|
| 4216 |
template <template <class T> class> struct Z { };
|
| 4217 |
template <class T> void f(typename T::Y*){}
|
| 4218 |
template <class T> void g(X<T::N>*){}
|
|
@@ -4232,82 +5077,124 @@ Type deduction may fail for the following reasons:
|
|
| 4232 |
f<B>(0); // The Y member of B is not a type
|
| 4233 |
g<C>(0); // The N member of C is not a non-type
|
| 4234 |
h<D>(0); // The TT member of D is not a template
|
| 4235 |
}
|
| 4236 |
```
|
|
|
|
|
|
|
| 4237 |
- Attempting to create a pointer to reference type.
|
| 4238 |
- Attempting to create a reference to `void`.
|
| 4239 |
- Attempting to create “pointer to member of `T`” when `T` is not a
|
| 4240 |
class type.
|
|
|
|
| 4241 |
``` cpp
|
| 4242 |
template <class T> int f(int T::*);
|
| 4243 |
int i = f<int>(0);
|
| 4244 |
```
|
|
|
|
|
|
|
| 4245 |
- Attempting to give an invalid type to a non-type template parameter.
|
|
|
|
| 4246 |
``` cpp
|
| 4247 |
template <class T, T> struct S {};
|
| 4248 |
template <class T> int f(S<T, T()>*);
|
| 4249 |
struct X {};
|
| 4250 |
int i0 = f<X>(0);
|
| 4251 |
```
|
|
|
|
|
|
|
| 4252 |
- Attempting to perform an invalid conversion in either a template
|
| 4253 |
argument expression, or an expression used in the function
|
| 4254 |
declaration.
|
|
|
|
| 4255 |
``` cpp
|
| 4256 |
template <class T, T*> int f(int);
|
| 4257 |
int i2 = f<int,1>(0); // can't conv 1 to int*
|
| 4258 |
```
|
|
|
|
|
|
|
| 4259 |
- Attempting to create a function type in which a parameter has a type
|
| 4260 |
of `void`, or in which the return type is a function type or array
|
| 4261 |
type.
|
| 4262 |
- Attempting to create a function type in which a parameter type or the
|
| 4263 |
return type is an abstract class type ([[class.abstract]]).
|
| 4264 |
|
| 4265 |
-
|
| 4266 |
-
|
| 4267 |
-
|
| 4268 |
-
|
| 4269 |
-
|
| 4270 |
-
|
|
|
|
|
|
|
|
|
|
| 4271 |
|
| 4272 |
``` cpp
|
| 4273 |
template <int> int f(int);
|
| 4274 |
template <signed char> int f(int);
|
| 4275 |
-
int i1 = f<
|
| 4276 |
-
int i2 = f<
|
| 4277 |
```
|
| 4278 |
|
|
|
|
|
|
|
| 4279 |
#### Deducing template arguments from a function call <a id="temp.deduct.call">[[temp.deduct.call]]</a>
|
| 4280 |
|
| 4281 |
Template argument deduction is done by comparing each function template
|
| 4282 |
-
parameter type (call it `P`)
|
| 4283 |
-
|
| 4284 |
-
|
| 4285 |
-
|
| 4286 |
-
|
| 4287 |
-
|
| 4288 |
-
|
| 4289 |
-
|
| 4290 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4291 |
|
| 4292 |
``` cpp
|
| 4293 |
template<class T> void f(std::initializer_list<T>);
|
| 4294 |
f({1,2,3}); // T deduced to int
|
| 4295 |
f({1,"asdf"}); // error: T deduced to both int and const char*
|
| 4296 |
|
| 4297 |
template<class T> void g(T);
|
| 4298 |
g({1,2,3}); // error: no argument deduced for T
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4299 |
```
|
| 4300 |
|
|
|
|
|
|
|
| 4301 |
For a function parameter pack that occurs at the end of the
|
| 4302 |
-
*parameter-declaration-list*,
|
| 4303 |
-
the call
|
| 4304 |
-
function parameter pack
|
| 4305 |
-
|
| 4306 |
-
|
| 4307 |
-
non-deduced context (
|
| 4308 |
-
pack is never deduced.
|
|
|
|
|
|
|
| 4309 |
|
| 4310 |
``` cpp
|
| 4311 |
template<class ... Types> void f(Types& ...);
|
| 4312 |
template<class T1, class ... Types> void g(T1, Types ...);
|
| 4313 |
template<class T1, class ... Types> void g1(Types ..., T1);
|
|
@@ -4316,112 +5203,185 @@ void h(int x, float& y) {
|
|
| 4316 |
const int z = x;
|
| 4317 |
f(x, y, z); // Types is deduced to int, float, const int
|
| 4318 |
g(x, y, z); // T1 is deduced to int; Types is deduced to float, int
|
| 4319 |
g1(x, y, z); // error: Types is not deduced
|
| 4320 |
g1<int, int, int>(x, y, z); // OK, no deduction occurs
|
| 4321 |
-
|
| 4322 |
}
|
| 4323 |
```
|
| 4324 |
|
|
|
|
|
|
|
| 4325 |
If `P` is not a reference type:
|
| 4326 |
|
| 4327 |
- If `A` is an array type, the pointer type produced by the
|
| 4328 |
array-to-pointer standard conversion ([[conv.array]]) is used in
|
| 4329 |
place of `A` for type deduction; otherwise,
|
| 4330 |
- If `A` is a function type, the pointer type produced by the
|
| 4331 |
function-to-pointer standard conversion ([[conv.func]]) is used in
|
| 4332 |
place of `A` for type deduction; otherwise,
|
| 4333 |
-
- If `A` is a cv-qualified type, the top
|
| 4334 |
type are ignored for type deduction.
|
| 4335 |
|
| 4336 |
-
If `P` is a cv-qualified type, the top
|
| 4337 |
are ignored for type deduction. If `P` is a reference type, the type
|
| 4338 |
-
referred to by `P` is used for type deduction.
|
| 4339 |
-
|
| 4340 |
-
|
| 4341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4342 |
|
| 4343 |
``` cpp
|
| 4344 |
-
template <class T> int f(T&&);
|
| 4345 |
template <class T> int g(const T&&);
|
| 4346 |
int i;
|
| 4347 |
int n1 = f(i); // calls f<int&>(int&)
|
| 4348 |
int n2 = f(0); // calls f<int>(int&&)
|
| 4349 |
int n3 = g(i); // error: would call g<int>(const int&&), which
|
| 4350 |
// would bind an rvalue reference to an lvalue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4351 |
```
|
| 4352 |
|
|
|
|
|
|
|
| 4353 |
In general, the deduction process attempts to find template argument
|
| 4354 |
values that will make the deduced `A` identical to `A` (after the type
|
| 4355 |
`A` is transformed as described above). However, there are three cases
|
| 4356 |
that allow a difference:
|
| 4357 |
|
| 4358 |
- If the original `P` is a reference type, the deduced `A` (i.e., the
|
| 4359 |
type referred to by the reference) can be more cv-qualified than the
|
| 4360 |
transformed `A`.
|
| 4361 |
- The transformed `A` can be another pointer or pointer to member type
|
| 4362 |
-
that can be converted to the deduced `A` via a
|
| 4363 |
-
conversion ([[conv.
|
|
|
|
| 4364 |
- If `P` is a class and `P` has the form *simple-template-id*, then the
|
| 4365 |
transformed `A` can be a derived class of the deduced `A`. Likewise,
|
| 4366 |
if `P` is a pointer to a class of the form *simple-template-id*, the
|
| 4367 |
transformed `A` can be a pointer to a derived class pointed to by the
|
| 4368 |
deduced `A`.
|
| 4369 |
|
| 4370 |
-
as specified in [[temp.arg.explicit]], implicit conversions will be
|
| 4371 |
-
performed on a function argument to convert it to the type of the
|
| 4372 |
-
corresponding function parameter if the parameter contains no
|
| 4373 |
-
*template-parameter*s that participate in template argument deduction.
|
| 4374 |
-
Such conversions are also allowed, in addition to the ones described in
|
| 4375 |
-
the preceding list.
|
| 4376 |
-
|
| 4377 |
These alternatives are considered only if type deduction would otherwise
|
| 4378 |
fail. If they yield more than one possible deduced `A`, the type
|
| 4379 |
-
deduction fails.
|
| 4380 |
-
function parameters of a function template, or is used only in a
|
| 4381 |
-
non-deduced context, its corresponding *template-argument* cannot be
|
| 4382 |
-
deduced from a function call and the *template-argument* must be
|
| 4383 |
-
explicitly specified.
|
| 4384 |
|
| 4385 |
-
|
| 4386 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4387 |
|
| 4388 |
- If the argument is an overload set containing one or more function
|
| 4389 |
templates, the parameter is treated as a non-deduced context.
|
| 4390 |
- If the argument is an overload set (not containing function
|
| 4391 |
templates), trial argument deduction is attempted using each of the
|
| 4392 |
members of the set. If deduction succeeds for only one of the overload
|
| 4393 |
set members, that member is used as the argument value for the
|
| 4394 |
deduction. If deduction succeeds for more than one member of the
|
| 4395 |
overload set the parameter is treated as a non-deduced context.
|
|
|
|
|
|
|
|
|
|
| 4396 |
``` cpp
|
| 4397 |
-
|
| 4398 |
-
// parameter is a deduced context.
|
| 4399 |
template <class T> int f(T (*p)(T));
|
| 4400 |
int g(int);
|
| 4401 |
int g(char);
|
| 4402 |
int i = f(g); // calls f(int (*)(int))
|
| 4403 |
```
|
| 4404 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4405 |
``` cpp
|
| 4406 |
-
|
| 4407 |
-
// non-deduced context.
|
| 4408 |
template <class T> int f(T, T (*p)(T));
|
| 4409 |
int g(int);
|
| 4410 |
char g(char);
|
| 4411 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 4412 |
```
|
| 4413 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4414 |
``` cpp
|
| 4415 |
-
|
| 4416 |
-
// parameter to be a non-deduced context.
|
| 4417 |
template <class T> int f(T, T (*p)(T));
|
| 4418 |
char g(char);
|
| 4419 |
template <class T> T g(T);
|
| 4420 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 4421 |
```
|
| 4422 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4423 |
#### Deducing template arguments taking the address of a function template <a id="temp.deduct.funcaddr">[[temp.deduct.funcaddr]]</a>
|
| 4424 |
|
| 4425 |
Template arguments can be deduced from the type specified when taking
|
| 4426 |
the address of an overloaded function ([[over.over]]). The function
|
| 4427 |
template’s function type and the specified type are used as the types of
|
|
@@ -4451,23 +5411,28 @@ If `A` is not a reference type:
|
|
| 4451 |
array-to-pointer standard conversion ([[conv.array]]) is used in
|
| 4452 |
place of `P` for type deduction; otherwise,
|
| 4453 |
- If `P` is a function type, the pointer type produced by the
|
| 4454 |
function-to-pointer standard conversion ([[conv.func]]) is used in
|
| 4455 |
place of `P` for type deduction; otherwise,
|
| 4456 |
-
- If `P` is a cv-qualified type, the top
|
| 4457 |
type are ignored for type deduction.
|
| 4458 |
|
| 4459 |
-
If `A` is a cv-qualified type, the top
|
| 4460 |
are ignored for type deduction. If `A` is a reference type, the type
|
| 4461 |
referred to by `A` is used for type deduction.
|
| 4462 |
|
| 4463 |
In general, the deduction process attempts to find template argument
|
| 4464 |
values that will make the deduced `A` identical to `A`. However, there
|
| 4465 |
-
are
|
| 4466 |
|
| 4467 |
- If the original `A` is a reference type, `A` can be more cv-qualified
|
| 4468 |
than the deduced `A` (i.e., the type referred to by the reference)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4469 |
- The deduced `A` can be another pointer or pointer to member type that
|
| 4470 |
can be converted to `A` via a qualification conversion.
|
| 4471 |
|
| 4472 |
These alternatives are considered only if type deduction would otherwise
|
| 4473 |
fail. If they yield more than one possible deduced `A`, the type
|
|
@@ -4479,36 +5444,44 @@ process is used to determine the deduced template argument values:
|
|
| 4479 |
|
| 4480 |
If `A` is a type
|
| 4481 |
|
| 4482 |
and `P` is a type
|
| 4483 |
|
| 4484 |
-
|
| 4485 |
-
respectively for type deduction.
|
|
|
|
|
|
|
| 4486 |
|
| 4487 |
``` cpp
|
| 4488 |
struct A {
|
| 4489 |
template <class T> operator T***();
|
| 4490 |
};
|
| 4491 |
A a;
|
| 4492 |
const int * const * const * p1 = a; // T is deduced as int, not const int
|
| 4493 |
```
|
| 4494 |
|
|
|
|
|
|
|
| 4495 |
#### Deducing template arguments during partial ordering <a id="temp.deduct.partial">[[temp.deduct.partial]]</a>
|
| 4496 |
|
| 4497 |
Template argument deduction is done by comparing certain types
|
| 4498 |
associated with the two function templates being compared.
|
| 4499 |
|
| 4500 |
Two sets of types are used to determine the partial ordering. For each
|
| 4501 |
of the templates involved there is the original function type and the
|
| 4502 |
-
transformed function type.
|
| 4503 |
-
|
| 4504 |
-
|
| 4505 |
-
|
| 4506 |
-
|
| 4507 |
-
|
| 4508 |
-
|
| 4509 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4510 |
|
| 4511 |
The types used to determine the ordering depend on the context in which
|
| 4512 |
the partial ordering is done:
|
| 4513 |
|
| 4514 |
- In the context of a function call, the types used are those function
|
|
@@ -4518,11 +5491,13 @@ the partial ordering is done:
|
|
| 4518 |
- In other contexts ([[temp.func.order]]) the function template’s
|
| 4519 |
function type is used.
|
| 4520 |
|
| 4521 |
Each type nominated above from the parameter template and the
|
| 4522 |
corresponding type from the argument template are used as the types of
|
| 4523 |
-
`P` and `A`.
|
|
|
|
|
|
|
| 4524 |
|
| 4525 |
Before the partial ordering is done, certain transformations are
|
| 4526 |
performed on the types used for partial ordering:
|
| 4527 |
|
| 4528 |
- If `P` is a reference type, `P` is replaced by the type referred to.
|
|
@@ -4539,70 +5514,88 @@ Remove any top-level cv-qualifiers:
|
|
| 4539 |
- If `P` is a cv-qualified type, `P` is replaced by the cv-unqualified
|
| 4540 |
version of `P`.
|
| 4541 |
- If `A` is a cv-qualified type, `A` is replaced by the cv-unqualified
|
| 4542 |
version of `A`.
|
| 4543 |
|
| 4544 |
-
|
| 4545 |
-
|
| 4546 |
-
|
| 4547 |
-
|
| 4548 |
-
|
| 4549 |
-
|
| 4550 |
-
|
| 4551 |
-
|
| 4552 |
-
deduction succeeds for a given type,
|
| 4553 |
-
is considered to be at least as
|
| 4554 |
-
parameter template.
|
|
|
|
|
|
|
| 4555 |
|
| 4556 |
``` cpp
|
| 4557 |
template<class... Args> void f(Args... args); // #1
|
| 4558 |
template<class T1, class... Args> void f(T1 a1, Args... args); // #2
|
| 4559 |
template<class T1, class T2> void f(T1 a1, T2 a2); // #3
|
| 4560 |
|
| 4561 |
f(); // calls #1
|
| 4562 |
f(1, 2, 3); // calls #2
|
| 4563 |
-
f(1, 2);
|
| 4564 |
-
|
| 4565 |
```
|
| 4566 |
|
|
|
|
|
|
|
| 4567 |
If, for a given type, deduction succeeds in both directions (i.e., the
|
| 4568 |
types are identical after the transformations above) and both `P` and
|
| 4569 |
`A` were reference types (before being replaced with the type referred
|
| 4570 |
to above):
|
| 4571 |
|
| 4572 |
- if the type from the argument template was an lvalue reference and the
|
| 4573 |
-
type from the parameter template was not, the
|
| 4574 |
-
considered to be
|
|
|
|
| 4575 |
- if the type from the argument template is more cv-qualified than the
|
| 4576 |
-
type from the parameter template (as described above), the
|
| 4577 |
-
type is considered to be
|
| 4578 |
-
|
| 4579 |
|
| 4580 |
-
|
| 4581 |
-
|
| 4582 |
-
|
| 4583 |
-
|
| 4584 |
-
|
| 4585 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4586 |
|
| 4587 |
In most cases, all template parameters must have values in order for
|
| 4588 |
deduction to succeed, but for partial ordering purposes a template
|
| 4589 |
parameter may remain without a value provided it is not used in the
|
| 4590 |
-
types being used for partial ordering.
|
| 4591 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4592 |
|
| 4593 |
``` cpp
|
| 4594 |
template <class T> T f(int); // #1
|
| 4595 |
template <class T, class U> T f(U); // #2
|
| 4596 |
void g() {
|
| 4597 |
f<int>(1); // calls #1
|
| 4598 |
}
|
| 4599 |
```
|
| 4600 |
|
| 4601 |
-
|
| 4602 |
-
|
| 4603 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4604 |
|
| 4605 |
``` cpp
|
| 4606 |
template<class ...> struct Tuple { };
|
| 4607 |
template<class ... Types> void g(Tuple<Types ...>); // #1
|
| 4608 |
template<class T1, class ... Types> void g(Tuple<T1, Types ...>); // #2
|
|
@@ -4612,10 +5605,12 @@ g(Tuple<>()); // calls #1
|
|
| 4612 |
g(Tuple<int, float>()); // calls #2
|
| 4613 |
g(Tuple<int, float&>()); // calls #3
|
| 4614 |
g(Tuple<int>()); // calls #3
|
| 4615 |
```
|
| 4616 |
|
|
|
|
|
|
|
| 4617 |
#### Deducing template arguments from a type <a id="temp.deduct.type">[[temp.deduct.type]]</a>
|
| 4618 |
|
| 4619 |
Template arguments can be deduced in several different contexts, but in
|
| 4620 |
each case a type that is specified in terms of template parameters (call
|
| 4621 |
it `P`) is compared with an actual type (call it `A`), and an attempt is
|
|
@@ -4630,11 +5625,12 @@ In some cases, the deduction is done using a single set of types `P` and
|
|
| 4630 |
deduced template argument values are then combined. If type deduction
|
| 4631 |
cannot be done for any `P/A` pair, or if for any pair the deduction
|
| 4632 |
leads to more than one possible set of deduced values, or if different
|
| 4633 |
pairs yield different deduced values, or if any template argument
|
| 4634 |
remains neither deduced nor explicitly specified, template argument
|
| 4635 |
-
deduction fails.
|
|
|
|
| 4636 |
|
| 4637 |
A given type `P` can be composed from a number of other types,
|
| 4638 |
templates, and non-type values:
|
| 4639 |
|
| 4640 |
- A function type includes the types of each of the function parameters
|
|
@@ -4655,10 +5651,15 @@ In certain contexts, however, the value does not participate in type
|
|
| 4655 |
deduction, but instead uses the values of template arguments that were
|
| 4656 |
either deduced elsewhere or explicitly specified. If a template
|
| 4657 |
parameter is used only in non-deduced contexts and is not explicitly
|
| 4658 |
specified, template argument deduction fails.
|
| 4659 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4660 |
The non-deduced contexts are:
|
| 4661 |
|
| 4662 |
- The *nested-name-specifier* of a type that was specified using a
|
| 4663 |
*qualified-id*.
|
| 4664 |
- The *expression* of a *decltype-specifier*.
|
|
@@ -4676,35 +5677,43 @@ The non-deduced contexts are:
|
|
| 4676 |
- no function matches the function parameter type, or
|
| 4677 |
- the set of functions supplied as an argument contains one or more
|
| 4678 |
function templates.
|
| 4679 |
- A function parameter for which the associated argument is an
|
| 4680 |
initializer list ([[dcl.init.list]]) but the parameter does not have
|
| 4681 |
-
|
| 4682 |
-
|
|
|
|
| 4683 |
``` cpp
|
| 4684 |
template<class T> void g(T);
|
| 4685 |
g({1,2,3}); // error: no argument deduced for T
|
| 4686 |
```
|
|
|
|
|
|
|
| 4687 |
- A function parameter pack that does not occur at the end of the
|
| 4688 |
*parameter-declaration-list*.
|
| 4689 |
|
| 4690 |
When a type name is specified in a way that includes a non-deduced
|
| 4691 |
context, all of the types that comprise that type name are also
|
| 4692 |
non-deduced. However, a compound type can include both deduced and
|
| 4693 |
-
non-deduced types.
|
|
|
|
|
|
|
| 4694 |
`T2` are non-deduced. Likewise, if a type is specified as
|
| 4695 |
`A<I+J>::X<T>`, `I`, `J`, and `T` are non-deduced. If a type is
|
| 4696 |
specified as `void` `f(typename` `A<T>::B,` `A<T>)`, the `T` in
|
| 4697 |
-
`A<T>::B` is non-deduced but the `T` in `A<T>` is
|
|
|
|
|
|
|
|
|
|
| 4698 |
|
| 4699 |
Here is an example in which different parameter/argument pairs produce
|
| 4700 |
inconsistent template argument deductions:
|
| 4701 |
|
| 4702 |
``` cpp
|
| 4703 |
-
template<class T> void f(T x, T y) {
|
| 4704 |
-
struct A {
|
| 4705 |
-
struct B : A {
|
| 4706 |
void g(A a, B b) {
|
| 4707 |
f(a,b); // error: T could be A or B
|
| 4708 |
f(b,a); // error: T could be A or B
|
| 4709 |
f(a,a); // OK: T is A
|
| 4710 |
f(b,b); // OK: T is B
|
|
@@ -4755,10 +5764,12 @@ void t() {
|
|
| 4755 |
f(d); // calls f(B<int>&)
|
| 4756 |
f(d2); // calls f(B<int>&)
|
| 4757 |
}
|
| 4758 |
```
|
| 4759 |
|
|
|
|
|
|
|
| 4760 |
A template type argument `T`, a template template argument `TT` or a
|
| 4761 |
template non-type argument `i` can be deduced if `P` and `A` have one of
|
| 4762 |
the following forms:
|
| 4763 |
|
| 4764 |
``` cpp
|
|
@@ -4787,20 +5798,20 @@ template-name<i> (where template-name refers to a class template)
|
|
| 4787 |
TT<T>
|
| 4788 |
TT<i>
|
| 4789 |
TT<>
|
| 4790 |
```
|
| 4791 |
|
| 4792 |
-
where `(T)` represents a
|
| 4793 |
-
parameter type contains a `T`, and `()` represents a
|
| 4794 |
-
|
| 4795 |
`<T>` represents template argument lists where at least one argument
|
| 4796 |
contains a `T`, `<i>` represents template argument lists where at least
|
| 4797 |
one argument contains an `i` and `<>` represents template argument lists
|
| 4798 |
where no argument contains a `T` or an `i`.
|
| 4799 |
|
| 4800 |
If `P` has a form that contains `<T>` or `<i>`, then each argument Pᵢ of
|
| 4801 |
-
the respective template argument list `P` is compared with the
|
| 4802 |
corresponding argument Aᵢ of the corresponding template argument list of
|
| 4803 |
`A`. If the template argument list of `P` contains a pack expansion that
|
| 4804 |
is not the last template argument, the entire template argument list is
|
| 4805 |
a non-deduced context. If `Pᵢ` is a pack expansion, then the pattern of
|
| 4806 |
`Pᵢ` is compared with each remaining argument in the template argument
|
|
@@ -4812,10 +5823,12 @@ pack expansion:
|
|
| 4812 |
- if `P` does not contain a template argument corresponding to `Aᵢ` then
|
| 4813 |
`Aᵢ` is ignored;
|
| 4814 |
- otherwise, if `Pᵢ` is not a pack expansion, template argument
|
| 4815 |
deduction fails.
|
| 4816 |
|
|
|
|
|
|
|
| 4817 |
``` cpp
|
| 4818 |
template<class T1, class... Z> class S; // #1
|
| 4819 |
template<class T1, class... Z> class S<T1, const Z&...> { }; // #2
|
| 4820 |
template<class T1, class T2> class S<T1, const T2&> { }; // #3
|
| 4821 |
S<int, const int&> s; // both #2 and #3 match; #3 is more specialized
|
|
@@ -4824,24 +5837,30 @@ template<class T, class... U> struct A { }; // #1
|
|
| 4824 |
template<class T1, class T2, class... U> struct A<T1, T2*, U...> { }; // #2
|
| 4825 |
template<class T1, class T2> struct A<T1, T2> { }; // #3
|
| 4826 |
template struct A<int, int*>; // selects #2
|
| 4827 |
```
|
| 4828 |
|
|
|
|
|
|
|
| 4829 |
Similarly, if `P` has a form that contains `(T)`, then each parameter
|
| 4830 |
-
type `Pᵢ` of the respective
|
| 4831 |
-
with the corresponding parameter type `Aᵢ` of the corresponding
|
| 4832 |
-
|
| 4833 |
originated from deduction when taking the address of a function
|
| 4834 |
template ([[temp.deduct.funcaddr]]) or when deducing template arguments
|
| 4835 |
from a function declaration ([[temp.deduct.decl]]) and `Pᵢ` and `Aᵢ`
|
| 4836 |
-
are parameters of the top-level
|
| 4837 |
-
respectively, `Pᵢ` is adjusted if it is
|
| 4838 |
-
|
| 4839 |
-
|
| 4840 |
-
|
| 4841 |
-
|
| 4842 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4843 |
|
| 4844 |
``` cpp
|
| 4845 |
template <class T> void f(T&&);
|
| 4846 |
template <> void f(int&) { } // #1
|
| 4847 |
template <> void f(int&&) { } // #2
|
|
@@ -4849,32 +5868,40 @@ void g(int i) {
|
|
| 4849 |
f(i); // calls f<int&>(int&), i.e., #1
|
| 4850 |
f(0); // calls f<int>(int&&), i.e., #2
|
| 4851 |
}
|
| 4852 |
```
|
| 4853 |
|
|
|
|
|
|
|
| 4854 |
If the *parameter-declaration* corresponding to `Pᵢ` is a function
|
| 4855 |
parameter pack, then the type of its *declarator-id* is compared with
|
| 4856 |
-
each remaining parameter type in the
|
| 4857 |
comparison deduces template arguments for subsequent positions in the
|
| 4858 |
template parameter packs expanded by the function parameter pack. During
|
| 4859 |
partial ordering ([[temp.deduct.partial]]), if `Aᵢ` was originally a
|
| 4860 |
function parameter pack:
|
| 4861 |
|
| 4862 |
- if `P` does not contain a function parameter type corresponding to
|
| 4863 |
`Aᵢ` then `Aᵢ` is ignored;
|
| 4864 |
- otherwise, if `Pᵢ` is not a function parameter pack, template argument
|
| 4865 |
deduction fails.
|
| 4866 |
|
|
|
|
|
|
|
| 4867 |
``` cpp
|
| 4868 |
template<class T, class... U> void f(T*, U...) { } // #1
|
| 4869 |
template<class T> void f(T) { } // #2
|
| 4870 |
template void f(int*); // selects #1
|
| 4871 |
```
|
| 4872 |
|
|
|
|
|
|
|
| 4873 |
These forms can be used in the same way as `T` is for further
|
| 4874 |
composition of types.
|
| 4875 |
|
|
|
|
|
|
|
| 4876 |
``` cpp
|
| 4877 |
X<int> (*)(char[6])
|
| 4878 |
```
|
| 4879 |
|
| 4880 |
is of the form
|
|
@@ -4889,22 +5916,67 @@ which is a variant of
|
|
| 4889 |
type (*)(T)
|
| 4890 |
```
|
| 4891 |
|
| 4892 |
where type is `X<int>` and `T` is `char[6]`.
|
| 4893 |
|
|
|
|
|
|
|
| 4894 |
Template arguments cannot be deduced from function arguments involving
|
| 4895 |
constructs other than the ones specified above.
|
| 4896 |
|
| 4897 |
-
|
| 4898 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4899 |
|
| 4900 |
``` cpp
|
| 4901 |
-
template<
|
| 4902 |
-
|
| 4903 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4904 |
```
|
| 4905 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4906 |
Except for reference and pointer types, a major array bound is not part
|
| 4907 |
of a function parameter type and cannot be deduced from an argument:
|
| 4908 |
|
| 4909 |
``` cpp
|
| 4910 |
template<int i> void f1(int a[10][i]);
|
|
@@ -4919,17 +5991,23 @@ void g() {
|
|
| 4919 |
f2<10>(v); // OK
|
| 4920 |
f3(v); // OK: i deduced to be 10
|
| 4921 |
}
|
| 4922 |
```
|
| 4923 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4924 |
If, in the declaration of a function template with a non-type template
|
| 4925 |
parameter, the non-type template parameter is used in a subexpression in
|
| 4926 |
the function parameter list, the expression is a non-deduced context as
|
| 4927 |
specified above.
|
| 4928 |
|
|
|
|
|
|
|
| 4929 |
``` cpp
|
| 4930 |
-
template <int i> class A {
|
| 4931 |
template <int i> void g(A<i+1>);
|
| 4932 |
template <int i> void f(A<i>, A<i+1>);
|
| 4933 |
void k() {
|
| 4934 |
A<1> a1;
|
| 4935 |
A<2> a2;
|
|
@@ -4937,10 +6015,16 @@ void k() {
|
|
| 4937 |
g<0>(a1); // OK
|
| 4938 |
f(a1, a2); // OK
|
| 4939 |
}
|
| 4940 |
```
|
| 4941 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4942 |
Template parameters do not participate in template argument deduction if
|
| 4943 |
they are used only in non-deduced contexts. For example,
|
| 4944 |
|
| 4945 |
``` cpp
|
| 4946 |
template<int i, typename T>
|
|
@@ -4949,23 +6033,26 @@ T deduce(typename A<T>::X x, // T is not deduced here
|
|
| 4949 |
typename B<i>::Y y); // i is not deduced here
|
| 4950 |
A<int> a;
|
| 4951 |
B<77> b;
|
| 4952 |
|
| 4953 |
int x = deduce<77>(a.xm, 62, b.ym);
|
| 4954 |
-
// T is deduced to be int, a.xm must be convertible to
|
| 4955 |
-
//
|
| 4956 |
-
// i is explicitly specified to be 77, b.ym must be convertible
|
| 4957 |
-
// to B<77>::Y
|
| 4958 |
```
|
| 4959 |
|
| 4960 |
-
|
| 4961 |
-
|
| 4962 |
-
|
| 4963 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4964 |
|
| 4965 |
``` cpp
|
| 4966 |
-
template<int i> class A {
|
| 4967 |
template<short s> void f(A<s>);
|
| 4968 |
void k1() {
|
| 4969 |
A<1> a;
|
| 4970 |
f(a); // error: deduction fails for conversion from int to short
|
| 4971 |
f<1>(a); // OK
|
|
@@ -4977,13 +6064,17 @@ void k2() {
|
|
| 4977 |
B<1> b;
|
| 4978 |
g(b); // OK: cv-qualifiers are ignored on template parameter types
|
| 4979 |
}
|
| 4980 |
```
|
| 4981 |
|
|
|
|
|
|
|
| 4982 |
A *template-argument* can be deduced from a function, pointer to
|
| 4983 |
function, or pointer to member function type.
|
| 4984 |
|
|
|
|
|
|
|
| 4985 |
``` cpp
|
| 4986 |
template<class T> void f(void(*)(T,int));
|
| 4987 |
template<class T> void foo(T,int);
|
| 4988 |
void g(int,int);
|
| 4989 |
void g(char,int);
|
|
@@ -4995,37 +6086,49 @@ int m() {
|
|
| 4995 |
f(&h); // OK: void h(char,int) is a unique match
|
| 4996 |
f(&foo); // error: type deduction fails because foo is a template
|
| 4997 |
}
|
| 4998 |
```
|
| 4999 |
|
|
|
|
|
|
|
| 5000 |
A template *type-parameter* cannot be deduced from the type of a
|
| 5001 |
function default argument.
|
| 5002 |
|
|
|
|
|
|
|
| 5003 |
``` cpp
|
| 5004 |
template <class T> void f(T = 5, T = 7);
|
| 5005 |
void g() {
|
| 5006 |
f(1); // OK: call f<int>(1,7)
|
| 5007 |
f(); // error: cannot deduce T
|
| 5008 |
f<int>(); // OK: call f<int>(5,7)
|
| 5009 |
}
|
| 5010 |
```
|
| 5011 |
|
|
|
|
|
|
|
| 5012 |
The *template-argument* corresponding to a template *template-parameter*
|
| 5013 |
is deduced from the type of the *template-argument* of a class template
|
| 5014 |
specialization used in the argument list of a function call.
|
| 5015 |
|
|
|
|
|
|
|
| 5016 |
``` cpp
|
| 5017 |
template <template <class T> class X> struct A { };
|
| 5018 |
template <template <class T> class X> void f(A<X>) { }
|
| 5019 |
template<class T> struct B { };
|
| 5020 |
A<B> ab;
|
| 5021 |
f(ab); // calls f(A<B>)
|
| 5022 |
```
|
| 5023 |
|
| 5024 |
-
|
|
|
|
|
|
|
| 5025 |
[[temp.variadic]]) can deduce zero or more arguments for each parameter
|
| 5026 |
-
pack.
|
|
|
|
|
|
|
| 5027 |
|
| 5028 |
``` cpp
|
| 5029 |
template<class> struct X { };
|
| 5030 |
template<class R, class ... ArgTypes> struct X<R(int, ArgTypes ...)> { };
|
| 5031 |
template<class ... Types> struct Y { };
|
|
@@ -5041,10 +6144,12 @@ Y<> y1; // use primary template; Types is empty
|
|
| 5041 |
Y<int&, float&, double&> y2; // uses partial specialization; T is int&, Types contains float, double
|
| 5042 |
Y<int, float, double> y3; // uses primary template; Types contains int, float, double
|
| 5043 |
int fv = f(g); // OK; Types contains int, float
|
| 5044 |
```
|
| 5045 |
|
|
|
|
|
|
|
| 5046 |
#### Deducing template arguments from a function declaration <a id="temp.deduct.decl">[[temp.deduct.decl]]</a>
|
| 5047 |
|
| 5048 |
In a declaration whose *declarator-id* refers to a specialization of a
|
| 5049 |
function template, template argument deduction is performed to identify
|
| 5050 |
the specialization to which the declaration refers. Specifically, this
|
|
@@ -5077,18 +6182,21 @@ instantiate a function template specialization that can be invoked with
|
|
| 5077 |
the call arguments. For each function template, if the argument
|
| 5078 |
deduction and checking succeeds, the *template-argument*s (deduced
|
| 5079 |
and/or explicit) are used to synthesize the declaration of a single
|
| 5080 |
function template specialization which is added to the candidate
|
| 5081 |
functions set to be used in overload resolution. If, for a given
|
| 5082 |
-
function template, argument deduction fails
|
|
|
|
| 5083 |
to the set of candidate functions for that template. The complete set of
|
| 5084 |
candidate functions includes all the synthesized declarations and all of
|
| 5085 |
the non-template overloaded functions of the same name. The synthesized
|
| 5086 |
declarations are treated like any other functions in the remainder of
|
| 5087 |
overload resolution, except as explicitly noted in
|
| 5088 |
[[over.match.best]].[^9]
|
| 5089 |
|
|
|
|
|
|
|
| 5090 |
``` cpp
|
| 5091 |
template<class T> T max(T a, T b) { return a>b?a:b; }
|
| 5092 |
|
| 5093 |
void f(int a, int b, char c, char d) {
|
| 5094 |
int m1 = max(a,b); // max(int a, int b)
|
|
@@ -5105,24 +6213,32 @@ int max(int,int);
|
|
| 5105 |
|
| 5106 |
to the example above would resolve the third call, by providing a
|
| 5107 |
function that could be called for `max(a,c)` after using the standard
|
| 5108 |
conversion of `char` to `int` for `c`.
|
| 5109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5110 |
Here is an example involving conversions on a function argument involved
|
| 5111 |
in *template-argument* deduction:
|
| 5112 |
|
| 5113 |
``` cpp
|
| 5114 |
-
template<class T> struct B {
|
| 5115 |
-
template<class T> struct D : public B<T> {
|
| 5116 |
template<class T> void f(B<T>&);
|
| 5117 |
|
| 5118 |
void g(B<int>& bi, D<int>& di) {
|
| 5119 |
f(bi); // f(bi)
|
| 5120 |
f(di); // f((B<int>&)di)
|
| 5121 |
}
|
| 5122 |
```
|
| 5123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5124 |
Here is an example involving conversions on a function argument not
|
| 5125 |
involved in *template-parameter* deduction:
|
| 5126 |
|
| 5127 |
``` cpp
|
| 5128 |
template<class T> void f(T*,int); // #1
|
|
@@ -5135,15 +6251,19 @@ void h(int* pi, int i, char c) {
|
|
| 5135 |
f(i,c); // #2: f<int>(i,c);
|
| 5136 |
f(i,i); // #2: f<int>(i,char(i))
|
| 5137 |
}
|
| 5138 |
```
|
| 5139 |
|
|
|
|
|
|
|
| 5140 |
Only the signature of a function template specialization is needed to
|
| 5141 |
enter the specialization in a set of candidate functions. Therefore only
|
| 5142 |
the function template declaration is needed to resolve a call for which
|
| 5143 |
a template specialization is a candidate.
|
| 5144 |
|
|
|
|
|
|
|
| 5145 |
``` cpp
|
| 5146 |
template<class T> void f(T); // declaration
|
| 5147 |
|
| 5148 |
void g() {
|
| 5149 |
f("Annemarie"); // call of f<const char*>
|
|
@@ -5153,11 +6273,56 @@ void g() {
|
|
| 5153 |
The call of `f` is well-formed even if the template `f` is only declared
|
| 5154 |
and not defined at the point of the call. The program will be ill-formed
|
| 5155 |
unless a specialization for `f<const char*>`, either implicitly or
|
| 5156 |
explicitly generated, is present in some translation unit.
|
| 5157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5158 |
<!-- Link reference definitions -->
|
|
|
|
| 5159 |
[basic.def.odr]: basic.md#basic.def.odr
|
| 5160 |
[basic.link]: basic.md#basic.link
|
| 5161 |
[basic.lookup]: basic.md#basic.lookup
|
| 5162 |
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
| 5163 |
[basic.lookup.classref]: basic.md#basic.lookup.classref
|
|
@@ -5172,48 +6337,62 @@ explicitly generated, is present in some translation unit.
|
|
| 5172 |
[class.access]: class.md#class.access
|
| 5173 |
[class.base.init]: special.md#class.base.init
|
| 5174 |
[class.derived]: class.md#class.derived
|
| 5175 |
[class.dtor]: special.md#class.dtor
|
| 5176 |
[class.friend]: class.md#class.friend
|
|
|
|
| 5177 |
[class.mem]: class.md#class.mem
|
| 5178 |
[class.member.lookup]: class.md#class.member.lookup
|
| 5179 |
[class.qual]: basic.md#class.qual
|
|
|
|
| 5180 |
[conv]: conv.md#conv
|
| 5181 |
[conv.array]: conv.md#conv.array
|
|
|
|
| 5182 |
[conv.func]: conv.md#conv.func
|
| 5183 |
-
[conv.integral]: conv.md#conv.integral
|
| 5184 |
-
[conv.mem]: conv.md#conv.mem
|
| 5185 |
-
[conv.ptr]: conv.md#conv.ptr
|
| 5186 |
[conv.qual]: conv.md#conv.qual
|
| 5187 |
[dcl.align]: dcl.md#dcl.align
|
| 5188 |
[dcl.attr.grammar]: dcl.md#dcl.attr.grammar
|
| 5189 |
[dcl.dcl]: dcl.md#dcl.dcl
|
|
|
|
| 5190 |
[dcl.fct]: dcl.md#dcl.fct
|
|
|
|
| 5191 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 5192 |
[dcl.init]: dcl.md#dcl.init
|
| 5193 |
[dcl.init.list]: dcl.md#dcl.init.list
|
| 5194 |
[dcl.meaning]: dcl.md#dcl.meaning
|
| 5195 |
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
|
|
|
|
|
|
|
|
|
| 5196 |
[dcl.type.elab]: dcl.md#dcl.type.elab
|
| 5197 |
[except.spec]: except.md#except.spec
|
| 5198 |
[expr.const]: expr.md#expr.const
|
| 5199 |
[expr.new]: expr.md#expr.new
|
|
|
|
| 5200 |
[expr.prim.lambda]: expr.md#expr.prim.lambda
|
|
|
|
| 5201 |
[expr.ref]: expr.md#expr.ref
|
| 5202 |
[expr.sizeof]: expr.md#expr.sizeof
|
| 5203 |
-
[expr.
|
|
|
|
| 5204 |
[intro.defs]: intro.md#intro.defs
|
|
|
|
| 5205 |
[lex.string]: lex.md#lex.string
|
| 5206 |
[namespace.def]: dcl.md#namespace.def
|
| 5207 |
[namespace.memdef]: dcl.md#namespace.memdef
|
|
|
|
| 5208 |
[over.ics.rank]: over.md#over.ics.rank
|
|
|
|
| 5209 |
[over.match.best]: over.md#over.match.best
|
|
|
|
| 5210 |
[over.match.conv]: over.md#over.match.conv
|
| 5211 |
[over.match.ref]: over.md#over.match.ref
|
| 5212 |
[over.over]: over.md#over.over
|
| 5213 |
[special]: special.md#special
|
|
|
|
| 5214 |
[support.types]: language.md#support.types
|
|
|
|
| 5215 |
[temp]: #temp
|
| 5216 |
[temp.alias]: #temp.alias
|
| 5217 |
[temp.arg]: #temp.arg
|
| 5218 |
[temp.arg.explicit]: #temp.arg.explicit
|
| 5219 |
[temp.arg.nontype]: #temp.arg.nontype
|
|
@@ -5228,10 +6407,11 @@ explicitly generated, is present in some translation unit.
|
|
| 5228 |
[temp.deduct]: #temp.deduct
|
| 5229 |
[temp.deduct.call]: #temp.deduct.call
|
| 5230 |
[temp.deduct.conv]: #temp.deduct.conv
|
| 5231 |
[temp.deduct.decl]: #temp.deduct.decl
|
| 5232 |
[temp.deduct.funcaddr]: #temp.deduct.funcaddr
|
|
|
|
| 5233 |
[temp.deduct.partial]: #temp.deduct.partial
|
| 5234 |
[temp.deduct.type]: #temp.deduct.type
|
| 5235 |
[temp.dep]: #temp.dep
|
| 5236 |
[temp.dep.candidate]: #temp.dep.candidate
|
| 5237 |
[temp.dep.constexpr]: #temp.dep.constexpr
|
|
@@ -5294,11 +6474,11 @@ explicitly generated, is present in some translation unit.
|
|
| 5294 |
selected.
|
| 5295 |
|
| 5296 |
[^8]: Although the *template-argument* corresponding to a
|
| 5297 |
*template-parameter* of type `bool` may be deduced from an array
|
| 5298 |
bound, the resulting value will always be `true` because the array
|
| 5299 |
-
bound will be
|
| 5300 |
|
| 5301 |
[^9]: The parameters of function template specializations contain no
|
| 5302 |
template parameter types. The set of conversions allowed on deduced
|
| 5303 |
arguments is limited, because the argument deduction process
|
| 5304 |
produces function templates with parameters that either match the
|
|
|
|
| 1 |
# Templates <a id="temp">[[temp]]</a>
|
| 2 |
|
| 3 |
+
A *template* defines a family of classes, functions, or variables, or an
|
| 4 |
+
alias for a family of types.
|
| 5 |
|
| 6 |
``` bnf
|
| 7 |
template-declaration:
|
| 8 |
'template <' template-parameter-list '>' declaration
|
| 9 |
```
|
|
|
|
| 12 |
template-parameter-list:
|
| 13 |
template-parameter
|
| 14 |
template-parameter-list ',' template-parameter
|
| 15 |
```
|
| 16 |
|
| 17 |
+
[*Note 1*: The `>` token following the *template-parameter-list* of a
|
| 18 |
+
*template-declaration* may be the product of replacing a `>{>}` token by
|
| 19 |
+
two consecutive `>` tokens ([[temp.names]]). — *end note*]
|
| 20 |
|
| 21 |
The *declaration* in a *template-declaration* shall
|
| 22 |
|
| 23 |
- declare or define a function, a class, or a variable, or
|
| 24 |
- define a member function, a member class, a member enumeration, or a
|
| 25 |
static data member of a class template or of a class nested within a
|
| 26 |
class template, or
|
| 27 |
- define a member template of a class or class template, or
|
| 28 |
+
- be a *deduction-guide*, or
|
| 29 |
- be an *alias-declaration*.
|
| 30 |
|
| 31 |
A *template-declaration* is a *declaration*. A *template-declaration* is
|
| 32 |
also a definition if its *declaration* defines a function, a class, a
|
| 33 |
variable, or a static data member. A declaration introduced by a
|
| 34 |
template declaration of a variable is a *variable template*. A variable
|
| 35 |
template at class scope is a *static data member template*.
|
| 36 |
|
| 37 |
+
[*Example 1*:
|
| 38 |
+
|
| 39 |
``` cpp
|
| 40 |
template<class T>
|
| 41 |
constexpr T pi = T(3.1415926535897932385L);
|
| 42 |
template<class T>
|
| 43 |
T circular_area(T r) {
|
|
|
|
| 53 |
template<class T>
|
| 54 |
constexpr pauli<T> sigma3 = { { 1, 0 }, { 0, -1 } };
|
| 55 |
};
|
| 56 |
```
|
| 57 |
|
| 58 |
+
— *end example*]
|
| 59 |
+
|
| 60 |
A *template-declaration* can appear only as a namespace scope or class
|
| 61 |
scope declaration. In a function template declaration, the last
|
| 62 |
+
component of the *declarator-id* shall not be a *template-id*.
|
| 63 |
+
|
| 64 |
+
[*Note 2*: That last component may be an *identifier*, an
|
| 65 |
+
*operator-function-id*, a *conversion-function-id*, or a
|
| 66 |
+
*literal-operator-id*. In a class template declaration, if the class
|
| 67 |
+
name is a *simple-template-id*, the declaration declares a class
|
| 68 |
+
template partial specialization ([[temp.class.spec]]). — *end note*]
|
| 69 |
|
| 70 |
In a *template-declaration*, explicit specialization, or explicit
|
| 71 |
instantiation the *init-declarator-list* in the declaration shall
|
| 72 |
contain at most one declarator. When such a declaration is used to
|
| 73 |
declare a class template, no declarator is permitted.
|
| 74 |
|
| 75 |
+
A template name has linkage ([[basic.link]]). Specializations (explicit
|
| 76 |
+
or implicit) of a template that has internal linkage are distinct from
|
| 77 |
+
all specializations in other translation units. A template, a template
|
| 78 |
+
explicit specialization ([[temp.expl.spec]]), and a class template
|
| 79 |
+
partial specialization shall not have C linkage. Use of a linkage
|
| 80 |
+
specification other than `"C"` or `"C++"` with any of these constructs
|
| 81 |
+
is conditionally-supported, with *implementation-defined* semantics.
|
| 82 |
+
Template definitions shall obey the one-definition rule (
|
| 83 |
+
[[basic.def.odr]]).
|
| 84 |
+
|
| 85 |
+
[*Note 3*: Default arguments for function templates and for member
|
| 86 |
+
functions of class templates are considered definitions for the purpose
|
| 87 |
+
of template instantiation ([[temp.decls]]) and must also obey the
|
| 88 |
+
one-definition rule. — *end note*]
|
| 89 |
|
| 90 |
A class template shall not have the same name as any other template,
|
| 91 |
class, function, variable, enumeration, enumerator, namespace, or type
|
| 92 |
+
in the same scope ([[basic.scope]]), except as specified in
|
| 93 |
+
[[temp.class.spec]]. Except that a function template can be overloaded
|
| 94 |
either by non-template functions ([[dcl.fct]]) with the same name or by
|
| 95 |
other function templates with the same name ([[temp.over]]), a template
|
| 96 |
name declared in namespace scope or in class scope shall be unique in
|
| 97 |
that scope.
|
| 98 |
|
| 99 |
+
A *templated entity* is
|
| 100 |
+
|
| 101 |
+
- a template,
|
| 102 |
+
- an entity defined ([[basic.def]]) or created ([[class.temporary]])
|
| 103 |
+
in a templated entity,
|
| 104 |
+
- a member of a templated entity,
|
| 105 |
+
- an enumerator for an enumeration that is a templated entity, or
|
| 106 |
+
- the closure type of a *lambda-expression* (
|
| 107 |
+
[[expr.prim.lambda.closure]]) appearing in the declaration of a
|
| 108 |
+
templated entity.
|
| 109 |
+
|
| 110 |
+
[*Note 4*: A local class, a local variable, or a friend function
|
| 111 |
+
defined in a templated entity is a templated entity. — *end note*]
|
| 112 |
+
|
| 113 |
A function template, member function of a class template, variable
|
| 114 |
template, or static data member of a class template shall be defined in
|
| 115 |
every translation unit in which it is implicitly instantiated (
|
| 116 |
[[temp.inst]]) unless the corresponding specialization is explicitly
|
| 117 |
instantiated ([[temp.explicit]]) in some translation unit; no
|
|
|
|
| 127 |
parameter-declaration
|
| 128 |
```
|
| 129 |
|
| 130 |
``` bnf
|
| 131 |
type-parameter:
|
| 132 |
+
type-parameter-key '...'ₒₚₜ identifierₒₚₜ
|
| 133 |
+
type-parameter-key identifierₒₚₜ '=' type-id
|
| 134 |
+
'template <' template-parameter-list '>' type-parameter-key '...'ₒₚₜ identifierₒₚₜ
|
| 135 |
+
'template <' template-parameter-list '>' type-parameter-key identifierₒₚₜ '=' id-expression
|
|
|
|
|
|
|
| 136 |
```
|
| 137 |
|
| 138 |
+
``` bnf
|
| 139 |
+
type-parameter-key:
|
| 140 |
+
'class'
|
| 141 |
+
'typename'
|
| 142 |
+
```
|
| 143 |
+
|
| 144 |
+
[*Note 1*: The `>` token following the *template-parameter-list* of a
|
| 145 |
+
*type-parameter* may be the product of replacing a `>{>}` token by two
|
| 146 |
+
consecutive `>` tokens ([[temp.names]]). — *end note*]
|
| 147 |
|
| 148 |
There is no semantic difference between `class` and `typename` in a
|
| 149 |
+
*type-parameter-key*. `typename` followed by an *unqualified-id* names a
|
| 150 |
template type parameter. `typename` followed by a *qualified-id* denotes
|
| 151 |
+
the type in a non-type [^1] *parameter-declaration*. A
|
| 152 |
+
*template-parameter* of the form `class` *identifier* is a
|
| 153 |
+
*type-parameter*.
|
|
|
|
| 154 |
|
| 155 |
+
[*Example 1*:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
``` cpp
|
| 158 |
+
class T { ... };
|
| 159 |
int i;
|
| 160 |
|
| 161 |
template<class T, T i> void f(T t) {
|
| 162 |
T t1 = i; // template-parameters T and i
|
| 163 |
::T t2 = ::i; // global namespace members T and i
|
|
|
|
| 165 |
```
|
| 166 |
|
| 167 |
Here, the template `f` has a *type-parameter* called `T`, rather than an
|
| 168 |
unnamed non-type *template-parameter* of class `T`.
|
| 169 |
|
| 170 |
+
— *end example*]
|
| 171 |
+
|
| 172 |
+
A storage class shall not be specified in a *template-parameter*
|
| 173 |
+
declaration. Types shall not be defined in a *template-parameter*
|
| 174 |
+
declaration.
|
| 175 |
+
|
| 176 |
+
A *type-parameter* whose identifier does not follow an ellipsis defines
|
| 177 |
+
its *identifier* to be a *typedef-name* (if declared without `template`)
|
| 178 |
+
or *template-name* (if declared with `template`) in the scope of the
|
| 179 |
+
template declaration.
|
| 180 |
+
|
| 181 |
+
[*Note 2*:
|
| 182 |
+
|
| 183 |
+
A template argument may be a class template or alias template. For
|
| 184 |
+
example,
|
| 185 |
+
|
| 186 |
+
``` cpp
|
| 187 |
+
template<class T> class myarray { ... };
|
| 188 |
+
|
| 189 |
+
template<class K, class V, template<class T> class C = myarray>
|
| 190 |
+
class Map {
|
| 191 |
+
C<K> key;
|
| 192 |
+
C<V> value;
|
| 193 |
+
};
|
| 194 |
+
```
|
| 195 |
+
|
| 196 |
+
— *end note*]
|
| 197 |
+
|
| 198 |
A non-type *template-parameter* shall have one of the following
|
| 199 |
+
(optionally cv-qualified) types:
|
| 200 |
|
| 201 |
- integral or enumeration type,
|
| 202 |
- pointer to object or pointer to function,
|
| 203 |
- lvalue reference to object or lvalue reference to function,
|
| 204 |
- pointer to member,
|
| 205 |
+
- `std::nullptr_t`, or
|
| 206 |
+
- a type that contains a placeholder type ([[dcl.spec.auto]]).
|
| 207 |
|
| 208 |
+
[*Note 3*: Other types are disallowed either explicitly below or
|
| 209 |
+
implicitly by the rules governing the form of *template-argument*s (
|
| 210 |
+
[[temp.arg]]). — *end note*]
|
| 211 |
+
|
| 212 |
+
The top-level *cv-qualifier*s on the *template-parameter* are ignored
|
| 213 |
+
when determining its type.
|
| 214 |
|
| 215 |
A non-type non-reference *template-parameter* is a prvalue. It shall not
|
| 216 |
be assigned to or in any other way have its value changed. A non-type
|
| 217 |
non-reference *template-parameter* cannot have its address taken. When a
|
| 218 |
non-type non-reference *template-parameter* is used as an initializer
|
| 219 |
for a reference, a temporary is always used.
|
| 220 |
|
| 221 |
+
[*Example 2*:
|
| 222 |
+
|
| 223 |
``` cpp
|
| 224 |
template<const X& x, int i> void f() {
|
| 225 |
i++; // error: change of template-parameter value
|
| 226 |
|
| 227 |
&x; // OK
|
|
|
|
| 230 |
int& ri = i; // error: non-const reference bound to temporary
|
| 231 |
const int& cri = i; // OK: const reference bound to temporary
|
| 232 |
}
|
| 233 |
```
|
| 234 |
|
| 235 |
+
— *end example*]
|
| 236 |
+
|
| 237 |
+
A non-type *template-parameter* shall not be declared to have
|
| 238 |
+
floating-point, class, or void type.
|
| 239 |
+
|
| 240 |
+
[*Example 3*:
|
| 241 |
|
| 242 |
``` cpp
|
| 243 |
template<double d> class X; // error
|
| 244 |
template<double* pd> class Y; // OK
|
| 245 |
template<double& rd> class Z; // OK
|
| 246 |
```
|
| 247 |
|
| 248 |
+
— *end example*]
|
| 249 |
+
|
| 250 |
+
A non-type *template-parameter* of type “array of `T`” or of function
|
| 251 |
+
type `T` is adjusted to be of type “pointer to `T`”.
|
| 252 |
+
|
| 253 |
+
[*Example 4*:
|
| 254 |
|
| 255 |
``` cpp
|
| 256 |
+
template<int* a> struct R { ... };
|
| 257 |
+
template<int b[5]> struct S { ... };
|
| 258 |
int p;
|
| 259 |
R<&p> w; // OK
|
| 260 |
S<&p> x; // OK due to parameter adjustment
|
| 261 |
int v[5];
|
| 262 |
R<v> y; // OK due to implicit argument conversion
|
| 263 |
S<v> z; // OK due to both adjustment and conversion
|
| 264 |
```
|
| 265 |
|
| 266 |
+
— *end example*]
|
| 267 |
+
|
| 268 |
A *default template-argument* is a *template-argument* ([[temp.arg]])
|
| 269 |
specified after `=` in a *template-parameter*. A default
|
| 270 |
*template-argument* may be specified for any kind of
|
| 271 |
*template-parameter* (type, non-type, template) that is not a template
|
| 272 |
parameter pack ([[temp.variadic]]). A default *template-argument* may
|
|
|
|
| 277 |
a friend class template declaration. If a friend function template
|
| 278 |
declaration specifies a default *template-argument*, that declaration
|
| 279 |
shall be a definition and shall be the only declaration of the function
|
| 280 |
template in the translation unit.
|
| 281 |
|
| 282 |
+
The set of default *template-argument*s available for use is obtained by
|
| 283 |
+
merging the default arguments from all prior declarations of the
|
| 284 |
+
template in the same way default function arguments are (
|
|
|
|
| 285 |
[[dcl.fct.default]]).
|
| 286 |
|
| 287 |
+
[*Example 5*:
|
| 288 |
+
|
| 289 |
``` cpp
|
| 290 |
template<class T1, class T2 = int> class A;
|
| 291 |
template<class T1 = int, class T2> class A;
|
| 292 |
```
|
| 293 |
|
|
|
|
| 295 |
|
| 296 |
``` cpp
|
| 297 |
template<class T1 = int, class T2 = int> class A;
|
| 298 |
```
|
| 299 |
|
| 300 |
+
— *end example*]
|
| 301 |
+
|
| 302 |
+
If a *template-parameter* of a class template, variable template, or
|
| 303 |
+
alias template has a default *template-argument*, each subsequent
|
| 304 |
+
*template-parameter* shall either have a default *template-argument*
|
| 305 |
+
supplied or be a template parameter pack. If a *template-parameter* of a
|
| 306 |
+
primary class template, primary variable template, or alias template is
|
| 307 |
+
a template parameter pack, it shall be the last *template-parameter*. A
|
| 308 |
+
template parameter pack of a function template shall not be followed by
|
| 309 |
+
another template parameter unless that template parameter can be deduced
|
| 310 |
+
from the parameter-type-list ([[dcl.fct]]) of the function template or
|
| 311 |
+
has a default argument ([[temp.deduct]]). A template parameter of a
|
| 312 |
+
deduction guide template ([[temp.deduct.guide]]) that does not have a
|
| 313 |
+
default argument shall be deducible from the parameter-type-list of the
|
| 314 |
+
deduction guide template.
|
| 315 |
+
|
| 316 |
+
[*Example 6*:
|
| 317 |
|
| 318 |
``` cpp
|
| 319 |
template<class T1 = int, class T2> class B; // error
|
| 320 |
|
| 321 |
// U can be neither deduced from the parameter-type-list nor specified
|
| 322 |
template<class... T, class... U> void f() { } // error
|
| 323 |
template<class... T, class U> void g() { } // error
|
| 324 |
```
|
| 325 |
|
| 326 |
+
— *end example*]
|
| 327 |
+
|
| 328 |
A *template-parameter* shall not be given default arguments by two
|
| 329 |
different declarations in the same scope.
|
| 330 |
|
| 331 |
+
[*Example 7*:
|
| 332 |
+
|
| 333 |
``` cpp
|
| 334 |
template<class T = int> class X;
|
| 335 |
+
template<class T = int> class X { ... }; // error
|
| 336 |
```
|
| 337 |
|
| 338 |
+
— *end example*]
|
| 339 |
+
|
| 340 |
When parsing a default *template-argument* for a non-type
|
| 341 |
*template-parameter*, the first non-nested `>` is taken as the end of
|
| 342 |
the *template-parameter-list* rather than a greater-than operator.
|
| 343 |
|
| 344 |
+
[*Example 8*:
|
| 345 |
+
|
| 346 |
``` cpp
|
| 347 |
template<int i = 3 > 4 > // syntax error
|
| 348 |
+
class X { ... };
|
| 349 |
|
| 350 |
template<int i = (3 > 4) > // OK
|
| 351 |
+
class Y { ... };
|
| 352 |
```
|
| 353 |
|
| 354 |
+
— *end example*]
|
| 355 |
+
|
| 356 |
A *template-parameter* of a template *template-parameter* is permitted
|
| 357 |
to have a default *template-argument*. When such default arguments are
|
| 358 |
specified, they apply to the template *template-parameter* in the scope
|
| 359 |
of the template *template-parameter*.
|
| 360 |
|
| 361 |
+
[*Example 9*:
|
| 362 |
+
|
| 363 |
``` cpp
|
| 364 |
template <class T = float> struct B {};
|
| 365 |
template <template <class TT = float> class T> struct A {
|
| 366 |
inline void f();
|
| 367 |
inline void g();
|
|
|
|
| 372 |
template <template <class TT = char> class T> void A<T>::g() {
|
| 373 |
T<> t; // OK - T<char>
|
| 374 |
}
|
| 375 |
```
|
| 376 |
|
| 377 |
+
— *end example*]
|
| 378 |
+
|
| 379 |
If a *template-parameter* is a *type-parameter* with an ellipsis prior
|
| 380 |
to its optional *identifier* or is a *parameter-declaration* that
|
| 381 |
declares a parameter pack ([[dcl.fct]]), then the *template-parameter*
|
| 382 |
is a template parameter pack ([[temp.variadic]]). A template parameter
|
| 383 |
pack that is a *parameter-declaration* whose type contains one or more
|
|
|
|
| 386 |
*template-parameter-list* containing one or more unexpanded parameter
|
| 387 |
packs is a pack expansion. A template parameter pack that is a pack
|
| 388 |
expansion shall not expand a parameter pack declared in the same
|
| 389 |
*template-parameter-list*.
|
| 390 |
|
| 391 |
+
[*Example 10*:
|
| 392 |
+
|
| 393 |
``` cpp
|
| 394 |
template <class... Types> class Tuple; // Types is a template type parameter pack
|
| 395 |
// but not a pack expansion
|
| 396 |
template <class T, int... Dims> struct multi_array; // Dims is a non-type template parameter pack
|
| 397 |
// but not a pack expansion
|
|
|
|
| 401 |
};
|
| 402 |
template<class... T, T... Values> struct static_array;// error: Values expands template type parameter
|
| 403 |
// pack T within the same template parameter list
|
| 404 |
```
|
| 405 |
|
| 406 |
+
— *end example*]
|
| 407 |
+
|
| 408 |
## Names of template specializations <a id="temp.names">[[temp.names]]</a>
|
| 409 |
|
| 410 |
A template specialization ([[temp.spec]]) can be referred to by a
|
| 411 |
*template-id*:
|
| 412 |
|
|
|
|
| 438 |
constant-expression
|
| 439 |
type-id
|
| 440 |
id-expression
|
| 441 |
```
|
| 442 |
|
| 443 |
+
[*Note 1*: The name lookup rules ([[basic.lookup]]) are used to
|
| 444 |
+
associate the use of a name with a template declaration; that is, to
|
| 445 |
+
identify a name as a *template-name*. — *end note*]
|
| 446 |
|
| 447 |
For a *template-name* to be explicitly qualified by the template
|
| 448 |
arguments, the name must be known to refer to a template.
|
| 449 |
|
| 450 |
After name lookup ([[basic.lookup]]) finds that a name is a
|
|
|
|
| 454 |
is always taken as the delimiter of a *template-argument-list* and never
|
| 455 |
as the less-than operator. When parsing a *template-argument-list*, the
|
| 456 |
first non-nested `>`[^2] is taken as the ending delimiter rather than a
|
| 457 |
greater-than operator. Similarly, the first non-nested `>{>}` is treated
|
| 458 |
as two consecutive but distinct `>` tokens, the first of which is taken
|
| 459 |
+
as the end of the *template-argument-list* and completes the
|
| 460 |
+
*template-id*.
|
| 461 |
+
|
| 462 |
+
[*Note 2*: The second `>` token produced by this replacement rule may
|
| 463 |
+
terminate an enclosing *template-id* construct or it may be part of a
|
| 464 |
+
different construct (e.g. a cast). — *end note*]
|
| 465 |
+
|
| 466 |
+
[*Example 1*:
|
| 467 |
|
| 468 |
``` cpp
|
| 469 |
+
template<int i> class X { ... };
|
| 470 |
|
| 471 |
X< 1>2 > x1; // syntax error
|
| 472 |
X<(1>2)> x2; // OK
|
| 473 |
|
| 474 |
+
template<class T> class Y { ... };
|
| 475 |
Y<X<1>> x3; // OK, same as Y<X<1> > x3;
|
| 476 |
Y<X<6>>1>> x4; // syntax error
|
| 477 |
Y<X<(6>>1)>> x5; // OK
|
| 478 |
```
|
| 479 |
|
| 480 |
+
— *end example*]
|
| 481 |
+
|
| 482 |
+
The keyword `template` is said to appear at the top level in a
|
| 483 |
+
*qualified-id* if it appears outside of a *template-argument-list* or
|
| 484 |
+
*decltype-specifier*. In a *qualified-id* of a *declarator-id* or in a
|
| 485 |
+
*qualified-id* formed by a *class-head-name* (Clause [[class]]) or
|
| 486 |
+
*enum-head-name* ([[dcl.enum]]), the keyword `template` shall not
|
| 487 |
+
appear at the top level. In a *qualified-id* used as the name in a
|
| 488 |
+
*typename-specifier* ([[temp.res]]), *elaborated-type-specifier* (
|
| 489 |
+
[[dcl.type.elab]]), *using-declaration* ([[namespace.udecl]]), or
|
| 490 |
+
*class-or-decltype* (Clause [[class.derived]]), an optional keyword
|
| 491 |
+
`template` appearing at the top level is ignored. In these contexts, a
|
| 492 |
+
`<` token is always assumed to introduce a *template-argument-list*. In
|
| 493 |
+
all other contexts, when naming a template specialization of a member of
|
| 494 |
+
an unknown specialization ([[temp.dep.type]]), the member template name
|
| 495 |
+
shall be prefixed by the keyword `template`.
|
| 496 |
+
|
| 497 |
+
[*Example 2*:
|
| 498 |
|
| 499 |
``` cpp
|
| 500 |
struct X {
|
| 501 |
template<std::size_t> X* alloc();
|
| 502 |
template<std::size_t> static X* adjust();
|
|
|
|
| 507 |
T::adjust<100>(); // ill-formed: < means less than
|
| 508 |
T::template adjust<100>(); // OK: < starts template argument list
|
| 509 |
}
|
| 510 |
```
|
| 511 |
|
| 512 |
+
— *end example*]
|
| 513 |
+
|
| 514 |
A name prefixed by the keyword `template` shall be a *template-id* or
|
| 515 |
+
the name shall refer to a class template or an alias template.
|
| 516 |
+
|
| 517 |
+
[*Note 3*: The keyword `template` may not be applied to non-template
|
| 518 |
+
members of class templates. — *end note*]
|
| 519 |
+
|
| 520 |
+
[*Note 4*: As is the case with the `typename` prefix, the `template`
|
| 521 |
+
prefix is allowed in cases where it is not strictly necessary; i.e.,
|
| 522 |
+
when the *nested-name-specifier* or the expression on the left of the
|
| 523 |
+
`->` or `.` is not dependent on a *template-parameter*, or the use does
|
| 524 |
+
not appear in the scope of a template. — *end note*]
|
| 525 |
+
|
| 526 |
+
[*Example 3*:
|
| 527 |
|
| 528 |
``` cpp
|
| 529 |
template <class T> struct A {
|
| 530 |
void f(int);
|
| 531 |
template <class U> void f(U);
|
|
|
|
| 544 |
// OK: T::template C names a class template:
|
| 545 |
template <class T, template <class X> class TT = T::template C> struct D { };
|
| 546 |
D<B<int> > db;
|
| 547 |
```
|
| 548 |
|
| 549 |
+
— *end example*]
|
| 550 |
+
|
| 551 |
A *simple-template-id* that names a class template specialization is a
|
| 552 |
*class-name* (Clause [[class]]).
|
| 553 |
|
| 554 |
A *template-id* that names an alias template specialization is a
|
| 555 |
*type-name*.
|
|
|
|
| 563 |
declared by the template in its *template-parameter-list*. When the
|
| 564 |
parameter declared by the template is a template parameter pack (
|
| 565 |
[[temp.variadic]]), it will correspond to zero or more
|
| 566 |
*template-argument*s.
|
| 567 |
|
| 568 |
+
[*Example 1*:
|
| 569 |
+
|
| 570 |
``` cpp
|
| 571 |
template<class T> class Array {
|
| 572 |
T* v;
|
| 573 |
int sz;
|
| 574 |
public:
|
|
|
|
| 576 |
T& operator[](int);
|
| 577 |
T& elem(int i) { return v[i]; }
|
| 578 |
};
|
| 579 |
|
| 580 |
Array<int> v1(20);
|
| 581 |
+
typedef std::complex<double> dcomplex; // std::complex is a standard library template
|
|
|
|
| 582 |
Array<dcomplex> v2(30);
|
| 583 |
Array<dcomplex> v3(40);
|
| 584 |
|
| 585 |
void bar() {
|
| 586 |
v1[3] = 7;
|
| 587 |
v2[3] = v3.elem(4) = dcomplex(7,8);
|
| 588 |
}
|
| 589 |
```
|
| 590 |
|
| 591 |
+
— *end example*]
|
| 592 |
+
|
| 593 |
In a *template-argument*, an ambiguity between a *type-id* and an
|
| 594 |
expression is resolved to a *type-id*, regardless of the form of the
|
| 595 |
corresponding *template-parameter*.[^3]
|
| 596 |
|
| 597 |
+
[*Example 2*:
|
| 598 |
+
|
| 599 |
``` cpp
|
| 600 |
template<class T> void f();
|
| 601 |
template<int I> void f();
|
| 602 |
|
| 603 |
void g() {
|
| 604 |
f<int()>(); // int() is a type-id: call the first f()
|
| 605 |
}
|
| 606 |
```
|
| 607 |
|
| 608 |
+
— *end example*]
|
| 609 |
+
|
| 610 |
The name of a *template-argument* shall be accessible at the point where
|
| 611 |
+
it is used as a *template-argument*.
|
| 612 |
+
|
| 613 |
+
[*Note 1*: If the name of the *template-argument* is accessible at the
|
| 614 |
+
point where it is used as a *template-argument*, there is no further
|
| 615 |
+
access restriction in the resulting instantiation where the
|
| 616 |
+
corresponding *template-parameter* name is used. — *end note*]
|
| 617 |
+
|
| 618 |
+
[*Example 3*:
|
| 619 |
|
| 620 |
``` cpp
|
| 621 |
template<class T> class X {
|
| 622 |
static T t;
|
| 623 |
};
|
| 624 |
|
| 625 |
class Y {
|
| 626 |
private:
|
| 627 |
+
struct S { ... };
|
| 628 |
X<S> x; // OK: S is accessible
|
| 629 |
// X<Y::S> has a static member of type Y::S
|
| 630 |
// OK: even though Y::S is private
|
| 631 |
};
|
| 632 |
|
| 633 |
X<Y::S> y; // error: S not accessible
|
| 634 |
```
|
| 635 |
|
| 636 |
+
— *end example*]
|
| 637 |
+
|
| 638 |
For a *template-argument* that is a class type or a class template, the
|
| 639 |
template definition has no special access rights to the members of the
|
| 640 |
*template-argument*.
|
| 641 |
|
| 642 |
+
[*Example 4*:
|
| 643 |
+
|
| 644 |
``` cpp
|
| 645 |
template <template <class TT> class T> class A {
|
| 646 |
typename T<int>::S s;
|
| 647 |
};
|
| 648 |
|
| 649 |
template <class U> class B {
|
| 650 |
private:
|
| 651 |
+
struct S { ... };
|
| 652 |
};
|
| 653 |
|
| 654 |
A<B> b; // ill-formed: A has no access to B::S
|
| 655 |
```
|
| 656 |
|
| 657 |
+
— *end example*]
|
| 658 |
+
|
| 659 |
When template argument packs or default *template-argument*s are used, a
|
| 660 |
*template-argument* list can be empty. In that case the empty `<>`
|
| 661 |
+
brackets shall still be used as the *template-argument-list*.
|
| 662 |
+
|
| 663 |
+
[*Example 5*:
|
| 664 |
|
| 665 |
``` cpp
|
| 666 |
template<class T = char> class String;
|
| 667 |
String<>* p; // OK: String<char>
|
| 668 |
String* q; // syntax error
|
| 669 |
template<class ... Elements> class Tuple;
|
| 670 |
Tuple<>* t; // OK: Elements is empty
|
| 671 |
Tuple* u; // syntax error
|
| 672 |
```
|
| 673 |
|
| 674 |
+
— *end example*]
|
| 675 |
+
|
| 676 |
An explicit destructor call ([[class.dtor]]) for an object that has a
|
| 677 |
type that is a class template specialization may explicitly specify the
|
| 678 |
*template-argument*s.
|
| 679 |
|
| 680 |
+
[*Example 6*:
|
| 681 |
+
|
| 682 |
``` cpp
|
| 683 |
template<class T> struct A {
|
| 684 |
~A();
|
| 685 |
};
|
| 686 |
void f(A<int>* p, A<int>* q) {
|
| 687 |
p->A<int>::~A(); // OK: destructor call
|
| 688 |
q->A<int>::~A<int>(); // OK: destructor call
|
| 689 |
}
|
| 690 |
```
|
| 691 |
|
| 692 |
+
— *end example*]
|
| 693 |
+
|
| 694 |
If the use of a *template-argument* gives rise to an ill-formed
|
| 695 |
construct in the instantiation of a template specialization, the program
|
| 696 |
is ill-formed.
|
| 697 |
|
| 698 |
When the template in a *template-id* is an overloaded function template,
|
| 699 |
both non-template functions in the overload set and function templates
|
| 700 |
in the overload set for which the *template-argument*s do not match the
|
| 701 |
*template-parameter*s are ignored. If none of the function templates
|
| 702 |
have matching *template-parameter*s, the program is ill-formed.
|
| 703 |
|
| 704 |
+
When a *simple-template-id* does not name a function, a default
|
| 705 |
+
*template-argument* is implicitly instantiated ([[temp.inst]]) when the
|
| 706 |
+
value of that default argument is needed.
|
| 707 |
+
|
| 708 |
+
[*Example 7*:
|
| 709 |
+
|
| 710 |
+
``` cpp
|
| 711 |
+
template<typename T, typename U = int> struct S { };
|
| 712 |
+
S<bool>* p; // the type of p is S<bool, int>*
|
| 713 |
+
```
|
| 714 |
+
|
| 715 |
+
The default argument for `U` is instantiated to form the type
|
| 716 |
+
`S<bool, int>*`.
|
| 717 |
+
|
| 718 |
+
— *end example*]
|
| 719 |
+
|
| 720 |
A *template-argument* followed by an ellipsis is a pack expansion (
|
| 721 |
[[temp.variadic]]).
|
| 722 |
|
| 723 |
### Template type arguments <a id="temp.arg.type">[[temp.arg.type]]</a>
|
| 724 |
|
| 725 |
A *template-argument* for a *template-parameter* which is a type shall
|
| 726 |
be a *type-id*.
|
| 727 |
|
| 728 |
+
[*Example 1*:
|
| 729 |
+
|
| 730 |
``` cpp
|
| 731 |
template <class T> class X { };
|
| 732 |
template <class T> void f(T t) { }
|
| 733 |
struct { } unnamed_obj;
|
| 734 |
|
|
|
|
| 744 |
f(unnamed_obj); // OK
|
| 745 |
f(b); // OK
|
| 746 |
}
|
| 747 |
```
|
| 748 |
|
| 749 |
+
— *end example*]
|
| 750 |
|
| 751 |
+
[*Note 1*: A template type argument may be an incomplete type (
|
| 752 |
+
[[basic.types]]). — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 753 |
|
| 754 |
### Template non-type arguments <a id="temp.arg.nontype">[[temp.arg.nontype]]</a>
|
| 755 |
|
| 756 |
+
If the type of a *template-parameter* contains a placeholder type (
|
| 757 |
+
[[dcl.spec.auto]], [[temp.param]]), the deduced parameter type is
|
| 758 |
+
determined from the type of the *template-argument* by placeholder type
|
| 759 |
+
deduction ([[dcl.type.auto.deduct]]). If a deduced parameter type is
|
| 760 |
+
not permitted for a *template-parameter* declaration ([[temp.param]]),
|
| 761 |
+
the program is ill-formed.
|
| 762 |
|
| 763 |
+
A *template-argument* for a non-type *template-parameter* shall be a
|
| 764 |
converted constant expression ([[expr.const]]) of the type of the
|
| 765 |
+
*template-parameter*. For a non-type *template-parameter* of reference
|
| 766 |
+
or pointer type, the value of the constant expression shall not refer to
|
| 767 |
+
(or for a pointer type, shall not be the address of):
|
| 768 |
+
|
| 769 |
+
- a subobject ([[intro.object]]),
|
| 770 |
+
- a temporary object ([[class.temporary]]),
|
| 771 |
+
- a string literal ([[lex.string]]),
|
| 772 |
+
- the result of a `typeid` expression ([[expr.typeid]]), or
|
| 773 |
+
- a predefined `__func__` variable ([[dcl.fct.def.general]]).
|
| 774 |
+
|
| 775 |
+
[*Note 1*: If the *template-argument* represents a set of overloaded
|
| 776 |
+
functions (or a pointer or member pointer to such), the matching
|
| 777 |
+
function is selected from the set ([[over.over]]). — *end note*]
|
| 778 |
+
|
| 779 |
+
[*Example 1*:
|
| 780 |
+
|
| 781 |
+
``` cpp
|
| 782 |
+
template<const int* pci> struct X { ... };
|
| 783 |
+
int ai[10];
|
| 784 |
+
X<ai> xi; // array to pointer and qualification conversions
|
| 785 |
+
|
| 786 |
+
struct Y { ... };
|
| 787 |
+
template<const Y& b> struct Z { ... };
|
| 788 |
+
Y y;
|
| 789 |
+
Z<y> z; // no conversion, but note extra cv-qualification
|
| 790 |
+
|
| 791 |
+
template<int (&pa)[5]> struct W { ... };
|
| 792 |
+
int b[5];
|
| 793 |
+
W<b> w; // no conversion
|
| 794 |
+
|
| 795 |
+
void f(char);
|
| 796 |
+
void f(int);
|
| 797 |
+
|
| 798 |
+
template<void (*pf)(int)> struct A { ... };
|
| 799 |
+
|
| 800 |
+
A<&f> a; // selects f(int)
|
| 801 |
+
|
| 802 |
+
template<auto n> struct B { ... };
|
| 803 |
+
B<5> b1; // OK: template parameter type is int
|
| 804 |
+
B<'a'> b2; // OK: template parameter type is char
|
| 805 |
+
B<2.5> b3; // error: template parameter type cannot be double
|
| 806 |
+
```
|
| 807 |
+
|
| 808 |
+
— *end example*]
|
| 809 |
+
|
| 810 |
+
[*Note 2*:
|
| 811 |
+
|
| 812 |
+
A string literal ([[lex.string]]) is not an acceptable
|
| 813 |
*template-argument*.
|
| 814 |
|
| 815 |
+
[*Example 2*:
|
| 816 |
+
|
| 817 |
``` cpp
|
| 818 |
template<class T, const char* p> class X {
|
| 819 |
+
...
|
| 820 |
};
|
| 821 |
|
| 822 |
X<int, "Studebaker"> x1; // error: string literal as template-argument
|
| 823 |
|
| 824 |
const char p[] = "Vivisectionist";
|
| 825 |
X<int,p> x2; // OK
|
| 826 |
```
|
| 827 |
|
| 828 |
+
— *end example*]
|
| 829 |
+
|
| 830 |
+
— *end note*]
|
| 831 |
+
|
| 832 |
+
[*Note 3*:
|
| 833 |
+
|
| 834 |
+
The address of an array element or non-static data member is not an
|
| 835 |
+
acceptable *template-argument*.
|
| 836 |
+
|
| 837 |
+
[*Example 3*:
|
| 838 |
|
| 839 |
``` cpp
|
| 840 |
template<int* p> class X { };
|
| 841 |
|
| 842 |
int a[10];
|
| 843 |
struct S { int m; static int s; } s;
|
| 844 |
|
| 845 |
X<&a[2]> x3; // error: address of array element
|
| 846 |
X<&s.m> x4; // error: address of non-static member
|
| 847 |
+
X<&s.s> x5; // OK: address of static member
|
| 848 |
X<&S::s> x6; // OK: address of static member
|
| 849 |
```
|
| 850 |
|
| 851 |
+
— *end example*]
|
| 852 |
+
|
| 853 |
+
— *end note*]
|
| 854 |
+
|
| 855 |
+
[*Note 4*:
|
| 856 |
+
|
| 857 |
+
A temporary object is not an acceptable *template-argument* when the
|
| 858 |
+
corresponding *template-parameter* has reference type.
|
| 859 |
+
|
| 860 |
+
[*Example 4*:
|
| 861 |
|
| 862 |
``` cpp
|
| 863 |
+
template<const int& CRI> struct B { ... };
|
| 864 |
|
| 865 |
B<1> b2; // error: temporary would be required for template argument
|
| 866 |
|
| 867 |
int c = 1;
|
| 868 |
B<c> b1; // OK
|
| 869 |
```
|
| 870 |
|
| 871 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
| 872 |
|
| 873 |
+
— *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 874 |
|
| 875 |
### Template template arguments <a id="temp.arg.template">[[temp.arg.template]]</a>
|
| 876 |
|
| 877 |
A *template-argument* for a template *template-parameter* shall be the
|
| 878 |
name of a class template or an alias template, expressed as
|
|
|
|
| 885 |
Any partial specializations ([[temp.class.spec]]) associated with the
|
| 886 |
primary class template or primary variable template are considered when
|
| 887 |
a specialization based on the template *template-parameter* is
|
| 888 |
instantiated. If a specialization is not visible at the point of
|
| 889 |
instantiation, and it would have been selected had it been visible, the
|
| 890 |
+
program is ill-formed, no diagnostic required.
|
| 891 |
+
|
| 892 |
+
[*Example 1*:
|
| 893 |
|
| 894 |
``` cpp
|
| 895 |
template<class T> class A { // primary template
|
| 896 |
int x;
|
| 897 |
};
|
|
|
|
| 900 |
};
|
| 901 |
template<template<class U> class V> class C {
|
| 902 |
V<int> y;
|
| 903 |
V<int*> z;
|
| 904 |
};
|
| 905 |
+
C<A> c; // V<int> within C<A> uses the primary template, so c.y.x has type int
|
| 906 |
+
// V<int*> within C<A> uses the partial specialization, so c.z.x has type long
|
|
|
|
|
|
|
| 907 |
```
|
| 908 |
|
| 909 |
+
— *end example*]
|
| 910 |
+
|
| 911 |
+
A *template-argument* matches a template *template-parameter* `P` when
|
| 912 |
+
`P` is at least as specialized as the *template-argument* `A`. If `P`
|
| 913 |
+
contains a parameter pack, then `A` also matches `P` if each of `A`’s
|
| 914 |
+
template parameters matches the corresponding template parameter in the
|
| 915 |
+
*template-parameter-list* of `P`. Two template parameters match if they
|
| 916 |
+
are of the same kind (type, non-type, template), for non-type
|
| 917 |
+
*template-parameter*s, their types are equivalent ([[temp.over.link]]),
|
| 918 |
+
and for template *template-parameter*s, each of their corresponding
|
| 919 |
+
*template-parameter*s matches, recursively. When `P`’s
|
| 920 |
+
*template-parameter-list* contains a template parameter pack (
|
| 921 |
+
[[temp.variadic]]), the template parameter pack will match zero or more
|
| 922 |
+
template parameters or template parameter packs in the
|
| 923 |
+
*template-parameter-list* of `A` with the same type and form as the
|
| 924 |
+
template parameter pack in `P` (ignoring whether those template
|
| 925 |
+
parameters are template parameter packs).
|
| 926 |
+
|
| 927 |
+
[*Example 2*:
|
| 928 |
|
| 929 |
``` cpp
|
| 930 |
+
template<class T> class A { ... };
|
| 931 |
+
template<class T, class U = T> class B { ... };
|
| 932 |
+
template<class ... Types> class C { ... };
|
| 933 |
+
template<auto n> class D { ... };
|
| 934 |
+
template<template<class> class P> class X { ... };
|
| 935 |
+
template<template<class ...> class Q> class Y { ... };
|
| 936 |
+
template<template<int> class R> class Z { ... };
|
| 937 |
|
| 938 |
X<A> xa; // OK
|
| 939 |
+
X<B> xb; // OK
|
| 940 |
+
X<C> xc; // OK
|
|
|
|
| 941 |
Y<A> ya; // OK
|
| 942 |
Y<B> yb; // OK
|
| 943 |
Y<C> yc; // OK
|
| 944 |
+
Z<D> zd; // OK
|
| 945 |
```
|
| 946 |
|
| 947 |
+
— *end example*]
|
| 948 |
+
|
| 949 |
+
[*Example 3*:
|
| 950 |
+
|
| 951 |
``` cpp
|
| 952 |
template <class T> struct eval;
|
| 953 |
|
| 954 |
template <template <class, class...> class TT, class T1, class... Rest>
|
| 955 |
struct eval<TT<T1, Rest...>> { };
|
|
|
|
| 965 |
eval<C<17>> eC; // error: C does not match TT in partial specialization
|
| 966 |
eval<D<int, 17>> eD; // error: D does not match TT in partial specialization
|
| 967 |
eval<E<int, float>> eE; // error: E does not match TT in partial specialization
|
| 968 |
```
|
| 969 |
|
| 970 |
+
— *end example*]
|
| 971 |
+
|
| 972 |
+
A template *template-parameter* `P` is at least as specialized as a
|
| 973 |
+
template *template-argument* `A` if, given the following rewrite to two
|
| 974 |
+
function templates, the function template corresponding to `P` is at
|
| 975 |
+
least as specialized as the function template corresponding to `A`
|
| 976 |
+
according to the partial ordering rules for function templates (
|
| 977 |
+
[[temp.func.order]]). Given an invented class template `X` with the
|
| 978 |
+
template parameter list of `A` (including default arguments):
|
| 979 |
+
|
| 980 |
+
- Each of the two function templates has the same template parameters,
|
| 981 |
+
respectively, as `P` or `A`.
|
| 982 |
+
- Each function template has a single function parameter whose type is a
|
| 983 |
+
specialization of `X` with template arguments corresponding to the
|
| 984 |
+
template parameters from the respective function template where, for
|
| 985 |
+
each template parameter `PP` in the template parameter list of the
|
| 986 |
+
function template, a corresponding template argument `AA` is formed.
|
| 987 |
+
If `PP` declares a parameter pack, then `AA` is the pack expansion
|
| 988 |
+
`PP...` ([[temp.variadic]]); otherwise, `AA` is the *id-expression*
|
| 989 |
+
`PP`.
|
| 990 |
+
|
| 991 |
+
If the rewrite produces an invalid type, then `P` is not at least as
|
| 992 |
+
specialized as `A`.
|
| 993 |
+
|
| 994 |
## Type equivalence <a id="temp.type">[[temp.type]]</a>
|
| 995 |
|
| 996 |
Two *template-id*s refer to the same class, function, or variable if
|
| 997 |
|
| 998 |
- their *template-name*s, *operator-function-id*s, or
|
| 999 |
*literal-operator-id*s refer to the same template and
|
| 1000 |
- their corresponding type *template-argument*s are the same type and
|
| 1001 |
- their corresponding non-type template arguments of integral or
|
| 1002 |
enumeration type have identical values and
|
| 1003 |
- their corresponding non-type *template-argument*s of pointer type
|
| 1004 |
+
refer to the same object or function or are both the null pointer
|
| 1005 |
+
value and
|
| 1006 |
- their corresponding non-type *template-argument*s of pointer-to-member
|
| 1007 |
type refer to the same class member or are both the null member
|
| 1008 |
pointer value and
|
| 1009 |
- their corresponding non-type *template-argument*s of reference type
|
| 1010 |
+
refer to the same object or function and
|
| 1011 |
- their corresponding template *template-argument*s refer to the same
|
| 1012 |
template.
|
| 1013 |
|
| 1014 |
+
[*Example 1*:
|
| 1015 |
+
|
| 1016 |
``` cpp
|
| 1017 |
+
template<class E, int size> class buffer { ... };
|
| 1018 |
buffer<char,2*512> x;
|
| 1019 |
buffer<char,1024> y;
|
| 1020 |
```
|
| 1021 |
|
| 1022 |
declares `x` and `y` to be of the same type, and
|
| 1023 |
|
| 1024 |
``` cpp
|
| 1025 |
+
template<class T, void(*err_fct)()> class list { ... };
|
| 1026 |
list<int,&error_handler1> x1;
|
| 1027 |
list<int,&error_handler2> x2;
|
| 1028 |
list<int,&error_handler2> x3;
|
| 1029 |
list<char,&error_handler2> x4;
|
| 1030 |
```
|
|
|
|
| 1040 |
X<Z<int> > z;
|
| 1041 |
```
|
| 1042 |
|
| 1043 |
declares `y` and `z` to be of the same type.
|
| 1044 |
|
| 1045 |
+
— *end example*]
|
| 1046 |
+
|
| 1047 |
+
If an expression e is type-dependent ([[temp.dep.expr]]), `decltype(e)`
|
| 1048 |
+
denotes a unique dependent type. Two such *decltype-specifier*s refer to
|
| 1049 |
+
the same type only if their *expression*s are equivalent (
|
| 1050 |
+
[[temp.over.link]]).
|
| 1051 |
+
|
| 1052 |
+
[*Note 1*: However, such a type may be aliased, e.g., by a
|
| 1053 |
+
*typedef-name*. — *end note*]
|
| 1054 |
|
| 1055 |
## Template declarations <a id="temp.decls">[[temp.decls]]</a>
|
| 1056 |
|
| 1057 |
A *template-id*, that is, the *template-name* followed by a
|
| 1058 |
*template-argument-list* shall not be specified in the declaration of a
|
| 1059 |
primary template declaration.
|
| 1060 |
|
| 1061 |
+
[*Example 1*:
|
| 1062 |
+
|
| 1063 |
``` cpp
|
| 1064 |
template<class T1, class T2, int I> class A<T1, T2, I> { }; // error
|
| 1065 |
template<class T1, int I> void sort<T1, I>(T1 data[I]); // error
|
| 1066 |
```
|
| 1067 |
|
| 1068 |
+
— *end example*]
|
| 1069 |
+
|
| 1070 |
+
[*Note 1*: However, this syntax is allowed in class template partial
|
| 1071 |
+
specializations ([[temp.class.spec]]). — *end note*]
|
| 1072 |
|
| 1073 |
For purposes of name lookup and instantiation, default arguments and
|
| 1074 |
+
*noexcept-specifier*s of function templates and default arguments and
|
| 1075 |
+
*noexcept-specifier*s of member functions of class templates are
|
| 1076 |
+
considered definitions; each default argument or *noexcept-specifier* is
|
| 1077 |
+
a separate definition which is unrelated to the function template
|
| 1078 |
+
definition or to any other default arguments or *noexcept-specifier*s.
|
| 1079 |
+
For the purpose of instantiation, the substatements of a constexpr if
|
| 1080 |
+
statement ([[stmt.if]]) are considered definitions.
|
| 1081 |
|
| 1082 |
Because an *alias-declaration* cannot declare a *template-id*, it is not
|
| 1083 |
possible to partially or explicitly specialize an alias template.
|
| 1084 |
|
| 1085 |
### Class templates <a id="temp.class">[[temp.class]]</a>
|
| 1086 |
|
| 1087 |
+
A *class template* defines the layout and operations for an unbounded
|
| 1088 |
+
set of related types.
|
|
|
|
|
|
|
| 1089 |
|
| 1090 |
+
[*Example 1*:
|
| 1091 |
+
|
| 1092 |
+
A single class template `List` might provide an unbounded set of class
|
| 1093 |
+
definitions: one class `List<T>` for every type `T`, each describing a
|
| 1094 |
+
linked list of elements of type `T`. Similarly, a class template `Array`
|
| 1095 |
+
describing a contiguous, dynamic array might be defined like this:
|
| 1096 |
|
| 1097 |
``` cpp
|
| 1098 |
template<class T> class Array {
|
| 1099 |
T* v;
|
| 1100 |
int sz;
|
|
|
|
| 1103 |
T& operator[](int);
|
| 1104 |
T& elem(int i) { return v[i]; }
|
| 1105 |
};
|
| 1106 |
```
|
| 1107 |
|
| 1108 |
+
The prefix `template<class T>` specifies that a template is being
|
| 1109 |
+
declared and that a *type-name* `T` may be used in the declaration. In
|
| 1110 |
other words, `Array` is a parameterized type with `T` as its parameter.
|
| 1111 |
|
| 1112 |
+
— *end example*]
|
| 1113 |
+
|
| 1114 |
When a member function, a member class, a member enumeration, a static
|
| 1115 |
data member or a member template of a class template is defined outside
|
| 1116 |
of the class template definition, the member definition is defined as a
|
| 1117 |
template definition in which the *template-parameter*s are those of the
|
| 1118 |
class template. The names of the template parameters used in the
|
|
|
|
| 1121 |
following the class template name in the member definition shall name
|
| 1122 |
the parameters in the same order as the one used in the template
|
| 1123 |
parameter list of the member. Each template parameter pack shall be
|
| 1124 |
expanded with an ellipsis in the template argument list.
|
| 1125 |
|
| 1126 |
+
[*Example 2*:
|
| 1127 |
+
|
| 1128 |
``` cpp
|
| 1129 |
template<class T1, class T2> struct A {
|
| 1130 |
void f1();
|
| 1131 |
void f2();
|
| 1132 |
};
|
|
|
|
| 1143 |
|
| 1144 |
template<class ... Types> void B<Types ...>::f3() { } // OK
|
| 1145 |
template<class ... Types> void B<Types>::f4() { } // error
|
| 1146 |
```
|
| 1147 |
|
| 1148 |
+
— *end example*]
|
| 1149 |
+
|
| 1150 |
In a redeclaration, partial specialization, explicit specialization or
|
| 1151 |
explicit instantiation of a class template, the *class-key* shall agree
|
| 1152 |
in kind with the original class template declaration (
|
| 1153 |
[[dcl.type.elab]]).
|
| 1154 |
|
| 1155 |
#### Member functions of class templates <a id="temp.mem.func">[[temp.mem.func]]</a>
|
| 1156 |
|
| 1157 |
A member function of a class template may be defined outside of the
|
| 1158 |
class template definition in which it is declared.
|
| 1159 |
|
| 1160 |
+
[*Example 1*:
|
| 1161 |
+
|
| 1162 |
``` cpp
|
| 1163 |
template<class T> class Array {
|
| 1164 |
T* v;
|
| 1165 |
int sz;
|
| 1166 |
public:
|
|
|
|
| 1178 |
if (i<0 || sz<=i) error("Array: range error");
|
| 1179 |
return v[i];
|
| 1180 |
}
|
| 1181 |
```
|
| 1182 |
|
| 1183 |
+
— *end example*]
|
| 1184 |
+
|
| 1185 |
The *template-argument*s for a member function of a class template are
|
| 1186 |
determined by the *template-argument*s of the type of the object for
|
| 1187 |
+
which the member function is called.
|
| 1188 |
+
|
| 1189 |
+
[*Example 2*:
|
| 1190 |
+
|
| 1191 |
+
The *template-argument* for `Array<T>::operator[]()` will be determined
|
| 1192 |
+
by the `Array` to which the subscripting operation is applied.
|
| 1193 |
|
| 1194 |
``` cpp
|
| 1195 |
Array<int> v1(20);
|
| 1196 |
Array<dcomplex> v2(30);
|
| 1197 |
|
| 1198 |
v1[3] = 7; // Array<int>::operator[]()
|
| 1199 |
v2[3] = dcomplex(7,8); // Array<dcomplex>::operator[]()
|
| 1200 |
```
|
| 1201 |
|
| 1202 |
+
— *end example*]
|
| 1203 |
+
|
| 1204 |
#### Member classes of class templates <a id="temp.mem.class">[[temp.mem.class]]</a>
|
| 1205 |
|
| 1206 |
A member class of a class template may be defined outside the class
|
| 1207 |
+
template definition in which it is declared.
|
| 1208 |
+
|
| 1209 |
+
[*Note 1*:
|
| 1210 |
+
|
| 1211 |
+
The member class must be defined before its first use that requires an
|
| 1212 |
+
instantiation ([[temp.inst]]). For example,
|
| 1213 |
|
| 1214 |
``` cpp
|
| 1215 |
template<class T> struct A {
|
| 1216 |
class B;
|
| 1217 |
};
|
| 1218 |
A<int>::B* b1; // OK: requires A to be defined but not A::B
|
| 1219 |
template<class T> class A<T>::B { };
|
| 1220 |
A<int>::B b2; // OK: requires A::B to be defined
|
| 1221 |
```
|
| 1222 |
|
| 1223 |
+
— *end note*]
|
| 1224 |
+
|
| 1225 |
#### Static data members of class templates <a id="temp.static">[[temp.static]]</a>
|
| 1226 |
|
| 1227 |
A definition for a static data member or static data member template may
|
| 1228 |
be provided in a namespace scope enclosing the definition of the static
|
| 1229 |
member’s class template.
|
| 1230 |
|
| 1231 |
+
[*Example 1*:
|
| 1232 |
+
|
| 1233 |
``` cpp
|
| 1234 |
template<class T> class X {
|
| 1235 |
static T s;
|
| 1236 |
};
|
| 1237 |
template<class T> T X<T>::s = 0;
|
|
|
|
| 1243 |
|
| 1244 |
template<class T>
|
| 1245 |
const T limits::min = { }; // definition
|
| 1246 |
```
|
| 1247 |
|
| 1248 |
+
— *end example*]
|
| 1249 |
+
|
| 1250 |
An explicit specialization of a static data member declared as an array
|
| 1251 |
of unknown bound can have a different bound from its definition, if any.
|
| 1252 |
|
| 1253 |
+
[*Example 2*:
|
| 1254 |
+
|
| 1255 |
``` cpp
|
| 1256 |
template <class T> struct A {
|
| 1257 |
static int i[];
|
| 1258 |
};
|
| 1259 |
template <class T> int A<T>::i[4]; // 4 elements
|
| 1260 |
template <> int A<int>::i[] = { 1 }; // OK: 1 element
|
| 1261 |
```
|
| 1262 |
|
| 1263 |
+
— *end example*]
|
| 1264 |
+
|
| 1265 |
#### Enumeration members of class templates <a id="temp.mem.enum">[[temp.mem.enum]]</a>
|
| 1266 |
|
| 1267 |
An enumeration member of a class template may be defined outside the
|
| 1268 |
class template definition.
|
| 1269 |
|
| 1270 |
+
[*Example 1*:
|
| 1271 |
+
|
| 1272 |
``` cpp
|
| 1273 |
template<class T> struct A {
|
| 1274 |
enum E : T;
|
| 1275 |
};
|
| 1276 |
A<int> a;
|
| 1277 |
template<class T> enum A<T>::E : T { e1, e2 };
|
| 1278 |
A<int>::E e = A<int>::e1;
|
| 1279 |
```
|
| 1280 |
|
| 1281 |
+
— *end example*]
|
| 1282 |
+
|
| 1283 |
### Member templates <a id="temp.mem">[[temp.mem]]</a>
|
| 1284 |
|
| 1285 |
A template can be declared within a class or class template; such a
|
| 1286 |
template is called a member template. A member template can be defined
|
| 1287 |
within or outside its class definition or class template definition. A
|
| 1288 |
member template of a class template that is defined outside of its class
|
| 1289 |
template definition shall be specified with the *template-parameter*s of
|
| 1290 |
the class template followed by the *template-parameter*s of the member
|
| 1291 |
template.
|
| 1292 |
|
| 1293 |
+
[*Example 1*:
|
| 1294 |
+
|
| 1295 |
``` cpp
|
| 1296 |
template<class T> struct string {
|
| 1297 |
template<class T2> int compare(const T2&);
|
| 1298 |
+
template<class T2> string(const string<T2>& s) { ... }
|
| 1299 |
};
|
| 1300 |
|
| 1301 |
template<class T> template<class T2> int string<T>::compare(const T2& s) {
|
| 1302 |
}
|
| 1303 |
```
|
| 1304 |
|
| 1305 |
+
— *end example*]
|
| 1306 |
+
|
| 1307 |
A local class of non-closure type shall not have member templates.
|
| 1308 |
Access control rules (Clause [[class.access]]) apply to member template
|
| 1309 |
names. A destructor shall not be a member template. A non-template
|
| 1310 |
member function ([[dcl.fct]]) with a given name and type and a member
|
| 1311 |
function template of the same name, which could be used to generate a
|
| 1312 |
specialization of the same type, can both be declared in a class. When
|
| 1313 |
both exist, a use of that name and type refers to the non-template
|
| 1314 |
member unless an explicit template argument list is supplied.
|
| 1315 |
|
| 1316 |
+
[*Example 2*:
|
| 1317 |
+
|
| 1318 |
``` cpp
|
| 1319 |
template <class T> struct A {
|
| 1320 |
void f(int);
|
| 1321 |
template <class T2> void f(T2);
|
| 1322 |
};
|
|
|
|
| 1330 |
ac.f('c'); // template
|
| 1331 |
ac.f<>(1); // template
|
| 1332 |
}
|
| 1333 |
```
|
| 1334 |
|
| 1335 |
+
— *end example*]
|
| 1336 |
+
|
| 1337 |
A member function template shall not be virtual.
|
| 1338 |
|
| 1339 |
+
[*Example 3*:
|
| 1340 |
+
|
| 1341 |
``` cpp
|
| 1342 |
template <class T> struct AA {
|
| 1343 |
template <class C> virtual void g(C); // error
|
| 1344 |
virtual void f(); // OK
|
| 1345 |
};
|
| 1346 |
```
|
| 1347 |
|
| 1348 |
+
— *end example*]
|
| 1349 |
+
|
| 1350 |
A specialization of a member function template does not override a
|
| 1351 |
virtual function from a base class.
|
| 1352 |
|
| 1353 |
+
[*Example 4*:
|
| 1354 |
+
|
| 1355 |
``` cpp
|
| 1356 |
class B {
|
| 1357 |
virtual void f(int);
|
| 1358 |
};
|
| 1359 |
|
| 1360 |
class D : public B {
|
| 1361 |
template <class T> void f(T); // does not override B::f(int)
|
| 1362 |
+
void f(int i) { f<>(i); } // overriding function that calls the template instantiation
|
|
|
|
| 1363 |
};
|
| 1364 |
```
|
| 1365 |
|
| 1366 |
+
— *end example*]
|
| 1367 |
+
|
| 1368 |
A specialization of a conversion function template is referenced in the
|
| 1369 |
same way as a non-template conversion function that converts to the same
|
| 1370 |
type.
|
| 1371 |
|
| 1372 |
+
[*Example 5*:
|
| 1373 |
+
|
| 1374 |
``` cpp
|
| 1375 |
struct A {
|
| 1376 |
template <class T> operator T*();
|
| 1377 |
};
|
| 1378 |
template <class T> A::operator T*(){ return 0; }
|
|
|
|
| 1380 |
template A::operator void*(); // explicit instantiation
|
| 1381 |
|
| 1382 |
int main() {
|
| 1383 |
A a;
|
| 1384 |
int* ip;
|
| 1385 |
+
ip = a.operator int*(); // explicit call to template operator A::operator int*()
|
|
|
|
| 1386 |
}
|
| 1387 |
```
|
| 1388 |
|
| 1389 |
+
— *end example*]
|
| 1390 |
+
|
| 1391 |
+
[*Note 1*: Because the explicit template argument list follows the
|
| 1392 |
+
function template name, and because conversion member function templates
|
| 1393 |
+
and constructor member function templates are called without using a
|
| 1394 |
function name, there is no way to provide an explicit template argument
|
| 1395 |
+
list for these function templates. — *end note*]
|
| 1396 |
|
| 1397 |
A specialization of a conversion function template is not found by name
|
| 1398 |
lookup. Instead, any conversion function templates visible in the
|
| 1399 |
context of the use are considered. For each such operator, if argument
|
| 1400 |
deduction succeeds ([[temp.deduct.conv]]), the resulting specialization
|
|
|
|
| 1411 |
### Variadic templates <a id="temp.variadic">[[temp.variadic]]</a>
|
| 1412 |
|
| 1413 |
A *template parameter pack* is a template parameter that accepts zero or
|
| 1414 |
more template arguments.
|
| 1415 |
|
| 1416 |
+
[*Example 1*:
|
| 1417 |
+
|
| 1418 |
``` cpp
|
| 1419 |
template<class ... Types> struct Tuple { };
|
| 1420 |
|
| 1421 |
Tuple<> t0; // Types contains no arguments
|
| 1422 |
Tuple<int> t1; // Types contains one argument: int
|
| 1423 |
Tuple<int, float> t2; // Types contains two arguments: int and float
|
| 1424 |
Tuple<0> error; // error: 0 is not a type
|
| 1425 |
```
|
| 1426 |
|
| 1427 |
+
— *end example*]
|
| 1428 |
+
|
| 1429 |
A *function parameter pack* is a function parameter that accepts zero or
|
| 1430 |
more function arguments.
|
| 1431 |
|
| 1432 |
+
[*Example 2*:
|
| 1433 |
+
|
| 1434 |
``` cpp
|
| 1435 |
template<class ... Types> void f(Types ... args);
|
| 1436 |
|
| 1437 |
f(); // OK: args contains no arguments
|
| 1438 |
f(1); // OK: args contains one argument: int
|
| 1439 |
f(2, 1.0); // OK: args contains two arguments: int and double
|
| 1440 |
```
|
| 1441 |
|
| 1442 |
+
— *end example*]
|
| 1443 |
+
|
| 1444 |
A *parameter pack* is either a template parameter pack or a function
|
| 1445 |
parameter pack.
|
| 1446 |
|
| 1447 |
A *pack expansion* consists of a *pattern* and an ellipsis, the
|
| 1448 |
instantiation of which produces zero or more instantiations of the
|
|
|
|
| 1450 |
the context in which the expansion occurs. Pack expansions can occur in
|
| 1451 |
the following contexts:
|
| 1452 |
|
| 1453 |
- In a function parameter pack ([[dcl.fct]]); the pattern is the
|
| 1454 |
*parameter-declaration* without the ellipsis.
|
| 1455 |
+
- In a *using-declaration* ([[namespace.udecl]]); the pattern is a
|
| 1456 |
+
*using-declarator*.
|
| 1457 |
- In a template parameter pack that is a pack expansion (
|
| 1458 |
[[temp.param]]):
|
| 1459 |
- if the template parameter pack is a *parameter-declaration*; the
|
| 1460 |
pattern is the *parameter-declaration* without the ellipsis;
|
| 1461 |
- if the template parameter pack is a *type-parameter* with a
|
|
|
|
| 1468 |
- In a *mem-initializer-list* ([[class.base.init]]) for a
|
| 1469 |
*mem-initializer* whose *mem-initializer-id* denotes a base class; the
|
| 1470 |
pattern is the *mem-initializer*.
|
| 1471 |
- In a *template-argument-list* ([[temp.arg]]); the pattern is a
|
| 1472 |
*template-argument*.
|
|
|
|
|
|
|
| 1473 |
- In an *attribute-list* ([[dcl.attr.grammar]]); the pattern is an
|
| 1474 |
*attribute*.
|
| 1475 |
- In an *alignment-specifier* ([[dcl.align]]); the pattern is the
|
| 1476 |
*alignment-specifier* without the ellipsis.
|
| 1477 |
- In a *capture-list* ([[expr.prim.lambda]]); the pattern is a
|
| 1478 |
*capture*.
|
| 1479 |
- In a `sizeof...` expression ([[expr.sizeof]]); the pattern is an
|
| 1480 |
*identifier*.
|
| 1481 |
+
- In a *fold-expression* ([[expr.prim.fold]]); the pattern is the
|
| 1482 |
+
*cast-expression* that contains an unexpanded parameter pack.
|
| 1483 |
+
|
| 1484 |
+
[*Example 3*:
|
| 1485 |
+
|
| 1486 |
+
``` cpp
|
| 1487 |
+
template<class ... Types> void f(Types ... rest);
|
| 1488 |
+
template<class ... Types> void g(Types ... rest) {
|
| 1489 |
+
f(&rest ...); // ``&rest ...'' is a pack expansion; ``&rest'' is its pattern
|
| 1490 |
+
}
|
| 1491 |
+
```
|
| 1492 |
+
|
| 1493 |
+
— *end example*]
|
| 1494 |
|
| 1495 |
For the purpose of determining whether a parameter pack satisfies a rule
|
| 1496 |
regarding entities other than parameter packs, the parameter pack is
|
| 1497 |
considered to be the entity that would result from an instantiation of
|
| 1498 |
the pattern in which it appears.
|
| 1499 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1500 |
A parameter pack whose name appears within the pattern of a pack
|
| 1501 |
expansion is expanded by that pack expansion. An appearance of the name
|
| 1502 |
of a parameter pack is only expanded by the innermost enclosing pack
|
| 1503 |
expansion. The pattern of a pack expansion shall name one or more
|
| 1504 |
parameter packs that are not expanded by a nested pack expansion; such
|
| 1505 |
+
parameter packs are called *unexpanded parameter packs* in the pattern.
|
| 1506 |
All of the parameter packs expanded by a pack expansion shall have the
|
| 1507 |
same number of arguments specified. An appearance of a name of a
|
| 1508 |
parameter pack that is not expanded is ill-formed.
|
| 1509 |
|
| 1510 |
+
[*Example 4*:
|
| 1511 |
+
|
| 1512 |
``` cpp
|
| 1513 |
template<typename...> struct Tuple {};
|
| 1514 |
template<typename T1, typename T2> struct Pair {};
|
| 1515 |
|
| 1516 |
template<class ... Args1> struct zip {
|
|
|
|
| 1527 |
template<class ... Args>
|
| 1528 |
void g(Args ... args) { // OK: Args is expanded by the function parameter pack args
|
| 1529 |
f(const_cast<const Args*>(&args)...); // OK: ``Args'' and ``args'' are expanded
|
| 1530 |
f(5 ...); // error: pattern does not contain any parameter packs
|
| 1531 |
f(args); // error: parameter pack ``args'' is not expanded
|
| 1532 |
+
f(h(args ...) + args ...); // OK: first ``args'' expanded within h,
|
| 1533 |
+
// second ``args'' expanded within f
|
| 1534 |
}
|
| 1535 |
```
|
| 1536 |
|
| 1537 |
+
— *end example*]
|
| 1538 |
+
|
| 1539 |
+
The instantiation of a pack expansion that is neither a `sizeof...`
|
| 1540 |
+
expression nor a *fold-expression* produces a list
|
| 1541 |
+
$\mathtt{E}_1, \mathtt{E}_2, \dotsc, \mathtt{E}_N$, where N is the
|
| 1542 |
+
number of elements in the pack expansion parameters. Each Eᵢ is
|
| 1543 |
+
generated by instantiating the pattern and replacing each pack expansion
|
| 1544 |
+
parameter with its ith element. Such an element, in the context of the
|
| 1545 |
instantiation, is interpreted as follows:
|
| 1546 |
|
| 1547 |
- if the pack is a template parameter pack, the element is a template
|
| 1548 |
parameter ([[temp.param]]) of the corresponding kind (type or
|
| 1549 |
non-type) designating the type or value from the template argument;
|
| 1550 |
otherwise,
|
| 1551 |
- if the pack is a function parameter pack, the element is an
|
| 1552 |
*id-expression* designating the function parameter that resulted from
|
| 1553 |
the instantiation of the pattern where the pack is declared.
|
| 1554 |
|
| 1555 |
+
All of the Eᵢ become elements in the enclosing list.
|
| 1556 |
+
|
| 1557 |
+
[*Note 1*: The variety of list varies with the context:
|
| 1558 |
+
*expression-list*, *base-specifier-list*, *template-argument-list*,
|
| 1559 |
+
etc. — *end note*]
|
| 1560 |
+
|
| 1561 |
+
When N is zero, the instantiation of the expansion produces an empty
|
| 1562 |
+
list. Such an instantiation does not alter the syntactic interpretation
|
| 1563 |
+
of the enclosing construct, even in cases where omitting the list
|
| 1564 |
+
entirely would otherwise be ill-formed or would result in an ambiguity
|
| 1565 |
+
in the grammar.
|
| 1566 |
+
|
| 1567 |
+
[*Example 5*:
|
| 1568 |
|
| 1569 |
``` cpp
|
| 1570 |
template<class... T> struct X : T... { };
|
| 1571 |
template<class... T> void f(T... values) {
|
| 1572 |
X<T...> x(values...);
|
|
|
|
| 1574 |
|
| 1575 |
template void f<>(); // OK: X<> has no base classes
|
| 1576 |
// x is a variable of type X<> that is value-initialized
|
| 1577 |
```
|
| 1578 |
|
| 1579 |
+
— *end example*]
|
| 1580 |
+
|
| 1581 |
The instantiation of a `sizeof...` expression ([[expr.sizeof]])
|
| 1582 |
produces an integral constant containing the number of elements in the
|
| 1583 |
parameter pack it expands.
|
| 1584 |
|
| 1585 |
+
The instantiation of a *fold-expression* produces:
|
| 1586 |
+
|
| 1587 |
+
- `((`E₁ *op* E₂`)` *op* ⋯`)` *op* $\mathtt{E}_N$ for a unary left fold,
|
| 1588 |
+
- E₁ *op* `(`⋯ *op* `(`$\mathtt{E}_{N-1}$ *op* $\mathtt{E}_N$`))` for a
|
| 1589 |
+
unary right fold,
|
| 1590 |
+
- `(((`E *op* E₁`)` *op* E₂`)` *op* ⋯`)` *op* $\mathtt{E}_N$ for a
|
| 1591 |
+
binary left fold, and
|
| 1592 |
+
- E₁ *op* `(`⋯ *op* `(`$\mathtt{E}_{N-1}$ *op* `(`$\mathtt{E}_{N}$ *op*
|
| 1593 |
+
E`)))` for a binary right fold.
|
| 1594 |
+
|
| 1595 |
+
In each case, *op* is the *fold-operator*, N is the number of elements
|
| 1596 |
+
in the pack expansion parameters, and each Eᵢ is generated by
|
| 1597 |
+
instantiating the pattern and replacing each pack expansion parameter
|
| 1598 |
+
with its ith element. For a binary fold-expression, E is generated by
|
| 1599 |
+
instantiating the *cast-expression* that did not contain an unexpanded
|
| 1600 |
+
parameter pack.
|
| 1601 |
+
|
| 1602 |
+
[*Example 6*:
|
| 1603 |
+
|
| 1604 |
+
``` cpp
|
| 1605 |
+
template<typename ...Args>
|
| 1606 |
+
bool all(Args ...args) { return (... && args); }
|
| 1607 |
+
|
| 1608 |
+
bool b = all(true, true, true, false);
|
| 1609 |
+
```
|
| 1610 |
+
|
| 1611 |
+
Within the instantiation of `all`, the returned expression expands to
|
| 1612 |
+
`((true && true) && true) && false`, which evaluates to `false`.
|
| 1613 |
+
|
| 1614 |
+
— *end example*]
|
| 1615 |
+
|
| 1616 |
+
If N is zero for a unary fold-expression, the value of the expression is
|
| 1617 |
+
shown in Table [[tab:fold.empty]]; if the operator is not listed in
|
| 1618 |
+
Table [[tab:fold.empty]], the instantiation is ill-formed.
|
| 1619 |
+
|
| 1620 |
+
**Table: Value of folding empty sequences** <a id="tab:fold.empty">[tab:fold.empty]</a>
|
| 1621 |
+
|
| 1622 |
+
| Operator | Value when parameter pack is empty |
|
| 1623 |
+
| -------- | ---------------------------------- |
|
| 1624 |
+
| `&&` | `true` |
|
| 1625 |
+
| `||` | `false` |
|
| 1626 |
+
| `,` | `void()` |
|
| 1627 |
+
|
| 1628 |
+
|
| 1629 |
### Friends <a id="temp.friend">[[temp.friend]]</a>
|
| 1630 |
|
| 1631 |
A friend of a class or class template can be a function template or
|
| 1632 |
class template, a specialization of a function template or class
|
| 1633 |
template, or a non-template function or class. For a friend function
|
|
|
|
| 1644 |
declaration refers to the deduced specialization of that function
|
| 1645 |
template ([[temp.deduct.decl]]), otherwise,
|
| 1646 |
- the name shall be an *unqualified-id* that declares (or redeclares) a
|
| 1647 |
non-template function.
|
| 1648 |
|
| 1649 |
+
[*Example 1*:
|
| 1650 |
+
|
| 1651 |
``` cpp
|
| 1652 |
template<class T> class task;
|
| 1653 |
template<class T> task<T>* preempt(task<T>*);
|
| 1654 |
|
| 1655 |
template<class T> class task {
|
|
|
|
| 1674 |
`task` class template has all specializations of the function template
|
| 1675 |
`func` as friends. Similarly, each specialization of the `task` class
|
| 1676 |
template has the class template specialization `task<int>` as a friend,
|
| 1677 |
and has all specializations of the class template `frd` as friends.
|
| 1678 |
|
| 1679 |
+
— *end example*]
|
| 1680 |
+
|
| 1681 |
A friend template may be declared within a class or class template. A
|
| 1682 |
friend function template may be defined within a class or class
|
| 1683 |
template, but a friend class template may not be defined in a class or
|
| 1684 |
class template. In these cases, all specializations of the friend class
|
| 1685 |
or friend function template are friends of the class or class template
|
| 1686 |
granting friendship.
|
| 1687 |
|
| 1688 |
+
[*Example 2*:
|
| 1689 |
+
|
| 1690 |
``` cpp
|
| 1691 |
class A {
|
| 1692 |
template<class T> friend class B; // OK
|
| 1693 |
+
template<class T> friend void f(T){ ... } // OK
|
| 1694 |
};
|
| 1695 |
```
|
| 1696 |
|
| 1697 |
+
— *end example*]
|
| 1698 |
+
|
| 1699 |
A template friend declaration specifies that all specializations of that
|
| 1700 |
template, whether they are implicitly instantiated ([[temp.inst]]),
|
| 1701 |
partially specialized ([[temp.class.spec]]) or explicitly specialized (
|
| 1702 |
[[temp.expl.spec]]), are friends of the class containing the template
|
| 1703 |
friend declaration.
|
| 1704 |
|
| 1705 |
+
[*Example 3*:
|
| 1706 |
+
|
| 1707 |
``` cpp
|
| 1708 |
class X {
|
| 1709 |
template<class T> friend struct A;
|
| 1710 |
class Y { };
|
| 1711 |
};
|
| 1712 |
|
| 1713 |
template<class T> struct A { X::Y ab; }; // OK
|
| 1714 |
template<class T> struct A<T*> { X::Y ab; }; // OK
|
| 1715 |
```
|
| 1716 |
|
| 1717 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1718 |
|
| 1719 |
A member of a class template may be declared to be a friend of a
|
| 1720 |
non-template class. In this case, the corresponding member of every
|
| 1721 |
+
specialization of the primary class template and class template partial
|
| 1722 |
+
specializations thereof is a friend of the class granting friendship.
|
| 1723 |
+
For explicit specializations and specializations of partial
|
| 1724 |
+
specializations, the corresponding member is the member (if any) that
|
| 1725 |
+
has the same name, kind (type, function, class template, or function
|
| 1726 |
+
template), template parameters, and signature as the member of the class
|
| 1727 |
+
template instantiation that would otherwise have been generated.
|
| 1728 |
+
|
| 1729 |
+
[*Example 4*:
|
| 1730 |
|
| 1731 |
``` cpp
|
| 1732 |
template<class T> struct A {
|
| 1733 |
struct B { };
|
| 1734 |
void f();
|
|
|
|
| 1752 |
template<class T> friend void A<T>::D::g(); // does not grant friendship to A<int>::D::g()
|
| 1753 |
// because A<int>::D is not a specialization of A<T>::D
|
| 1754 |
};
|
| 1755 |
```
|
| 1756 |
|
| 1757 |
+
— *end example*]
|
| 1758 |
+
|
| 1759 |
+
[*Note 1*: A friend declaration may first declare a member of an
|
| 1760 |
+
enclosing namespace scope ([[temp.inject]]). — *end note*]
|
| 1761 |
|
| 1762 |
A friend template shall not be declared in a local class.
|
| 1763 |
|
| 1764 |
Friend declarations shall not declare partial specializations.
|
| 1765 |
|
| 1766 |
+
[*Example 5*:
|
| 1767 |
+
|
| 1768 |
``` cpp
|
| 1769 |
template<class T> class A { };
|
| 1770 |
class X {
|
| 1771 |
template<class T> friend class A<T*>; // error
|
| 1772 |
};
|
| 1773 |
```
|
| 1774 |
|
| 1775 |
+
— *end example*]
|
| 1776 |
+
|
| 1777 |
When a friend declaration refers to a specialization of a function
|
| 1778 |
template, the function parameter declarations shall not include default
|
| 1779 |
arguments, nor shall the inline specifier be used in such a declaration.
|
| 1780 |
|
| 1781 |
### Class template partial specializations <a id="temp.class.spec">[[temp.class.spec]]</a>
|
| 1782 |
|
| 1783 |
+
A *primary class template* declaration is one in which the class
|
| 1784 |
template name is an identifier. A template declaration in which the
|
| 1785 |
class template name is a *simple-template-id* is a *partial
|
| 1786 |
specialization* of the class template named in the *simple-template-id*.
|
| 1787 |
A partial specialization of a class template provides an alternative
|
| 1788 |
definition of the template that is used instead of the primary
|
|
|
|
| 1797 |
|
| 1798 |
Each class template partial specialization is a distinct template and
|
| 1799 |
definitions shall be provided for the members of a template partial
|
| 1800 |
specialization ([[temp.class.spec.mfunc]]).
|
| 1801 |
|
| 1802 |
+
[*Example 1*:
|
| 1803 |
+
|
| 1804 |
``` cpp
|
| 1805 |
+
template<class T1, class T2, int I> class A { };
|
| 1806 |
+
template<class T, int I> class A<T, T*, I> { };
|
| 1807 |
+
template<class T1, class T2, int I> class A<T1*, T2, I> { };
|
| 1808 |
+
template<class T> class A<int, T*, 5> { };
|
| 1809 |
+
template<class T1, class T2, int I> class A<T1, T2*, I> { };
|
| 1810 |
```
|
| 1811 |
|
| 1812 |
The first declaration declares the primary (unspecialized) class
|
| 1813 |
template. The second and subsequent declarations declare partial
|
| 1814 |
specializations of the primary template.
|
| 1815 |
|
| 1816 |
+
— *end example*]
|
| 1817 |
+
|
| 1818 |
The template parameters are specified in the angle bracket enclosed list
|
| 1819 |
that immediately follows the keyword `template`. For partial
|
| 1820 |
specializations, the template argument list is explicitly written
|
| 1821 |
immediately following the class template name. For primary templates,
|
| 1822 |
this list is implicitly described by the template parameter list.
|
| 1823 |
Specifically, the order of the template arguments is the sequence in
|
| 1824 |
+
which they appear in the template parameter list.
|
| 1825 |
+
|
| 1826 |
+
[*Example 2*: The template argument list for the primary template in
|
| 1827 |
+
the example above is `<T1,` `T2,` `I>`. — *end example*]
|
| 1828 |
+
|
| 1829 |
+
[*Note 1*:
|
| 1830 |
+
|
| 1831 |
The template argument list shall not be specified in the primary
|
| 1832 |
template declaration. For example,
|
| 1833 |
|
| 1834 |
``` cpp
|
| 1835 |
+
template<class T1, class T2, int I>
|
| 1836 |
+
class A<T1, T2, I> { }; // error
|
| 1837 |
```
|
| 1838 |
|
| 1839 |
+
— *end note*]
|
| 1840 |
+
|
| 1841 |
+
A class template partial specialization may be declared in any scope in
|
| 1842 |
+
which the corresponding primary template may be defined (
|
| 1843 |
+
[[namespace.memdef]], [[class.mem]], [[temp.mem]]).
|
| 1844 |
+
|
| 1845 |
+
[*Example 3*:
|
| 1846 |
|
| 1847 |
``` cpp
|
| 1848 |
template<class T> struct A {
|
| 1849 |
struct C {
|
| 1850 |
template<class T2> struct B { };
|
| 1851 |
+
template<class T2> struct B<T2**> { }; // partial specialization #1
|
| 1852 |
};
|
| 1853 |
};
|
| 1854 |
|
| 1855 |
// partial specialization of A<T>::C::B<T2>
|
| 1856 |
template<class T> template<class T2>
|
| 1857 |
+
struct A<T>::C::B<T2*> { }; // #2
|
| 1858 |
|
| 1859 |
+
A<short>::C::B<int*> absip; // uses partial specialization #2
|
| 1860 |
```
|
| 1861 |
|
| 1862 |
+
— *end example*]
|
| 1863 |
+
|
| 1864 |
Partial specialization declarations themselves are not found by name
|
| 1865 |
lookup. Rather, when the primary template name is used, any
|
| 1866 |
previously-declared partial specializations of the primary template are
|
| 1867 |
also considered. One consequence is that a *using-declaration* which
|
| 1868 |
refers to a class template does not restrict the set of partial
|
| 1869 |
specializations which may be found through the *using-declaration*.
|
| 1870 |
|
| 1871 |
+
[*Example 4*:
|
| 1872 |
+
|
| 1873 |
``` cpp
|
| 1874 |
namespace N {
|
| 1875 |
template<class T1, class T2> class A { }; // primary template
|
| 1876 |
}
|
| 1877 |
|
|
|
|
| 1879 |
|
| 1880 |
namespace N {
|
| 1881 |
template<class T> class A<T, T*> { }; // partial specialization
|
| 1882 |
}
|
| 1883 |
|
| 1884 |
+
A<int,int*> a; // uses the partial specialization, which is found through the using-declaration
|
| 1885 |
+
// which refers to the primary template
|
| 1886 |
```
|
| 1887 |
|
| 1888 |
+
— *end example*]
|
| 1889 |
+
|
| 1890 |
A non-type argument is non-specialized if it is the name of a non-type
|
| 1891 |
parameter. All other non-type arguments are specialized.
|
| 1892 |
|
| 1893 |
Within the argument list of a class template partial specialization, the
|
| 1894 |
following restrictions apply:
|
| 1895 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1896 |
- The type of a template parameter corresponding to a specialized
|
| 1897 |
non-type argument shall not be dependent on a parameter of the
|
| 1898 |
specialization.
|
| 1899 |
+
\[*Example 5*:
|
| 1900 |
``` cpp
|
| 1901 |
template <class T, T t> struct C {};
|
| 1902 |
template <class T> struct C<T, 1>; // error
|
| 1903 |
|
| 1904 |
template< int X, int (*array_ptr)[X] > class A {};
|
| 1905 |
int array[5];
|
| 1906 |
template< int X > class A<X,&array> { }; // error
|
| 1907 |
```
|
| 1908 |
+
|
| 1909 |
+
— *end example*]
|
| 1910 |
- The specialization shall be more specialized than the primary
|
| 1911 |
template ([[temp.class.order]]).
|
| 1912 |
- The template parameter list of a specialization shall not contain
|
| 1913 |
default template argument values.[^4]
|
| 1914 |
- An argument shall not contain an unexpanded parameter pack. If an
|
|
|
|
| 1937 |
|
| 1938 |
A partial specialization matches a given actual template argument list
|
| 1939 |
if the template arguments of the partial specialization can be deduced
|
| 1940 |
from the actual template argument list ([[temp.deduct]]).
|
| 1941 |
|
| 1942 |
+
[*Example 1*:
|
| 1943 |
+
|
| 1944 |
``` cpp
|
| 1945 |
+
template<class T1, class T2, int I> class A { }; // #1
|
| 1946 |
+
template<class T, int I> class A<T, T*, I> { }; // #2
|
| 1947 |
+
template<class T1, class T2, int I> class A<T1*, T2, I> { }; // #3
|
| 1948 |
+
template<class T> class A<int, T*, 5> { }; // #4
|
| 1949 |
+
template<class T1, class T2, int I> class A<T1, T2*, I> { }; // #5
|
| 1950 |
+
|
| 1951 |
A<int, int, 1> a1; // uses #1
|
| 1952 |
A<int, int*, 1> a2; // uses #2, T is int, I is 1
|
| 1953 |
A<int, char*, 5> a3; // uses #4, T is char
|
| 1954 |
A<int, char*, 1> a4; // uses #5, T1 is int, T2 is char, I is 1
|
| 1955 |
A<int*, int*, 2> a5; // ambiguous: matches #3 and #5
|
| 1956 |
```
|
| 1957 |
|
| 1958 |
+
— *end example*]
|
| 1959 |
+
|
| 1960 |
+
If the template arguments of a partial specialization cannot be deduced
|
| 1961 |
+
because of the structure of its *template-parameter-list* and the
|
| 1962 |
+
*template-id*, the program is ill-formed.
|
| 1963 |
+
|
| 1964 |
+
[*Example 2*:
|
| 1965 |
+
|
| 1966 |
+
``` cpp
|
| 1967 |
+
template <int I, int J> struct A {};
|
| 1968 |
+
template <int I> struct A<I+5, I*2> {}; // error
|
| 1969 |
+
|
| 1970 |
+
template <int I> struct A<I, I> {}; // OK
|
| 1971 |
+
|
| 1972 |
+
template <int I, int J, int K> struct B {};
|
| 1973 |
+
template <int I> struct B<I, I*2, 2> {}; // OK
|
| 1974 |
+
```
|
| 1975 |
+
|
| 1976 |
+
— *end example*]
|
| 1977 |
|
| 1978 |
In a type name that refers to a class template specialization, (e.g.,
|
| 1979 |
`A<int, int, 1>`) the argument list shall match the template parameter
|
| 1980 |
list of the primary template. The template arguments of a specialization
|
| 1981 |
are deduced from the arguments of the primary template.
|
| 1982 |
|
| 1983 |
#### Partial ordering of class template specializations <a id="temp.class.order">[[temp.class.order]]</a>
|
| 1984 |
|
| 1985 |
+
For two class template partial specializations, the first is *more
|
| 1986 |
+
specialized* than the second if, given the following rewrite to two
|
| 1987 |
+
function templates, the first function template is more specialized than
|
| 1988 |
+
the second according to the ordering rules for function templates (
|
| 1989 |
+
[[temp.func.order]]):
|
| 1990 |
|
| 1991 |
+
- Each of the two function templates has the same template parameters as
|
| 1992 |
+
the corresponding partial specialization.
|
| 1993 |
+
- Each function template has a single function parameter whose type is a
|
| 1994 |
+
class template specialization where the template arguments are the
|
| 1995 |
+
corresponding template parameters from the function template for each
|
| 1996 |
+
template argument in the *template-argument-list* of the
|
| 1997 |
+
*simple-template-id* of the partial specialization.
|
| 1998 |
+
|
| 1999 |
+
[*Example 1*:
|
| 2000 |
|
| 2001 |
``` cpp
|
| 2002 |
template<int I, int J, class T> class X { };
|
| 2003 |
template<int I, int J> class X<I, J, int> { }; // #1
|
| 2004 |
template<int I> class X<I, I, int> { }; // #2
|
| 2005 |
|
| 2006 |
+
template<int I0, int J0> void f(X<I0, J0, int>); // A
|
| 2007 |
+
template<int I0> void f(X<I0, I0, int>); // B
|
| 2008 |
+
|
| 2009 |
+
template <auto v> class Y { };
|
| 2010 |
+
template <auto* p> class Y<p> { }; // #3
|
| 2011 |
+
template <auto** pp> class Y<pp> { }; // #4
|
| 2012 |
+
|
| 2013 |
+
template <auto* p0> void g(Y<p0>); // C
|
| 2014 |
+
template <auto** pp0> void g(Y<pp0>); // D
|
| 2015 |
```
|
| 2016 |
|
| 2017 |
+
According to the ordering rules for function templates, the function
|
| 2018 |
+
template *B* is more specialized than the function template *A* and the
|
| 2019 |
+
function template *D* is more specialized than the function template
|
| 2020 |
+
*C*. Therefore, the partial specialization \#2 is more specialized than
|
| 2021 |
+
the partial specialization \#1 and the partial specialization \#4 is
|
| 2022 |
+
more specialized than the partial specialization \#3.
|
| 2023 |
+
|
| 2024 |
+
— *end example*]
|
| 2025 |
|
| 2026 |
#### Members of class template specializations <a id="temp.class.spec.mfunc">[[temp.class.spec.mfunc]]</a>
|
| 2027 |
|
| 2028 |
The template parameter list of a member of a class template partial
|
| 2029 |
specialization shall match the template parameter list of the class
|
|
|
|
| 2038 |
definitions for members of a class template partial specialization. An
|
| 2039 |
explicit specialization of a member of a class template partial
|
| 2040 |
specialization is declared in the same way as an explicit specialization
|
| 2041 |
of the primary template.
|
| 2042 |
|
| 2043 |
+
[*Example 1*:
|
| 2044 |
+
|
| 2045 |
``` cpp
|
| 2046 |
+
// primary class template
|
| 2047 |
template<class T, int I> struct A {
|
| 2048 |
void f();
|
| 2049 |
};
|
| 2050 |
|
| 2051 |
+
// member of primary class template
|
| 2052 |
template<class T, int I> void A<T,I>::f() { }
|
| 2053 |
|
| 2054 |
// class template partial specialization
|
| 2055 |
template<class T> struct A<T,2> {
|
| 2056 |
void f();
|
|
|
|
| 2066 |
|
| 2067 |
int main() {
|
| 2068 |
A<char,0> a0;
|
| 2069 |
A<char,2> a2;
|
| 2070 |
a0.f(); // OK, uses definition of primary template's member
|
| 2071 |
+
a2.g(); // OK, uses definition of partial specialization's member
|
| 2072 |
+
a2.h(); // OK, uses definition of explicit specialization's member
|
| 2073 |
+
a2.f(); // ill-formed, no definition of f for A<T,2>; the primary template is not used here
|
|
|
|
|
|
|
|
|
|
| 2074 |
}
|
| 2075 |
```
|
| 2076 |
|
| 2077 |
+
— *end example*]
|
| 2078 |
+
|
| 2079 |
If a member template of a class template is partially specialized, the
|
| 2080 |
member template partial specializations are member templates of the
|
| 2081 |
enclosing class template; if the enclosing class template is
|
| 2082 |
instantiated ([[temp.inst]], [[temp.explicit]]), a declaration for
|
| 2083 |
every member template partial specialization is also instantiated as
|
|
|
|
| 2089 |
specialization of the member template is explicitly specialized for a
|
| 2090 |
given (implicit) specialization of the enclosing class template, the
|
| 2091 |
primary member template and its other partial specializations are still
|
| 2092 |
considered for this specialization of the enclosing class template.
|
| 2093 |
|
| 2094 |
+
[*Example 2*:
|
| 2095 |
+
|
| 2096 |
``` cpp
|
| 2097 |
template<class T> struct A {
|
| 2098 |
template<class T2> struct B {}; // #1
|
| 2099 |
template<class T2> struct B<T2*> {}; // #2
|
| 2100 |
};
|
|
|
|
| 2104 |
A<char>::B<int*> abcip; // uses #2
|
| 2105 |
A<short>::B<int*> absip; // uses #3
|
| 2106 |
A<char>::B<int> abci; // uses #1
|
| 2107 |
```
|
| 2108 |
|
| 2109 |
+
— *end example*]
|
| 2110 |
+
|
| 2111 |
### Function templates <a id="temp.fct">[[temp.fct]]</a>
|
| 2112 |
|
| 2113 |
+
A function template defines an unbounded set of related functions.
|
| 2114 |
+
|
| 2115 |
+
[*Example 1*:
|
| 2116 |
+
|
| 2117 |
+
A family of sort functions might be declared like this:
|
| 2118 |
|
| 2119 |
``` cpp
|
| 2120 |
template<class T> class Array { };
|
| 2121 |
template<class T> void sort(Array<T>&);
|
| 2122 |
```
|
| 2123 |
|
| 2124 |
+
— *end example*]
|
| 2125 |
+
|
| 2126 |
A function template can be overloaded with other function templates and
|
| 2127 |
with non-template functions ([[dcl.fct]]). A non-template function is
|
| 2128 |
not related to a function template (i.e., it is never considered to be a
|
| 2129 |
specialization), even if it has the same name and type as a potentially
|
| 2130 |
generated function template specialization.[^5]
|
|
|
|
| 2132 |
#### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
|
| 2133 |
|
| 2134 |
It is possible to overload function templates so that two different
|
| 2135 |
function template specializations have the same type.
|
| 2136 |
|
| 2137 |
+
[*Example 1*:
|
| 2138 |
+
|
| 2139 |
``` cpp
|
| 2140 |
+
// translation unit 1:
|
| 2141 |
template<class T>
|
| 2142 |
void f(T*);
|
| 2143 |
void g(int* p) {
|
| 2144 |
f(p); // calls f<int>(int*)
|
| 2145 |
}
|
| 2146 |
```
|
| 2147 |
|
| 2148 |
``` cpp
|
| 2149 |
+
// translation unit 2:
|
| 2150 |
template<class T>
|
| 2151 |
void f(T);
|
| 2152 |
void h(int* p) {
|
| 2153 |
f(p); // calls f<int*>(int*)
|
| 2154 |
}
|
| 2155 |
```
|
| 2156 |
|
| 2157 |
+
— *end example*]
|
| 2158 |
+
|
| 2159 |
+
Such specializations are distinct functions and do not violate the
|
| 2160 |
+
one-definition rule ([[basic.def.odr]]).
|
| 2161 |
+
|
| 2162 |
+
The signature of a function template is defined in Clause
|
| 2163 |
+
[[intro.defs]]. The names of the template parameters are significant
|
| 2164 |
+
only for establishing the relationship between the template parameters
|
| 2165 |
+
and the rest of the signature.
|
| 2166 |
+
|
| 2167 |
+
[*Note 1*:
|
| 2168 |
+
|
| 2169 |
+
Two distinct function templates may have identical function return types
|
| 2170 |
+
and function parameter lists, even if overload resolution alone cannot
|
| 2171 |
+
distinguish them.
|
| 2172 |
|
| 2173 |
``` cpp
|
| 2174 |
template<class T> void f();
|
| 2175 |
template<int I> void f(); // OK: overloads the first template
|
| 2176 |
// distinguishable with an explicit template argument list
|
| 2177 |
```
|
| 2178 |
|
| 2179 |
+
— *end note*]
|
| 2180 |
+
|
| 2181 |
When an expression that references a template parameter is used in the
|
| 2182 |
function parameter list or the return type in the declaration of a
|
| 2183 |
function template, the expression that references the template parameter
|
| 2184 |
is part of the signature of the function template. This is necessary to
|
| 2185 |
permit a declaration of a function template in one translation unit to
|
| 2186 |
be linked with another declaration of the function template in another
|
| 2187 |
translation unit and, conversely, to ensure that function templates that
|
| 2188 |
are intended to be distinct are not linked with one another.
|
| 2189 |
|
| 2190 |
+
[*Example 2*:
|
| 2191 |
+
|
| 2192 |
``` cpp
|
| 2193 |
template <int I, int J> A<I+J> f(A<I>, A<J>); // #1
|
| 2194 |
template <int K, int L> A<K+L> f(A<K>, A<L>); // same as #1
|
| 2195 |
template <int I, int J> A<I-J> f(A<I>, A<J>); // different from #1
|
| 2196 |
```
|
| 2197 |
|
| 2198 |
+
— *end example*]
|
| 2199 |
+
|
| 2200 |
+
[*Note 2*: Most expressions that use template parameters use non-type
|
| 2201 |
+
template parameters, but it is possible for an expression to reference a
|
| 2202 |
+
type parameter. For example, a template type parameter can be used in
|
| 2203 |
+
the `sizeof` operator. — *end note*]
|
| 2204 |
|
| 2205 |
Two expressions involving template parameters are considered
|
| 2206 |
*equivalent* if two function definitions containing the expressions
|
| 2207 |
+
would satisfy the one-definition rule ([[basic.def.odr]]), except that
|
| 2208 |
the tokens used to name the template parameters may differ as long as a
|
| 2209 |
token used to name a template parameter in one expression is replaced by
|
| 2210 |
another token that names the same template parameter in the other
|
| 2211 |
expression. For determining whether two dependent names ([[temp.dep]])
|
| 2212 |
are equivalent, only the name itself is considered, not the result of
|
| 2213 |
name lookup in the context of the template. If multiple declarations of
|
| 2214 |
the same function template differ in the result of this name lookup, the
|
| 2215 |
result for the first declaration is used.
|
| 2216 |
|
| 2217 |
+
[*Example 3*:
|
| 2218 |
+
|
| 2219 |
``` cpp
|
| 2220 |
template <int I, int J> void f(A<I+J>); // #1
|
| 2221 |
template <int K, int L> void f(A<K+L>); // same as #1
|
| 2222 |
|
| 2223 |
template <class T> decltype(g(T())) h();
|
|
|
|
| 2226 |
{ return g(T()); } // ...although the lookup here does find g(int)
|
| 2227 |
int i = h<int>(); // template argument substitution fails; g(int)
|
| 2228 |
// was not in scope at the first declaration of h()
|
| 2229 |
```
|
| 2230 |
|
| 2231 |
+
— *end example*]
|
| 2232 |
+
|
| 2233 |
Two expressions involving template parameters that are not equivalent
|
| 2234 |
are *functionally equivalent* if, for any given set of template
|
| 2235 |
arguments, the evaluation of the expression results in the same value.
|
| 2236 |
|
| 2237 |
Two function templates are *equivalent* if they are declared in the same
|
|
|
|
| 2242 |
are equivalent except that one or more expressions that involve template
|
| 2243 |
parameters in the return types and parameter lists are functionally
|
| 2244 |
equivalent using the rules described above to compare expressions
|
| 2245 |
involving template parameters. If a program contains declarations of
|
| 2246 |
function templates that are functionally equivalent but not equivalent,
|
| 2247 |
+
the program is ill-formed, no diagnostic required.
|
| 2248 |
+
|
| 2249 |
+
[*Note 3*:
|
| 2250 |
|
| 2251 |
This rule guarantees that equivalent declarations will be linked with
|
| 2252 |
one another, while not requiring implementations to use heroic efforts
|
| 2253 |
to guarantee that functionally equivalent declarations will be treated
|
| 2254 |
as distinct. For example, the last two declarations are functionally
|
|
|
|
| 2266 |
// Ill-formed, no diagnostic required
|
| 2267 |
template <int I> void f(A<I>, A<I+10>);
|
| 2268 |
template <int I> void f(A<I>, A<I+1+2+3+4>);
|
| 2269 |
```
|
| 2270 |
|
| 2271 |
+
— *end note*]
|
| 2272 |
+
|
| 2273 |
#### Partial ordering of function templates <a id="temp.func.order">[[temp.func.order]]</a>
|
| 2274 |
|
| 2275 |
If a function template is overloaded, the use of a function template
|
| 2276 |
specialization might be ambiguous because template argument deduction (
|
| 2277 |
[[temp.deduct]]) may associate the function template specialization with
|
|
|
|
| 2299 |
|
| 2300 |
To produce the transformed template, for each type, non-type, or
|
| 2301 |
template template parameter (including template parameter packs (
|
| 2302 |
[[temp.variadic]]) thereof) synthesize a unique type, value, or class
|
| 2303 |
template respectively and substitute it for each occurrence of that
|
| 2304 |
+
parameter in the function type of the template.
|
| 2305 |
+
|
| 2306 |
+
[*Note 1*: The type replacing the placeholder in the type of the value
|
| 2307 |
+
synthesized for a non-type template parameter is also a unique
|
| 2308 |
+
synthesized type. — *end note*]
|
| 2309 |
+
|
| 2310 |
+
If only one of the function templates *M* is a non-static member of some
|
| 2311 |
+
class *A*, *M* is considered to have a new first parameter inserted in
|
| 2312 |
+
its function parameter list. Given cv as the cv-qualifiers of *M* (if
|
| 2313 |
+
any), the new parameter is of type “rvalue reference to cv *A*” if the
|
| 2314 |
+
optional *ref-qualifier* of *M* is `&&` or if *M* has no *ref-qualifier*
|
| 2315 |
+
and the first parameter of the other template has rvalue reference type.
|
| 2316 |
+
Otherwise, the new parameter is of type “lvalue reference to cv *A*”.
|
| 2317 |
+
|
| 2318 |
+
[*Note 2*: This allows a non-static member to be ordered with respect
|
| 2319 |
+
to a non-member function and for the results to be equivalent to the
|
| 2320 |
+
ordering of two equivalent non-members. — *end note*]
|
| 2321 |
+
|
| 2322 |
+
[*Example 1*:
|
| 2323 |
|
| 2324 |
``` cpp
|
| 2325 |
struct A { };
|
| 2326 |
template<class T> struct B {
|
| 2327 |
template<class R> int operator*(R&); // #1
|
|
|
|
| 2337 |
B<A> b;
|
| 2338 |
b * a; // calls #1a
|
| 2339 |
}
|
| 2340 |
```
|
| 2341 |
|
| 2342 |
+
— *end example*]
|
| 2343 |
+
|
| 2344 |
Using the transformed function template’s function type, perform type
|
| 2345 |
deduction against the other template as described in
|
| 2346 |
[[temp.deduct.partial]].
|
| 2347 |
|
| 2348 |
+
[*Example 2*:
|
| 2349 |
+
|
| 2350 |
``` cpp
|
| 2351 |
template<class T> struct A { A(); };
|
| 2352 |
|
| 2353 |
template<class T> void f(T);
|
| 2354 |
template<class T> void f(T*);
|
|
|
|
| 2362 |
|
| 2363 |
void m() {
|
| 2364 |
const int* p;
|
| 2365 |
f(p); // f(const T*) is more specialized than f(T) or f(T*)
|
| 2366 |
float x;
|
| 2367 |
+
g(x); // ambiguous: g(T) or g(T&)
|
| 2368 |
A<int> z;
|
| 2369 |
h(z); // overload resolution selects h(A<T>&)
|
| 2370 |
const A<int> z2;
|
| 2371 |
h(z2); // h(const T&) is called because h(A<T>&) is not callable
|
| 2372 |
}
|
| 2373 |
```
|
| 2374 |
|
| 2375 |
+
— *end example*]
|
| 2376 |
+
|
| 2377 |
+
[*Note 3*:
|
| 2378 |
+
|
| 2379 |
Since partial ordering in a call context considers only parameters for
|
| 2380 |
which there are explicit call arguments, some parameters are ignored
|
| 2381 |
(namely, function parameter packs, parameters with default arguments,
|
| 2382 |
and ellipsis parameters).
|
| 2383 |
|
| 2384 |
+
[*Example 3*:
|
| 2385 |
+
|
| 2386 |
``` cpp
|
| 2387 |
template<class T> void f(T); // #1
|
| 2388 |
template<class T> void f(T*, int=1); // #2
|
| 2389 |
template<class T> void g(T); // #3
|
| 2390 |
template<class T> void g(T*, ...); // #4
|
|
|
|
| 2396 |
f(ip); // calls #2
|
| 2397 |
g(ip); // calls #4
|
| 2398 |
}
|
| 2399 |
```
|
| 2400 |
|
| 2401 |
+
— *end example*]
|
| 2402 |
+
|
| 2403 |
+
[*Example 4*:
|
| 2404 |
+
|
| 2405 |
``` cpp
|
| 2406 |
template<class T, class U> struct A { };
|
| 2407 |
|
| 2408 |
template<class T, class U> void f(U, A<U, T>* p = 0); // #1
|
| 2409 |
template< class U> void f(U, A<U, U>* p = 0); // #2
|
|
|
|
| 2415 |
f<int>(42); // error: ambiguous
|
| 2416 |
g(42); // error: ambiguous
|
| 2417 |
}
|
| 2418 |
```
|
| 2419 |
|
| 2420 |
+
— *end example*]
|
| 2421 |
+
|
| 2422 |
+
[*Example 5*:
|
| 2423 |
+
|
| 2424 |
``` cpp
|
| 2425 |
template<class T, class... U> void f(T, U...); // #1
|
| 2426 |
template<class T > void f(T); // #2
|
| 2427 |
template<class T, class... U> void g(T*, U...); // #3
|
| 2428 |
template<class T > void g(T); // #4
|
|
|
|
| 2431 |
f(&i); // error: ambiguous
|
| 2432 |
g(&i); // OK: calls #3
|
| 2433 |
}
|
| 2434 |
```
|
| 2435 |
|
| 2436 |
+
— *end example*]
|
| 2437 |
+
|
| 2438 |
+
— *end note*]
|
| 2439 |
+
|
| 2440 |
### Alias templates <a id="temp.alias">[[temp.alias]]</a>
|
| 2441 |
|
| 2442 |
A *template-declaration* in which the *declaration* is an
|
| 2443 |
*alias-declaration* (Clause [[dcl.dcl]]) declares the *identifier* to
|
| 2444 |
+
be an *alias template*. An alias template is a name for a family of
|
| 2445 |
types. The name of the alias template is a *template-name*.
|
| 2446 |
|
| 2447 |
When a *template-id* refers to the specialization of an alias template,
|
| 2448 |
it is equivalent to the associated type obtained by substitution of its
|
| 2449 |
*template-argument*s for the *template-parameter*s in the *type-id* of
|
| 2450 |
+
the alias template.
|
| 2451 |
+
|
| 2452 |
+
[*Note 1*: An alias template name is never deduced. — *end note*]
|
| 2453 |
+
|
| 2454 |
+
[*Example 1*:
|
| 2455 |
|
| 2456 |
``` cpp
|
| 2457 |
+
template<class T> struct Alloc { ... };
|
| 2458 |
template<class T> using Vec = vector<T, Alloc<T>>;
|
| 2459 |
Vec<int> v; // same as vector<int, Alloc<int>{> v;}
|
| 2460 |
|
| 2461 |
template<class T>
|
| 2462 |
void process(Vec<T>& v)
|
| 2463 |
+
{ ... }
|
| 2464 |
|
| 2465 |
template<class T>
|
| 2466 |
void process(vector<T, Alloc<T>>& w)
|
| 2467 |
+
{ ... } // error: redefinition
|
| 2468 |
|
| 2469 |
template<template<class> class TT>
|
| 2470 |
void f(TT<int>);
|
| 2471 |
|
| 2472 |
f(v); // error: Vec not deduced
|
|
|
|
| 2474 |
template<template<class,class> class TT>
|
| 2475 |
void g(TT<int, Alloc<int>>);
|
| 2476 |
g(v); // OK: TT = vector
|
| 2477 |
```
|
| 2478 |
|
| 2479 |
+
— *end example*]
|
| 2480 |
+
|
| 2481 |
+
However, if the *template-id* is dependent, subsequent template argument
|
| 2482 |
+
substitution still applies to the *template-id*.
|
| 2483 |
+
|
| 2484 |
+
[*Example 2*:
|
| 2485 |
+
|
| 2486 |
+
``` cpp
|
| 2487 |
+
template<typename...> using void_t = void;
|
| 2488 |
+
template<typename T> void_t<typename T::foo> f();
|
| 2489 |
+
f<int>(); // error, int does not have a nested type foo
|
| 2490 |
+
```
|
| 2491 |
+
|
| 2492 |
+
— *end example*]
|
| 2493 |
+
|
| 2494 |
The *type-id* in an alias template declaration shall not refer to the
|
| 2495 |
alias template being declared. The type produced by an alias template
|
| 2496 |
specialization shall not directly or indirectly make use of that
|
| 2497 |
specialization.
|
| 2498 |
|
| 2499 |
+
[*Example 3*:
|
| 2500 |
+
|
| 2501 |
``` cpp
|
| 2502 |
template <class T> struct A;
|
| 2503 |
template <class T> using B = typename A<T>::U;
|
| 2504 |
template <class T> struct A {
|
| 2505 |
typedef B<T> U;
|
| 2506 |
};
|
| 2507 |
B<short> b; // error: instantiation of B<short> uses own type via A<short>::U
|
| 2508 |
```
|
| 2509 |
|
| 2510 |
+
— *end example*]
|
| 2511 |
+
|
| 2512 |
## Name resolution <a id="temp.res">[[temp.res]]</a>
|
| 2513 |
|
| 2514 |
Three kinds of names can be used within a template definition:
|
| 2515 |
|
| 2516 |
- The name of the template itself, and names declared within the
|
|
|
|
| 2521 |
A name used in a template declaration or definition and that is
|
| 2522 |
dependent on a *template-parameter* is assumed not to name a type unless
|
| 2523 |
the applicable name lookup finds a type name or the name is qualified by
|
| 2524 |
the keyword `typename`.
|
| 2525 |
|
| 2526 |
+
[*Example 1*:
|
| 2527 |
+
|
| 2528 |
``` cpp
|
| 2529 |
// no B declared here
|
| 2530 |
|
| 2531 |
class X;
|
| 2532 |
|
|
|
|
| 2540 |
Z* a4; // declare pointer to Z
|
| 2541 |
typedef typename T::A TA;
|
| 2542 |
TA* a5; // declare pointer to T's A
|
| 2543 |
typename T::A* a6; // declare pointer to T's A
|
| 2544 |
T::A* a7; // T::A is not a type name:
|
| 2545 |
+
// multiplication of T::A by a7; ill-formed, no visible declaration of a7
|
|
|
|
| 2546 |
B* a8; // B is not a type name:
|
| 2547 |
+
// multiplication of B by a8; ill-formed, no visible declarations of B and a8
|
|
|
|
| 2548 |
}
|
| 2549 |
};
|
| 2550 |
```
|
| 2551 |
|
| 2552 |
+
— *end example*]
|
| 2553 |
+
|
| 2554 |
+
When a *qualified-id* is intended to refer to a type that is not a
|
| 2555 |
+
member of the current instantiation ([[temp.dep.type]]) and its
|
| 2556 |
+
*nested-name-specifier* refers to a dependent type, it shall be prefixed
|
| 2557 |
+
by the keyword `typename`, forming a *typename-specifier*. If the
|
| 2558 |
+
*qualified-id* in a *typename-specifier* does not denote a type or a
|
| 2559 |
+
class template, the program is ill-formed.
|
| 2560 |
|
| 2561 |
``` bnf
|
| 2562 |
typename-specifier:
|
| 2563 |
'typename' nested-name-specifier identifier
|
| 2564 |
'typename' nested-name-specifier 'template'ₒₚₜ simple-template-id
|
| 2565 |
```
|
| 2566 |
|
| 2567 |
If a specialization of a template is instantiated for a set of
|
| 2568 |
*template-argument*s such that the *qualified-id* prefixed by `typename`
|
| 2569 |
+
does not denote a type or a class template, the specialization is
|
| 2570 |
+
ill-formed. The usual qualified name lookup ([[basic.lookup.qual]]) is
|
| 2571 |
+
used to find the *qualified-id* even in the presence of `typename`.
|
| 2572 |
+
|
| 2573 |
+
[*Example 2*:
|
| 2574 |
|
| 2575 |
``` cpp
|
| 2576 |
struct A {
|
| 2577 |
struct X { };
|
| 2578 |
int X;
|
|
|
|
| 2589 |
f(b); // OK: T::X refers to B::X
|
| 2590 |
f(a); // error: T::X refers to the data member A::X not the struct A::X
|
| 2591 |
}
|
| 2592 |
```
|
| 2593 |
|
| 2594 |
+
— *end example*]
|
| 2595 |
+
|
| 2596 |
+
A qualified name used as the name in a *class-or-decltype* (Clause
|
| 2597 |
+
[[class.derived]]) or an *elaborated-type-specifier* is implicitly
|
| 2598 |
assumed to name a type, without the use of the `typename` keyword. In a
|
| 2599 |
*nested-name-specifier* that immediately contains a
|
| 2600 |
*nested-name-specifier* that depends on a template parameter, the
|
| 2601 |
*identifier* or *simple-template-id* is implicitly assumed to name a
|
| 2602 |
+
type, without the use of the `typename` keyword.
|
| 2603 |
+
|
| 2604 |
+
[*Note 1*: The `typename` keyword is not permitted by the syntax of
|
| 2605 |
+
these constructs. — *end note*]
|
| 2606 |
|
| 2607 |
If, for a given set of template arguments, a specialization of a
|
| 2608 |
template is instantiated that refers to a *qualified-id* that denotes a
|
| 2609 |
+
type or a class template, and the *qualified-id* refers to a member of
|
| 2610 |
+
an unknown specialization, the *qualified-id* shall either be prefixed
|
| 2611 |
+
by `typename` or shall be used in a context in which it implicitly names
|
| 2612 |
+
a type as described above.
|
| 2613 |
+
|
| 2614 |
+
[*Example 3*:
|
| 2615 |
|
| 2616 |
``` cpp
|
| 2617 |
template <class T> void f(int i) {
|
| 2618 |
T::x * i; // T::x must not be a type
|
| 2619 |
}
|
|
|
|
| 2630 |
f<Bar>(1); // OK
|
| 2631 |
f<Foo>(1); // error: Foo::x is a type
|
| 2632 |
}
|
| 2633 |
```
|
| 2634 |
|
| 2635 |
+
— *end example*]
|
| 2636 |
+
|
| 2637 |
Within the definition of a class template or within the definition of a
|
| 2638 |
member of a class template following the *declarator-id*, the keyword
|
| 2639 |
`typename` is not required when referring to the name of a previously
|
| 2640 |
+
declared member of the class template that declares a type or a class
|
| 2641 |
+
template.
|
| 2642 |
+
|
| 2643 |
+
[*Note 2*: Such names can be found using unqualified name lookup (
|
| 2644 |
+
[[basic.lookup.unqual]]), class member lookup ([[class.qual]]) into the
|
| 2645 |
+
current instantiation ([[temp.dep.type]]), or class member access
|
| 2646 |
+
expression lookup ([[basic.lookup.classref]]) when the type of the
|
| 2647 |
+
object expression is the current instantiation (
|
| 2648 |
+
[[temp.dep.expr]]). — *end note*]
|
| 2649 |
+
|
| 2650 |
+
[*Example 4*:
|
| 2651 |
|
| 2652 |
``` cpp
|
| 2653 |
template<class T> struct A {
|
| 2654 |
typedef int B;
|
| 2655 |
B b; // OK, no typename required
|
| 2656 |
};
|
| 2657 |
```
|
| 2658 |
|
| 2659 |
+
— *end example*]
|
| 2660 |
+
|
| 2661 |
Knowing which names are type names allows the syntax of every template
|
| 2662 |
+
to be checked. The program is ill-formed, no diagnostic required, if:
|
| 2663 |
+
|
| 2664 |
+
- no valid specialization can be generated for a template or a
|
| 2665 |
+
substatement of a constexpr if statement ([[stmt.if]]) within a
|
| 2666 |
+
template and the template is not instantiated, or
|
| 2667 |
+
- every valid specialization of a variadic template requires an empty
|
| 2668 |
+
template parameter pack, or
|
| 2669 |
+
- a hypothetical instantiation of a template immediately following its
|
| 2670 |
+
definition would be ill-formed due to a construct that does not depend
|
| 2671 |
+
on a template parameter, or
|
| 2672 |
+
- the interpretation of such a construct in the hypothetical
|
| 2673 |
+
instantiation is different from the interpretation of the
|
| 2674 |
+
corresponding construct in any actual instantiation of the template.
|
| 2675 |
+
\[*Note 3*:
|
| 2676 |
+
This can happen in situations including the following:
|
| 2677 |
+
- a type used in a non-dependent name is incomplete at the point at
|
| 2678 |
+
which a template is defined but is complete at the point at which an
|
| 2679 |
+
instantiation is performed, or
|
| 2680 |
+
- lookup for a name in the template definition found a
|
| 2681 |
+
*using-declaration*, but the lookup in the corresponding scope in
|
| 2682 |
+
the instantiation does not find any declarations because the
|
| 2683 |
+
*using-declaration* was a pack expansion and the corresponding pack
|
| 2684 |
+
is empty, or
|
| 2685 |
+
- an instantiation uses a default argument or default template
|
| 2686 |
+
argument that had not been defined at the point at which the
|
| 2687 |
+
template was defined, or
|
| 2688 |
+
- constant expression evaluation ([[expr.const]]) within the template
|
| 2689 |
+
instantiation uses
|
| 2690 |
+
- the value of a `const` object of integral or unscoped enumeration
|
| 2691 |
+
type or
|
| 2692 |
+
- the value of a `constexpr` object or
|
| 2693 |
+
- the value of a reference or
|
| 2694 |
+
- the definition of a constexpr function,
|
| 2695 |
+
|
| 2696 |
+
and that entity was not defined when the template was defined, or
|
| 2697 |
+
- a class template specialization or variable template specialization
|
| 2698 |
+
that is specified by a non-dependent *simple-template-id* is used by
|
| 2699 |
+
the template, and either it is instantiated from a partial
|
| 2700 |
+
specialization that was not defined when the template was defined or
|
| 2701 |
+
it names an explicit specialization that was not declared when the
|
| 2702 |
+
template was defined.
|
| 2703 |
+
|
| 2704 |
+
— *end note*]
|
| 2705 |
+
|
| 2706 |
+
Otherwise, no diagnostic shall be issued for a template for which a
|
| 2707 |
+
valid specialization can be generated.
|
| 2708 |
+
|
| 2709 |
+
[*Note 4*: If a template is instantiated, errors will be diagnosed
|
| 2710 |
+
according to the other rules in this International Standard. Exactly
|
| 2711 |
+
when these errors are diagnosed is a quality of implementation
|
| 2712 |
+
issue. — *end note*]
|
| 2713 |
+
|
| 2714 |
+
[*Example 5*:
|
| 2715 |
|
| 2716 |
``` cpp
|
| 2717 |
int j;
|
| 2718 |
template<class T> class X {
|
| 2719 |
void f(T t, int i, char* p) {
|
| 2720 |
+
t = i; // diagnosed if X::f is instantiated, and the assignment to t is an error
|
| 2721 |
+
p = i; // may be diagnosed even if X::f is not instantiated
|
| 2722 |
+
p = j; // may be diagnosed even if X::f is not instantiated
|
|
|
|
|
|
|
|
|
|
| 2723 |
}
|
| 2724 |
void g(T t) {
|
| 2725 |
+
+; // may be diagnosed even if X::g is not instantiated
|
|
|
|
| 2726 |
}
|
| 2727 |
};
|
| 2728 |
|
| 2729 |
template<class... T> struct A {
|
| 2730 |
void operator++(int, T... t); // error: too many parameters
|
| 2731 |
};
|
| 2732 |
template<class... T> union X : T... { }; // error: union with base class
|
| 2733 |
template<class... T> struct A : T..., T... { }; // error: duplicate base class
|
| 2734 |
```
|
| 2735 |
|
| 2736 |
+
— *end example*]
|
| 2737 |
+
|
| 2738 |
When looking for the declaration of a name used in a template
|
| 2739 |
definition, the usual lookup rules ([[basic.lookup.unqual]],
|
| 2740 |
[[basic.lookup.argdep]]) are used for non-dependent names. The lookup of
|
| 2741 |
names dependent on the template parameters is postponed until the actual
|
| 2742 |
template argument is known ([[temp.dep]]).
|
| 2743 |
|
| 2744 |
+
[*Example 6*:
|
| 2745 |
+
|
| 2746 |
``` cpp
|
| 2747 |
#include <iostream>
|
| 2748 |
using namespace std;
|
| 2749 |
|
| 2750 |
template<class T> class Set {
|
|
|
|
| 2768 |
name `operator<<` is known within the definition of `printall()` and a
|
| 2769 |
declaration of it can be found in `<iostream>`, the actual declaration
|
| 2770 |
of `operator<<` needed to print `p[i]` cannot be known until it is known
|
| 2771 |
what type `T` is ([[temp.dep]]).
|
| 2772 |
|
| 2773 |
+
— *end example*]
|
| 2774 |
+
|
| 2775 |
If a name does not depend on a *template-parameter* (as defined in
|
| 2776 |
[[temp.dep]]), a declaration (or set of declarations) for that name
|
| 2777 |
shall be in scope at the point where the name appears in the template
|
| 2778 |
definition; the name is bound to the declaration (or declarations) found
|
| 2779 |
at that point and this binding is not affected by declarations that are
|
| 2780 |
visible at the point of instantiation.
|
| 2781 |
|
| 2782 |
+
[*Example 7*:
|
| 2783 |
+
|
| 2784 |
``` cpp
|
| 2785 |
void f(char);
|
| 2786 |
|
| 2787 |
template<class T> void g(T t) {
|
| 2788 |
f(1); // f(char)
|
| 2789 |
f(T(1)); // dependent
|
| 2790 |
f(t); // dependent
|
| 2791 |
+
dd++; // not dependent; error: declaration for dd not found
|
|
|
|
| 2792 |
}
|
| 2793 |
|
| 2794 |
enum E { e };
|
| 2795 |
void f(E);
|
| 2796 |
|
| 2797 |
double dd;
|
| 2798 |
void h() {
|
| 2799 |
+
g(e); // will cause one call of f(char) followed by two calls of f(E)
|
|
|
|
| 2800 |
g('a'); // will cause three calls of f(char)
|
| 2801 |
}
|
| 2802 |
```
|
| 2803 |
|
| 2804 |
+
— *end example*]
|
| 2805 |
+
|
| 2806 |
+
[*Note 5*: For purposes of name lookup, default arguments and
|
| 2807 |
+
*noexcept-specifier*s of function templates and default arguments and
|
| 2808 |
+
*noexcept-specifier*s of member functions of class templates are
|
| 2809 |
+
considered definitions ([[temp.decls]]). — *end note*]
|
| 2810 |
|
| 2811 |
### Locally declared names <a id="temp.local">[[temp.local]]</a>
|
| 2812 |
|
| 2813 |
Like normal (non-template) classes, class templates have an
|
| 2814 |
injected-class-name (Clause [[class]]). The injected-class-name can be
|
|
|
|
| 2824 |
specialization, when the injected-class-name is used as a *type-name*,
|
| 2825 |
it is equivalent to the *template-name* followed by the
|
| 2826 |
*template-argument*s of the class template specialization or partial
|
| 2827 |
specialization enclosed in `<>`.
|
| 2828 |
|
| 2829 |
+
[*Example 1*:
|
| 2830 |
+
|
| 2831 |
``` cpp
|
| 2832 |
template<template<class> class T> class A { };
|
| 2833 |
template<class T> class Y;
|
| 2834 |
template<> class Y<int> {
|
| 2835 |
Y* p; // meaning Y<int>
|
|
|
|
| 2839 |
template<class> friend class Y; // meaning ::Y
|
| 2840 |
};
|
| 2841 |
};
|
| 2842 |
```
|
| 2843 |
|
| 2844 |
+
— *end example*]
|
| 2845 |
+
|
| 2846 |
The injected-class-name of a class template or class template
|
| 2847 |
specialization can be used either as a *template-name* or a *type-name*
|
| 2848 |
wherever it is in scope.
|
| 2849 |
|
| 2850 |
+
[*Example 2*:
|
| 2851 |
+
|
| 2852 |
``` cpp
|
| 2853 |
template <class T> struct Base {
|
| 2854 |
Base* p;
|
| 2855 |
};
|
| 2856 |
|
|
|
|
| 2860 |
|
| 2861 |
template<class T, template<class> class U = T::template Base> struct Third { };
|
| 2862 |
Third<Base<int> > t; // OK: default argument uses injected-class-name as a template
|
| 2863 |
```
|
| 2864 |
|
| 2865 |
+
— *end example*]
|
| 2866 |
+
|
| 2867 |
A lookup that finds an injected-class-name ([[class.member.lookup]])
|
| 2868 |
can result in an ambiguity in certain cases (for example, if it is found
|
| 2869 |
in more than one base class). If all of the injected-class-names that
|
| 2870 |
are found refer to specializations of the same class template, and if
|
| 2871 |
the name is used as a *template-name*, the reference refers to the class
|
| 2872 |
template itself and not a specialization thereof, and is not ambiguous.
|
| 2873 |
|
| 2874 |
+
[*Example 3*:
|
| 2875 |
+
|
| 2876 |
``` cpp
|
| 2877 |
template <class T> struct Base { };
|
| 2878 |
template <class T> struct Derived: Base<int>, Base<char> {
|
| 2879 |
typename Derived::Base b; // error: ambiguous
|
| 2880 |
typename Derived::Base<double> d; // OK
|
| 2881 |
};
|
| 2882 |
```
|
| 2883 |
|
| 2884 |
+
— *end example*]
|
| 2885 |
+
|
| 2886 |
When the normal name of the template (i.e., the name from the enclosing
|
| 2887 |
scope, not the injected-class-name) is used, it always refers to the
|
| 2888 |
class template itself and not a specialization of the template.
|
| 2889 |
|
| 2890 |
+
[*Example 4*:
|
| 2891 |
+
|
| 2892 |
``` cpp
|
| 2893 |
template<class T> class X {
|
| 2894 |
X* p; // meaning X<T>
|
| 2895 |
X<T>* p2;
|
| 2896 |
X<int>* p3;
|
| 2897 |
::X* p4; // error: missing template argument list
|
| 2898 |
// ::X does not refer to the injected-class-name
|
| 2899 |
};
|
| 2900 |
```
|
| 2901 |
|
| 2902 |
+
— *end example*]
|
| 2903 |
+
|
| 2904 |
A *template-parameter* shall not be redeclared within its scope
|
| 2905 |
(including nested scopes). A *template-parameter* shall not have the
|
| 2906 |
same name as the template name.
|
| 2907 |
|
| 2908 |
+
[*Example 5*:
|
| 2909 |
+
|
| 2910 |
``` cpp
|
| 2911 |
template<class T, int i> class Y {
|
| 2912 |
int T; // error: template-parameter redeclared
|
| 2913 |
void f() {
|
| 2914 |
char T; // error: template-parameter redeclared
|
|
|
|
| 2916 |
};
|
| 2917 |
|
| 2918 |
template<class X> class X; // error: template-parameter redeclared
|
| 2919 |
```
|
| 2920 |
|
| 2921 |
+
— *end example*]
|
| 2922 |
+
|
| 2923 |
In the definition of a member of a class template that appears outside
|
| 2924 |
of the class template definition, the name of a member of the class
|
| 2925 |
template hides the name of a *template-parameter* of any enclosing class
|
| 2926 |
templates (but not a *template-parameter* of the member if the member is
|
| 2927 |
a class or function template).
|
| 2928 |
|
| 2929 |
+
[*Example 6*:
|
| 2930 |
+
|
| 2931 |
``` cpp
|
| 2932 |
template<class T> struct A {
|
| 2933 |
+
struct B { ... };
|
| 2934 |
typedef void C;
|
| 2935 |
void f();
|
| 2936 |
template<class U> void g(U);
|
| 2937 |
};
|
| 2938 |
|
|
|
|
| 2944 |
B b; // A's B, not the template parameter
|
| 2945 |
C c; // the template parameter C, not A's C
|
| 2946 |
}
|
| 2947 |
```
|
| 2948 |
|
| 2949 |
+
— *end example*]
|
| 2950 |
+
|
| 2951 |
In the definition of a member of a class template that appears outside
|
| 2952 |
of the namespace containing the class template definition, the name of a
|
| 2953 |
*template-parameter* hides the name of a member of this namespace.
|
| 2954 |
|
| 2955 |
+
[*Example 7*:
|
| 2956 |
+
|
| 2957 |
``` cpp
|
| 2958 |
namespace N {
|
| 2959 |
class C { };
|
| 2960 |
template<class T> class B {
|
| 2961 |
void f(T);
|
|
|
|
| 2964 |
template<class C> void N::B<C>::f(C) {
|
| 2965 |
C b; // C is the template parameter, not N::C
|
| 2966 |
}
|
| 2967 |
```
|
| 2968 |
|
| 2969 |
+
— *end example*]
|
| 2970 |
+
|
| 2971 |
In the definition of a class template or in the definition of a member
|
| 2972 |
of such a template that appears outside of the template definition, for
|
| 2973 |
+
each non-dependent base class ([[temp.dep.type]]), if the name of the
|
| 2974 |
+
base class or the name of a member of the base class is the same as the
|
| 2975 |
+
name of a *template-parameter*, the base class name or member name hides
|
| 2976 |
+
the *template-parameter* name ([[basic.scope.hiding]]).
|
| 2977 |
+
|
| 2978 |
+
[*Example 8*:
|
| 2979 |
|
| 2980 |
``` cpp
|
| 2981 |
struct A {
|
| 2982 |
+
struct B { ... };
|
| 2983 |
int a;
|
| 2984 |
int Y;
|
| 2985 |
};
|
| 2986 |
|
| 2987 |
template<class B, class a> struct X : A {
|
| 2988 |
B b; // A's B
|
| 2989 |
a b; // error: A's a isn't a type name
|
| 2990 |
};
|
| 2991 |
```
|
| 2992 |
|
| 2993 |
+
— *end example*]
|
| 2994 |
+
|
| 2995 |
### Dependent names <a id="temp.dep">[[temp.dep]]</a>
|
| 2996 |
|
| 2997 |
Inside a template, some constructs have semantics which may differ from
|
| 2998 |
one instantiation to another. Such a construct *depends* on the template
|
| 2999 |
parameters. In particular, types and expressions may depend on the type
|
| 3000 |
and/or value of template parameters (as determined by the template
|
| 3001 |
arguments) and this determines the context for name lookup for certain
|
| 3002 |
+
names. An expressions may be *type-dependent* (that is, its type may
|
| 3003 |
+
depend on a template parameter) or *value-dependent* (that is, its value
|
| 3004 |
+
when evaluated as a constant expression ([[expr.const]]) may depend on
|
| 3005 |
+
a template parameter) as described in this subclause. In an expression
|
| 3006 |
+
of the form:
|
| 3007 |
|
| 3008 |
where the *postfix-expression* is an *unqualified-id*, the
|
| 3009 |
*unqualified-id* denotes a *dependent name* if
|
| 3010 |
|
| 3011 |
- any of the expressions in the *expression-list* is a pack expansion (
|
| 3012 |
[[temp.variadic]]),
|
| 3013 |
+
- any of the expressions or *braced-init-list*s in the *expression-list*
|
| 3014 |
+
is type-dependent ([[temp.dep.expr]]), or
|
| 3015 |
+
- the *unqualified-id* is a *template-id* in which any of the template
|
| 3016 |
+
arguments depends on a template parameter.
|
| 3017 |
|
| 3018 |
If an operand of an operator is a type-dependent expression, the
|
| 3019 |
operator also denotes a dependent name. Such names are unbound and are
|
| 3020 |
looked up at the point of the template instantiation ([[temp.point]])
|
| 3021 |
in both the context of the template definition and the context of the
|
| 3022 |
point of instantiation.
|
| 3023 |
|
| 3024 |
+
[*Example 1*:
|
| 3025 |
+
|
| 3026 |
``` cpp
|
| 3027 |
template<class T> struct X : B<T> {
|
| 3028 |
typename T::A* pa;
|
| 3029 |
void f(B<T>* pb) {
|
| 3030 |
static int i = B<T>::i;
|
|
|
|
| 3034 |
```
|
| 3035 |
|
| 3036 |
the base class name `B<T>`, the type name `T::A`, the names `B<T>::i`
|
| 3037 |
and `pb->j` explicitly depend on the *template-parameter*.
|
| 3038 |
|
| 3039 |
+
— *end example*]
|
| 3040 |
+
|
| 3041 |
+
In the definition of a class or class template, the scope of a dependent
|
| 3042 |
+
base class ([[temp.dep.type]]) is not examined during unqualified name
|
| 3043 |
+
lookup either at the point of definition of the class template or member
|
| 3044 |
+
or during an instantiation of the class template or member.
|
| 3045 |
+
|
| 3046 |
+
[*Example 2*:
|
| 3047 |
|
| 3048 |
``` cpp
|
| 3049 |
typedef double A;
|
| 3050 |
template<class T> class B {
|
| 3051 |
typedef int A;
|
|
|
|
| 3057 |
|
| 3058 |
The type name `A` in the definition of `X<T>` binds to the typedef name
|
| 3059 |
defined in the global namespace scope, not to the typedef name defined
|
| 3060 |
in the base class `B<T>`.
|
| 3061 |
|
| 3062 |
+
— *end example*]
|
| 3063 |
+
|
| 3064 |
+
[*Example 3*:
|
| 3065 |
+
|
| 3066 |
``` cpp
|
| 3067 |
struct A {
|
| 3068 |
+
struct B { ... };
|
| 3069 |
int a;
|
| 3070 |
int Y;
|
| 3071 |
};
|
| 3072 |
|
| 3073 |
int a;
|
| 3074 |
|
| 3075 |
template<class T> struct Y : T {
|
| 3076 |
+
struct B { ... };
|
| 3077 |
B b; // The B defined in Y
|
| 3078 |
void f(int i) { a = i; } // ::a
|
| 3079 |
Y* p; // Y<T>
|
| 3080 |
};
|
| 3081 |
|
|
|
|
| 3083 |
```
|
| 3084 |
|
| 3085 |
The members `A::B`, `A::a`, and `A::Y` of the template argument `A` do
|
| 3086 |
not affect the binding of names in `Y<A>`.
|
| 3087 |
|
| 3088 |
+
— *end example*]
|
| 3089 |
+
|
| 3090 |
#### Dependent types <a id="temp.dep.type">[[temp.dep.type]]</a>
|
| 3091 |
|
| 3092 |
A name refers to the *current instantiation* if it is
|
| 3093 |
|
| 3094 |
- in the definition of a class template, a nested class of a class
|
|
|
|
| 3123 |
current instantiation. In the case of a non-type template argument, the
|
| 3124 |
argument must have been given the value of the template parameter and
|
| 3125 |
not an expression in which the template parameter appears as a
|
| 3126 |
subexpression.
|
| 3127 |
|
| 3128 |
+
[*Example 1*:
|
| 3129 |
+
|
| 3130 |
``` cpp
|
| 3131 |
template <class T> class A {
|
| 3132 |
A* p1; // A is the current instantiation
|
| 3133 |
A<T>* p2; // A<T> is the current instantiation
|
| 3134 |
A<T*> p3; // A<T*> is not the current instantiation
|
| 3135 |
::A<T>* p4; // ::A<T> is the current instantiation
|
| 3136 |
class B {
|
| 3137 |
B* p1; // B is the current instantiation
|
| 3138 |
A<T>::B* p2; // A<T>::B is the current instantiation
|
| 3139 |
+
typename A<T*>::B* p3; // A<T*>::B is not the current instantiation
|
|
|
|
| 3140 |
};
|
| 3141 |
};
|
| 3142 |
|
| 3143 |
template <class T> class A<T*> {
|
| 3144 |
A<T*>* p1; // A<T*> is the current instantiation
|
|
|
|
| 3156 |
B<my_T1, T2, my_I2>* b4; // not the current instantiation
|
| 3157 |
B<my_T1, T2, my_I3>* b5; // refers to the current instantiation
|
| 3158 |
};
|
| 3159 |
```
|
| 3160 |
|
| 3161 |
+
— *end example*]
|
| 3162 |
+
|
| 3163 |
+
A *dependent base class* is a base class that is a dependent type and is
|
| 3164 |
+
not the current instantiation.
|
| 3165 |
+
|
| 3166 |
+
[*Note 1*:
|
| 3167 |
+
|
| 3168 |
+
A base class can be the current instantiation in the case of a nested
|
| 3169 |
+
class naming an enclosing class as a base.
|
| 3170 |
+
|
| 3171 |
+
[*Example 2*:
|
| 3172 |
+
|
| 3173 |
+
``` cpp
|
| 3174 |
+
template<class T> struct A {
|
| 3175 |
+
typedef int M;
|
| 3176 |
+
struct B {
|
| 3177 |
+
typedef void M;
|
| 3178 |
+
struct C;
|
| 3179 |
+
};
|
| 3180 |
+
};
|
| 3181 |
+
|
| 3182 |
+
template<class T> struct A<T>::B::C : A<T> {
|
| 3183 |
+
M m; // OK, A<T>::M
|
| 3184 |
+
};
|
| 3185 |
+
```
|
| 3186 |
+
|
| 3187 |
+
— *end example*]
|
| 3188 |
+
|
| 3189 |
+
— *end note*]
|
| 3190 |
+
|
| 3191 |
A name is a *member of the current instantiation* if it is
|
| 3192 |
|
| 3193 |
- An unqualified name that, when looked up, refers to at least one
|
| 3194 |
member of a class that is the current instantiation or a non-dependent
|
| 3195 |
+
base class thereof. \[*Note 2*: This can only occur when looking up a
|
| 3196 |
+
name in a scope enclosed by the definition of a class
|
| 3197 |
+
template. — *end note*]
|
| 3198 |
- A *qualified-id* in which the *nested-name-specifier* refers to the
|
| 3199 |
current instantiation and that, when looked up, refers to at least one
|
| 3200 |
member of a class that is the current instantiation or a non-dependent
|
| 3201 |
+
base class thereof. \[*Note 3*: If no such member is found, and the
|
| 3202 |
+
current instantiation has any dependent base classes, then the
|
| 3203 |
+
*qualified-id* is a member of an unknown specialization; see
|
| 3204 |
+
below. — *end note*]
|
| 3205 |
- An *id-expression* denoting the member in a class member access
|
| 3206 |
expression ([[expr.ref]]) for which the type of the object expression
|
| 3207 |
is the current instantiation, and the *id-expression*, when looked
|
| 3208 |
up ([[basic.lookup.classref]]), refers to at least one member of a
|
| 3209 |
class that is the current instantiation or a non-dependent base class
|
| 3210 |
+
thereof. \[*Note 4*: If no such member is found, and the current
|
| 3211 |
+
instantiation has any dependent base classes, then the *id-expression*
|
| 3212 |
+
is a member of an unknown specialization; see below. — *end note*]
|
| 3213 |
+
|
| 3214 |
+
[*Example 3*:
|
| 3215 |
|
| 3216 |
``` cpp
|
| 3217 |
template <class T> class A {
|
| 3218 |
static const int i = 5;
|
| 3219 |
int n1[i]; // i refers to a member of the current instantiation
|
|
|
|
| 3225 |
template <class T> int A<T>::f() {
|
| 3226 |
return i; // i refers to a member of the current instantiation
|
| 3227 |
}
|
| 3228 |
```
|
| 3229 |
|
| 3230 |
+
— *end example*]
|
| 3231 |
+
|
| 3232 |
A name is a *dependent member of the current instantiation* if it is a
|
| 3233 |
member of the current instantiation that, when looked up, refers to at
|
| 3234 |
least one member of a class that is the current instantiation.
|
| 3235 |
|
| 3236 |
A name is a *member of an unknown specialization* if it is
|
|
|
|
| 3261 |
current instantiation does not refer to a member of the current
|
| 3262 |
instantiation or a member of an unknown specialization, the program is
|
| 3263 |
ill-formed even if the template containing the member access expression
|
| 3264 |
is not instantiated; no diagnostic required.
|
| 3265 |
|
| 3266 |
+
[*Example 4*:
|
| 3267 |
+
|
| 3268 |
``` cpp
|
| 3269 |
template<class T> class A {
|
| 3270 |
typedef int type;
|
| 3271 |
void f() {
|
| 3272 |
A<T>::type i; // OK: refers to a member of the current instantiation
|
|
|
|
| 3274 |
// a member of an unknown specialization
|
| 3275 |
}
|
| 3276 |
};
|
| 3277 |
```
|
| 3278 |
|
| 3279 |
+
— *end example*]
|
| 3280 |
+
|
| 3281 |
If, for a given set of template arguments, a specialization of a
|
| 3282 |
template is instantiated that refers to a member of the current
|
| 3283 |
instantiation with a *qualified-id* or class member access expression,
|
| 3284 |
the name in the *qualified-id* or class member access expression is
|
| 3285 |
looked up in the template instantiation context. If the result of this
|
| 3286 |
lookup differs from the result of name lookup in the template definition
|
| 3287 |
+
context, name lookup is ambiguous.
|
| 3288 |
+
|
| 3289 |
+
[*Example 5*:
|
| 3290 |
+
|
| 3291 |
+
``` cpp
|
| 3292 |
+
struct A {
|
| 3293 |
+
int m;
|
| 3294 |
+
};
|
| 3295 |
+
|
| 3296 |
+
struct B {
|
| 3297 |
+
int m;
|
| 3298 |
+
};
|
| 3299 |
+
|
| 3300 |
+
template<typename T>
|
| 3301 |
+
struct C : A, T {
|
| 3302 |
+
int f() { return this->m; } // finds A::m in the template definition context
|
| 3303 |
+
int g() { return m; } // finds A::m in the template definition context
|
| 3304 |
+
};
|
| 3305 |
+
|
| 3306 |
+
template int C<B>::f(); // error: finds both A::m and B::m
|
| 3307 |
+
template int C<B>::g(); // OK: transformation to class member access syntax
|
| 3308 |
+
// does not occur in the template definition context; see~[class.mfct.non-static]
|
| 3309 |
+
```
|
| 3310 |
+
|
| 3311 |
+
— *end example*]
|
| 3312 |
|
| 3313 |
A type is dependent if it is
|
| 3314 |
|
| 3315 |
- a template parameter,
|
| 3316 |
- a member of an unknown specialization,
|
| 3317 |
- a nested class or enumeration that is a dependent member of the
|
| 3318 |
current instantiation,
|
| 3319 |
- a cv-qualified type where the cv-unqualified type is dependent,
|
| 3320 |
- a compound type constructed from any dependent type,
|
| 3321 |
+
- an array type whose element type is dependent or whose bound (if any)
|
| 3322 |
+
is value-dependent,
|
| 3323 |
+
- a function type whose exception specification is value-dependent,
|
| 3324 |
- a *simple-template-id* in which either the template name is a template
|
| 3325 |
parameter or any of the template arguments is a dependent type or an
|
| 3326 |
+
expression that is type-dependent or value-dependent or is a pack
|
| 3327 |
+
expansion \[*Note 5*: This includes an injected-class-name (Clause
|
| 3328 |
+
[[class]]) of a class template used without a
|
| 3329 |
+
*template-argument-list*. — *end note*] , or
|
| 3330 |
- denoted by `decltype(`*expression*`)`, where *expression* is
|
| 3331 |
type-dependent ([[temp.dep.expr]]).
|
| 3332 |
|
| 3333 |
+
[*Note 6*: Because typedefs do not introduce new types, but instead
|
| 3334 |
+
simply refer to other types, a name that refers to a typedef that is a
|
| 3335 |
+
member of the current instantiation is dependent only if the type
|
| 3336 |
+
referred to is dependent. — *end note*]
|
| 3337 |
|
| 3338 |
#### Type-dependent expressions <a id="temp.dep.expr">[[temp.dep.expr]]</a>
|
| 3339 |
|
| 3340 |
Except as described below, an expression is type-dependent if any
|
| 3341 |
subexpression is type-dependent.
|
|
|
|
| 3347 |
|
| 3348 |
An *id-expression* is type-dependent if it contains
|
| 3349 |
|
| 3350 |
- an *identifier* associated by name lookup with one or more
|
| 3351 |
declarations declared with a dependent type,
|
| 3352 |
+
- an *identifier* associated by name lookup with a non-type
|
| 3353 |
+
*template-parameter* declared with a type that contains a placeholder
|
| 3354 |
+
type ([[dcl.spec.auto]]),
|
| 3355 |
- an *identifier* associated by name lookup with one or more
|
| 3356 |
declarations of member functions of the current instantiation declared
|
| 3357 |
+
with a return type that contains a placeholder type,
|
| 3358 |
+
- an *identifier* associated by name lookup with a structured binding
|
| 3359 |
+
declaration ([[dcl.struct.bind]]) whose *brace-or-equal-initializer*
|
| 3360 |
+
is type-dependent,
|
| 3361 |
+
- the *identifier* `__func__` ([[dcl.fct.def.general]]), where any
|
| 3362 |
+
enclosing function is a template, a member of a class template, or a
|
| 3363 |
+
generic lambda,
|
| 3364 |
- a *template-id* that is dependent,
|
| 3365 |
- a *conversion-function-id* that specifies a dependent type, or
|
| 3366 |
- a *nested-name-specifier* or a *qualified-id* that names a member of
|
| 3367 |
an unknown specialization;
|
| 3368 |
|
|
|
|
| 3374 |
subexpression is type-dependent:
|
| 3375 |
|
| 3376 |
Expressions of the following forms are never type-dependent (because the
|
| 3377 |
type of the expression cannot be dependent):
|
| 3378 |
|
| 3379 |
+
[*Note 1*: For the standard library macro `offsetof`, see
|
| 3380 |
+
[[support.types]]. — *end note*]
|
| 3381 |
|
| 3382 |
A class member access expression ([[expr.ref]]) is type-dependent if
|
| 3383 |
the expression refers to a member of the current instantiation and the
|
| 3384 |
type of the referenced member is dependent, or the class member access
|
| 3385 |
+
expression refers to a member of an unknown specialization.
|
| 3386 |
+
|
| 3387 |
+
[*Note 2*: In an expression of the form `x.y` or `xp->y` the type of
|
| 3388 |
+
the expression is usually the type of the member `y` of the class of `x`
|
| 3389 |
+
(or the class pointed to by `xp`). However, if `x` or `xp` refers to a
|
| 3390 |
+
dependent type that is not the current instantiation, the type of `y` is
|
| 3391 |
+
always dependent. If `x` or `xp` refers to a non-dependent type or
|
| 3392 |
+
refers to the current instantiation, the type of `y` is the type of the
|
| 3393 |
+
class member access expression. — *end note*]
|
| 3394 |
+
|
| 3395 |
+
A *braced-init-list* is type-dependent if any element is type-dependent
|
| 3396 |
+
or is a pack expansion.
|
| 3397 |
+
|
| 3398 |
+
A *fold-expression* is type-dependent.
|
| 3399 |
|
| 3400 |
#### Value-dependent expressions <a id="temp.dep.constexpr">[[temp.dep.constexpr]]</a>
|
| 3401 |
|
| 3402 |
+
Except as described below, an expression used in a context where a
|
| 3403 |
+
constant expression is required is value-dependent if any subexpression
|
| 3404 |
+
is value-dependent.
|
| 3405 |
|
| 3406 |
An *id-expression* is value-dependent if:
|
| 3407 |
|
| 3408 |
+
- it is type-dependent,
|
| 3409 |
- it is the name of a non-type template parameter,
|
|
|
|
| 3410 |
- it names a static data member that is a dependent member of the
|
| 3411 |
current instantiation and is not initialized in a *member-declarator*,
|
| 3412 |
- it names a static member function that is a dependent member of the
|
| 3413 |
current instantiation, or
|
| 3414 |
- it is a constant with literal type and is initialized with an
|
|
|
|
| 3416 |
|
| 3417 |
Expressions of the following form are value-dependent if the
|
| 3418 |
*unary-expression* or *expression* is type-dependent or the *type-id* is
|
| 3419 |
dependent:
|
| 3420 |
|
| 3421 |
+
[*Note 1*: For the standard library macro `offsetof`, see
|
| 3422 |
+
[[support.types]]. — *end note*]
|
| 3423 |
|
| 3424 |
Expressions of the following form are value-dependent if either the
|
| 3425 |
*type-id* or *simple-type-specifier* is dependent or the *expression* or
|
| 3426 |
*cast-expression* is value-dependent:
|
| 3427 |
|
| 3428 |
Expressions of the following form are value-dependent:
|
| 3429 |
|
| 3430 |
An expression of the form `&`*qualified-id* where the *qualified-id*
|
| 3431 |
names a dependent member of the current instantiation is
|
| 3432 |
+
value-dependent. An expression of the form `&`*cast-expression* is also
|
| 3433 |
+
value-dependent if evaluating *cast-expression* as a core constant
|
| 3434 |
+
expression ([[expr.const]]) succeeds and the result of the evaluation
|
| 3435 |
+
refers to a templated entity that is an object with static or thread
|
| 3436 |
+
storage duration or a member function.
|
| 3437 |
|
| 3438 |
#### Dependent template arguments <a id="temp.dep.temp">[[temp.dep.temp]]</a>
|
| 3439 |
|
| 3440 |
A type *template-argument* is dependent if the type it specifies is
|
| 3441 |
dependent.
|
|
|
|
| 3455 |
### Non-dependent names <a id="temp.nondep">[[temp.nondep]]</a>
|
| 3456 |
|
| 3457 |
Non-dependent names used in a template definition are found using the
|
| 3458 |
usual name lookup and bound at the point they are used.
|
| 3459 |
|
| 3460 |
+
[*Example 1*:
|
| 3461 |
+
|
| 3462 |
``` cpp
|
| 3463 |
void g(double);
|
| 3464 |
void h();
|
| 3465 |
|
| 3466 |
template<class T> class Z {
|
| 3467 |
public:
|
| 3468 |
void f() {
|
| 3469 |
g(1); // calls g(double)
|
| 3470 |
+
h++; // ill-formed: cannot increment function; this could be diagnosed
|
| 3471 |
+
// either here or at the point of instantiation
|
|
|
|
| 3472 |
}
|
| 3473 |
};
|
| 3474 |
|
| 3475 |
+
void g(int); // not in scope at the point of the template definition, not considered for the call g(1)
|
|
|
|
| 3476 |
```
|
| 3477 |
|
| 3478 |
+
— *end example*]
|
| 3479 |
+
|
| 3480 |
### Dependent name resolution <a id="temp.dep.res">[[temp.dep.res]]</a>
|
| 3481 |
|
| 3482 |
In resolving dependent names, names from the following sources are
|
| 3483 |
considered:
|
| 3484 |
|
|
|
|
| 3505 |
in a way which uses the definition of a default argument of that
|
| 3506 |
function template or member function, the point of instantiation of the
|
| 3507 |
default argument is the point of instantiation of the function template
|
| 3508 |
or member function specialization.
|
| 3509 |
|
| 3510 |
+
For a *noexcept-specifier* of a function template specialization or
|
| 3511 |
+
specialization of a member function of a class template, if the
|
| 3512 |
+
*noexcept-specifier* is implicitly instantiated because it is needed by
|
| 3513 |
+
another template specialization and the context that requires it depends
|
| 3514 |
+
on a template parameter, the point of instantiation of the
|
| 3515 |
+
*noexcept-specifier* is the point of instantiation of the specialization
|
| 3516 |
+
that requires it. Otherwise, the point of instantiation for such a
|
| 3517 |
+
*noexcept-specifier* immediately follows the namespace scope declaration
|
| 3518 |
+
or definition that requires the *noexcept-specifier*.
|
|
|
|
| 3519 |
|
| 3520 |
For a class template specialization, a class member template
|
| 3521 |
specialization, or a specialization for a class member of a class
|
| 3522 |
template, if the specialization is implicitly instantiated because it is
|
| 3523 |
referenced from within another template specialization, if the context
|
|
|
|
| 3550 |
unit, the end of the translation unit is also considered a point of
|
| 3551 |
instantiation. A specialization for a class template has at most one
|
| 3552 |
point of instantiation within a translation unit. A specialization for
|
| 3553 |
any template may have points of instantiation in multiple translation
|
| 3554 |
units. If two different points of instantiation give a template
|
| 3555 |
+
specialization different meanings according to the one-definition rule (
|
| 3556 |
[[basic.def.odr]]), the program is ill-formed, no diagnostic required.
|
| 3557 |
|
| 3558 |
#### Candidate functions <a id="temp.dep.candidate">[[temp.dep.candidate]]</a>
|
| 3559 |
|
| 3560 |
For a function call where the *postfix-expression* is a dependent name,
|
|
|
|
| 3587 |
functions of a class template specialization are not visible during an
|
| 3588 |
ordinary lookup unless explicitly declared at namespace scope (
|
| 3589 |
[[class.friend]]). Such names may be found under the rules for
|
| 3590 |
associated classes ([[basic.lookup.argdep]]).[^6]
|
| 3591 |
|
| 3592 |
+
[*Example 1*:
|
| 3593 |
+
|
| 3594 |
``` cpp
|
| 3595 |
template<typename T> struct number {
|
| 3596 |
number(int);
|
| 3597 |
friend number gcd(number x, number y) { return 0; };
|
| 3598 |
};
|
| 3599 |
|
| 3600 |
void g() {
|
| 3601 |
number<double> a(3), b(4);
|
| 3602 |
+
a = gcd(a,b); // finds gcd because number<double> is an associated class,
|
| 3603 |
+
// making gcd visible in its namespace (global scope)
|
|
|
|
| 3604 |
b = gcd(3,4); // ill-formed; gcd is not visible
|
| 3605 |
}
|
| 3606 |
```
|
| 3607 |
|
| 3608 |
+
— *end example*]
|
| 3609 |
+
|
| 3610 |
## Template instantiation and specialization <a id="temp.spec">[[temp.spec]]</a>
|
| 3611 |
|
| 3612 |
The act of instantiating a function, a class, a member of a class
|
| 3613 |
template or a member template is referred to as *template
|
| 3614 |
instantiation*.
|
|
|
|
| 3632 |
explicitly specialized shall be a *simple-template-id*. In the explicit
|
| 3633 |
specialization declaration for a function template or a member function
|
| 3634 |
template, the name of the function or member function explicitly
|
| 3635 |
specialized may be a *template-id*.
|
| 3636 |
|
| 3637 |
+
[*Example 1*:
|
| 3638 |
+
|
| 3639 |
``` cpp
|
| 3640 |
template<class T = int> struct A {
|
| 3641 |
static int x;
|
| 3642 |
};
|
| 3643 |
template<class U> void g(U) { }
|
|
|
|
| 3653 |
static int x;
|
| 3654 |
};
|
| 3655 |
template<> int B<>::x = 1; // specialize for T == int
|
| 3656 |
```
|
| 3657 |
|
| 3658 |
+
— *end example*]
|
| 3659 |
+
|
| 3660 |
An instantiated template specialization can be either implicitly
|
| 3661 |
instantiated ([[temp.inst]]) for a given argument list or be explicitly
|
| 3662 |
instantiated ([[temp.explicit]]). A specialization is a class,
|
| 3663 |
function, or class member that is either instantiated or explicitly
|
| 3664 |
specialized ([[temp.expl.spec]]).
|
|
|
|
| 3676 |
An implementation is not required to diagnose a violation of this rule.
|
| 3677 |
|
| 3678 |
Each class template specialization instantiated from a template has its
|
| 3679 |
own copy of any static members.
|
| 3680 |
|
| 3681 |
+
[*Example 2*:
|
| 3682 |
+
|
| 3683 |
``` cpp
|
| 3684 |
template<class T> class X {
|
| 3685 |
static T s;
|
| 3686 |
};
|
| 3687 |
template<class T> T X<T>::s = 0;
|
|
|
|
| 3692 |
`X<int>`
|
| 3693 |
|
| 3694 |
has a static member `s` of type `int` and `X<char*>` has a static member
|
| 3695 |
`s` of type `char*`.
|
| 3696 |
|
| 3697 |
+
— *end example*]
|
| 3698 |
+
|
| 3699 |
+
If a function declaration acquired its function type through a dependent
|
| 3700 |
+
type ([[temp.dep.type]]) without using the syntactic form of a function
|
| 3701 |
+
declarator, the program is ill-formed.
|
| 3702 |
+
|
| 3703 |
+
[*Example 3*:
|
| 3704 |
+
|
| 3705 |
+
``` cpp
|
| 3706 |
+
template<class T> struct A {
|
| 3707 |
+
static T t;
|
| 3708 |
+
};
|
| 3709 |
+
typedef int function();
|
| 3710 |
+
A<function> a; // ill-formed: would declare A<function>::t as a static member function
|
| 3711 |
+
```
|
| 3712 |
+
|
| 3713 |
+
— *end example*]
|
| 3714 |
+
|
| 3715 |
### Implicit instantiation <a id="temp.inst">[[temp.inst]]</a>
|
| 3716 |
|
| 3717 |
Unless a class template specialization has been explicitly
|
| 3718 |
instantiated ([[temp.explicit]]) or explicitly specialized (
|
| 3719 |
[[temp.expl.spec]]), the class template specialization is implicitly
|
| 3720 |
instantiated when the specialization is referenced in a context that
|
| 3721 |
requires a completely-defined object type or when the completeness of
|
| 3722 |
+
the class type affects the semantics of the program.
|
| 3723 |
+
|
| 3724 |
+
[*Note 1*: In particular, if the semantics of an expression depend on
|
| 3725 |
+
the member or base class lists of a class template specialization, the
|
| 3726 |
+
class template specialization is implicitly generated. For instance,
|
| 3727 |
+
deleting a pointer to class type depends on whether or not the class
|
| 3728 |
+
declares a destructor, and a conversion between pointers to class type
|
| 3729 |
+
depends on the inheritance relationship between the two classes
|
| 3730 |
+
involved. — *end note*]
|
| 3731 |
+
|
| 3732 |
+
[*Example 1*:
|
| 3733 |
+
|
| 3734 |
+
``` cpp
|
| 3735 |
+
template<class T> class B { ... };
|
| 3736 |
+
template<class T> class D : public B<T> { ... };
|
| 3737 |
+
|
| 3738 |
+
void f(void*);
|
| 3739 |
+
void f(B<int>*);
|
| 3740 |
+
|
| 3741 |
+
void g(D<int>* p, D<char>* pp, D<double>* ppp) {
|
| 3742 |
+
f(p); // instantiation of D<int> required: call f(B<int>*)
|
| 3743 |
+
B<char>* q = pp; // instantiation of D<char> required: convert D<char>* to B<char>*
|
| 3744 |
+
delete ppp; // instantiation of D<double> required
|
| 3745 |
+
}
|
| 3746 |
+
```
|
| 3747 |
+
|
| 3748 |
+
— *end example*]
|
| 3749 |
+
|
| 3750 |
+
If a class template has been declared, but not defined, at the point of
|
| 3751 |
+
instantiation ([[temp.point]]), the instantiation yields an incomplete
|
| 3752 |
+
class type ([[basic.types]]).
|
| 3753 |
+
|
| 3754 |
+
[*Example 2*:
|
| 3755 |
+
|
| 3756 |
+
``` cpp
|
| 3757 |
+
template<class T> class X;
|
| 3758 |
+
X<char> ch; // error: incomplete type X<char>
|
| 3759 |
+
```
|
| 3760 |
+
|
| 3761 |
+
— *end example*]
|
| 3762 |
+
|
| 3763 |
+
[*Note 2*: Within a template declaration, a local class (
|
| 3764 |
+
[[class.local]]) or enumeration and the members of a local class are
|
| 3765 |
+
never considered to be entities that can be separately instantiated
|
| 3766 |
+
(this includes their default arguments, *noexcept-specifier*s, and
|
| 3767 |
+
non-static data member initializers, if any). As a result, the dependent
|
| 3768 |
+
names are looked up, the semantic constraints are checked, and any
|
| 3769 |
+
templates used are instantiated as part of the instantiation of the
|
| 3770 |
+
entity within which the local class or enumeration is
|
| 3771 |
+
declared. — *end note*]
|
| 3772 |
+
|
| 3773 |
+
The implicit instantiation of a class template specialization causes the
|
| 3774 |
+
implicit instantiation of the declarations, but not of the definitions,
|
| 3775 |
+
default arguments, or *noexcept-specifier*s of the class member
|
| 3776 |
+
functions, member classes, scoped member enumerations, static data
|
| 3777 |
+
members, member templates, and friends; and it causes the implicit
|
| 3778 |
+
instantiation of the definitions of unscoped member enumerations and
|
| 3779 |
+
member anonymous unions. However, for the purpose of determining whether
|
| 3780 |
+
an instantiated redeclaration is valid according to [[basic.def.odr]]
|
| 3781 |
+
and [[class.mem]], a declaration that corresponds to a definition in the
|
| 3782 |
+
template is considered to be a definition.
|
| 3783 |
+
|
| 3784 |
+
[*Example 3*:
|
| 3785 |
|
| 3786 |
``` cpp
|
| 3787 |
template<class T, class U>
|
| 3788 |
struct Outer {
|
| 3789 |
template<class X, class Y> struct Inner;
|
|
|
|
| 3799 |
defined but noted as being associated with a definition in
|
| 3800 |
`Outer<T, U>`.) \#2 is also a redeclaration of \#1a. It is noted as
|
| 3801 |
associated with a definition, so it is an invalid redeclaration of the
|
| 3802 |
same partial specialization.
|
| 3803 |
|
| 3804 |
+
``` cpp
|
| 3805 |
+
template<typename T> struct Friendly {
|
| 3806 |
+
template<typename U> friend int f(U) { return sizeof(T); }
|
| 3807 |
+
};
|
| 3808 |
+
Friendly<char> fc;
|
| 3809 |
+
Friendly<float> ff; // ill-formed: produces second definition of f(U)
|
| 3810 |
+
```
|
| 3811 |
+
|
| 3812 |
+
— *end example*]
|
| 3813 |
+
|
| 3814 |
Unless a member of a class template or a member template has been
|
| 3815 |
explicitly instantiated or explicitly specialized, the specialization of
|
| 3816 |
the member is implicitly instantiated when the specialization is
|
| 3817 |
referenced in a context that requires the member definition to exist; in
|
| 3818 |
+
particular, the initialization (and any associated side effects) of a
|
| 3819 |
static data member does not occur unless the static data member is
|
| 3820 |
itself used in a way that requires the definition of the static data
|
| 3821 |
member to exist.
|
| 3822 |
|
| 3823 |
Unless a function template specialization has been explicitly
|
| 3824 |
instantiated or explicitly specialized, the function template
|
| 3825 |
specialization is implicitly instantiated when the specialization is
|
| 3826 |
+
referenced in a context that requires a function definition to exist. A
|
| 3827 |
+
function whose declaration was instantiated from a friend function
|
| 3828 |
+
definition is implicitly instantiated when it is referenced in a context
|
| 3829 |
+
that requires a function definition to exist. Unless a call is to a
|
| 3830 |
+
function template explicit specialization or to a member function of an
|
| 3831 |
+
explicitly specialized class template, a default argument for a function
|
| 3832 |
+
template or a member function of a class template is implicitly
|
| 3833 |
+
instantiated when the function is called in a context that requires the
|
| 3834 |
+
value of the default argument.
|
| 3835 |
+
|
| 3836 |
+
[*Example 4*:
|
| 3837 |
|
| 3838 |
``` cpp
|
| 3839 |
template<class T> struct Z {
|
| 3840 |
void f();
|
| 3841 |
void g();
|
|
|
|
| 3853 |
```
|
| 3854 |
|
| 3855 |
Nothing in this example requires `class` `Z<double>`, `Z<int>::g()`, or
|
| 3856 |
`Z<char>::f()` to be implicitly instantiated.
|
| 3857 |
|
| 3858 |
+
— *end example*]
|
| 3859 |
+
|
| 3860 |
Unless a variable template specialization has been explicitly
|
| 3861 |
instantiated or explicitly specialized, the variable template
|
| 3862 |
specialization is implicitly instantiated when the specialization is
|
| 3863 |
used. A default template argument for a variable template is implicitly
|
| 3864 |
instantiated when the variable template is referenced in a context that
|
| 3865 |
requires the value of the default argument.
|
| 3866 |
|
| 3867 |
+
If the function selected by overload resolution ([[over.match]]) can be
|
| 3868 |
+
determined without instantiating a class template definition, it is
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3869 |
unspecified whether that instantiation actually takes place.
|
| 3870 |
|
| 3871 |
+
[*Example 5*:
|
| 3872 |
+
|
| 3873 |
``` cpp
|
| 3874 |
template <class T> struct S {
|
| 3875 |
operator int();
|
| 3876 |
};
|
| 3877 |
|
|
|
|
| 3883 |
f(sr); // instantiation of S<int> allowed but not required
|
| 3884 |
// instantiation of S<float> allowed but not required
|
| 3885 |
};
|
| 3886 |
```
|
| 3887 |
|
| 3888 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3889 |
|
| 3890 |
If a function template or a member function template specialization is
|
| 3891 |
used in a way that involves overload resolution, a declaration of the
|
| 3892 |
specialization is implicitly instantiated ([[temp.over]]).
|
| 3893 |
|
| 3894 |
An implementation shall not implicitly instantiate a function template,
|
| 3895 |
a variable template, a member template, a non-virtual member function, a
|
| 3896 |
+
member class, a static data member of a class template, or a
|
| 3897 |
+
substatement of a constexpr if statement ([[stmt.if]]), unless such
|
| 3898 |
+
instantiation is required. It is unspecified whether or not an
|
| 3899 |
implementation implicitly instantiates a virtual member function of a
|
| 3900 |
class template if the virtual member function would not otherwise be
|
| 3901 |
instantiated. The use of a template specialization in a default argument
|
| 3902 |
shall not cause the template to be implicitly instantiated except that a
|
| 3903 |
class template may be instantiated where its complete type is needed to
|
|
|
|
| 3910 |
defined. Implicitly instantiated specializations for members of a class
|
| 3911 |
template are placed in the namespace where the enclosing class template
|
| 3912 |
is defined. Implicitly instantiated member templates are placed in the
|
| 3913 |
namespace where the enclosing class or class template is defined.
|
| 3914 |
|
| 3915 |
+
[*Example 6*:
|
| 3916 |
+
|
| 3917 |
``` cpp
|
| 3918 |
namespace N {
|
| 3919 |
template<class T> class List {
|
| 3920 |
public:
|
| 3921 |
T* get();
|
|
|
|
| 3935 |
|
| 3936 |
a call of `lt.get()` from `Map<const char*,int>::get()` would place
|
| 3937 |
`List<int>::get()` in the namespace `N` rather than in the global
|
| 3938 |
namespace.
|
| 3939 |
|
| 3940 |
+
— *end example*]
|
| 3941 |
+
|
| 3942 |
If a function template `f` is called in a way that requires a default
|
| 3943 |
argument to be used, the dependent names are looked up, the semantics
|
| 3944 |
constraints are checked, and the instantiation of any template used in
|
| 3945 |
the default argument is done as if the default argument had been an
|
| 3946 |
initializer used in a function template specialization with the same
|
| 3947 |
scope, the same template parameters and the same access as that of the
|
| 3948 |
function template `f` used at that point, except that the scope in which
|
| 3949 |
+
a closure type is declared ([[expr.prim.lambda.closure]]) – and
|
| 3950 |
+
therefore its associated namespaces – remain as determined from the
|
| 3951 |
+
context of the definition for the default argument. This analysis is
|
| 3952 |
+
called *default argument instantiation*. The instantiated default
|
| 3953 |
+
argument is then used as the argument of `f`.
|
| 3954 |
|
| 3955 |
Each default argument is instantiated independently.
|
| 3956 |
|
| 3957 |
+
[*Example 7*:
|
| 3958 |
+
|
| 3959 |
``` cpp
|
| 3960 |
template<class T> void f(T x, T y = ydef(T()), T z = zdef(T()));
|
| 3961 |
|
| 3962 |
class A { };
|
| 3963 |
|
|
|
|
| 3968 |
f(a, b); // default argument z = zdef(T()) instantiated
|
| 3969 |
f(a); // ill-formed; ydef is not declared
|
| 3970 |
}
|
| 3971 |
```
|
| 3972 |
|
| 3973 |
+
— *end example*]
|
| 3974 |
+
|
| 3975 |
+
The *noexcept-specifier* of a function template specialization is not
|
| 3976 |
+
instantiated along with the function declaration; it is instantiated
|
| 3977 |
+
when needed ([[except.spec]]). If such an *noexcept-specifier* is
|
| 3978 |
needed but has not yet been instantiated, the dependent names are looked
|
| 3979 |
up, the semantics constraints are checked, and the instantiation of any
|
| 3980 |
+
template used in the *noexcept-specifier* is done as if it were being
|
| 3981 |
+
done as part of instantiating the declaration of the specialization at
|
| 3982 |
+
that point.
|
| 3983 |
|
| 3984 |
+
[*Note 3*: [[temp.point]] defines the point of instantiation of a
|
| 3985 |
+
template specialization. — *end note*]
|
| 3986 |
|
| 3987 |
+
There is an *implementation-defined* quantity that specifies the limit
|
| 3988 |
+
on the total depth of recursive instantiations ([[implimits]]), which
|
| 3989 |
+
could involve more than one template. The result of an infinite
|
| 3990 |
+
recursion in instantiation is undefined.
|
| 3991 |
+
|
| 3992 |
+
[*Example 8*:
|
| 3993 |
|
| 3994 |
``` cpp
|
| 3995 |
template<class T> class X {
|
| 3996 |
X<T>* p; // OK
|
| 3997 |
X<T*> a; // implicit generation of X<T> requires
|
| 3998 |
// the implicit instantiation of X<T*> which requires
|
| 3999 |
+
// the implicit instantiation of X<T**> which …
|
| 4000 |
};
|
| 4001 |
```
|
| 4002 |
|
| 4003 |
+
— *end example*]
|
| 4004 |
+
|
| 4005 |
### Explicit instantiation <a id="temp.explicit">[[temp.explicit]]</a>
|
| 4006 |
|
| 4007 |
A class, function, variable, or member template specialization can be
|
| 4008 |
explicitly instantiated from its template. A member function, member
|
| 4009 |
class or static data member of a class template can be explicitly
|
|
|
|
| 4026 |
If the explicit instantiation is for a class or member class, the
|
| 4027 |
*elaborated-type-specifier* in the *declaration* shall include a
|
| 4028 |
*simple-template-id*. If the explicit instantiation is for a function or
|
| 4029 |
member function, the *unqualified-id* in the *declaration* shall be
|
| 4030 |
either a *template-id* or, where all template arguments can be deduced,
|
| 4031 |
+
a *template-name* or *operator-function-id*.
|
| 4032 |
+
|
| 4033 |
+
[*Note 1*: The declaration may declare a *qualified-id*, in which case
|
| 4034 |
+
the *unqualified-id* of the *qualified-id* must be a
|
| 4035 |
+
*template-id*. — *end note*]
|
| 4036 |
+
|
| 4037 |
+
If the explicit instantiation is for a member function, a member class
|
| 4038 |
+
or a static data member of a class template specialization, the name of
|
| 4039 |
+
the class template specialization in the *qualified-id* for the member
|
| 4040 |
+
name shall be a *simple-template-id*. If the explicit instantiation is
|
| 4041 |
+
for a variable, the *unqualified-id* in the declaration shall be a
|
| 4042 |
+
*template-id*. An explicit instantiation shall appear in an enclosing
|
| 4043 |
+
namespace of its template. If the name declared in the explicit
|
| 4044 |
+
instantiation is an unqualified name, the explicit instantiation shall
|
| 4045 |
+
appear in the namespace where its template is declared or, if that
|
| 4046 |
+
namespace is inline ([[namespace.def]]), any namespace from its
|
| 4047 |
+
enclosing namespace set.
|
| 4048 |
+
|
| 4049 |
+
[*Note 2*: Regarding qualified names in declarators, see
|
| 4050 |
+
[[dcl.meaning]]. — *end note*]
|
| 4051 |
+
|
| 4052 |
+
[*Example 1*:
|
| 4053 |
|
| 4054 |
``` cpp
|
| 4055 |
template<class T> class Array { void mf(); };
|
| 4056 |
template class Array<char>;
|
| 4057 |
template void Array<int>::mf();
|
| 4058 |
|
| 4059 |
+
template<class T> void sort(Array<T>& v) { ... }
|
| 4060 |
template void sort(Array<char>&); // argument is deduced here
|
| 4061 |
|
| 4062 |
namespace N {
|
| 4063 |
template<class T> void f(T&) { }
|
| 4064 |
}
|
| 4065 |
template void N::f<int>(int&);
|
| 4066 |
```
|
| 4067 |
|
| 4068 |
+
— *end example*]
|
| 4069 |
+
|
| 4070 |
A declaration of a function template, a variable template, a member
|
| 4071 |
function or static data member of a class template, or a member function
|
| 4072 |
template of a class or class template shall precede an explicit
|
| 4073 |
instantiation of that entity. A definition of a class template, a member
|
| 4074 |
class of a class template, or a member class template of a class or
|
|
|
|
| 4092 |
is defined. An explicit instantiation for a member of a class template
|
| 4093 |
is placed in the namespace where the enclosing class template is
|
| 4094 |
defined. An explicit instantiation for a member template is placed in
|
| 4095 |
the namespace where the enclosing class or class template is defined.
|
| 4096 |
|
| 4097 |
+
[*Example 2*:
|
| 4098 |
+
|
| 4099 |
``` cpp
|
| 4100 |
namespace N {
|
| 4101 |
template<class T> class Y { void mf() { } };
|
| 4102 |
}
|
| 4103 |
|
| 4104 |
+
template class Y<int>; // error: class template Y not visible in the global namespace
|
|
|
|
| 4105 |
|
| 4106 |
using N::Y;
|
| 4107 |
+
template class Y<int>; // error: explicit instantiation outside of the namespace of the template
|
|
|
|
| 4108 |
|
| 4109 |
template class N::Y<char*>; // OK: explicit instantiation in namespace N
|
| 4110 |
+
template void N::Y<double>::mf(); // OK: explicit instantiation in namespace N
|
|
|
|
| 4111 |
```
|
| 4112 |
|
| 4113 |
+
— *end example*]
|
| 4114 |
+
|
| 4115 |
A trailing *template-argument* can be left unspecified in an explicit
|
| 4116 |
instantiation of a function template specialization or of a member
|
| 4117 |
function template specialization provided it can be deduced from the
|
| 4118 |
type of a function parameter ([[temp.deduct]]).
|
| 4119 |
|
| 4120 |
+
[*Example 3*:
|
| 4121 |
+
|
| 4122 |
``` cpp
|
| 4123 |
+
template<class T> class Array { ... };
|
| 4124 |
+
template<class T> void sort(Array<T>& v) { ... }
|
| 4125 |
|
| 4126 |
+
// instantiate sort(Array<int>&) -- template-argument deduced
|
| 4127 |
template void sort<>(Array<int>&);
|
| 4128 |
```
|
| 4129 |
|
| 4130 |
+
— *end example*]
|
| 4131 |
+
|
| 4132 |
An explicit instantiation that names a class template specialization is
|
| 4133 |
also an explicit instantiation of the same kind (declaration or
|
| 4134 |
definition) of each of its members (not including members inherited from
|
| 4135 |
base classes and members that are templates) that has not been
|
| 4136 |
previously explicitly specialized in the translation unit containing the
|
| 4137 |
+
explicit instantiation, except as described below.
|
| 4138 |
+
|
| 4139 |
+
[*Note 3*: In addition, it will typically be an explicit instantiation
|
| 4140 |
+
of certain implementation-dependent data about the class. — *end note*]
|
| 4141 |
|
| 4142 |
An explicit instantiation definition that names a class template
|
| 4143 |
specialization explicitly instantiates the class template specialization
|
| 4144 |
and is an explicit instantiation definition of only those members that
|
| 4145 |
have been defined at the point of instantiation.
|
| 4146 |
|
| 4147 |
+
Except for inline functions and variables, declarations with types
|
| 4148 |
+
deduced from their initializer or return value ([[dcl.spec.auto]]),
|
| 4149 |
+
`const` variables of literal types, variables of reference types, and
|
| 4150 |
+
class template specializations, explicit instantiation declarations have
|
| 4151 |
+
the effect of suppressing the implicit instantiation of the entity to
|
| 4152 |
+
which they refer.
|
| 4153 |
+
|
| 4154 |
+
[*Note 4*: The intent is that an inline function that is the subject of
|
| 4155 |
+
an explicit instantiation declaration will still be implicitly
|
| 4156 |
+
instantiated when odr-used ([[basic.def.odr]]) so that the body can be
|
| 4157 |
+
considered for inlining, but that no out-of-line copy of the inline
|
| 4158 |
+
function would be generated in the translation unit. — *end note*]
|
| 4159 |
|
| 4160 |
If an entity is the subject of both an explicit instantiation
|
| 4161 |
declaration and an explicit instantiation definition in the same
|
| 4162 |
translation unit, the definition shall follow the declaration. An entity
|
| 4163 |
that is the subject of an explicit instantiation declaration and that is
|
| 4164 |
also used in a way that would otherwise cause an implicit
|
| 4165 |
instantiation ([[temp.inst]]) in the translation unit shall be the
|
| 4166 |
subject of an explicit instantiation definition somewhere in the
|
| 4167 |
program; otherwise the program is ill-formed, no diagnostic required.
|
| 4168 |
+
|
| 4169 |
+
[*Note 5*: This rule does apply to inline functions even though an
|
| 4170 |
+
explicit instantiation declaration of such an entity has no other
|
| 4171 |
+
normative effect. This is needed to ensure that if the address of an
|
| 4172 |
+
inline function is taken in a translation unit in which the
|
| 4173 |
+
implementation chose to suppress the out-of-line body, another
|
| 4174 |
+
translation unit will supply the body. — *end note*]
|
| 4175 |
+
|
| 4176 |
+
An explicit instantiation declaration shall not name a specialization of
|
| 4177 |
+
a template with internal linkage.
|
| 4178 |
|
| 4179 |
The usual access checking rules do not apply to names used to specify
|
| 4180 |
+
explicit instantiations.
|
| 4181 |
+
|
| 4182 |
+
[*Note 6*: In particular, the template arguments and names used in the
|
| 4183 |
+
function declarator (including parameter types, return types and
|
| 4184 |
+
exception specifications) may be private types or objects which would
|
| 4185 |
+
normally not be accessible and the template may be a member template or
|
| 4186 |
+
member function which would not normally be accessible. — *end note*]
|
| 4187 |
|
| 4188 |
An explicit instantiation does not constitute a use of a default
|
| 4189 |
argument, so default argument instantiation is not done.
|
| 4190 |
|
| 4191 |
+
[*Example 4*:
|
| 4192 |
+
|
| 4193 |
``` cpp
|
| 4194 |
char* p = 0;
|
| 4195 |
template<class T> T g(T x = &p) { return x; }
|
| 4196 |
template int g<int>(int); // OK even though &p isn't an int.
|
| 4197 |
```
|
| 4198 |
|
| 4199 |
+
— *end example*]
|
| 4200 |
+
|
| 4201 |
### Explicit specialization <a id="temp.expl.spec">[[temp.expl.spec]]</a>
|
| 4202 |
|
| 4203 |
An explicit specialization of any of the following:
|
| 4204 |
|
| 4205 |
- function template
|
|
|
|
| 4217 |
``` bnf
|
| 4218 |
explicit-specialization:
|
| 4219 |
'template < >' declaration
|
| 4220 |
```
|
| 4221 |
|
| 4222 |
+
[*Example 1*:
|
| 4223 |
+
|
| 4224 |
``` cpp
|
| 4225 |
template<class T> class stream;
|
| 4226 |
|
| 4227 |
+
template<> class stream<char> { ... };
|
| 4228 |
|
| 4229 |
+
template<class T> class Array { ... };
|
| 4230 |
+
template<class T> void sort(Array<T>& v) { ... }
|
| 4231 |
|
| 4232 |
template<> void sort<char*>(Array<char*>&);
|
| 4233 |
```
|
| 4234 |
|
| 4235 |
Given these declarations, `stream<char>` will be used as the definition
|
|
|
|
| 4237 |
specializations instantiated from the class template. Similarly,
|
| 4238 |
`sort<char*>` will be used as the sort function for arguments of type
|
| 4239 |
`Array<char*>`; other `Array` types will be sorted by functions
|
| 4240 |
generated from the template.
|
| 4241 |
|
| 4242 |
+
— *end example*]
|
| 4243 |
+
|
| 4244 |
+
An explicit specialization may be declared in any scope in which the
|
| 4245 |
+
corresponding primary template may be defined ([[namespace.memdef]],
|
| 4246 |
+
[[class.mem]], [[temp.mem]]).
|
|
|
|
|
|
|
|
|
|
| 4247 |
|
| 4248 |
A declaration of a function template, class template, or variable
|
| 4249 |
template being explicitly specialized shall precede the declaration of
|
| 4250 |
+
the explicit specialization.
|
| 4251 |
+
|
| 4252 |
+
[*Note 1*: A declaration, but not a definition of the template is
|
| 4253 |
+
required. — *end note*]
|
| 4254 |
+
|
| 4255 |
+
The definition of a class or class template shall precede the
|
| 4256 |
+
declaration of an explicit specialization for a member template of the
|
| 4257 |
+
class or class template.
|
| 4258 |
+
|
| 4259 |
+
[*Example 2*:
|
| 4260 |
|
| 4261 |
``` cpp
|
| 4262 |
+
template<> class X<int> { ... }; // error: X not a template
|
| 4263 |
|
| 4264 |
template<class T> class X;
|
| 4265 |
|
| 4266 |
+
template<> class X<char*> { ... }; // OK: X is a template
|
| 4267 |
```
|
| 4268 |
|
| 4269 |
+
— *end example*]
|
| 4270 |
+
|
| 4271 |
A member function, a member function template, a member class, a member
|
| 4272 |
enumeration, a member class template, a static data member, or a static
|
| 4273 |
data member template of a class template may be explicitly specialized
|
| 4274 |
for a class specialization that is implicitly instantiated; in this
|
| 4275 |
case, the definition of the class template shall precede the explicit
|
|
|
|
| 4292 |
syntax. The same is true when defining a member of an explicitly
|
| 4293 |
specialized member class. However, `template<>` is used in defining a
|
| 4294 |
member of an explicitly specialized member class template that is
|
| 4295 |
specialized as a class template.
|
| 4296 |
|
| 4297 |
+
[*Example 3*:
|
| 4298 |
+
|
| 4299 |
``` cpp
|
| 4300 |
template<class T> struct A {
|
| 4301 |
struct B { };
|
| 4302 |
template<class U> struct C { };
|
| 4303 |
};
|
|
|
|
| 4309 |
void h() {
|
| 4310 |
A<int> a;
|
| 4311 |
a.f(16); // A<int>::f must be defined somewhere
|
| 4312 |
}
|
| 4313 |
|
| 4314 |
+
// template<> not used for a member of an explicitly specialized class template
|
| 4315 |
+
void A<int>::f(int) { ... }
|
|
|
|
| 4316 |
|
| 4317 |
template<> struct A<char>::B {
|
| 4318 |
void f();
|
| 4319 |
};
|
| 4320 |
+
// template<> also not used when defining a member of an explicitly specialized member class
|
| 4321 |
+
void A<char>::B::f() { ... }
|
|
|
|
| 4322 |
|
| 4323 |
template<> template<class U> struct A<char>::C {
|
| 4324 |
void f();
|
| 4325 |
};
|
| 4326 |
+
// template<> is used when defining a member of an explicitly specialized member class template
|
| 4327 |
+
// specialized as a class template
|
| 4328 |
template<>
|
| 4329 |
+
template<class U> void A<char>::C<U>::f() { ... }
|
| 4330 |
|
| 4331 |
template<> struct A<short>::B {
|
| 4332 |
void f();
|
| 4333 |
};
|
| 4334 |
+
template<> void A<short>::B::f() { ... } // error: template<> not permitted
|
| 4335 |
|
| 4336 |
template<> template<class U> struct A<short>::C {
|
| 4337 |
void f();
|
| 4338 |
};
|
| 4339 |
+
template<class U> void A<short>::C<U>::f() { ... } // error: template<> required
|
| 4340 |
```
|
| 4341 |
|
| 4342 |
+
— *end example*]
|
| 4343 |
+
|
| 4344 |
If a template, a member template or a member of a class template is
|
| 4345 |
explicitly specialized then that specialization shall be declared before
|
| 4346 |
the first use of that specialization that would cause an implicit
|
| 4347 |
instantiation to take place, in every translation unit in which such a
|
| 4348 |
use occurs; no diagnostic is required. If the program does not provide a
|
|
|
|
| 4351 |
place or the member is a virtual member function, the program is
|
| 4352 |
ill-formed, no diagnostic required. An implicit instantiation is never
|
| 4353 |
generated for an explicit specialization that is declared but not
|
| 4354 |
defined.
|
| 4355 |
|
| 4356 |
+
[*Example 4*:
|
| 4357 |
+
|
| 4358 |
``` cpp
|
| 4359 |
class String { };
|
| 4360 |
+
template<class T> class Array { ... };
|
| 4361 |
+
template<class T> void sort(Array<T>& v) { ... }
|
| 4362 |
|
| 4363 |
void f(Array<String>& v) {
|
| 4364 |
+
sort(v); // use primary template sort(Array<T>&), T is String
|
|
|
|
| 4365 |
}
|
| 4366 |
|
| 4367 |
+
template<> void sort<String>(Array<String>& v); // error: specialization after use of primary template
|
|
|
|
| 4368 |
template<> void sort<>(Array<char*>& v); // OK: sort<char*> not yet used
|
| 4369 |
template<class T> struct A {
|
| 4370 |
enum E : T;
|
| 4371 |
enum class S : T;
|
| 4372 |
};
|
|
|
|
| 4377 |
template<> enum A<char>::E : char { echar }; // ill-formed, A<char>::E was instantiated
|
| 4378 |
// when A<char> was instantiated
|
| 4379 |
template<> enum class A<char>::S : char { schar }; // OK
|
| 4380 |
```
|
| 4381 |
|
| 4382 |
+
— *end example*]
|
| 4383 |
+
|
| 4384 |
The placement of explicit specialization declarations for function
|
| 4385 |
templates, class templates, variable templates, member functions of
|
| 4386 |
class templates, static data members of class templates, member classes
|
| 4387 |
of class templates, member enumerations of class templates, member class
|
| 4388 |
templates of class templates, member function templates of class
|
|
|
|
| 4401 |
it compile will be such a trial as to kindle its self-immolation.
|
| 4402 |
|
| 4403 |
A template explicit specialization is in the scope of the namespace in
|
| 4404 |
which the template was defined.
|
| 4405 |
|
| 4406 |
+
[*Example 5*:
|
| 4407 |
+
|
| 4408 |
``` cpp
|
| 4409 |
namespace N {
|
| 4410 |
+
template<class T> class X { ... };
|
| 4411 |
+
template<class T> class Y { ... };
|
| 4412 |
|
| 4413 |
+
template<> class X<int> { ... }; // OK: specialization in same namespace
|
| 4414 |
+
template<> class Y<double>; // forward-declare intent to specialize for double
|
|
|
|
|
|
|
| 4415 |
}
|
| 4416 |
|
| 4417 |
+
template<> class N::Y<double> { ... }; // OK: specialization in enclosing namespace
|
| 4418 |
+
template<> class N::Y<short> { ... }; // OK: specialization in enclosing namespace
|
|
|
|
|
|
|
| 4419 |
```
|
| 4420 |
|
| 4421 |
+
— *end example*]
|
| 4422 |
+
|
| 4423 |
A *simple-template-id* that names a class template explicit
|
| 4424 |
specialization that has been declared but not defined can be used
|
| 4425 |
exactly like the names of other incompletely-defined classes (
|
| 4426 |
[[basic.types]]).
|
| 4427 |
|
| 4428 |
+
[*Example 6*:
|
| 4429 |
+
|
| 4430 |
``` cpp
|
| 4431 |
template<class T> class X; // X is a class template
|
| 4432 |
template<> class X<int>;
|
| 4433 |
|
| 4434 |
X<int>* p; // OK: pointer to declared class X<int>
|
| 4435 |
X<int> x; // error: object of incomplete class X<int>
|
| 4436 |
```
|
| 4437 |
|
| 4438 |
+
— *end example*]
|
| 4439 |
+
|
| 4440 |
A trailing *template-argument* can be left unspecified in the
|
| 4441 |
*template-id* naming an explicit function template specialization
|
| 4442 |
provided it can be deduced from the function argument type.
|
| 4443 |
|
| 4444 |
+
[*Example 7*:
|
| 4445 |
+
|
| 4446 |
``` cpp
|
| 4447 |
+
template<class T> class Array { ... };
|
| 4448 |
template<class T> void sort(Array<T>& v);
|
| 4449 |
|
| 4450 |
// explicit specialization for sort(Array<int>&)
|
| 4451 |
// with deduced template-argument of type int
|
| 4452 |
template<> void sort(Array<int>&);
|
| 4453 |
```
|
| 4454 |
|
| 4455 |
+
— *end example*]
|
| 4456 |
+
|
| 4457 |
A function with the same name as a template and a type that exactly
|
| 4458 |
matches that of a template specialization is not an explicit
|
| 4459 |
specialization ([[temp.fct]]).
|
| 4460 |
|
| 4461 |
+
An explicit specialization of a function or variable template is inline
|
| 4462 |
+
only if it is declared with the `inline` specifier or defined as
|
| 4463 |
+
deleted, and independently of whether its function or variable template
|
| 4464 |
+
is inline.
|
| 4465 |
+
|
| 4466 |
+
[*Example 8*:
|
| 4467 |
|
| 4468 |
``` cpp
|
| 4469 |
+
template<class T> void f(T) { ... }
|
| 4470 |
+
template<class T> inline T g(T) { ... }
|
| 4471 |
|
| 4472 |
+
template<> inline void f<>(int) { ... } // OK: inline
|
| 4473 |
+
template<> int g<>(int) { ... } // OK: not inline
|
| 4474 |
```
|
| 4475 |
|
| 4476 |
+
— *end example*]
|
| 4477 |
+
|
| 4478 |
An explicit specialization of a static data member of a template or an
|
| 4479 |
explicit specialization of a static data member template is a definition
|
| 4480 |
if the declaration includes an initializer; otherwise, it is a
|
| 4481 |
+
declaration.
|
| 4482 |
+
|
| 4483 |
+
[*Note 2*:
|
| 4484 |
+
|
| 4485 |
+
The definition of a static data member of a template that requires
|
| 4486 |
+
default-initialization must use a *braced-init-list*:
|
| 4487 |
|
| 4488 |
``` cpp
|
| 4489 |
template<> X Q<int>::x; // declaration
|
| 4490 |
template<> X Q<int>::x (); // error: declares a function
|
| 4491 |
template<> X Q<int>::x { }; // definition
|
| 4492 |
```
|
| 4493 |
|
| 4494 |
+
— *end note*]
|
| 4495 |
+
|
| 4496 |
A member or a member template of a class template may be explicitly
|
| 4497 |
specialized for a given implicit instantiation of the class template,
|
| 4498 |
even if the member or member template is defined in the class template
|
| 4499 |
definition. An explicit specialization of a member or member template is
|
| 4500 |
specified using the syntax for explicit specialization.
|
| 4501 |
|
| 4502 |
+
[*Example 9*:
|
| 4503 |
+
|
| 4504 |
``` cpp
|
| 4505 |
template<class T> struct A {
|
| 4506 |
void f(T);
|
| 4507 |
template<class X1> void g1(T, X1);
|
| 4508 |
template<class X2> void g2(T, X2);
|
|
|
|
| 4526 |
|
| 4527 |
// member specialization even if defined in class definition
|
| 4528 |
template<> void A<int>::h(int) { }
|
| 4529 |
```
|
| 4530 |
|
| 4531 |
+
— *end example*]
|
| 4532 |
+
|
| 4533 |
A member or a member template may be nested within many enclosing class
|
| 4534 |
templates. In an explicit specialization for such a member, the member
|
| 4535 |
declaration shall be preceded by a `template<>` for each enclosing class
|
| 4536 |
template that is explicitly specialized.
|
| 4537 |
|
| 4538 |
+
[*Example 10*:
|
| 4539 |
+
|
| 4540 |
``` cpp
|
| 4541 |
template<class T1> class A {
|
| 4542 |
template<class T2> class B {
|
| 4543 |
void mf();
|
| 4544 |
};
|
| 4545 |
};
|
| 4546 |
template<> template<> class A<int>::B<double>;
|
| 4547 |
template<> template<> void A<char>::B<char>::mf();
|
| 4548 |
```
|
| 4549 |
|
| 4550 |
+
— *end example*]
|
| 4551 |
+
|
| 4552 |
In an explicit specialization declaration for a member of a class
|
| 4553 |
template or a member template that appears in namespace scope, the
|
| 4554 |
member template and some of its enclosing class templates may remain
|
| 4555 |
unspecialized, except that the declaration shall not explicitly
|
| 4556 |
specialize a class member template if its enclosing class templates are
|
| 4557 |
not explicitly specialized as well. In such explicit specialization
|
| 4558 |
declaration, the keyword `template` followed by a
|
| 4559 |
*template-parameter-list* shall be provided instead of the `template<>`
|
| 4560 |
preceding the explicit specialization declaration of the member. The
|
| 4561 |
+
types of the *template-parameter*s in the *template-parameter-list*
|
| 4562 |
shall be the same as those specified in the primary template definition.
|
| 4563 |
|
| 4564 |
+
[*Example 11*:
|
| 4565 |
+
|
| 4566 |
``` cpp
|
| 4567 |
template <class T1> class A {
|
| 4568 |
template<class T2> class B {
|
| 4569 |
template<class T3> void mf1(T3);
|
| 4570 |
void mf2();
|
|
|
|
| 4579 |
template <class Y> template <>
|
| 4580 |
void A<Y>::B<double>::mf2() { } // ill-formed; B<double> is specialized but
|
| 4581 |
// its enclosing class template A is not
|
| 4582 |
```
|
| 4583 |
|
| 4584 |
+
— *end example*]
|
| 4585 |
+
|
| 4586 |
A specialization of a member function template, member class template,
|
| 4587 |
or static data member template of a non-specialized class template is
|
| 4588 |
itself a template.
|
| 4589 |
|
| 4590 |
An explicit specialization declaration shall not be a friend
|
|
|
|
| 4595 |
|
| 4596 |
- the explicit specialization of a function template;
|
| 4597 |
- the explicit specialization of a member function template;
|
| 4598 |
- the explicit specialization of a member function of a class template
|
| 4599 |
where the class template specialization to which the member function
|
| 4600 |
+
specialization belongs is implicitly instantiated. \[*Note 3*: Default
|
| 4601 |
+
function arguments may be specified in the declaration or definition
|
| 4602 |
+
of a member function of a class template specialization that is
|
| 4603 |
+
explicitly specialized. — *end note*]
|
| 4604 |
|
| 4605 |
## Function template specializations <a id="temp.fct.spec">[[temp.fct.spec]]</a>
|
| 4606 |
|
| 4607 |
A function instantiated from a function template is called a function
|
| 4608 |
template specialization; so is an explicit specialization of a function
|
|
|
|
| 4613 |
arguments.
|
| 4614 |
|
| 4615 |
Each function template specialization instantiated from a template has
|
| 4616 |
its own copy of any static variable.
|
| 4617 |
|
| 4618 |
+
[*Example 1*:
|
| 4619 |
+
|
| 4620 |
``` cpp
|
| 4621 |
template<class T> void f(T* p) {
|
| 4622 |
static T s;
|
| 4623 |
};
|
| 4624 |
|
|
|
|
| 4629 |
```
|
| 4630 |
|
| 4631 |
Here `f<int>(int*)` has a static variable `s` of type `int` and
|
| 4632 |
`f<char*>(char**)` has a static variable `s` of type `char*`.
|
| 4633 |
|
| 4634 |
+
— *end example*]
|
| 4635 |
+
|
| 4636 |
### Explicit template argument specification <a id="temp.arg.explicit">[[temp.arg.explicit]]</a>
|
| 4637 |
|
| 4638 |
Template arguments can be specified when referring to a function
|
| 4639 |
template specialization by qualifying the function template name with
|
| 4640 |
the list of *template-argument*s in the same way as *template-argument*s
|
| 4641 |
are specified in uses of a class template specialization.
|
| 4642 |
|
| 4643 |
+
[*Example 1*:
|
| 4644 |
+
|
| 4645 |
``` cpp
|
| 4646 |
template<class T> void sort(Array<T>& v);
|
| 4647 |
void f(Array<dcomplex>& cv, Array<int>& ci) {
|
| 4648 |
sort<dcomplex>(cv); // sort(Array<dcomplex>&)
|
| 4649 |
sort<int>(ci); // sort(Array<int>&)
|
|
|
|
| 4659 |
int i = convert<int,double>(d); // int convert(double)
|
| 4660 |
char c = convert<char,double>(d); // char convert(double)
|
| 4661 |
}
|
| 4662 |
```
|
| 4663 |
|
| 4664 |
+
— *end example*]
|
| 4665 |
+
|
| 4666 |
A template argument list may be specified when referring to a
|
| 4667 |
specialization of a function template
|
| 4668 |
|
| 4669 |
- when a function is called,
|
| 4670 |
- when the address of a function is taken, when a function initializes a
|
|
|
|
| 4684 |
if a template argument list is specified and it, along with any default
|
| 4685 |
template arguments, identifies a single function template
|
| 4686 |
specialization, then the *template-id* is an lvalue for the function
|
| 4687 |
template specialization.
|
| 4688 |
|
| 4689 |
+
[*Example 2*:
|
| 4690 |
+
|
| 4691 |
``` cpp
|
| 4692 |
template<class X, class Y> X f(Y);
|
| 4693 |
template<class X, class Y, class ... Z> X g(Y);
|
| 4694 |
void h() {
|
| 4695 |
int i = f<int>(5.6); // Y is deduced to be double
|
| 4696 |
int j = f(5.6); // ill-formed: X cannot be deduced
|
| 4697 |
+
f<void>(f<int, bool>); // Y for outer f deduced to be int (*)(bool)
|
| 4698 |
+
f<void>(f<int>); // ill-formed: f<int> does not denote a single function template specialization
|
|
|
|
|
|
|
| 4699 |
int k = g<int>(5.6); // Y is deduced to be double, Z is deduced to an empty sequence
|
| 4700 |
+
f<void>(g<int, bool>); // Y for outer f is deduced to be int (*)(bool),
|
| 4701 |
+
// Z is deduced to an empty sequence
|
| 4702 |
}
|
| 4703 |
```
|
| 4704 |
|
| 4705 |
+
— *end example*]
|
| 4706 |
+
|
| 4707 |
+
[*Note 1*:
|
| 4708 |
+
|
| 4709 |
An empty template argument list can be used to indicate that a given use
|
| 4710 |
refers to a specialization of a function template even when a
|
| 4711 |
non-template function ([[dcl.fct]]) is visible that would otherwise be
|
| 4712 |
used. For example:
|
| 4713 |
|
|
|
|
| 4716 |
int f(int); // #2
|
| 4717 |
int k = f(1); // uses #2
|
| 4718 |
int l = f<>(1); // uses #1
|
| 4719 |
```
|
| 4720 |
|
| 4721 |
+
— *end note*]
|
| 4722 |
+
|
| 4723 |
Template arguments that are present shall be specified in the
|
| 4724 |
declaration order of their corresponding *template-parameter*s. The
|
| 4725 |
template argument list shall not specify more *template-argument*s than
|
| 4726 |
there are corresponding *template-parameter*s unless one of the
|
| 4727 |
*template-parameter*s is a template parameter pack.
|
| 4728 |
|
| 4729 |
+
[*Example 3*:
|
| 4730 |
+
|
| 4731 |
``` cpp
|
| 4732 |
template<class X, class Y, class Z> X f(Y,Z);
|
| 4733 |
template<class ... Args> void f2();
|
| 4734 |
void g() {
|
| 4735 |
f<int,const char*,double>("aa",3.0);
|
| 4736 |
f<int,const char*>("aa",3.0); // Z is deduced to be double
|
| 4737 |
+
f<int>("aa",3.0); // Y is deduced to be const char*, and Z is deduced to be double
|
|
|
|
| 4738 |
f("aa",3.0); // error: X cannot be deduced
|
| 4739 |
f2<char, short, int, long>(); // OK
|
| 4740 |
}
|
| 4741 |
```
|
| 4742 |
|
| 4743 |
+
— *end example*]
|
| 4744 |
+
|
| 4745 |
Implicit conversions (Clause [[conv]]) will be performed on a function
|
| 4746 |
argument to convert it to the type of the corresponding function
|
| 4747 |
parameter if the parameter type contains no *template-parameter*s that
|
| 4748 |
+
participate in template argument deduction.
|
| 4749 |
+
|
| 4750 |
+
[*Note 2*:
|
| 4751 |
+
|
| 4752 |
+
Template parameters do not participate in template argument deduction if
|
| 4753 |
+
they are explicitly specified. For example,
|
| 4754 |
|
| 4755 |
``` cpp
|
| 4756 |
template<class T> void f(T);
|
| 4757 |
|
| 4758 |
class Complex {
|
|
|
|
| 4762 |
void g() {
|
| 4763 |
f<Complex>(1); // OK, means f<Complex>(Complex(1))
|
| 4764 |
}
|
| 4765 |
```
|
| 4766 |
|
| 4767 |
+
— *end note*]
|
| 4768 |
+
|
| 4769 |
+
[*Note 3*: Because the explicit template argument list follows the
|
| 4770 |
+
function template name, and because conversion member function templates
|
| 4771 |
+
and constructor member function templates are called without using a
|
| 4772 |
function name, there is no way to provide an explicit template argument
|
| 4773 |
+
list for these function templates. — *end note*]
|
| 4774 |
+
|
| 4775 |
+
[*Note 4*:
|
| 4776 |
|
| 4777 |
For simple function names, argument dependent lookup (
|
| 4778 |
[[basic.lookup.argdep]]) applies even when the function name is not
|
| 4779 |
visible within the scope of the call. This is because the call still has
|
| 4780 |
the syntactic form of a function call ([[basic.lookup.unqual]]). But
|
|
|
|
| 4784 |
name is visible, the call is not syntactically well-formed and
|
| 4785 |
argument-dependent lookup does not apply. If some such name is visible,
|
| 4786 |
argument dependent lookup applies and additional function templates may
|
| 4787 |
be found in other namespaces.
|
| 4788 |
|
| 4789 |
+
[*Example 4*:
|
| 4790 |
+
|
| 4791 |
``` cpp
|
| 4792 |
namespace A {
|
| 4793 |
struct B { };
|
| 4794 |
template<int X> void f(B);
|
| 4795 |
}
|
|
|
|
| 4797 |
template<class T> void f(T t);
|
| 4798 |
}
|
| 4799 |
void g(A::B b) {
|
| 4800 |
f<3>(b); // ill-formed: not a function call
|
| 4801 |
A::f<3>(b); // well-formed
|
| 4802 |
+
C::f<3>(b); // ill-formed; argument dependent lookup applies only to unqualified names
|
|
|
|
| 4803 |
using C::f;
|
| 4804 |
+
f<3>(b); // well-formed because C::f is visible; then A::f is found by argument dependent lookup
|
|
|
|
| 4805 |
}
|
| 4806 |
```
|
| 4807 |
|
| 4808 |
+
— *end example*]
|
| 4809 |
+
|
| 4810 |
+
— *end note*]
|
| 4811 |
+
|
| 4812 |
Template argument deduction can extend the sequence of template
|
| 4813 |
arguments corresponding to a template parameter pack, even when the
|
| 4814 |
sequence contains explicitly specified template arguments.
|
| 4815 |
|
| 4816 |
+
[*Example 5*:
|
| 4817 |
+
|
| 4818 |
``` cpp
|
| 4819 |
template<class ... Types> void f(Types ... values);
|
| 4820 |
|
| 4821 |
void g() {
|
| 4822 |
f<int*, float*>(0, 0, 0); // Types is deduced to the sequence int*, float*, int
|
| 4823 |
}
|
| 4824 |
```
|
| 4825 |
|
| 4826 |
+
— *end example*]
|
| 4827 |
+
|
| 4828 |
### Template argument deduction <a id="temp.deduct">[[temp.deduct]]</a>
|
| 4829 |
|
| 4830 |
When a function template specialization is referenced, all of the
|
| 4831 |
template arguments shall have values. The values can be explicitly
|
| 4832 |
specified or, in some cases, be deduced from the use or obtained from
|
| 4833 |
default *template-argument*s.
|
| 4834 |
|
| 4835 |
+
[*Example 1*:
|
| 4836 |
+
|
| 4837 |
``` cpp
|
| 4838 |
void f(Array<dcomplex>& cv, Array<int>& ci) {
|
| 4839 |
sort(cv); // calls sort(Array<dcomplex>&)
|
| 4840 |
sort(ci); // calls sort(Array<int>&)
|
| 4841 |
}
|
|
|
|
| 4848 |
int i = convert<int>(d); // calls convert<int,double>(double)
|
| 4849 |
int c = convert<char>(d); // calls convert<char,double>(double)
|
| 4850 |
}
|
| 4851 |
```
|
| 4852 |
|
| 4853 |
+
— *end example*]
|
| 4854 |
+
|
| 4855 |
When an explicit template argument list is specified, the template
|
| 4856 |
arguments must be compatible with the template parameter list and must
|
| 4857 |
result in a valid function type as described below; otherwise type
|
| 4858 |
deduction fails. Specifically, the following steps are performed when
|
| 4859 |
evaluating an explicitly specified template argument list with respect
|
|
|
|
| 4870 |
[[temp.arg.nontype]], otherwise type deduction fails.
|
| 4871 |
- The specified template argument values are substituted for the
|
| 4872 |
corresponding template parameters as specified below.
|
| 4873 |
|
| 4874 |
After this substitution is performed, the function parameter type
|
| 4875 |
+
adjustments described in [[dcl.fct]] are performed.
|
| 4876 |
+
|
| 4877 |
+
[*Example 2*: A parameter type of “`void (const int, int[5])`” becomes
|
| 4878 |
+
“`void(*)(int,int*)`”. — *end example*]
|
| 4879 |
+
|
| 4880 |
+
[*Note 1*: A top-level qualifier in a function parameter declaration
|
| 4881 |
+
does not affect the function type but still affects the type of the
|
| 4882 |
+
function parameter variable within the function. — *end note*]
|
| 4883 |
+
|
| 4884 |
+
[*Example 3*:
|
| 4885 |
|
| 4886 |
``` cpp
|
| 4887 |
template <class T> void f(T t);
|
| 4888 |
template <class X> void g(const X x);
|
| 4889 |
template <class Z> void h(Z, Z*);
|
|
|
|
| 4904 |
// #5: function type is h(int, const int*)
|
| 4905 |
h<const int>(1,0);
|
| 4906 |
}
|
| 4907 |
```
|
| 4908 |
|
| 4909 |
+
— *end example*]
|
| 4910 |
+
|
| 4911 |
+
[*Note 2*: `f<int>(1)` and `f<const int>(1)` call distinct functions
|
| 4912 |
+
even though both of the functions called have the same function
|
| 4913 |
+
type. — *end note*]
|
| 4914 |
|
| 4915 |
The resulting substituted and adjusted function type is used as the type
|
| 4916 |
of the function template for template argument deduction. If a template
|
| 4917 |
argument has not been deduced and its corresponding template parameter
|
| 4918 |
has a default argument, the template argument is determined by
|
| 4919 |
substituting the template arguments determined for preceding template
|
| 4920 |
parameters into the default argument. If the substitution results in an
|
| 4921 |
invalid type, as described above, type deduction fails.
|
| 4922 |
|
| 4923 |
+
[*Example 4*:
|
| 4924 |
+
|
| 4925 |
``` cpp
|
| 4926 |
template <class T, class U = double>
|
| 4927 |
void f(T t = 0, U u = 0);
|
| 4928 |
|
| 4929 |
void g() {
|
|
|
|
| 4933 |
f<int>(); // f<int,double>(0,0)
|
| 4934 |
f<int,char>(); // f<int,char>(0,0)
|
| 4935 |
}
|
| 4936 |
```
|
| 4937 |
|
| 4938 |
+
— *end example*]
|
| 4939 |
+
|
| 4940 |
When all template arguments have been deduced or obtained from default
|
| 4941 |
template arguments, all uses of template parameters in the template
|
| 4942 |
parameter list of the template and the function type are replaced with
|
| 4943 |
the corresponding deduced or default argument values. If the
|
| 4944 |
substitution results in an invalid type, as described above, type
|
|
|
|
| 4958 |
expressions include not only constant expressions such as those that
|
| 4959 |
appear in array bounds or as nontype template arguments but also general
|
| 4960 |
expressions (i.e., non-constant expressions) inside `sizeof`,
|
| 4961 |
`decltype`, and other contexts that allow non-constant expressions. The
|
| 4962 |
substitution proceeds in lexical order and stops when a condition that
|
| 4963 |
+
causes deduction to fail is encountered.
|
| 4964 |
+
|
| 4965 |
+
[*Note 3*: The equivalent substitution in exception specifications is
|
| 4966 |
+
done only when the *noexcept-specifier* is instantiated, at which point
|
| 4967 |
+
a program is ill-formed if the substitution results in an invalid type
|
| 4968 |
+
or expression. — *end note*]
|
| 4969 |
+
|
| 4970 |
+
[*Example 5*:
|
| 4971 |
|
| 4972 |
``` cpp
|
| 4973 |
template <class T> struct A { using X = typename T::X; };
|
| 4974 |
template <class T> typename T::X f(typename A<T>::X);
|
| 4975 |
template <class T> void f(...) { }
|
|
|
|
| 4980 |
f<int>(0); // OK, substituting return type causes deduction to fail
|
| 4981 |
g<int>(0); // error, substituting parameter type instantiates A<int>
|
| 4982 |
}
|
| 4983 |
```
|
| 4984 |
|
| 4985 |
+
— *end example*]
|
| 4986 |
+
|
| 4987 |
If a substitution results in an invalid type or expression, type
|
| 4988 |
deduction fails. An invalid type or expression is one that would be
|
| 4989 |
ill-formed, with a diagnostic required, if written using the substituted
|
| 4990 |
+
arguments.
|
| 4991 |
+
|
| 4992 |
+
[*Note 4*: If no diagnostic is required, the program is still
|
| 4993 |
+
ill-formed. Access checking is done as part of the substitution
|
| 4994 |
+
process. — *end note*]
|
| 4995 |
+
|
| 4996 |
Only invalid types and expressions in the immediate context of the
|
| 4997 |
function type and its template parameter types can result in a deduction
|
| 4998 |
+
failure.
|
| 4999 |
+
|
| 5000 |
+
[*Note 5*: The substitution into types and expressions can result in
|
| 5001 |
+
effects such as the instantiation of class template specializations
|
| 5002 |
+
and/or function template specializations, the generation of
|
| 5003 |
+
implicitly-defined functions, etc. Such effects are not in the
|
| 5004 |
+
“immediate context” and can result in the program being
|
| 5005 |
+
ill-formed. — *end note*]
|
| 5006 |
+
|
| 5007 |
+
[*Example 6*:
|
| 5008 |
|
| 5009 |
``` cpp
|
| 5010 |
struct X { };
|
| 5011 |
struct Y {
|
| 5012 |
Y(X){}
|
|
|
|
| 5017 |
|
| 5018 |
X x1, x2;
|
| 5019 |
X x3 = f(x1, x2); // deduction fails on #1 (cannot add X+X), calls #2
|
| 5020 |
```
|
| 5021 |
|
| 5022 |
+
— *end example*]
|
| 5023 |
+
|
| 5024 |
+
[*Note 6*:
|
| 5025 |
+
|
| 5026 |
Type deduction may fail for the following reasons:
|
| 5027 |
|
| 5028 |
- Attempting to instantiate a pack expansion containing multiple
|
| 5029 |
parameter packs of differing lengths.
|
| 5030 |
- Attempting to create an array with an element type that is `void`, a
|
| 5031 |
function type, a reference type, or an abstract class type, or
|
| 5032 |
attempting to create an array with a size that is zero or negative.
|
| 5033 |
+
\[*Example 7*:
|
| 5034 |
``` cpp
|
| 5035 |
template <class T> int f(T[5]);
|
| 5036 |
int I = f<int>(0);
|
| 5037 |
int j = f<void>(0); // invalid array
|
| 5038 |
```
|
| 5039 |
+
|
| 5040 |
+
— *end example*]
|
| 5041 |
- Attempting to use a type that is not a class or enumeration type in a
|
| 5042 |
qualified name.
|
| 5043 |
+
\[*Example 8*:
|
| 5044 |
``` cpp
|
| 5045 |
template <class T> int f(typename T::B*);
|
| 5046 |
int i = f<int>(0);
|
| 5047 |
```
|
| 5048 |
+
|
| 5049 |
+
— *end example*]
|
| 5050 |
- Attempting to use a type in a *nested-name-specifier* of a
|
| 5051 |
*qualified-id* when that type does not contain the specified member,
|
| 5052 |
or
|
| 5053 |
- the specified member is not a type where a type is required, or
|
| 5054 |
- the specified member is not a template where a template is required,
|
| 5055 |
or
|
| 5056 |
- the specified member is not a non-type where a non-type is required.
|
| 5057 |
|
| 5058 |
+
\[*Example 9*:
|
| 5059 |
``` cpp
|
| 5060 |
template <int I> struct X { };
|
| 5061 |
template <template <class T> class> struct Z { };
|
| 5062 |
template <class T> void f(typename T::Y*){}
|
| 5063 |
template <class T> void g(X<T::N>*){}
|
|
|
|
| 5077 |
f<B>(0); // The Y member of B is not a type
|
| 5078 |
g<C>(0); // The N member of C is not a non-type
|
| 5079 |
h<D>(0); // The TT member of D is not a template
|
| 5080 |
}
|
| 5081 |
```
|
| 5082 |
+
|
| 5083 |
+
— *end example*]
|
| 5084 |
- Attempting to create a pointer to reference type.
|
| 5085 |
- Attempting to create a reference to `void`.
|
| 5086 |
- Attempting to create “pointer to member of `T`” when `T` is not a
|
| 5087 |
class type.
|
| 5088 |
+
\[*Example 10*:
|
| 5089 |
``` cpp
|
| 5090 |
template <class T> int f(int T::*);
|
| 5091 |
int i = f<int>(0);
|
| 5092 |
```
|
| 5093 |
+
|
| 5094 |
+
— *end example*]
|
| 5095 |
- Attempting to give an invalid type to a non-type template parameter.
|
| 5096 |
+
\[*Example 11*:
|
| 5097 |
``` cpp
|
| 5098 |
template <class T, T> struct S {};
|
| 5099 |
template <class T> int f(S<T, T()>*);
|
| 5100 |
struct X {};
|
| 5101 |
int i0 = f<X>(0);
|
| 5102 |
```
|
| 5103 |
+
|
| 5104 |
+
— *end example*]
|
| 5105 |
- Attempting to perform an invalid conversion in either a template
|
| 5106 |
argument expression, or an expression used in the function
|
| 5107 |
declaration.
|
| 5108 |
+
\[*Example 12*:
|
| 5109 |
``` cpp
|
| 5110 |
template <class T, T*> int f(int);
|
| 5111 |
int i2 = f<int,1>(0); // can't conv 1 to int*
|
| 5112 |
```
|
| 5113 |
+
|
| 5114 |
+
— *end example*]
|
| 5115 |
- Attempting to create a function type in which a parameter has a type
|
| 5116 |
of `void`, or in which the return type is a function type or array
|
| 5117 |
type.
|
| 5118 |
- Attempting to create a function type in which a parameter type or the
|
| 5119 |
return type is an abstract class type ([[class.abstract]]).
|
| 5120 |
|
| 5121 |
+
— *end note*]
|
| 5122 |
+
|
| 5123 |
+
[*Example 13*:
|
| 5124 |
+
|
| 5125 |
+
In the following example, assuming a `signed char` cannot represent the
|
| 5126 |
+
value 1000, a narrowing conversion ([[dcl.init.list]]) would be
|
| 5127 |
+
required to convert the *template-argument* of type `int` to
|
| 5128 |
+
`signed char`, therefore substitution fails for the second template (
|
| 5129 |
+
[[temp.arg.nontype]]).
|
| 5130 |
|
| 5131 |
``` cpp
|
| 5132 |
template <int> int f(int);
|
| 5133 |
template <signed char> int f(int);
|
| 5134 |
+
int i1 = f<1000>(0); // OK
|
| 5135 |
+
int i2 = f<1>(0); // ambiguous; not narrowing
|
| 5136 |
```
|
| 5137 |
|
| 5138 |
+
— *end example*]
|
| 5139 |
+
|
| 5140 |
#### Deducing template arguments from a function call <a id="temp.deduct.call">[[temp.deduct.call]]</a>
|
| 5141 |
|
| 5142 |
Template argument deduction is done by comparing each function template
|
| 5143 |
+
parameter type (call it `P`) that contains *template-parameter*s that
|
| 5144 |
+
participate in template argument deduction with the type of the
|
| 5145 |
+
corresponding argument of the call (call it `A`) as described below. If
|
| 5146 |
+
removing references and cv-qualifiers from `P` gives
|
| 5147 |
+
`std::initializer_list<P'>` or `P'[N]` for some `P'` and `N` and the
|
| 5148 |
+
argument is a non-empty initializer list ([[dcl.init.list]]), then
|
| 5149 |
+
deduction is performed instead for each element of the initializer list,
|
| 5150 |
+
taking `P'` as a function template parameter type and the initializer
|
| 5151 |
+
element as its argument, and in the `P'[N]` case, if `N` is a non-type
|
| 5152 |
+
template parameter, `N` is deduced from the length of the initializer
|
| 5153 |
+
list. Otherwise, an initializer list argument causes the parameter to be
|
| 5154 |
+
considered a non-deduced context ([[temp.deduct.type]]).
|
| 5155 |
+
|
| 5156 |
+
[*Example 1*:
|
| 5157 |
|
| 5158 |
``` cpp
|
| 5159 |
template<class T> void f(std::initializer_list<T>);
|
| 5160 |
f({1,2,3}); // T deduced to int
|
| 5161 |
f({1,"asdf"}); // error: T deduced to both int and const char*
|
| 5162 |
|
| 5163 |
template<class T> void g(T);
|
| 5164 |
g({1,2,3}); // error: no argument deduced for T
|
| 5165 |
+
|
| 5166 |
+
template<class T, int N> void h(T const(&)[N]);
|
| 5167 |
+
h({1,2,3}); // T deduced to int, N deduced to 3
|
| 5168 |
+
|
| 5169 |
+
template<class T> void j(T const(&)[3]);
|
| 5170 |
+
j({42}); // T deduced to int, array bound not considered
|
| 5171 |
+
|
| 5172 |
+
struct Aggr { int i; int j; };
|
| 5173 |
+
template<int N> void k(Aggr const(&)[N]);
|
| 5174 |
+
k({1,2,3}); // error: deduction fails, no conversion from int to Aggr
|
| 5175 |
+
k({{1},{2},{3}}); // OK, N deduced to 3
|
| 5176 |
+
|
| 5177 |
+
template<int M, int N> void m(int const(&)[M][N]);
|
| 5178 |
+
m({{1,2},{3,4}}); // M and N both deduced to 2
|
| 5179 |
+
|
| 5180 |
+
template<class T, int N> void n(T const(&)[N], T);
|
| 5181 |
+
n({{1},{2},{3}},Aggr()); // OK, T is Aggr, N is 3
|
| 5182 |
```
|
| 5183 |
|
| 5184 |
+
— *end example*]
|
| 5185 |
+
|
| 5186 |
For a function parameter pack that occurs at the end of the
|
| 5187 |
+
*parameter-declaration-list*, deduction is performed for each remaining
|
| 5188 |
+
argument of the call, taking the type `P` of the *declarator-id* of the
|
| 5189 |
+
function parameter pack as the corresponding function template parameter
|
| 5190 |
+
type. Each deduction deduces template arguments for subsequent positions
|
| 5191 |
+
in the template parameter packs expanded by the function parameter pack.
|
| 5192 |
+
When a function parameter pack appears in a non-deduced context (
|
| 5193 |
+
[[temp.deduct.type]]), the type of that parameter pack is never deduced.
|
| 5194 |
+
|
| 5195 |
+
[*Example 2*:
|
| 5196 |
|
| 5197 |
``` cpp
|
| 5198 |
template<class ... Types> void f(Types& ...);
|
| 5199 |
template<class T1, class ... Types> void g(T1, Types ...);
|
| 5200 |
template<class T1, class ... Types> void g1(Types ..., T1);
|
|
|
|
| 5203 |
const int z = x;
|
| 5204 |
f(x, y, z); // Types is deduced to int, float, const int
|
| 5205 |
g(x, y, z); // T1 is deduced to int; Types is deduced to float, int
|
| 5206 |
g1(x, y, z); // error: Types is not deduced
|
| 5207 |
g1<int, int, int>(x, y, z); // OK, no deduction occurs
|
|
|
|
| 5208 |
}
|
| 5209 |
```
|
| 5210 |
|
| 5211 |
+
— *end example*]
|
| 5212 |
+
|
| 5213 |
If `P` is not a reference type:
|
| 5214 |
|
| 5215 |
- If `A` is an array type, the pointer type produced by the
|
| 5216 |
array-to-pointer standard conversion ([[conv.array]]) is used in
|
| 5217 |
place of `A` for type deduction; otherwise,
|
| 5218 |
- If `A` is a function type, the pointer type produced by the
|
| 5219 |
function-to-pointer standard conversion ([[conv.func]]) is used in
|
| 5220 |
place of `A` for type deduction; otherwise,
|
| 5221 |
+
- If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s
|
| 5222 |
type are ignored for type deduction.
|
| 5223 |
|
| 5224 |
+
If `P` is a cv-qualified type, the top-level cv-qualifiers of `P`’s type
|
| 5225 |
are ignored for type deduction. If `P` is a reference type, the type
|
| 5226 |
+
referred to by `P` is used for type deduction.
|
| 5227 |
+
|
| 5228 |
+
[*Example 3*:
|
| 5229 |
+
|
| 5230 |
+
``` cpp
|
| 5231 |
+
template<class T> int f(const T&);
|
| 5232 |
+
int n1 = f(5); // calls f<int>(const int&)
|
| 5233 |
+
const int i = 0;
|
| 5234 |
+
int n2 = f(i); // calls f<int>(const int&)
|
| 5235 |
+
template <class T> int g(volatile T&);
|
| 5236 |
+
int n3 = g(i); // calls g<const int>(const volatile int&)
|
| 5237 |
+
```
|
| 5238 |
+
|
| 5239 |
+
— *end example*]
|
| 5240 |
+
|
| 5241 |
+
A *forwarding reference* is an rvalue reference to a cv-unqualified
|
| 5242 |
+
template parameter that does not represent a template parameter of a
|
| 5243 |
+
class template (during class template argument deduction (
|
| 5244 |
+
[[over.match.class.deduct]])). If `P` is a forwarding reference and the
|
| 5245 |
+
argument is an lvalue, the type “lvalue reference to `A`” is used in
|
| 5246 |
+
place of `A` for type deduction.
|
| 5247 |
+
|
| 5248 |
+
[*Example 4*:
|
| 5249 |
|
| 5250 |
``` cpp
|
| 5251 |
+
template <class T> int f(T&& heisenreference);
|
| 5252 |
template <class T> int g(const T&&);
|
| 5253 |
int i;
|
| 5254 |
int n1 = f(i); // calls f<int&>(int&)
|
| 5255 |
int n2 = f(0); // calls f<int>(int&&)
|
| 5256 |
int n3 = g(i); // error: would call g<int>(const int&&), which
|
| 5257 |
// would bind an rvalue reference to an lvalue
|
| 5258 |
+
|
| 5259 |
+
template <class T> struct A {
|
| 5260 |
+
template <class U>
|
| 5261 |
+
A(T&&, U&&, int*); // #1: T&& is not a forwarding reference.
|
| 5262 |
+
// U&& is a forwarding reference.
|
| 5263 |
+
A(T&&, int*); // #2
|
| 5264 |
+
};
|
| 5265 |
+
|
| 5266 |
+
template <class T> A(T&&, int*) -> A<T>; // #3: T&& is a forwarding reference.
|
| 5267 |
+
|
| 5268 |
+
int *ip;
|
| 5269 |
+
A a{i, 0, ip}; // error: cannot deduce from #1
|
| 5270 |
+
A a0{0, 0, ip}; // uses #1 to deduce A<int> and #1 to initialize
|
| 5271 |
+
A a2{i, ip}; // uses #3 to deduce A<int&> and #2 to initialize
|
| 5272 |
```
|
| 5273 |
|
| 5274 |
+
— *end example*]
|
| 5275 |
+
|
| 5276 |
In general, the deduction process attempts to find template argument
|
| 5277 |
values that will make the deduced `A` identical to `A` (after the type
|
| 5278 |
`A` is transformed as described above). However, there are three cases
|
| 5279 |
that allow a difference:
|
| 5280 |
|
| 5281 |
- If the original `P` is a reference type, the deduced `A` (i.e., the
|
| 5282 |
type referred to by the reference) can be more cv-qualified than the
|
| 5283 |
transformed `A`.
|
| 5284 |
- The transformed `A` can be another pointer or pointer to member type
|
| 5285 |
+
that can be converted to the deduced `A` via a function pointer
|
| 5286 |
+
conversion ([[conv.fctptr]]) and/or qualification conversion (
|
| 5287 |
+
[[conv.qual]]).
|
| 5288 |
- If `P` is a class and `P` has the form *simple-template-id*, then the
|
| 5289 |
transformed `A` can be a derived class of the deduced `A`. Likewise,
|
| 5290 |
if `P` is a pointer to a class of the form *simple-template-id*, the
|
| 5291 |
transformed `A` can be a pointer to a derived class pointed to by the
|
| 5292 |
deduced `A`.
|
| 5293 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5294 |
These alternatives are considered only if type deduction would otherwise
|
| 5295 |
fail. If they yield more than one possible deduced `A`, the type
|
| 5296 |
+
deduction fails.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5297 |
|
| 5298 |
+
[*Note 1*: If a *template-parameter* is not used in any of the function
|
| 5299 |
+
parameters of a function template, or is used only in a non-deduced
|
| 5300 |
+
context, its corresponding *template-argument* cannot be deduced from a
|
| 5301 |
+
function call and the *template-argument* must be explicitly
|
| 5302 |
+
specified. — *end note*]
|
| 5303 |
+
|
| 5304 |
+
When `P` is a function type, function pointer type, or pointer to member
|
| 5305 |
+
function type:
|
| 5306 |
|
| 5307 |
- If the argument is an overload set containing one or more function
|
| 5308 |
templates, the parameter is treated as a non-deduced context.
|
| 5309 |
- If the argument is an overload set (not containing function
|
| 5310 |
templates), trial argument deduction is attempted using each of the
|
| 5311 |
members of the set. If deduction succeeds for only one of the overload
|
| 5312 |
set members, that member is used as the argument value for the
|
| 5313 |
deduction. If deduction succeeds for more than one member of the
|
| 5314 |
overload set the parameter is treated as a non-deduced context.
|
| 5315 |
+
|
| 5316 |
+
[*Example 5*:
|
| 5317 |
+
|
| 5318 |
``` cpp
|
| 5319 |
+
// Only one function of an overload set matches the call so the function parameter is a deduced context.
|
|
|
|
| 5320 |
template <class T> int f(T (*p)(T));
|
| 5321 |
int g(int);
|
| 5322 |
int g(char);
|
| 5323 |
int i = f(g); // calls f(int (*)(int))
|
| 5324 |
```
|
| 5325 |
|
| 5326 |
+
— *end example*]
|
| 5327 |
+
|
| 5328 |
+
[*Example 6*:
|
| 5329 |
+
|
| 5330 |
``` cpp
|
| 5331 |
+
// Ambiguous deduction causes the second function parameter to be a non-deduced context.
|
|
|
|
| 5332 |
template <class T> int f(T, T (*p)(T));
|
| 5333 |
int g(int);
|
| 5334 |
char g(char);
|
| 5335 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 5336 |
```
|
| 5337 |
|
| 5338 |
+
— *end example*]
|
| 5339 |
+
|
| 5340 |
+
[*Example 7*:
|
| 5341 |
+
|
| 5342 |
``` cpp
|
| 5343 |
+
// The overload set contains a template, causing the second function parameter to be a non-deduced context.
|
|
|
|
| 5344 |
template <class T> int f(T, T (*p)(T));
|
| 5345 |
char g(char);
|
| 5346 |
template <class T> T g(T);
|
| 5347 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 5348 |
```
|
| 5349 |
|
| 5350 |
+
— *end example*]
|
| 5351 |
+
|
| 5352 |
+
If deduction succeeds for all parameters that contain
|
| 5353 |
+
*template-parameter*s that participate in template argument deduction,
|
| 5354 |
+
and all template arguments are explicitly specified, deduced, or
|
| 5355 |
+
obtained from default template arguments, remaining parameters are then
|
| 5356 |
+
compared with the corresponding arguments. For each remaining parameter
|
| 5357 |
+
`P` with a type that was non-dependent before substitution of any
|
| 5358 |
+
explicitly-specified template arguments, if the corresponding argument
|
| 5359 |
+
`A` cannot be implicitly converted to `P`, deduction fails.
|
| 5360 |
+
|
| 5361 |
+
[*Note 2*: Parameters with dependent types in which no
|
| 5362 |
+
*template-parameter*s participate in template argument deduction, and
|
| 5363 |
+
parameters that became non-dependent due to substitution of
|
| 5364 |
+
explicitly-specified template arguments, will be checked during overload
|
| 5365 |
+
resolution. — *end note*]
|
| 5366 |
+
|
| 5367 |
+
[*Example 8*:
|
| 5368 |
+
|
| 5369 |
+
``` cpp
|
| 5370 |
+
template <class T> struct Z {
|
| 5371 |
+
typedef typename T::x xx;
|
| 5372 |
+
};
|
| 5373 |
+
template <class T> typename Z<T>::xx f(void *, T); // #1
|
| 5374 |
+
template <class T> void f(int, T); // #2
|
| 5375 |
+
struct A {} a;
|
| 5376 |
+
int main() {
|
| 5377 |
+
f(1, a); // OK, deduction fails for #1 because there is no conversion from int to void*
|
| 5378 |
+
}
|
| 5379 |
+
```
|
| 5380 |
+
|
| 5381 |
+
— *end example*]
|
| 5382 |
+
|
| 5383 |
#### Deducing template arguments taking the address of a function template <a id="temp.deduct.funcaddr">[[temp.deduct.funcaddr]]</a>
|
| 5384 |
|
| 5385 |
Template arguments can be deduced from the type specified when taking
|
| 5386 |
the address of an overloaded function ([[over.over]]). The function
|
| 5387 |
template’s function type and the specified type are used as the types of
|
|
|
|
| 5411 |
array-to-pointer standard conversion ([[conv.array]]) is used in
|
| 5412 |
place of `P` for type deduction; otherwise,
|
| 5413 |
- If `P` is a function type, the pointer type produced by the
|
| 5414 |
function-to-pointer standard conversion ([[conv.func]]) is used in
|
| 5415 |
place of `P` for type deduction; otherwise,
|
| 5416 |
+
- If `P` is a cv-qualified type, the top-level cv-qualifiers of `P`’s
|
| 5417 |
type are ignored for type deduction.
|
| 5418 |
|
| 5419 |
+
If `A` is a cv-qualified type, the top-level cv-qualifiers of `A`’s type
|
| 5420 |
are ignored for type deduction. If `A` is a reference type, the type
|
| 5421 |
referred to by `A` is used for type deduction.
|
| 5422 |
|
| 5423 |
In general, the deduction process attempts to find template argument
|
| 5424 |
values that will make the deduced `A` identical to `A`. However, there
|
| 5425 |
+
are four cases that allow a difference:
|
| 5426 |
|
| 5427 |
- If the original `A` is a reference type, `A` can be more cv-qualified
|
| 5428 |
than the deduced `A` (i.e., the type referred to by the reference)
|
| 5429 |
+
- If the original `A` is a function pointer type, `A` can be “pointer to
|
| 5430 |
+
function” even if the deduced `A` is “pointer to noexcept function”.
|
| 5431 |
+
- If the original `A` is a pointer to member function type, `A` can be
|
| 5432 |
+
“pointer to member of type function” even if the deduced `A` is
|
| 5433 |
+
“pointer to member of type noexcept function”.
|
| 5434 |
- The deduced `A` can be another pointer or pointer to member type that
|
| 5435 |
can be converted to `A` via a qualification conversion.
|
| 5436 |
|
| 5437 |
These alternatives are considered only if type deduction would otherwise
|
| 5438 |
fail. If they yield more than one possible deduced `A`, the type
|
|
|
|
| 5444 |
|
| 5445 |
If `A` is a type
|
| 5446 |
|
| 5447 |
and `P` is a type
|
| 5448 |
|
| 5449 |
+
then the cv-unqualified `T1` and `T2` are used as the types of `A` and
|
| 5450 |
+
`P` respectively for type deduction.
|
| 5451 |
+
|
| 5452 |
+
[*Example 1*:
|
| 5453 |
|
| 5454 |
``` cpp
|
| 5455 |
struct A {
|
| 5456 |
template <class T> operator T***();
|
| 5457 |
};
|
| 5458 |
A a;
|
| 5459 |
const int * const * const * p1 = a; // T is deduced as int, not const int
|
| 5460 |
```
|
| 5461 |
|
| 5462 |
+
— *end example*]
|
| 5463 |
+
|
| 5464 |
#### Deducing template arguments during partial ordering <a id="temp.deduct.partial">[[temp.deduct.partial]]</a>
|
| 5465 |
|
| 5466 |
Template argument deduction is done by comparing certain types
|
| 5467 |
associated with the two function templates being compared.
|
| 5468 |
|
| 5469 |
Two sets of types are used to determine the partial ordering. For each
|
| 5470 |
of the templates involved there is the original function type and the
|
| 5471 |
+
transformed function type.
|
| 5472 |
+
|
| 5473 |
+
[*Note 1*: The creation of the transformed type is described in
|
| 5474 |
+
[[temp.func.order]]. — *end note*]
|
| 5475 |
+
|
| 5476 |
+
The deduction process uses the transformed type as the argument template
|
| 5477 |
+
and the original type of the other template as the parameter template.
|
| 5478 |
+
This process is done twice for each type involved in the partial
|
| 5479 |
+
ordering comparison: once using the transformed template-1 as the
|
| 5480 |
+
argument template and template-2 as the parameter template and again
|
| 5481 |
+
using the transformed template-2 as the argument template and template-1
|
| 5482 |
+
as the parameter template.
|
| 5483 |
|
| 5484 |
The types used to determine the ordering depend on the context in which
|
| 5485 |
the partial ordering is done:
|
| 5486 |
|
| 5487 |
- In the context of a function call, the types used are those function
|
|
|
|
| 5491 |
- In other contexts ([[temp.func.order]]) the function template’s
|
| 5492 |
function type is used.
|
| 5493 |
|
| 5494 |
Each type nominated above from the parameter template and the
|
| 5495 |
corresponding type from the argument template are used as the types of
|
| 5496 |
+
`P` and `A`. If a particular `P` contains no *template-parameter*s that
|
| 5497 |
+
participate in template argument deduction, that `P` is not used to
|
| 5498 |
+
determine the ordering.
|
| 5499 |
|
| 5500 |
Before the partial ordering is done, certain transformations are
|
| 5501 |
performed on the types used for partial ordering:
|
| 5502 |
|
| 5503 |
- If `P` is a reference type, `P` is replaced by the type referred to.
|
|
|
|
| 5514 |
- If `P` is a cv-qualified type, `P` is replaced by the cv-unqualified
|
| 5515 |
version of `P`.
|
| 5516 |
- If `A` is a cv-qualified type, `A` is replaced by the cv-unqualified
|
| 5517 |
version of `A`.
|
| 5518 |
|
| 5519 |
+
Using the resulting types `P` and `A`, the deduction is then done as
|
| 5520 |
+
described in [[temp.deduct.type]]. If `P` is a function parameter pack,
|
| 5521 |
+
the type `A` of each remaining parameter type of the argument template
|
| 5522 |
+
is compared with the type `P` of the *declarator-id* of the function
|
| 5523 |
+
parameter pack. Each comparison deduces template arguments for
|
| 5524 |
+
subsequent positions in the template parameter packs expanded by the
|
| 5525 |
+
function parameter pack. Similarly, if `A` was transformed from a
|
| 5526 |
+
function parameter pack, it is compared with each remaining parameter
|
| 5527 |
+
type of the parameter template. If deduction succeeds for a given type,
|
| 5528 |
+
the type from the argument template is considered to be at least as
|
| 5529 |
+
specialized as the type from the parameter template.
|
| 5530 |
+
|
| 5531 |
+
[*Example 1*:
|
| 5532 |
|
| 5533 |
``` cpp
|
| 5534 |
template<class... Args> void f(Args... args); // #1
|
| 5535 |
template<class T1, class... Args> void f(T1 a1, Args... args); // #2
|
| 5536 |
template<class T1, class T2> void f(T1 a1, T2 a2); // #3
|
| 5537 |
|
| 5538 |
f(); // calls #1
|
| 5539 |
f(1, 2, 3); // calls #2
|
| 5540 |
+
f(1, 2); // calls #3; non-variadic template #3 is more specialized
|
| 5541 |
+
// than the variadic templates #1 and #2
|
| 5542 |
```
|
| 5543 |
|
| 5544 |
+
— *end example*]
|
| 5545 |
+
|
| 5546 |
If, for a given type, deduction succeeds in both directions (i.e., the
|
| 5547 |
types are identical after the transformations above) and both `P` and
|
| 5548 |
`A` were reference types (before being replaced with the type referred
|
| 5549 |
to above):
|
| 5550 |
|
| 5551 |
- if the type from the argument template was an lvalue reference and the
|
| 5552 |
+
type from the parameter template was not, the parameter type is not
|
| 5553 |
+
considered to be at least as specialized as the argument type;
|
| 5554 |
+
otherwise,
|
| 5555 |
- if the type from the argument template is more cv-qualified than the
|
| 5556 |
+
type from the parameter template (as described above), the parameter
|
| 5557 |
+
type is not considered to be at least as specialized as the argument
|
| 5558 |
+
type.
|
| 5559 |
|
| 5560 |
+
Function template `F` is *at least as specialized as* function template
|
| 5561 |
+
`G` if, for each pair of types used to determine the ordering, the type
|
| 5562 |
+
from `F` is at least as specialized as the type from `G`. `F` is *more
|
| 5563 |
+
specialized than* `G` if `F` is at least as specialized as `G` and `G`
|
| 5564 |
+
is not at least as specialized as `F`.
|
| 5565 |
+
|
| 5566 |
+
If, after considering the above, function template `F` is at least as
|
| 5567 |
+
specialized as function template `G` and vice-versa, and if `G` has a
|
| 5568 |
+
trailing parameter pack for which `F` does not have a corresponding
|
| 5569 |
+
parameter, and if `F` does not have a trailing parameter pack, then `F`
|
| 5570 |
+
is more specialized than `G`.
|
| 5571 |
|
| 5572 |
In most cases, all template parameters must have values in order for
|
| 5573 |
deduction to succeed, but for partial ordering purposes a template
|
| 5574 |
parameter may remain without a value provided it is not used in the
|
| 5575 |
+
types being used for partial ordering.
|
| 5576 |
+
|
| 5577 |
+
[*Note 2*: A template parameter used in a non-deduced context is
|
| 5578 |
+
considered used. — *end note*]
|
| 5579 |
+
|
| 5580 |
+
[*Example 2*:
|
| 5581 |
|
| 5582 |
``` cpp
|
| 5583 |
template <class T> T f(int); // #1
|
| 5584 |
template <class T, class U> T f(U); // #2
|
| 5585 |
void g() {
|
| 5586 |
f<int>(1); // calls #1
|
| 5587 |
}
|
| 5588 |
```
|
| 5589 |
|
| 5590 |
+
— *end example*]
|
| 5591 |
+
|
| 5592 |
+
[*Note 3*: Partial ordering of function templates containing template
|
| 5593 |
+
parameter packs is independent of the number of deduced arguments for
|
| 5594 |
+
those template parameter packs. — *end note*]
|
| 5595 |
+
|
| 5596 |
+
[*Example 3*:
|
| 5597 |
|
| 5598 |
``` cpp
|
| 5599 |
template<class ...> struct Tuple { };
|
| 5600 |
template<class ... Types> void g(Tuple<Types ...>); // #1
|
| 5601 |
template<class T1, class ... Types> void g(Tuple<T1, Types ...>); // #2
|
|
|
|
| 5605 |
g(Tuple<int, float>()); // calls #2
|
| 5606 |
g(Tuple<int, float&>()); // calls #3
|
| 5607 |
g(Tuple<int>()); // calls #3
|
| 5608 |
```
|
| 5609 |
|
| 5610 |
+
— *end example*]
|
| 5611 |
+
|
| 5612 |
#### Deducing template arguments from a type <a id="temp.deduct.type">[[temp.deduct.type]]</a>
|
| 5613 |
|
| 5614 |
Template arguments can be deduced in several different contexts, but in
|
| 5615 |
each case a type that is specified in terms of template parameters (call
|
| 5616 |
it `P`) is compared with an actual type (call it `A`), and an attempt is
|
|
|
|
| 5625 |
deduced template argument values are then combined. If type deduction
|
| 5626 |
cannot be done for any `P/A` pair, or if for any pair the deduction
|
| 5627 |
leads to more than one possible set of deduced values, or if different
|
| 5628 |
pairs yield different deduced values, or if any template argument
|
| 5629 |
remains neither deduced nor explicitly specified, template argument
|
| 5630 |
+
deduction fails. The type of a type parameter is only deduced from an
|
| 5631 |
+
array bound if it is not otherwise deduced.
|
| 5632 |
|
| 5633 |
A given type `P` can be composed from a number of other types,
|
| 5634 |
templates, and non-type values:
|
| 5635 |
|
| 5636 |
- A function type includes the types of each of the function parameters
|
|
|
|
| 5651 |
deduction, but instead uses the values of template arguments that were
|
| 5652 |
either deduced elsewhere or explicitly specified. If a template
|
| 5653 |
parameter is used only in non-deduced contexts and is not explicitly
|
| 5654 |
specified, template argument deduction fails.
|
| 5655 |
|
| 5656 |
+
[*Note 1*: Under [[temp.deduct.call]] and [[temp.deduct.partial]], if
|
| 5657 |
+
`P` contains no *template-parameter*s that appear in deduced contexts,
|
| 5658 |
+
no deduction is done, so `P` and `A` need not have the same
|
| 5659 |
+
form. — *end note*]
|
| 5660 |
+
|
| 5661 |
The non-deduced contexts are:
|
| 5662 |
|
| 5663 |
- The *nested-name-specifier* of a type that was specified using a
|
| 5664 |
*qualified-id*.
|
| 5665 |
- The *expression* of a *decltype-specifier*.
|
|
|
|
| 5677 |
- no function matches the function parameter type, or
|
| 5678 |
- the set of functions supplied as an argument contains one or more
|
| 5679 |
function templates.
|
| 5680 |
- A function parameter for which the associated argument is an
|
| 5681 |
initializer list ([[dcl.init.list]]) but the parameter does not have
|
| 5682 |
+
a type for which deduction from an initializer list is specified (
|
| 5683 |
+
[[temp.deduct.call]]).
|
| 5684 |
+
\[*Example 1*:
|
| 5685 |
``` cpp
|
| 5686 |
template<class T> void g(T);
|
| 5687 |
g({1,2,3}); // error: no argument deduced for T
|
| 5688 |
```
|
| 5689 |
+
|
| 5690 |
+
— *end example*]
|
| 5691 |
- A function parameter pack that does not occur at the end of the
|
| 5692 |
*parameter-declaration-list*.
|
| 5693 |
|
| 5694 |
When a type name is specified in a way that includes a non-deduced
|
| 5695 |
context, all of the types that comprise that type name are also
|
| 5696 |
non-deduced. However, a compound type can include both deduced and
|
| 5697 |
+
non-deduced types.
|
| 5698 |
+
|
| 5699 |
+
[*Example 2*: If a type is specified as `A<T>::B<T2>`, both `T` and
|
| 5700 |
`T2` are non-deduced. Likewise, if a type is specified as
|
| 5701 |
`A<I+J>::X<T>`, `I`, `J`, and `T` are non-deduced. If a type is
|
| 5702 |
specified as `void` `f(typename` `A<T>::B,` `A<T>)`, the `T` in
|
| 5703 |
+
`A<T>::B` is non-deduced but the `T` in `A<T>` is
|
| 5704 |
+
deduced. — *end example*]
|
| 5705 |
+
|
| 5706 |
+
[*Example 3*:
|
| 5707 |
|
| 5708 |
Here is an example in which different parameter/argument pairs produce
|
| 5709 |
inconsistent template argument deductions:
|
| 5710 |
|
| 5711 |
``` cpp
|
| 5712 |
+
template<class T> void f(T x, T y) { ... }
|
| 5713 |
+
struct A { ... };
|
| 5714 |
+
struct B : A { ... };
|
| 5715 |
void g(A a, B b) {
|
| 5716 |
f(a,b); // error: T could be A or B
|
| 5717 |
f(b,a); // error: T could be A or B
|
| 5718 |
f(a,a); // OK: T is A
|
| 5719 |
f(b,b); // OK: T is B
|
|
|
|
| 5764 |
f(d); // calls f(B<int>&)
|
| 5765 |
f(d2); // calls f(B<int>&)
|
| 5766 |
}
|
| 5767 |
```
|
| 5768 |
|
| 5769 |
+
— *end example*]
|
| 5770 |
+
|
| 5771 |
A template type argument `T`, a template template argument `TT` or a
|
| 5772 |
template non-type argument `i` can be deduced if `P` and `A` have one of
|
| 5773 |
the following forms:
|
| 5774 |
|
| 5775 |
``` cpp
|
|
|
|
| 5798 |
TT<T>
|
| 5799 |
TT<i>
|
| 5800 |
TT<>
|
| 5801 |
```
|
| 5802 |
|
| 5803 |
+
where `(T)` represents a parameter-type-list ([[dcl.fct]]) where at
|
| 5804 |
+
least one parameter type contains a `T`, and `()` represents a
|
| 5805 |
+
parameter-type-list where no parameter type contains a `T`. Similarly,
|
| 5806 |
`<T>` represents template argument lists where at least one argument
|
| 5807 |
contains a `T`, `<i>` represents template argument lists where at least
|
| 5808 |
one argument contains an `i` and `<>` represents template argument lists
|
| 5809 |
where no argument contains a `T` or an `i`.
|
| 5810 |
|
| 5811 |
If `P` has a form that contains `<T>` or `<i>`, then each argument Pᵢ of
|
| 5812 |
+
the respective template argument list of `P` is compared with the
|
| 5813 |
corresponding argument Aᵢ of the corresponding template argument list of
|
| 5814 |
`A`. If the template argument list of `P` contains a pack expansion that
|
| 5815 |
is not the last template argument, the entire template argument list is
|
| 5816 |
a non-deduced context. If `Pᵢ` is a pack expansion, then the pattern of
|
| 5817 |
`Pᵢ` is compared with each remaining argument in the template argument
|
|
|
|
| 5823 |
- if `P` does not contain a template argument corresponding to `Aᵢ` then
|
| 5824 |
`Aᵢ` is ignored;
|
| 5825 |
- otherwise, if `Pᵢ` is not a pack expansion, template argument
|
| 5826 |
deduction fails.
|
| 5827 |
|
| 5828 |
+
[*Example 4*:
|
| 5829 |
+
|
| 5830 |
``` cpp
|
| 5831 |
template<class T1, class... Z> class S; // #1
|
| 5832 |
template<class T1, class... Z> class S<T1, const Z&...> { }; // #2
|
| 5833 |
template<class T1, class T2> class S<T1, const T2&> { }; // #3
|
| 5834 |
S<int, const int&> s; // both #2 and #3 match; #3 is more specialized
|
|
|
|
| 5837 |
template<class T1, class T2, class... U> struct A<T1, T2*, U...> { }; // #2
|
| 5838 |
template<class T1, class T2> struct A<T1, T2> { }; // #3
|
| 5839 |
template struct A<int, int*>; // selects #2
|
| 5840 |
```
|
| 5841 |
|
| 5842 |
+
— *end example*]
|
| 5843 |
+
|
| 5844 |
Similarly, if `P` has a form that contains `(T)`, then each parameter
|
| 5845 |
+
type `Pᵢ` of the respective parameter-type-list ([[dcl.fct]]) of `P` is
|
| 5846 |
+
compared with the corresponding parameter type `Aᵢ` of the corresponding
|
| 5847 |
+
parameter-type-list of `A`. If `P` and `A` are function types that
|
| 5848 |
originated from deduction when taking the address of a function
|
| 5849 |
template ([[temp.deduct.funcaddr]]) or when deducing template arguments
|
| 5850 |
from a function declaration ([[temp.deduct.decl]]) and `Pᵢ` and `Aᵢ`
|
| 5851 |
+
are parameters of the top-level parameter-type-list of `P` and `A`,
|
| 5852 |
+
respectively, `Pᵢ` is adjusted if it is a forwarding reference (
|
| 5853 |
+
[[temp.deduct.call]]) and `Aᵢ` is an lvalue reference, in which case the
|
| 5854 |
+
type of `Pᵢ` is changed to be the template parameter type (i.e., `T&&`
|
| 5855 |
+
is changed to simply `T`).
|
| 5856 |
+
|
| 5857 |
+
[*Note 2*: As a result, when `Pᵢ` is `T&&` and `Aᵢ` is `X&`, the
|
| 5858 |
+
adjusted `Pᵢ` will be `T`, causing `T` to be deduced as
|
| 5859 |
+
`X&`. — *end note*]
|
| 5860 |
+
|
| 5861 |
+
[*Example 5*:
|
| 5862 |
|
| 5863 |
``` cpp
|
| 5864 |
template <class T> void f(T&&);
|
| 5865 |
template <> void f(int&) { } // #1
|
| 5866 |
template <> void f(int&&) { } // #2
|
|
|
|
| 5868 |
f(i); // calls f<int&>(int&), i.e., #1
|
| 5869 |
f(0); // calls f<int>(int&&), i.e., #2
|
| 5870 |
}
|
| 5871 |
```
|
| 5872 |
|
| 5873 |
+
— *end example*]
|
| 5874 |
+
|
| 5875 |
If the *parameter-declaration* corresponding to `Pᵢ` is a function
|
| 5876 |
parameter pack, then the type of its *declarator-id* is compared with
|
| 5877 |
+
each remaining parameter type in the parameter-type-list of `A`. Each
|
| 5878 |
comparison deduces template arguments for subsequent positions in the
|
| 5879 |
template parameter packs expanded by the function parameter pack. During
|
| 5880 |
partial ordering ([[temp.deduct.partial]]), if `Aᵢ` was originally a
|
| 5881 |
function parameter pack:
|
| 5882 |
|
| 5883 |
- if `P` does not contain a function parameter type corresponding to
|
| 5884 |
`Aᵢ` then `Aᵢ` is ignored;
|
| 5885 |
- otherwise, if `Pᵢ` is not a function parameter pack, template argument
|
| 5886 |
deduction fails.
|
| 5887 |
|
| 5888 |
+
[*Example 6*:
|
| 5889 |
+
|
| 5890 |
``` cpp
|
| 5891 |
template<class T, class... U> void f(T*, U...) { } // #1
|
| 5892 |
template<class T> void f(T) { } // #2
|
| 5893 |
template void f(int*); // selects #1
|
| 5894 |
```
|
| 5895 |
|
| 5896 |
+
— *end example*]
|
| 5897 |
+
|
| 5898 |
These forms can be used in the same way as `T` is for further
|
| 5899 |
composition of types.
|
| 5900 |
|
| 5901 |
+
[*Example 7*:
|
| 5902 |
+
|
| 5903 |
``` cpp
|
| 5904 |
X<int> (*)(char[6])
|
| 5905 |
```
|
| 5906 |
|
| 5907 |
is of the form
|
|
|
|
| 5916 |
type (*)(T)
|
| 5917 |
```
|
| 5918 |
|
| 5919 |
where type is `X<int>` and `T` is `char[6]`.
|
| 5920 |
|
| 5921 |
+
— *end example*]
|
| 5922 |
+
|
| 5923 |
Template arguments cannot be deduced from function arguments involving
|
| 5924 |
constructs other than the ones specified above.
|
| 5925 |
|
| 5926 |
+
When the value of the argument corresponding to a non-type template
|
| 5927 |
+
parameter `P` that is declared with a dependent type is deduced from an
|
| 5928 |
+
expression, the template parameters in the type of `P` are deduced from
|
| 5929 |
+
the type of the value.
|
| 5930 |
+
|
| 5931 |
+
[*Example 8*:
|
| 5932 |
|
| 5933 |
``` cpp
|
| 5934 |
+
template<long n> struct A { };
|
| 5935 |
+
|
| 5936 |
+
template<typename T> struct C;
|
| 5937 |
+
template<typename T, T n> struct C<A<n>> {
|
| 5938 |
+
using Q = T;
|
| 5939 |
+
};
|
| 5940 |
+
|
| 5941 |
+
using R = long;
|
| 5942 |
+
using R = C<A<2>>::Q; // OK; T was deduced to long from the
|
| 5943 |
+
// template argument value in the type A<2>
|
| 5944 |
```
|
| 5945 |
|
| 5946 |
+
— *end example*]
|
| 5947 |
+
|
| 5948 |
+
The type of `N` in the type `T[N]` is `std::size_t`.
|
| 5949 |
+
|
| 5950 |
+
[*Example 9*:
|
| 5951 |
+
|
| 5952 |
+
``` cpp
|
| 5953 |
+
template<typename T> struct S;
|
| 5954 |
+
template<typename T, T n> struct S<int[n]> {
|
| 5955 |
+
using Q = T;
|
| 5956 |
+
};
|
| 5957 |
+
|
| 5958 |
+
using V = decltype(sizeof 0);
|
| 5959 |
+
using V = S<int[42]>::Q; // OK; T was deduced to std::size_t from the type int[42]
|
| 5960 |
+
```
|
| 5961 |
+
|
| 5962 |
+
— *end example*]
|
| 5963 |
+
|
| 5964 |
+
[*Example 10*:
|
| 5965 |
+
|
| 5966 |
+
``` cpp
|
| 5967 |
+
template<class T, T i> void f(int (&a)[i]);
|
| 5968 |
+
int v[10];
|
| 5969 |
+
void g() {
|
| 5970 |
+
f(v); // OK: T is std::size_t
|
| 5971 |
+
}
|
| 5972 |
+
```
|
| 5973 |
+
|
| 5974 |
+
— *end example*]
|
| 5975 |
+
|
| 5976 |
+
[*Note 3*:
|
| 5977 |
+
|
| 5978 |
Except for reference and pointer types, a major array bound is not part
|
| 5979 |
of a function parameter type and cannot be deduced from an argument:
|
| 5980 |
|
| 5981 |
``` cpp
|
| 5982 |
template<int i> void f1(int a[10][i]);
|
|
|
|
| 5991 |
f2<10>(v); // OK
|
| 5992 |
f3(v); // OK: i deduced to be 10
|
| 5993 |
}
|
| 5994 |
```
|
| 5995 |
|
| 5996 |
+
— *end note*]
|
| 5997 |
+
|
| 5998 |
+
[*Note 4*:
|
| 5999 |
+
|
| 6000 |
If, in the declaration of a function template with a non-type template
|
| 6001 |
parameter, the non-type template parameter is used in a subexpression in
|
| 6002 |
the function parameter list, the expression is a non-deduced context as
|
| 6003 |
specified above.
|
| 6004 |
|
| 6005 |
+
[*Example 11*:
|
| 6006 |
+
|
| 6007 |
``` cpp
|
| 6008 |
+
template <int i> class A { ... };
|
| 6009 |
template <int i> void g(A<i+1>);
|
| 6010 |
template <int i> void f(A<i>, A<i+1>);
|
| 6011 |
void k() {
|
| 6012 |
A<1> a1;
|
| 6013 |
A<2> a2;
|
|
|
|
| 6015 |
g<0>(a1); // OK
|
| 6016 |
f(a1, a2); // OK
|
| 6017 |
}
|
| 6018 |
```
|
| 6019 |
|
| 6020 |
+
— *end example*]
|
| 6021 |
+
|
| 6022 |
+
— *end note*]
|
| 6023 |
+
|
| 6024 |
+
[*Note 5*:
|
| 6025 |
+
|
| 6026 |
Template parameters do not participate in template argument deduction if
|
| 6027 |
they are used only in non-deduced contexts. For example,
|
| 6028 |
|
| 6029 |
``` cpp
|
| 6030 |
template<int i, typename T>
|
|
|
|
| 6033 |
typename B<i>::Y y); // i is not deduced here
|
| 6034 |
A<int> a;
|
| 6035 |
B<77> b;
|
| 6036 |
|
| 6037 |
int x = deduce<77>(a.xm, 62, b.ym);
|
| 6038 |
+
// T is deduced to be int, a.xm must be convertible to A<int>::X
|
| 6039 |
+
// i is explicitly specified to be 77, b.ym must be convertible to B<77>::Y
|
|
|
|
|
|
|
| 6040 |
```
|
| 6041 |
|
| 6042 |
+
— *end note*]
|
| 6043 |
+
|
| 6044 |
+
If `P` has a form that contains `<i>`, and if the type of `i` differs
|
| 6045 |
+
from the type of the corresponding template parameter of the template
|
| 6046 |
+
named by the enclosing *simple-template-id*, deduction fails. If `P` has
|
| 6047 |
+
a form that contains `[i]`, and if the type of `i` is not an integral
|
| 6048 |
+
type, deduction fails.[^8]
|
| 6049 |
+
|
| 6050 |
+
[*Example 12*:
|
| 6051 |
|
| 6052 |
``` cpp
|
| 6053 |
+
template<int i> class A { ... };
|
| 6054 |
template<short s> void f(A<s>);
|
| 6055 |
void k1() {
|
| 6056 |
A<1> a;
|
| 6057 |
f(a); // error: deduction fails for conversion from int to short
|
| 6058 |
f<1>(a); // OK
|
|
|
|
| 6064 |
B<1> b;
|
| 6065 |
g(b); // OK: cv-qualifiers are ignored on template parameter types
|
| 6066 |
}
|
| 6067 |
```
|
| 6068 |
|
| 6069 |
+
— *end example*]
|
| 6070 |
+
|
| 6071 |
A *template-argument* can be deduced from a function, pointer to
|
| 6072 |
function, or pointer to member function type.
|
| 6073 |
|
| 6074 |
+
[*Example 13*:
|
| 6075 |
+
|
| 6076 |
``` cpp
|
| 6077 |
template<class T> void f(void(*)(T,int));
|
| 6078 |
template<class T> void foo(T,int);
|
| 6079 |
void g(int,int);
|
| 6080 |
void g(char,int);
|
|
|
|
| 6086 |
f(&h); // OK: void h(char,int) is a unique match
|
| 6087 |
f(&foo); // error: type deduction fails because foo is a template
|
| 6088 |
}
|
| 6089 |
```
|
| 6090 |
|
| 6091 |
+
— *end example*]
|
| 6092 |
+
|
| 6093 |
A template *type-parameter* cannot be deduced from the type of a
|
| 6094 |
function default argument.
|
| 6095 |
|
| 6096 |
+
[*Example 14*:
|
| 6097 |
+
|
| 6098 |
``` cpp
|
| 6099 |
template <class T> void f(T = 5, T = 7);
|
| 6100 |
void g() {
|
| 6101 |
f(1); // OK: call f<int>(1,7)
|
| 6102 |
f(); // error: cannot deduce T
|
| 6103 |
f<int>(); // OK: call f<int>(5,7)
|
| 6104 |
}
|
| 6105 |
```
|
| 6106 |
|
| 6107 |
+
— *end example*]
|
| 6108 |
+
|
| 6109 |
The *template-argument* corresponding to a template *template-parameter*
|
| 6110 |
is deduced from the type of the *template-argument* of a class template
|
| 6111 |
specialization used in the argument list of a function call.
|
| 6112 |
|
| 6113 |
+
[*Example 15*:
|
| 6114 |
+
|
| 6115 |
``` cpp
|
| 6116 |
template <template <class T> class X> struct A { };
|
| 6117 |
template <template <class T> class X> void f(A<X>) { }
|
| 6118 |
template<class T> struct B { };
|
| 6119 |
A<B> ab;
|
| 6120 |
f(ab); // calls f(A<B>)
|
| 6121 |
```
|
| 6122 |
|
| 6123 |
+
— *end example*]
|
| 6124 |
+
|
| 6125 |
+
[*Note 6*: Template argument deduction involving parameter packs (
|
| 6126 |
[[temp.variadic]]) can deduce zero or more arguments for each parameter
|
| 6127 |
+
pack. — *end note*]
|
| 6128 |
+
|
| 6129 |
+
[*Example 16*:
|
| 6130 |
|
| 6131 |
``` cpp
|
| 6132 |
template<class> struct X { };
|
| 6133 |
template<class R, class ... ArgTypes> struct X<R(int, ArgTypes ...)> { };
|
| 6134 |
template<class ... Types> struct Y { };
|
|
|
|
| 6144 |
Y<int&, float&, double&> y2; // uses partial specialization; T is int&, Types contains float, double
|
| 6145 |
Y<int, float, double> y3; // uses primary template; Types contains int, float, double
|
| 6146 |
int fv = f(g); // OK; Types contains int, float
|
| 6147 |
```
|
| 6148 |
|
| 6149 |
+
— *end example*]
|
| 6150 |
+
|
| 6151 |
#### Deducing template arguments from a function declaration <a id="temp.deduct.decl">[[temp.deduct.decl]]</a>
|
| 6152 |
|
| 6153 |
In a declaration whose *declarator-id* refers to a specialization of a
|
| 6154 |
function template, template argument deduction is performed to identify
|
| 6155 |
the specialization to which the declaration refers. Specifically, this
|
|
|
|
| 6182 |
the call arguments. For each function template, if the argument
|
| 6183 |
deduction and checking succeeds, the *template-argument*s (deduced
|
| 6184 |
and/or explicit) are used to synthesize the declaration of a single
|
| 6185 |
function template specialization which is added to the candidate
|
| 6186 |
functions set to be used in overload resolution. If, for a given
|
| 6187 |
+
function template, argument deduction fails or the synthesized function
|
| 6188 |
+
template specialization would be ill-formed, no such function is added
|
| 6189 |
to the set of candidate functions for that template. The complete set of
|
| 6190 |
candidate functions includes all the synthesized declarations and all of
|
| 6191 |
the non-template overloaded functions of the same name. The synthesized
|
| 6192 |
declarations are treated like any other functions in the remainder of
|
| 6193 |
overload resolution, except as explicitly noted in
|
| 6194 |
[[over.match.best]].[^9]
|
| 6195 |
|
| 6196 |
+
[*Example 1*:
|
| 6197 |
+
|
| 6198 |
``` cpp
|
| 6199 |
template<class T> T max(T a, T b) { return a>b?a:b; }
|
| 6200 |
|
| 6201 |
void f(int a, int b, char c, char d) {
|
| 6202 |
int m1 = max(a,b); // max(int a, int b)
|
|
|
|
| 6213 |
|
| 6214 |
to the example above would resolve the third call, by providing a
|
| 6215 |
function that could be called for `max(a,c)` after using the standard
|
| 6216 |
conversion of `char` to `int` for `c`.
|
| 6217 |
|
| 6218 |
+
— *end example*]
|
| 6219 |
+
|
| 6220 |
+
[*Example 2*:
|
| 6221 |
+
|
| 6222 |
Here is an example involving conversions on a function argument involved
|
| 6223 |
in *template-argument* deduction:
|
| 6224 |
|
| 6225 |
``` cpp
|
| 6226 |
+
template<class T> struct B { ... };
|
| 6227 |
+
template<class T> struct D : public B<T> { ... };
|
| 6228 |
template<class T> void f(B<T>&);
|
| 6229 |
|
| 6230 |
void g(B<int>& bi, D<int>& di) {
|
| 6231 |
f(bi); // f(bi)
|
| 6232 |
f(di); // f((B<int>&)di)
|
| 6233 |
}
|
| 6234 |
```
|
| 6235 |
|
| 6236 |
+
— *end example*]
|
| 6237 |
+
|
| 6238 |
+
[*Example 3*:
|
| 6239 |
+
|
| 6240 |
Here is an example involving conversions on a function argument not
|
| 6241 |
involved in *template-parameter* deduction:
|
| 6242 |
|
| 6243 |
``` cpp
|
| 6244 |
template<class T> void f(T*,int); // #1
|
|
|
|
| 6251 |
f(i,c); // #2: f<int>(i,c);
|
| 6252 |
f(i,i); // #2: f<int>(i,char(i))
|
| 6253 |
}
|
| 6254 |
```
|
| 6255 |
|
| 6256 |
+
— *end example*]
|
| 6257 |
+
|
| 6258 |
Only the signature of a function template specialization is needed to
|
| 6259 |
enter the specialization in a set of candidate functions. Therefore only
|
| 6260 |
the function template declaration is needed to resolve a call for which
|
| 6261 |
a template specialization is a candidate.
|
| 6262 |
|
| 6263 |
+
[*Example 4*:
|
| 6264 |
+
|
| 6265 |
``` cpp
|
| 6266 |
template<class T> void f(T); // declaration
|
| 6267 |
|
| 6268 |
void g() {
|
| 6269 |
f("Annemarie"); // call of f<const char*>
|
|
|
|
| 6273 |
The call of `f` is well-formed even if the template `f` is only declared
|
| 6274 |
and not defined at the point of the call. The program will be ill-formed
|
| 6275 |
unless a specialization for `f<const char*>`, either implicitly or
|
| 6276 |
explicitly generated, is present in some translation unit.
|
| 6277 |
|
| 6278 |
+
— *end example*]
|
| 6279 |
+
|
| 6280 |
+
## Deduction guides <a id="temp.deduct.guide">[[temp.deduct.guide]]</a>
|
| 6281 |
+
|
| 6282 |
+
Deduction guides are used when a *template-name* appears as a type
|
| 6283 |
+
specifier for a deduced class type ([[dcl.type.class.deduct]]).
|
| 6284 |
+
Deduction guides are not found by name lookup. Instead, when performing
|
| 6285 |
+
class template argument deduction ([[over.match.class.deduct]]), any
|
| 6286 |
+
deduction guides declared for the class template are considered.
|
| 6287 |
+
|
| 6288 |
+
``` bnf
|
| 6289 |
+
deduction-guide:
|
| 6290 |
+
'explicit'ₒₚₜ template-name '(' parameter-declaration-clause ') ->' simple-template-id ';'
|
| 6291 |
+
```
|
| 6292 |
+
|
| 6293 |
+
[*Example 1*:
|
| 6294 |
+
|
| 6295 |
+
``` cpp
|
| 6296 |
+
template<class T, class D = int>
|
| 6297 |
+
struct S {
|
| 6298 |
+
T data;
|
| 6299 |
+
};
|
| 6300 |
+
template<class U>
|
| 6301 |
+
S(U) -> S<typename U::type>;
|
| 6302 |
+
|
| 6303 |
+
struct A {
|
| 6304 |
+
using type = short;
|
| 6305 |
+
operator type();
|
| 6306 |
+
};
|
| 6307 |
+
S x{A()}; // x is of type S<short, int>
|
| 6308 |
+
```
|
| 6309 |
+
|
| 6310 |
+
— *end example*]
|
| 6311 |
+
|
| 6312 |
+
The same restrictions apply to the *parameter-declaration-clause* of a
|
| 6313 |
+
deduction guide as in a function declaration ([[dcl.fct]]). The
|
| 6314 |
+
*simple-template-id* shall name a class template specialization. The
|
| 6315 |
+
*template-name* shall be the same *identifier* as the *template-name* of
|
| 6316 |
+
the *simple-template-id*. A *deduction-guide* shall be declared in the
|
| 6317 |
+
same scope as the corresponding class template and, for a member class
|
| 6318 |
+
template, with the same access. Two deduction guide declarations in the
|
| 6319 |
+
same translation unit for the same class template shall not have
|
| 6320 |
+
equivalent *parameter-declaration-clause*s.
|
| 6321 |
+
|
| 6322 |
<!-- Link reference definitions -->
|
| 6323 |
+
[basic.def]: basic.md#basic.def
|
| 6324 |
[basic.def.odr]: basic.md#basic.def.odr
|
| 6325 |
[basic.link]: basic.md#basic.link
|
| 6326 |
[basic.lookup]: basic.md#basic.lookup
|
| 6327 |
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
| 6328 |
[basic.lookup.classref]: basic.md#basic.lookup.classref
|
|
|
|
| 6337 |
[class.access]: class.md#class.access
|
| 6338 |
[class.base.init]: special.md#class.base.init
|
| 6339 |
[class.derived]: class.md#class.derived
|
| 6340 |
[class.dtor]: special.md#class.dtor
|
| 6341 |
[class.friend]: class.md#class.friend
|
| 6342 |
+
[class.local]: class.md#class.local
|
| 6343 |
[class.mem]: class.md#class.mem
|
| 6344 |
[class.member.lookup]: class.md#class.member.lookup
|
| 6345 |
[class.qual]: basic.md#class.qual
|
| 6346 |
+
[class.temporary]: special.md#class.temporary
|
| 6347 |
[conv]: conv.md#conv
|
| 6348 |
[conv.array]: conv.md#conv.array
|
| 6349 |
+
[conv.fctptr]: conv.md#conv.fctptr
|
| 6350 |
[conv.func]: conv.md#conv.func
|
|
|
|
|
|
|
|
|
|
| 6351 |
[conv.qual]: conv.md#conv.qual
|
| 6352 |
[dcl.align]: dcl.md#dcl.align
|
| 6353 |
[dcl.attr.grammar]: dcl.md#dcl.attr.grammar
|
| 6354 |
[dcl.dcl]: dcl.md#dcl.dcl
|
| 6355 |
+
[dcl.enum]: dcl.md#dcl.enum
|
| 6356 |
[dcl.fct]: dcl.md#dcl.fct
|
| 6357 |
+
[dcl.fct.def.general]: dcl.md#dcl.fct.def.general
|
| 6358 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 6359 |
[dcl.init]: dcl.md#dcl.init
|
| 6360 |
[dcl.init.list]: dcl.md#dcl.init.list
|
| 6361 |
[dcl.meaning]: dcl.md#dcl.meaning
|
| 6362 |
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
| 6363 |
+
[dcl.struct.bind]: dcl.md#dcl.struct.bind
|
| 6364 |
+
[dcl.type.auto.deduct]: dcl.md#dcl.type.auto.deduct
|
| 6365 |
+
[dcl.type.class.deduct]: dcl.md#dcl.type.class.deduct
|
| 6366 |
[dcl.type.elab]: dcl.md#dcl.type.elab
|
| 6367 |
[except.spec]: except.md#except.spec
|
| 6368 |
[expr.const]: expr.md#expr.const
|
| 6369 |
[expr.new]: expr.md#expr.new
|
| 6370 |
+
[expr.prim.fold]: expr.md#expr.prim.fold
|
| 6371 |
[expr.prim.lambda]: expr.md#expr.prim.lambda
|
| 6372 |
+
[expr.prim.lambda.closure]: expr.md#expr.prim.lambda.closure
|
| 6373 |
[expr.ref]: expr.md#expr.ref
|
| 6374 |
[expr.sizeof]: expr.md#expr.sizeof
|
| 6375 |
+
[expr.typeid]: expr.md#expr.typeid
|
| 6376 |
+
[implimits]: limits.md#implimits
|
| 6377 |
[intro.defs]: intro.md#intro.defs
|
| 6378 |
+
[intro.object]: intro.md#intro.object
|
| 6379 |
[lex.string]: lex.md#lex.string
|
| 6380 |
[namespace.def]: dcl.md#namespace.def
|
| 6381 |
[namespace.memdef]: dcl.md#namespace.memdef
|
| 6382 |
+
[namespace.udecl]: dcl.md#namespace.udecl
|
| 6383 |
[over.ics.rank]: over.md#over.ics.rank
|
| 6384 |
+
[over.match]: over.md#over.match
|
| 6385 |
[over.match.best]: over.md#over.match.best
|
| 6386 |
+
[over.match.class.deduct]: over.md#over.match.class.deduct
|
| 6387 |
[over.match.conv]: over.md#over.match.conv
|
| 6388 |
[over.match.ref]: over.md#over.match.ref
|
| 6389 |
[over.over]: over.md#over.over
|
| 6390 |
[special]: special.md#special
|
| 6391 |
+
[stmt.if]: stmt.md#stmt.if
|
| 6392 |
[support.types]: language.md#support.types
|
| 6393 |
+
[tab:fold.empty]: #tab:fold.empty
|
| 6394 |
[temp]: #temp
|
| 6395 |
[temp.alias]: #temp.alias
|
| 6396 |
[temp.arg]: #temp.arg
|
| 6397 |
[temp.arg.explicit]: #temp.arg.explicit
|
| 6398 |
[temp.arg.nontype]: #temp.arg.nontype
|
|
|
|
| 6407 |
[temp.deduct]: #temp.deduct
|
| 6408 |
[temp.deduct.call]: #temp.deduct.call
|
| 6409 |
[temp.deduct.conv]: #temp.deduct.conv
|
| 6410 |
[temp.deduct.decl]: #temp.deduct.decl
|
| 6411 |
[temp.deduct.funcaddr]: #temp.deduct.funcaddr
|
| 6412 |
+
[temp.deduct.guide]: #temp.deduct.guide
|
| 6413 |
[temp.deduct.partial]: #temp.deduct.partial
|
| 6414 |
[temp.deduct.type]: #temp.deduct.type
|
| 6415 |
[temp.dep]: #temp.dep
|
| 6416 |
[temp.dep.candidate]: #temp.dep.candidate
|
| 6417 |
[temp.dep.constexpr]: #temp.dep.constexpr
|
|
|
|
| 6474 |
selected.
|
| 6475 |
|
| 6476 |
[^8]: Although the *template-argument* corresponding to a
|
| 6477 |
*template-parameter* of type `bool` may be deduced from an array
|
| 6478 |
bound, the resulting value will always be `true` because the array
|
| 6479 |
+
bound will be nonzero.
|
| 6480 |
|
| 6481 |
[^9]: The parameters of function template specializations contain no
|
| 6482 |
template parameter types. The set of conversions allowed on deduced
|
| 6483 |
arguments is limited, because the argument deduction process
|
| 6484 |
produces function templates with parameters that either match the
|