- tmp/tmp74nppxtx/{from.md → to.md} +1287 -599
tmp/tmp74nppxtx/{from.md → to.md}
RENAMED
|
@@ -48,10 +48,11 @@ The *declaration* in a *template-declaration* (if any) shall
|
|
| 48 |
- declare or define a function, a class, or a variable, or
|
| 49 |
- define a member function, a member class, a member enumeration, or a
|
| 50 |
static data member of a class template or of a class nested within a
|
| 51 |
class template, or
|
| 52 |
- define a member template of a class or class template, or
|
|
|
|
| 53 |
- be a *deduction-guide*, or
|
| 54 |
- be an *alias-declaration*.
|
| 55 |
|
| 56 |
A *template-declaration* is a *declaration*. A declaration introduced by
|
| 57 |
a template declaration of a variable is a *variable template*. A
|
|
@@ -90,11 +91,11 @@ be a name.
|
|
| 90 |
[*Note 3*: A class or variable template declaration of a
|
| 91 |
*simple-template-id* declares a partial specialization
|
| 92 |
[[temp.spec.partial]]. — *end note*]
|
| 93 |
|
| 94 |
In a *template-declaration*, explicit specialization, or explicit
|
| 95 |
-
instantiation the *init-declarator-list* in the declaration shall
|
| 96 |
contain at most one declarator. When such a declaration is used to
|
| 97 |
declare a class template, no declarator is permitted.
|
| 98 |
|
| 99 |
A specialization (explicit or implicit) of one template is distinct from
|
| 100 |
all specializations of any other template. A template, an explicit
|
|
@@ -104,24 +105,46 @@ not have C language linkage.
|
|
| 104 |
[*Note 4*: Default arguments for function templates and for member
|
| 105 |
functions of class templates are considered definitions for the purpose
|
| 106 |
of template instantiation [[temp.decls]] and must obey the
|
| 107 |
one-definition rule [[basic.def.odr]]. — *end note*]
|
| 108 |
|
| 109 |
-
[*Note 5*:
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
An entity is *templated* if it is
|
| 119 |
|
| 120 |
- a template,
|
| 121 |
-
- an entity defined [[basic.def]] or created [[class.temporary]]
|
| 122 |
-
|
|
|
|
| 123 |
- a member of a templated entity,
|
| 124 |
- an enumerator for an enumeration that is a templated entity, or
|
| 125 |
- the closure type of a *lambda-expression* [[expr.prim.lambda.closure]]
|
| 126 |
appearing in the declaration of a templated entity.
|
| 127 |
|
|
@@ -147,11 +170,11 @@ unevaluated operand [[expr.context]].
|
|
| 147 |
|
| 148 |
The expression in a *requires-clause* uses a restricted grammar to avoid
|
| 149 |
ambiguities. Parentheses can be used to specify arbitrary expressions in
|
| 150 |
a *requires-clause*.
|
| 151 |
|
| 152 |
-
[*Example
|
| 153 |
|
| 154 |
``` cpp
|
| 155 |
template<int N> requires N == sizeof new unsigned short
|
| 156 |
int f(); // error: parentheses required around == expression
|
| 157 |
```
|
|
@@ -173,20 +196,21 @@ The syntax for *template-parameter*s is:
|
|
| 173 |
|
| 174 |
``` bnf
|
| 175 |
template-parameter:
|
| 176 |
type-parameter
|
| 177 |
parameter-declaration
|
|
|
|
|
|
|
|
|
|
| 178 |
```
|
| 179 |
|
| 180 |
``` bnf
|
| 181 |
type-parameter:
|
| 182 |
type-parameter-key '...'ₒₚₜ identifierₒₚₜ
|
| 183 |
type-parameter-key identifierₒₚₜ '=' type-id
|
| 184 |
type-constraint '...'ₒₚₜ identifierₒₚₜ
|
| 185 |
type-constraint identifierₒₚₜ '=' type-id
|
| 186 |
-
template-head type-parameter-key '...'ₒₚₜ identifierₒₚₜ
|
| 187 |
-
template-head type-parameter-key identifierₒₚₜ '=' id-expression
|
| 188 |
```
|
| 189 |
|
| 190 |
``` bnf
|
| 191 |
type-parameter-key:
|
| 192 |
class
|
|
@@ -197,83 +221,150 @@ type-parameter-key:
|
|
| 197 |
type-constraint:
|
| 198 |
nested-name-specifierₒₚₜ concept-name
|
| 199 |
nested-name-specifierₒₚₜ concept-name '<' template-argument-listₒₚₜ '>'
|
| 200 |
```
|
| 201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
The component names of a *type-constraint* are its *concept-name* and
|
| 203 |
those of its *nested-name-specifier* (if any).
|
| 204 |
|
| 205 |
[*Note 1*: The `>` token following the *template-parameter-list* of a
|
| 206 |
-
*type-parameter*
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
There is no semantic difference between `class` and `typename` in a
|
| 210 |
*type-parameter-key*. `typename` followed by an *unqualified-id* names a
|
| 211 |
template type parameter. `typename` followed by a *qualified-id* denotes
|
| 212 |
-
the type in a
|
|
|
|
| 213 |
|
| 214 |
-
*
|
| 215 |
-
*identifier* is a *type-parameter*.
|
| 216 |
-
|
| 217 |
-
[*Example 1*:
|
| 218 |
|
| 219 |
``` cpp
|
| 220 |
class T { ... };
|
| 221 |
int i;
|
| 222 |
|
| 223 |
template<class T, T i> void f(T t) {
|
| 224 |
-
T t1 = i; // template
|
| 225 |
::T t2 = ::i; // global namespace members T and i
|
| 226 |
}
|
| 227 |
```
|
| 228 |
|
| 229 |
-
Here, the template `f` has a
|
| 230 |
-
unnamed
|
| 231 |
|
| 232 |
— *end example*]
|
| 233 |
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
declaration.
|
| 237 |
|
| 238 |
-
The *identifier* in a *
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
or *template-name* (if declared with `template`) in the scope of the
|
| 242 |
-
template declaration.
|
| 243 |
|
| 244 |
-
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
-
|
| 247 |
-
example,
|
| 248 |
-
|
| 249 |
-
``` cpp
|
| 250 |
-
template<class T> class myarray { ... };
|
| 251 |
-
|
| 252 |
-
template<class K, class V, template<class T> class C = myarray>
|
| 253 |
-
class Map {
|
| 254 |
-
C<K> key;
|
| 255 |
-
C<V> value;
|
| 256 |
-
};
|
| 257 |
-
```
|
| 258 |
-
|
| 259 |
-
— *end note*]
|
| 260 |
|
| 261 |
A *type-constraint* `Q` that designates a concept `C` can be used to
|
| 262 |
constrain a contextually-determined type or template type parameter pack
|
| 263 |
`T` with a *constraint-expression* `E` defined as follows. If `Q` is of
|
| 264 |
-
the form `C<A₁,
|
| 265 |
let `E'` be `C<T>`. If `T` is not a pack, then `E` is `E'`, otherwise
|
| 266 |
`E` is `(E' && ...)`. This *constraint-expression* `E` is called the
|
| 267 |
*immediately-declared constraint* of `Q` for `T`. The concept designated
|
| 268 |
by a *type-constraint* shall be a type concept [[temp.concept]].
|
| 269 |
|
| 270 |
A *type-parameter* that starts with a *type-constraint* introduces the
|
| 271 |
immediately-declared constraint of the *type-constraint* for the
|
| 272 |
parameter.
|
| 273 |
|
| 274 |
-
[*Example
|
| 275 |
|
| 276 |
``` cpp
|
| 277 |
template<typename T> concept C1 = true;
|
| 278 |
template<typename... Ts> concept C2 = true;
|
| 279 |
template<typename T, typename U> concept C3 = true;
|
|
@@ -285,12 +376,12 @@ template<C3<int> T> struct s4; // associates C3<T, int>
|
|
| 285 |
template<C3<int>... T> struct s5; // associates (C3<T, int> && ...)
|
| 286 |
```
|
| 287 |
|
| 288 |
— *end example*]
|
| 289 |
|
| 290 |
-
A
|
| 291 |
-
|
| 292 |
|
| 293 |
- a structural type (see below),
|
| 294 |
- a type that contains a placeholder type [[dcl.spec.auto]], or
|
| 295 |
- a placeholder for a deduced class type [[dcl.type.class.deduct]].
|
| 296 |
|
|
@@ -302,66 +393,63 @@ A *structural type* is one of the following:
|
|
| 302 |
- a scalar type, or
|
| 303 |
- an lvalue reference type, or
|
| 304 |
- a literal class type with the following properties:
|
| 305 |
- all base classes and non-static data members are public and
|
| 306 |
non-mutable and
|
| 307 |
-
- the types of all
|
| 308 |
-
structural types or (possibly multidimensional)
|
| 309 |
|
| 310 |
-
An *id-expression* naming a
|
| 311 |
`T` denotes a static storage duration object of type `const T`, known as
|
| 312 |
-
a *template parameter object*,
|
| 313 |
-
template argument after it has been
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
A template parameter object shall have constant destruction
|
| 317 |
-
[[expr.const]].
|
| 318 |
|
| 319 |
-
[*Note
|
| 320 |
-
|
| 321 |
Otherwise, if it is of class type `T`, it is an lvalue and has type
|
| 322 |
`const T` [[expr.prim.id.unqual]]. — *end note*]
|
| 323 |
|
| 324 |
-
[*Example
|
| 325 |
|
| 326 |
``` cpp
|
| 327 |
using X = int;
|
| 328 |
struct A {};
|
| 329 |
template<const X& x, int i, A a> void f() {
|
| 330 |
-
i++; // error: change of template
|
| 331 |
|
| 332 |
&x; // OK
|
| 333 |
-
&i; // error: address of non-reference template
|
| 334 |
&a; // OK
|
| 335 |
int& ri = i; // error: attempt to bind non-const reference to temporary
|
| 336 |
const int& cri = i; // OK, const reference binds to temporary
|
| 337 |
const A& ra = a; // OK, const reference binds to a template parameter object
|
| 338 |
}
|
| 339 |
```
|
| 340 |
|
| 341 |
— *end example*]
|
| 342 |
|
| 343 |
-
[*Note
|
| 344 |
|
| 345 |
-
A
|
| 346 |
-
`void`.
|
| 347 |
|
| 348 |
-
[*Example
|
| 349 |
|
| 350 |
``` cpp
|
| 351 |
template<void v> class X; // error
|
| 352 |
template<void* pv> class Y; // OK
|
| 353 |
```
|
| 354 |
|
| 355 |
— *end example*]
|
| 356 |
|
| 357 |
— *end note*]
|
| 358 |
|
| 359 |
-
A
|
| 360 |
-
|
| 361 |
|
| 362 |
-
[*Example
|
| 363 |
|
| 364 |
``` cpp
|
| 365 |
template<int* a> struct R { ... };
|
| 366 |
template<int b[5]> struct S { ... };
|
| 367 |
int p;
|
|
@@ -372,35 +460,35 @@ R<v> y; // OK due to implicit argument conversion
|
|
| 372 |
S<v> z; // OK due to both adjustment and conversion
|
| 373 |
```
|
| 374 |
|
| 375 |
— *end example*]
|
| 376 |
|
| 377 |
-
A
|
| 378 |
placeholder type with a *type-constraint* introduces the
|
| 379 |
immediately-declared constraint of the *type-constraint* for the
|
| 380 |
invented type corresponding to the placeholder [[dcl.fct]].
|
| 381 |
|
| 382 |
-
A *default template
|
| 383 |
-
specified after `=` in a *template-parameter*. A default
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
|
| 396 |
-
The set of default
|
| 397 |
merging the default arguments from all prior declarations of the
|
| 398 |
template in the same way default function arguments are
|
| 399 |
[[dcl.fct.default]].
|
| 400 |
|
| 401 |
-
[*Example
|
| 402 |
|
| 403 |
``` cpp
|
| 404 |
template<class T1, class T2 = int> class A;
|
| 405 |
template<class T1 = int, class T2> class A;
|
| 406 |
```
|
|
@@ -412,24 +500,25 @@ template<class T1 = int, class T2 = int> class A;
|
|
| 412 |
```
|
| 413 |
|
| 414 |
— *end example*]
|
| 415 |
|
| 416 |
If a *template-parameter* of a class template, variable template, or
|
| 417 |
-
alias template has a default
|
| 418 |
-
*template-parameter* shall either have a default
|
| 419 |
-
supplied or
|
| 420 |
-
primary class template, primary variable template, or alias
|
| 421 |
-
a template parameter pack, it shall be the last
|
| 422 |
-
template
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
|
|
|
| 426 |
guide template [[temp.deduct.guide]] that does not have a default
|
| 427 |
argument shall be deducible from the parameter-type-list of the
|
| 428 |
deduction guide template.
|
| 429 |
|
| 430 |
-
[*Example
|
| 431 |
|
| 432 |
``` cpp
|
| 433 |
template<class T1 = int, class T2> class B; // error
|
| 434 |
|
| 435 |
// U can be neither deduced from the parameter-type-list nor specified
|
|
@@ -437,15 +526,15 @@ template<class... T, class... U> void f() { } // error
|
|
| 437 |
template<class... T, class U> void g() { } // error
|
| 438 |
```
|
| 439 |
|
| 440 |
— *end example*]
|
| 441 |
|
| 442 |
-
When parsing a default
|
| 443 |
-
|
| 444 |
-
|
| 445 |
|
| 446 |
-
[*Example
|
| 447 |
|
| 448 |
``` cpp
|
| 449 |
template<int i = 3 > 4 > // syntax error
|
| 450 |
class X { ... };
|
| 451 |
|
|
@@ -454,15 +543,15 @@ class Y { ... };
|
|
| 454 |
```
|
| 455 |
|
| 456 |
— *end example*]
|
| 457 |
|
| 458 |
A *template-parameter* of a template *template-parameter* is permitted
|
| 459 |
-
to have a default
|
| 460 |
specified, they apply to the template *template-parameter* in the scope
|
| 461 |
of the template *template-parameter*.
|
| 462 |
|
| 463 |
-
[*Example
|
| 464 |
|
| 465 |
``` cpp
|
| 466 |
template <template <class TT = float> class T> struct A {
|
| 467 |
inline void f();
|
| 468 |
inline void g();
|
|
@@ -475,39 +564,21 @@ template <template <class TT = char> class T> void A<T>::g() {
|
|
| 475 |
}
|
| 476 |
```
|
| 477 |
|
| 478 |
— *end example*]
|
| 479 |
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
declares a pack [[dcl.fct]], then the *template-parameter* is a template
|
| 483 |
-
parameter pack [[temp.variadic]]. A template parameter pack that is a
|
| 484 |
-
*parameter-declaration* whose type contains one or more unexpanded packs
|
| 485 |
-
is a pack expansion. Similarly, a template parameter pack that is a
|
| 486 |
-
*type-parameter* with a *template-parameter-list* containing one or more
|
| 487 |
-
unexpanded packs is a pack expansion. A type parameter pack with a
|
| 488 |
-
*type-constraint* that contains an unexpanded parameter pack is a pack
|
| 489 |
-
expansion. A template parameter pack that is a pack expansion shall not
|
| 490 |
-
expand a template parameter pack declared in the same
|
| 491 |
-
*template-parameter-list*.
|
| 492 |
|
| 493 |
-
[*Example
|
| 494 |
|
| 495 |
``` cpp
|
| 496 |
-
template
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
template <class... T>
|
| 503 |
-
struct value_holder {
|
| 504 |
-
template <T... Values> struct apply { }; // Values is a non-type template parameter pack
|
| 505 |
-
}; // and a pack expansion
|
| 506 |
-
|
| 507 |
-
template <class... T, T... Values> // error: Values expands template type parameter
|
| 508 |
-
struct static_array; // pack T within the same template parameter list
|
| 509 |
```
|
| 510 |
|
| 511 |
— *end example*]
|
| 512 |
|
| 513 |
## Names of template specializations <a id="temp.names">[[temp.names]]</a>
|
|
@@ -540,28 +611,34 @@ template-argument-list:
|
|
| 540 |
|
| 541 |
``` bnf
|
| 542 |
template-argument:
|
| 543 |
constant-expression
|
| 544 |
type-id
|
| 545 |
-
|
|
|
|
|
|
|
| 546 |
```
|
| 547 |
|
| 548 |
The component name of a *simple-template-id*, *template-id*, or
|
| 549 |
*template-name* is the first name in it.
|
| 550 |
|
| 551 |
A `<` is interpreted as the delimiter of a *template-argument-list* if
|
| 552 |
-
|
| 553 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 554 |
- that follows the keyword `template` or a `~` after a
|
| 555 |
*nested-name-specifier* or in a class member access expression, or
|
| 556 |
- for which name lookup finds the injected-class-name of a class
|
| 557 |
template or finds any declaration of a template, or
|
| 558 |
-
- that is an unqualified name for which name lookup either finds one
|
| 559 |
-
|
| 560 |
- that is a terminal name in a *using-declarator* [[namespace.udecl]],
|
| 561 |
-
|
| 562 |
-
|
| 563 |
|
| 564 |
[*Note 1*: If the name is an *identifier*, it is then interpreted as a
|
| 565 |
*template-name*. The keyword `template` is used to indicate that a
|
| 566 |
dependent qualified name [[temp.dep.type]] denotes a template where an
|
| 567 |
expression might appear. — *end note*]
|
|
@@ -576,24 +653,30 @@ struct X {
|
|
| 576 |
template<class T> void f(T* p) {
|
| 577 |
T* p1 = p->alloc<200>(); // error: < means less than
|
| 578 |
T* p2 = p->template alloc<200>(); // OK, < starts template argument list
|
| 579 |
T::adjust<100>(); // error: < means less than
|
| 580 |
T::template adjust<100>(); // OK, < starts template argument list
|
| 581 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 582 |
```
|
| 583 |
|
| 584 |
— *end example*]
|
| 585 |
|
| 586 |
-
When parsing a *template-argument-list*, the first non-nested `>`[^
|
| 587 |
|
| 588 |
is taken as the ending delimiter rather than a greater-than operator.
|
| 589 |
Similarly, the first non-nested `>>` is treated as two consecutive but
|
| 590 |
distinct `>` tokens, the first of which is taken as the end of the
|
| 591 |
-
*template-argument-list* and completes the *template-id*
|
|
|
|
| 592 |
|
| 593 |
[*Note 2*: The second `>` token produced by this replacement rule can
|
| 594 |
-
terminate an enclosing *template-id*
|
|
|
|
| 595 |
different construct (e.g., a cast). — *end note*]
|
| 596 |
|
| 597 |
[*Example 2*:
|
| 598 |
|
| 599 |
``` cpp
|
|
@@ -611,24 +694,40 @@ Y<X<(6>>1)>> x5; // OK
|
|
| 611 |
— *end example*]
|
| 612 |
|
| 613 |
The keyword `template` shall not appear immediately after a declarative
|
| 614 |
*nested-name-specifier* [[expr.prim.id.qual]].
|
| 615 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 616 |
A name prefixed by the keyword `template` shall be followed by a
|
| 617 |
template argument list or refer to a class template or an alias
|
| 618 |
template. The latter case is deprecated [[depr.template.template]]. The
|
| 619 |
keyword `template` shall not appear immediately before a `~` token (as
|
| 620 |
to name a destructor).
|
| 621 |
|
| 622 |
[*Note 3*: The keyword `template` cannot be applied to non-template
|
| 623 |
members of class templates. — *end note*]
|
| 624 |
|
| 625 |
[*Note 4*: As is the case with the `typename` prefix, the `template`
|
| 626 |
-
prefix is
|
| 627 |
template. — *end note*]
|
| 628 |
|
| 629 |
-
[*Example
|
| 630 |
|
| 631 |
``` cpp
|
| 632 |
template <class T> struct A {
|
| 633 |
void f(int);
|
| 634 |
template <class U> void f(U);
|
|
@@ -649,27 +748,29 @@ template <class T, template <class X> class TT = T::template C> struct D { };
|
|
| 649 |
D<B<int> > db;
|
| 650 |
```
|
| 651 |
|
| 652 |
— *end example*]
|
| 653 |
|
| 654 |
-
A *template-id* is *valid* if
|
| 655 |
|
| 656 |
- there are at most as many arguments as there are parameters or a
|
| 657 |
parameter is a template parameter pack [[temp.variadic]],
|
| 658 |
- there is an argument for each non-deducible non-pack parameter that
|
| 659 |
does not have a default *template-argument*,
|
| 660 |
-
- each *template-argument* matches the corresponding
|
| 661 |
-
|
| 662 |
- substitution of each template argument into the following template
|
| 663 |
parameters (if any) succeeds, and
|
| 664 |
-
- if the *template-id*
|
| 665 |
-
|
|
|
|
| 666 |
|
| 667 |
-
A *simple-template-id*
|
| 668 |
-
template
|
|
|
|
| 669 |
|
| 670 |
-
[*Example
|
| 671 |
|
| 672 |
``` cpp
|
| 673 |
template<class T, T::type n = 0> class X;
|
| 674 |
struct S {
|
| 675 |
using type = int;
|
|
@@ -681,18 +782,19 @@ using T4 = X<int>; // error: substitution failure for second templa
|
|
| 681 |
using T5 = X<S>; // OK
|
| 682 |
```
|
| 683 |
|
| 684 |
— *end example*]
|
| 685 |
|
| 686 |
-
When the *template-name* of a *simple-template-id*
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
[[temp.
|
| 691 |
-
[[temp.constr.
|
|
|
|
| 692 |
|
| 693 |
-
[*Example
|
| 694 |
|
| 695 |
``` cpp
|
| 696 |
template<typename T> concept C1 = sizeof(T) != sizeof(int);
|
| 697 |
|
| 698 |
template<C1 T> struct S1 { };
|
|
@@ -734,11 +836,11 @@ satisfied [[temp.constr.constr]] by the specified template arguments and
|
|
| 734 |
[*Note 5*: Since a *constraint-expression* is an unevaluated operand, a
|
| 735 |
concept-id appearing in a *constraint-expression* is not evaluated
|
| 736 |
except as necessary to determine whether the normalized constraints are
|
| 737 |
satisfied. — *end note*]
|
| 738 |
|
| 739 |
-
[*Example
|
| 740 |
|
| 741 |
``` cpp
|
| 742 |
template<typename T> concept C = true;
|
| 743 |
static_assert(C<int>); // OK
|
| 744 |
```
|
|
@@ -747,18 +849,16 @@ static_assert(C<int>); // OK
|
|
| 747 |
|
| 748 |
## Template arguments <a id="temp.arg">[[temp.arg]]</a>
|
| 749 |
|
| 750 |
### General <a id="temp.arg.general">[[temp.arg.general]]</a>
|
| 751 |
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
form
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
[[temp.variadic]], it will correspond to zero or more
|
| 759 |
-
*template-argument*s.
|
| 760 |
|
| 761 |
[*Example 1*:
|
| 762 |
|
| 763 |
``` cpp
|
| 764 |
template<class T> class Array {
|
|
@@ -790,11 +890,11 @@ is a template parameter pack [[temp.variadic]], the nᵗʰ template
|
|
| 790 |
argument is a pack expansion whose pattern is the name of the template
|
| 791 |
parameter pack.
|
| 792 |
|
| 793 |
In a *template-argument*, an ambiguity between a *type-id* and an
|
| 794 |
expression is resolved to a *type-id*, regardless of the form of the
|
| 795 |
-
corresponding *template-parameter*.[^
|
| 796 |
|
| 797 |
[*Example 2*:
|
| 798 |
|
| 799 |
``` cpp
|
| 800 |
template<class T> void f();
|
|
@@ -806,12 +906,13 @@ void g() {
|
|
| 806 |
```
|
| 807 |
|
| 808 |
— *end example*]
|
| 809 |
|
| 810 |
[*Note 1*: Names used in a *template-argument* are subject to access
|
| 811 |
-
control where they appear. Because a
|
| 812 |
-
member, no access control applies
|
|
|
|
| 813 |
|
| 814 |
[*Example 3*:
|
| 815 |
|
| 816 |
``` cpp
|
| 817 |
template<class T> class X {
|
|
@@ -829,13 +930,13 @@ private:
|
|
| 829 |
X<Y::S> y; // error: S not accessible
|
| 830 |
```
|
| 831 |
|
| 832 |
— *end example*]
|
| 833 |
|
| 834 |
-
For a
|
| 835 |
template definition has no special access rights to the members of the
|
| 836 |
-
|
| 837 |
|
| 838 |
[*Example 4*:
|
| 839 |
|
| 840 |
``` cpp
|
| 841 |
template <template <class TT> class T> class A {
|
|
@@ -850,11 +951,11 @@ private:
|
|
| 850 |
A<B> b; // error: A has no access to B::S
|
| 851 |
```
|
| 852 |
|
| 853 |
— *end example*]
|
| 854 |
|
| 855 |
-
When template argument packs or default
|
| 856 |
*template-argument* list can be empty. In that case the empty `<>`
|
| 857 |
brackets shall still be used as the *template-argument-list*.
|
| 858 |
|
| 859 |
[*Example 5*:
|
| 860 |
|
|
@@ -885,25 +986,26 @@ void f(A<int>* p, A<int>* q) {
|
|
| 885 |
}
|
| 886 |
```
|
| 887 |
|
| 888 |
— *end example*]
|
| 889 |
|
| 890 |
-
If the use of a
|
| 891 |
-
|
| 892 |
-
|
| 893 |
|
| 894 |
When name lookup for the component name of a *template-id* finds an
|
| 895 |
overload set, both non-template functions in the overload set and
|
| 896 |
function templates in the overload set for which the
|
| 897 |
*template-argument*s do not match the *template-parameter*s are ignored.
|
| 898 |
|
| 899 |
[*Note 2*: If none of the function templates have matching
|
| 900 |
*template-parameter*s, the program is ill-formed. — *end note*]
|
| 901 |
|
| 902 |
-
When a *simple-template-id*
|
| 903 |
-
*template-argument* is implicitly
|
| 904 |
-
value of that default argument is
|
|
|
|
| 905 |
|
| 906 |
[*Example 7*:
|
| 907 |
|
| 908 |
``` cpp
|
| 909 |
template<typename T, typename U = int> struct S { };
|
|
@@ -916,14 +1018,14 @@ The default argument for `U` is instantiated to form the type
|
|
| 916 |
— *end example*]
|
| 917 |
|
| 918 |
A *template-argument* followed by an ellipsis is a pack expansion
|
| 919 |
[[temp.variadic]].
|
| 920 |
|
| 921 |
-
###
|
| 922 |
|
| 923 |
-
A *template-argument* for a
|
| 924 |
-
|
| 925 |
|
| 926 |
[*Example 1*:
|
| 927 |
|
| 928 |
``` cpp
|
| 929 |
template <class T> class X { };
|
|
@@ -947,45 +1049,92 @@ void f() {
|
|
| 947 |
— *end example*]
|
| 948 |
|
| 949 |
[*Note 1*: A template type argument can be an incomplete type
|
| 950 |
[[term.incomplete.type]]. — *end note*]
|
| 951 |
|
| 952 |
-
###
|
| 953 |
|
| 954 |
-
|
| 955 |
-
|
| 956 |
-
type [[dcl.type.class.deduct]], the type of the parameter is the type
|
| 957 |
-
deduced for the variable `x` in the invented declaration
|
| 958 |
|
| 959 |
``` cpp
|
| 960 |
-
T x =
|
| 961 |
```
|
| 962 |
|
| 963 |
-
|
| 964 |
-
|
|
|
|
|
|
|
|
|
|
| 965 |
|
| 966 |
-
|
| 967 |
-
|
| 968 |
-
*template-parameter*.
|
| 969 |
|
| 970 |
-
|
| 971 |
-
|
| 972 |
-
function is selected from the set [[over.over]]. — *end note*]
|
| 973 |
|
| 974 |
-
|
| 975 |
-
|
| 976 |
-
|
| 977 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 978 |
|
| 979 |
- a temporary object [[class.temporary]],
|
| 980 |
- a string literal object [[lex.string]],
|
| 981 |
- the result of a `typeid` expression [[expr.typeid]],
|
| 982 |
- a predefined `__func__` variable [[dcl.fct.def.general]], or
|
| 983 |
- a subobject [[intro.object]] of one of the above.
|
| 984 |
|
| 985 |
[*Example 1*:
|
| 986 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 987 |
``` cpp
|
| 988 |
template<const int* pci> struct X { ... };
|
| 989 |
int ai[10];
|
| 990 |
X<ai> xi; // array to pointer and qualification conversions
|
| 991 |
|
|
@@ -1008,20 +1157,35 @@ A<&f> a; // selects f(int)
|
|
| 1008 |
template<auto n> struct B { ... };
|
| 1009 |
B<5> b1; // OK, template parameter type is int
|
| 1010 |
B<'a'> b2; // OK, template parameter type is char
|
| 1011 |
B<2.5> b3; // OK, template parameter type is double
|
| 1012 |
B<void(0)> b4; // error: template parameter type cannot be void
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1013 |
```
|
| 1014 |
|
| 1015 |
— *end example*]
|
| 1016 |
|
| 1017 |
[*Note 2*:
|
| 1018 |
|
| 1019 |
A *string-literal* [[lex.string]] is not an acceptable
|
| 1020 |
-
*template-argument* for a
|
| 1021 |
|
| 1022 |
-
[*Example
|
| 1023 |
|
| 1024 |
``` cpp
|
| 1025 |
template<class T, T p> class X {
|
| 1026 |
...
|
| 1027 |
};
|
|
@@ -1044,13 +1208,13 @@ X<A, "Pyrophoricity"> z; // OK, string-literal is a constructor argument
|
|
| 1044 |
— *end note*]
|
| 1045 |
|
| 1046 |
[*Note 3*:
|
| 1047 |
|
| 1048 |
A temporary object is not an acceptable *template-argument* when the
|
| 1049 |
-
corresponding
|
| 1050 |
|
| 1051 |
-
[*Example
|
| 1052 |
|
| 1053 |
``` cpp
|
| 1054 |
template<const int& CRI> struct B { ... };
|
| 1055 |
|
| 1056 |
B<1> b1; // error: temporary would be required for template argument
|
|
@@ -1069,21 +1233,23 @@ C<Y{X{1}.n}> c; // error: subobject of temporary object used to
|
|
| 1069 |
|
| 1070 |
— *end note*]
|
| 1071 |
|
| 1072 |
### Template template arguments <a id="temp.arg.template">[[temp.arg.template]]</a>
|
| 1073 |
|
| 1074 |
-
A *template-argument* for a template
|
| 1075 |
-
name of a
|
| 1076 |
-
|
| 1077 |
-
|
| 1078 |
-
|
| 1079 |
-
|
|
|
|
|
|
|
| 1080 |
|
| 1081 |
Any partial specializations [[temp.spec.partial]] associated with the
|
| 1082 |
primary template are considered when a specialization based on the
|
| 1083 |
-
template
|
| 1084 |
-
|
| 1085 |
selected had it been reachable, the program is ill-formed, no diagnostic
|
| 1086 |
required.
|
| 1087 |
|
| 1088 |
[*Example 1*:
|
| 1089 |
|
|
@@ -1102,26 +1268,34 @@ C<A> c; // V<int> within C<A> uses the primary template, so c.y.x ha
|
|
| 1102 |
// V<int*> within C<A> uses the partial specialization, so c.z.x has type long
|
| 1103 |
```
|
| 1104 |
|
| 1105 |
— *end example*]
|
| 1106 |
|
| 1107 |
-
A
|
| 1108 |
-
|
| 1109 |
-
|
| 1110 |
-
|
| 1111 |
-
|
| 1112 |
-
|
| 1113 |
-
template
|
| 1114 |
-
|
| 1115 |
-
|
| 1116 |
-
|
| 1117 |
-
|
| 1118 |
-
|
| 1119 |
-
|
| 1120 |
-
|
| 1121 |
-
|
| 1122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1123 |
|
| 1124 |
[*Example 2*:
|
| 1125 |
|
| 1126 |
``` cpp
|
| 1127 |
template<class T> class A { ... };
|
|
@@ -1183,11 +1357,11 @@ S<Y> s2; // error: P is not at least as specialized as Y
|
|
| 1183 |
S<Z> s3; // OK, P is at least as specialized as Z
|
| 1184 |
```
|
| 1185 |
|
| 1186 |
— *end example*]
|
| 1187 |
|
| 1188 |
-
A template
|
| 1189 |
template *template-argument* `A` if, given the following rewrite to two
|
| 1190 |
function templates, the function template corresponding to `P` is at
|
| 1191 |
least as specialized as the function template corresponding to `A`
|
| 1192 |
according to the partial ordering rules for function templates
|
| 1193 |
[[temp.func.order]]. Given an invented class template `X` with the
|
|
@@ -1197,15 +1371,15 @@ according to the partial ordering rules for function templates
|
|
| 1197 |
- Each of the two function templates has the same template parameters
|
| 1198 |
and *requires-clause* (if any), respectively, as `P` or `A`.
|
| 1199 |
- Each function template has a single function parameter whose type is a
|
| 1200 |
specialization of `X` with template arguments corresponding to the
|
| 1201 |
template parameters from the respective function template where, for
|
| 1202 |
-
each template
|
| 1203 |
-
template, a corresponding template
|
| 1204 |
declares a template parameter pack, then `AA` is the pack expansion
|
| 1205 |
-
`PP...` [[temp.variadic]]; otherwise, `AA` is
|
| 1206 |
-
`PP`.
|
| 1207 |
|
| 1208 |
If the rewrite produces an invalid type, then `P` is not at least as
|
| 1209 |
specialized as `A`.
|
| 1210 |
|
| 1211 |
## Template constraints <a id="temp.constr">[[temp.constr]]</a>
|
|
@@ -1222,16 +1396,18 @@ by their associated constraints [[temp.constr.order]]. — *end note*]
|
|
| 1222 |
|
| 1223 |
#### General <a id="temp.constr.constr.general">[[temp.constr.constr.general]]</a>
|
| 1224 |
|
| 1225 |
A *constraint* is a sequence of logical operations and operands that
|
| 1226 |
specifies requirements on template arguments. The operands of a logical
|
| 1227 |
-
operation are constraints. There are
|
| 1228 |
constraints:
|
| 1229 |
|
| 1230 |
- conjunctions [[temp.constr.op]],
|
| 1231 |
-
- disjunctions [[temp.constr.op]],
|
| 1232 |
-
- atomic constraints [[temp.constr.atomic]]
|
|
|
|
|
|
|
| 1233 |
|
| 1234 |
In order for a constrained template to be instantiated [[temp.spec]],
|
| 1235 |
its associated constraints [[temp.constr.decl]] shall be satisfied as
|
| 1236 |
described in the following subclauses.
|
| 1237 |
|
|
@@ -1410,11 +1586,12 @@ void h2() {
|
|
| 1410 |
|
| 1411 |
— *end note*]
|
| 1412 |
|
| 1413 |
To determine if an atomic constraint is *satisfied*, the parameter
|
| 1414 |
mapping and template arguments are first substituted into its
|
| 1415 |
-
expression. If substitution results in an invalid type or expression
|
|
|
|
| 1416 |
the constraint is not satisfied. Otherwise, the lvalue-to-rvalue
|
| 1417 |
conversion [[conv.lval]] is performed if necessary, and `E` shall be a
|
| 1418 |
constant expression of type `bool`. The constraint is satisfied if and
|
| 1419 |
only if evaluation of `E` results in `true`. If, at different points in
|
| 1420 |
the program, the satisfaction result is different for identical atomic
|
|
@@ -1441,10 +1618,93 @@ void g() {
|
|
| 1441 |
// call is ill-formed even though #2 is a better match
|
| 1442 |
```
|
| 1443 |
|
| 1444 |
— *end example*]
|
| 1445 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1446 |
### Constrained declarations <a id="temp.constr.decl">[[temp.constr.decl]]</a>
|
| 1447 |
|
| 1448 |
A template declaration [[temp.pre]] or templated function declaration
|
| 1449 |
[[dcl.fct]] can be constrained by the use of a *requires-clause*. This
|
| 1450 |
allows the specification of constraints for that declaration as an
|
|
@@ -1555,16 +1815,24 @@ The *normal form* of an *expression* `E` is a constraint
|
|
| 1555 |
- The normal form of an expression `( E )` is the normal form of `E`.
|
| 1556 |
- The normal form of an expression `E1 || E2` is the disjunction
|
| 1557 |
[[temp.constr.op]] of the normal forms of `E1` and `E2`.
|
| 1558 |
- The normal form of an expression `E1 && E2` is the conjunction of the
|
| 1559 |
normal forms of `E1` and `E2`.
|
| 1560 |
-
-
|
| 1561 |
-
|
| 1562 |
-
|
| 1563 |
-
|
| 1564 |
-
|
| 1565 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1566 |
\[*Example 1*:
|
| 1567 |
``` cpp
|
| 1568 |
template<typename T> concept A = T::value || true;
|
| 1569 |
template<typename U> concept B = A<U*>;
|
| 1570 |
template<typename V> concept C = B<V&>;
|
|
@@ -1575,10 +1843,35 @@ The *normal form* of an *expression* `E` is a constraint
|
|
| 1575 |
mapping), despite the expression `T::value` being ill-formed for a
|
| 1576 |
pointer type `T`. Normalization of `C`'s *constraint-expression*
|
| 1577 |
results in the program being ill-formed, because it would form the
|
| 1578 |
invalid type `V&*` in the parameter mapping.
|
| 1579 |
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1580 |
- The normal form of any other expression `E` is the atomic constraint
|
| 1581 |
whose expression is `E` and whose parameter mapping is the identity
|
| 1582 |
mapping.
|
| 1583 |
|
| 1584 |
The process of obtaining the normal form of a *constraint-expression* is
|
|
@@ -1609,27 +1902,95 @@ The associated constraints of \#2 are `requires { typename T::type; }`
|
|
| 1609 |
The associated constraints of \#3 are `requires (T x) { ++x; }` (with
|
| 1610 |
mapping `T` ↦ `U`).
|
| 1611 |
|
| 1612 |
— *end example*]
|
| 1613 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1614 |
### Partial ordering by constraints <a id="temp.constr.order">[[temp.constr.order]]</a>
|
| 1615 |
|
| 1616 |
A constraint P *subsumes* a constraint Q if and only if, for every
|
| 1617 |
-
disjunctive clause Pᵢ in the disjunctive normal form[^
|
| 1618 |
|
| 1619 |
of P, Pᵢ subsumes every conjunctive clause Qⱼ in the conjunctive normal
|
| 1620 |
-
form[^
|
| 1621 |
|
| 1622 |
of Q, where
|
| 1623 |
|
| 1624 |
- a disjunctive clause Pᵢ subsumes a conjunctive clause Qⱼ if and only
|
| 1625 |
if there exists an atomic constraint Pᵢₐ in Pᵢ for which there exists
|
| 1626 |
an atomic constraint $Q_{jb}$ in Qⱼ such that Pᵢₐ subsumes $Q_{jb}$,
|
| 1627 |
-
and
|
| 1628 |
- an atomic constraint A subsumes another atomic constraint B if and
|
| 1629 |
only if A and B are identical using the rules described in
|
| 1630 |
-
[[temp.constr.atomic]]
|
|
|
|
|
|
|
|
|
|
| 1631 |
|
| 1632 |
[*Example 1*: Let A and B be atomic constraints [[temp.constr.atomic]].
|
| 1633 |
The constraint A ∧ B subsumes A, but A does not subsume A ∧ B. The
|
| 1634 |
constraint A subsumes A ∨ B, but A ∨ B does not subsume A. Also note
|
| 1635 |
that every constraint subsumes itself. — *end example*]
|
|
@@ -1647,14 +2008,17 @@ partial ordering is used to determine
|
|
| 1647 |
[[temp.spec.partial.order]], and
|
| 1648 |
- the partial ordering of function templates [[temp.func.order]].
|
| 1649 |
|
| 1650 |
— *end note*]
|
| 1651 |
|
|
|
|
|
|
|
|
|
|
| 1652 |
A declaration `D1` is *at least as constrained* as a declaration `D2` if
|
| 1653 |
|
| 1654 |
- `D1` and `D2` are both constrained declarations and `D1`’s associated
|
| 1655 |
-
constraints subsume those of `D2`; or
|
| 1656 |
- `D2` has no associated constraints.
|
| 1657 |
|
| 1658 |
A declaration `D1` is *more constrained* than another declaration `D2`
|
| 1659 |
when `D1` is at least as constrained as `D2`, and `D2` is not at least
|
| 1660 |
as constrained as `D1`.
|
|
@@ -1676,20 +2040,49 @@ g(true); // selects #3 because C1<bool> is not satisfied
|
|
| 1676 |
g(0); // selects #4
|
| 1677 |
```
|
| 1678 |
|
| 1679 |
— *end example*]
|
| 1680 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1681 |
## Type equivalence <a id="temp.type">[[temp.type]]</a>
|
| 1682 |
|
| 1683 |
Two *template-id*s are the same if
|
| 1684 |
|
| 1685 |
- their *template-name*s, *operator-function-id*s, or
|
| 1686 |
*literal-operator-id*s refer to the same template, and
|
| 1687 |
- their corresponding type *template-argument*s are the same type, and
|
| 1688 |
-
-
|
| 1689 |
-
template
|
| 1690 |
-
|
| 1691 |
- their corresponding template *template-argument*s refer to the same
|
| 1692 |
template.
|
| 1693 |
|
| 1694 |
Two *template-id*s that are the same refer to the same class, function,
|
| 1695 |
or variable.
|
|
@@ -1698,21 +2091,24 @@ Two values are *template-argument-equivalent* if they are of the same
|
|
| 1698 |
type and
|
| 1699 |
|
| 1700 |
- they are of integral type and their values are the same, or
|
| 1701 |
- they are of floating-point type and their values are identical, or
|
| 1702 |
- they are of type `std::nullptr_t`, or
|
| 1703 |
-
- they are of
|
|
|
|
|
|
|
| 1704 |
- they are of pointer type and they have the same pointer value, or
|
| 1705 |
- they are of pointer-to-member type and they refer to the same class
|
| 1706 |
member or are both the null member pointer value, or
|
| 1707 |
- they are of reference type and they refer to the same object or
|
| 1708 |
function, or
|
| 1709 |
- they are of array type and their corresponding elements are
|
| 1710 |
-
template-argument-equivalent,[^
|
| 1711 |
- they are of union type and either they both have no active member or
|
| 1712 |
they have the same active member and their active members are
|
| 1713 |
template-argument-equivalent, or
|
|
|
|
| 1714 |
- they are of class type and their corresponding direct subobjects and
|
| 1715 |
reference members are template-argument-equivalent.
|
| 1716 |
|
| 1717 |
[*Example 1*:
|
| 1718 |
|
|
@@ -1753,10 +2149,19 @@ the same type only if their *expression*s are equivalent
|
|
| 1753 |
[[temp.over.link]].
|
| 1754 |
|
| 1755 |
[*Note 1*: However, such a type might be aliased, e.g., by a
|
| 1756 |
*typedef-name*. — *end note*]
|
| 1757 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1758 |
## Template declarations <a id="temp.decls">[[temp.decls]]</a>
|
| 1759 |
|
| 1760 |
### General <a id="temp.decls.general">[[temp.decls.general]]</a>
|
| 1761 |
|
| 1762 |
The template parameters of a template are specified in the angle bracket
|
|
@@ -1777,11 +2182,13 @@ class templates are considered definitions; each default argument,
|
|
| 1777 |
*type-constraint*, *requires-clause*, or *noexcept-specifier* is a
|
| 1778 |
separate definition which is unrelated to the templated function
|
| 1779 |
definition or to any other default arguments, *type-constraint*s,
|
| 1780 |
*requires-clause*s, or *noexcept-specifier*s. For the purpose of
|
| 1781 |
instantiation, the substatements of a constexpr if statement [[stmt.if]]
|
| 1782 |
-
are considered definitions.
|
|
|
|
|
|
|
| 1783 |
|
| 1784 |
Because an *alias-declaration* cannot declare a *template-id*, it is not
|
| 1785 |
possible to partially or explicitly specialize an alias template.
|
| 1786 |
|
| 1787 |
### Class templates <a id="temp.class">[[temp.class]]</a>
|
|
@@ -1945,19 +2352,20 @@ v2[3] = dcomplex(7,8); // Array<dcomplex>::operator[]
|
|
| 1945 |
|
| 1946 |
— *end example*]
|
| 1947 |
|
| 1948 |
#### Deduction guides <a id="temp.deduct.guide">[[temp.deduct.guide]]</a>
|
| 1949 |
|
| 1950 |
-
Deduction guides are used when a *template-name*
|
| 1951 |
-
specifier
|
| 1952 |
-
guides are not found by name
|
| 1953 |
-
|
| 1954 |
-
deduction guides declared for
|
|
|
|
| 1955 |
|
| 1956 |
``` bnf
|
| 1957 |
deduction-guide:
|
| 1958 |
-
explicit-specifierₒₚₜ template-name '(' parameter-declaration-clause ')' '->' simple-template-id ';'
|
| 1959 |
```
|
| 1960 |
|
| 1961 |
[*Example 1*:
|
| 1962 |
|
| 1963 |
``` cpp
|
|
@@ -1976,11 +2384,13 @@ S x{A()}; // x is of type S<short, int>
|
|
| 1976 |
```
|
| 1977 |
|
| 1978 |
— *end example*]
|
| 1979 |
|
| 1980 |
The same restrictions apply to the *parameter-declaration-clause* of a
|
| 1981 |
-
deduction guide as in a function declaration [[dcl.fct]]
|
|
|
|
|
|
|
| 1982 |
*simple-template-id* shall name a class template specialization. The
|
| 1983 |
*template-name* shall be the same *identifier* as the *template-name* of
|
| 1984 |
the *simple-template-id*. A *deduction-guide* shall inhabit the scope to
|
| 1985 |
which the corresponding class template belongs and, for a member class
|
| 1986 |
template, have the same access. Two deduction guide declarations for the
|
|
@@ -2057,11 +2467,10 @@ class template definition.
|
|
| 2057 |
|
| 2058 |
``` cpp
|
| 2059 |
template<class T> struct A {
|
| 2060 |
enum E : T;
|
| 2061 |
};
|
| 2062 |
-
A<int> a;
|
| 2063 |
template<class T> enum A<T>::E : T { e1, e2 };
|
| 2064 |
A<int>::E e = A<int>::e1;
|
| 2065 |
```
|
| 2066 |
|
| 2067 |
— *end example*]
|
|
@@ -2170,13 +2579,13 @@ class D : public B {
|
|
| 2170 |
|
| 2171 |
— *end example*]
|
| 2172 |
|
| 2173 |
[*Note 1*:
|
| 2174 |
|
| 2175 |
-
A specialization of a conversion function template is
|
| 2176 |
-
|
| 2177 |
-
|
| 2178 |
|
| 2179 |
[*Example 6*:
|
| 2180 |
|
| 2181 |
``` cpp
|
| 2182 |
struct A {
|
|
@@ -2193,13 +2602,15 @@ int main() {
|
|
| 2193 |
}
|
| 2194 |
```
|
| 2195 |
|
| 2196 |
— *end example*]
|
| 2197 |
|
| 2198 |
-
|
| 2199 |
-
|
| 2200 |
-
|
|
|
|
|
|
|
| 2201 |
|
| 2202 |
— *end note*]
|
| 2203 |
|
| 2204 |
### Variadic templates <a id="temp.variadic">[[temp.variadic]]</a>
|
| 2205 |
|
|
@@ -2252,15 +2663,33 @@ foo(); // xs contains zero init-captures
|
|
| 2252 |
foo(1); // xs contains one init-capture
|
| 2253 |
```
|
| 2254 |
|
| 2255 |
— *end example*]
|
| 2256 |
|
| 2257 |
-
A *pack* is
|
| 2258 |
-
|
| 2259 |
-
|
| 2260 |
-
|
| 2261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2262 |
|
| 2263 |
A *pack expansion* consists of a *pattern* and an ellipsis, the
|
| 2264 |
instantiation of which produces zero or more instantiations of the
|
| 2265 |
pattern in a list (described below). The form of the pattern depends on
|
| 2266 |
the context in which the expansion occurs. Pack expansions can occur in
|
|
@@ -2268,15 +2697,21 @@ the following contexts:
|
|
| 2268 |
|
| 2269 |
- In a function parameter pack [[dcl.fct]]; the pattern is the
|
| 2270 |
*parameter-declaration* without the ellipsis.
|
| 2271 |
- In a *using-declaration* [[namespace.udecl]]; the pattern is a
|
| 2272 |
*using-declarator*.
|
|
|
|
|
|
|
| 2273 |
- In a template parameter pack that is a pack expansion [[temp.param]]:
|
| 2274 |
- if the template parameter pack is a *parameter-declaration*; the
|
| 2275 |
pattern is the *parameter-declaration* without the ellipsis;
|
| 2276 |
- if the template parameter pack is a *type-parameter*; the pattern is
|
| 2277 |
-
the corresponding *type-parameter* without the ellipsis
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2278 |
- In an *initializer-list* [[dcl.init]]; the pattern is an
|
| 2279 |
*initializer-clause*.
|
| 2280 |
- In a *base-specifier-list* [[class.derived]]; the pattern is a
|
| 2281 |
*base-specifier*.
|
| 2282 |
- In a *mem-initializer-list* [[class.base.init]] for a
|
|
@@ -2284,20 +2719,26 @@ the following contexts:
|
|
| 2284 |
pattern is the *mem-initializer*.
|
| 2285 |
- In a *template-argument-list* [[temp.arg]]; the pattern is a
|
| 2286 |
*template-argument*.
|
| 2287 |
- In an *attribute-list* [[dcl.attr.grammar]]; the pattern is an
|
| 2288 |
*attribute*.
|
|
|
|
|
|
|
| 2289 |
- In an *alignment-specifier* [[dcl.align]]; the pattern is the
|
| 2290 |
*alignment-specifier* without the ellipsis.
|
| 2291 |
- In a *capture-list* [[expr.prim.lambda.capture]]; the pattern is the
|
| 2292 |
*capture* without the ellipsis.
|
| 2293 |
- In a `sizeof...` expression [[expr.sizeof]]; the pattern is an
|
| 2294 |
*identifier*.
|
|
|
|
|
|
|
| 2295 |
- In a *fold-expression* [[expr.prim.fold]]; the pattern is the
|
| 2296 |
*cast-expression* that contains an unexpanded pack.
|
|
|
|
|
|
|
| 2297 |
|
| 2298 |
-
[*Example
|
| 2299 |
|
| 2300 |
``` cpp
|
| 2301 |
template<class ... Types> void f(Types ... rest);
|
| 2302 |
template<class ... Types> void g(Types ... rest) {
|
| 2303 |
f(&rest ...); // ``&rest ...'' is a pack expansion; ``&rest'' is its pattern
|
|
@@ -2317,11 +2758,11 @@ a pack expansion shall name one or more packs that are not expanded by a
|
|
| 2317 |
nested pack expansion; such packs are called *unexpanded packs* in the
|
| 2318 |
pattern. All of the packs expanded by a pack expansion shall have the
|
| 2319 |
same number of arguments specified. An appearance of a name of a pack
|
| 2320 |
that is not expanded is ill-formed.
|
| 2321 |
|
| 2322 |
-
[*Example
|
| 2323 |
|
| 2324 |
``` cpp
|
| 2325 |
template<typename...> struct Tuple {};
|
| 2326 |
template<typename T1, typename T2> struct Pair {};
|
| 2327 |
|
|
@@ -2353,33 +2794,45 @@ The instantiation of a pack expansion considers items
|
|
| 2353 |
expansion parameters. Each `Eᵢ` is generated by instantiating the
|
| 2354 |
pattern and replacing each pack expansion parameter with its iᵗʰ
|
| 2355 |
element. Such an element, in the context of the instantiation, is
|
| 2356 |
interpreted as follows:
|
| 2357 |
|
| 2358 |
-
- if the pack is a template parameter pack, the element is
|
| 2359 |
-
*
|
| 2360 |
-
*
|
| 2361 |
-
|
| 2362 |
-
|
| 2363 |
-
|
|
|
|
| 2364 |
- if the pack is a function parameter pack, the element is an
|
| 2365 |
*id-expression* designating the iᵗʰ function parameter that resulted
|
| 2366 |
from instantiation of the function parameter pack declaration;
|
| 2367 |
-
otherwise
|
| 2368 |
- if the pack is an *init-capture* pack, the element is an
|
| 2369 |
*id-expression* designating the variable introduced by the iᵗʰ
|
| 2370 |
*init-capture* that resulted from instantiation of the *init-capture*
|
| 2371 |
-
pack declaration
|
|
|
|
|
|
|
|
|
|
| 2372 |
|
| 2373 |
When N is zero, the instantiation of a pack expansion does not alter the
|
| 2374 |
syntactic interpretation of the enclosing construct, even in cases where
|
| 2375 |
omitting the pack expansion entirely would otherwise be ill-formed or
|
| 2376 |
would result in an ambiguity in the grammar.
|
| 2377 |
|
| 2378 |
The instantiation of a `sizeof...` expression [[expr.sizeof]] produces
|
| 2379 |
an integral constant with value N.
|
| 2380 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2381 |
The instantiation of a *fold-expression* [[expr.prim.fold]] produces:
|
| 2382 |
|
| 2383 |
- `(` `((`E₁ *op* E₂`)` *op* ⋯`)` *op* $\mathtt{E}_N$ `)` for a unary
|
| 2384 |
left fold,
|
| 2385 |
- `(` E₁ *op* `(`⋯ *op* `(`$\mathtt{E}_{N-1}$ *op* $\mathtt{E}_N$`))`
|
|
@@ -2391,11 +2844,11 @@ The instantiation of a *fold-expression* [[expr.prim.fold]] produces:
|
|
| 2391 |
|
| 2392 |
In each case, *op* is the *fold-operator*. For a binary fold, E is
|
| 2393 |
generated by instantiating the *cast-expression* that did not contain an
|
| 2394 |
unexpanded pack.
|
| 2395 |
|
| 2396 |
-
[*Example
|
| 2397 |
|
| 2398 |
``` cpp
|
| 2399 |
template<typename ...Args>
|
| 2400 |
bool all(Args ...args) { return (... && args); }
|
| 2401 |
|
|
@@ -2418,21 +2871,23 @@ If N is zero for a unary fold, the value of the expression is shown in
|
|
| 2418 |
| `&&` | `true` |
|
| 2419 |
| `||` | `false` |
|
| 2420 |
| `,` | `void()` |
|
| 2421 |
|
| 2422 |
|
|
|
|
|
|
|
| 2423 |
The instantiation of any other pack expansion produces a list of
|
| 2424 |
elements `E₁`, `E₂`, …, `E_N`.
|
| 2425 |
|
| 2426 |
[*Note 1*: The variety of list varies with the context:
|
| 2427 |
*expression-list*, *base-specifier-list*, *template-argument-list*,
|
| 2428 |
etc. — *end note*]
|
| 2429 |
|
| 2430 |
When N is zero, the instantiation of the expansion produces an empty
|
| 2431 |
list.
|
| 2432 |
|
| 2433 |
-
[*Example
|
| 2434 |
|
| 2435 |
``` cpp
|
| 2436 |
template<class... T> struct X : T... { };
|
| 2437 |
template<class... T> void f(T... values) {
|
| 2438 |
X<T...> x(values...);
|
|
@@ -2714,19 +3169,20 @@ A<int,int*> a; // uses the partial specialization, which is found through t
|
|
| 2714 |
// which refers to the primary template
|
| 2715 |
```
|
| 2716 |
|
| 2717 |
— *end example*]
|
| 2718 |
|
| 2719 |
-
A
|
| 2720 |
-
parameter. All other
|
|
|
|
| 2721 |
|
| 2722 |
Within the argument list of a partial specialization, the following
|
| 2723 |
restrictions apply:
|
| 2724 |
|
| 2725 |
- The type of a template parameter corresponding to a specialized
|
| 2726 |
-
|
| 2727 |
-
specialization.
|
| 2728 |
\[*Example 5*:
|
| 2729 |
``` cpp
|
| 2730 |
template <class T, T t> struct C {};
|
| 2731 |
template <class T> struct C<T, 1>; // error
|
| 2732 |
|
|
@@ -2737,11 +3193,11 @@ restrictions apply:
|
|
| 2737 |
|
| 2738 |
— *end example*]
|
| 2739 |
- The partial specialization shall be more specialized than the primary
|
| 2740 |
template [[temp.spec.partial.order]].
|
| 2741 |
- The template parameter list of a partial specialization shall not
|
| 2742 |
-
contain default template argument values.[^
|
| 2743 |
- An argument shall not contain an unexpanded pack. If an argument is a
|
| 2744 |
pack expansion [[temp.variadic]], it shall be the last argument in the
|
| 2745 |
template argument list.
|
| 2746 |
|
| 2747 |
The usual access checking rules do not apply to non-dependent names used
|
|
@@ -3001,11 +3457,11 @@ templates and non-template functions [[dcl.fct]] in the same
|
|
| 3001 |
scope. — *end note*]
|
| 3002 |
|
| 3003 |
A non-template function is not related to a function template (i.e., it
|
| 3004 |
is never considered to be a specialization), even if it has the same
|
| 3005 |
name and type as a potentially generated function template
|
| 3006 |
-
specialization.[^
|
| 3007 |
|
| 3008 |
#### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
|
| 3009 |
|
| 3010 |
It is possible to overload function templates so that two different
|
| 3011 |
function template specializations have the same type.
|
|
@@ -3071,11 +3527,11 @@ template <int K, int L> A<K+L> f(A<K>, A<L>); // same as #1
|
|
| 3071 |
template <int I, int J> A<I-J> f(A<I>, A<J>); // different from #1
|
| 3072 |
```
|
| 3073 |
|
| 3074 |
— *end example*]
|
| 3075 |
|
| 3076 |
-
[*Note 2*: Most expressions that use template parameters use
|
| 3077 |
template parameters, but it is possible for an expression to reference a
|
| 3078 |
type parameter. For example, a template type parameter can be used in
|
| 3079 |
the `sizeof` operator. — *end note*]
|
| 3080 |
|
| 3081 |
Two expressions involving template parameters are considered
|
|
@@ -3102,12 +3558,12 @@ signature of a function template with external linkage. — *end note*]
|
|
| 3102 |
|
| 3103 |
For determining whether two dependent names [[temp.dep]] are equivalent,
|
| 3104 |
only the name itself is considered, not the result of name lookup.
|
| 3105 |
|
| 3106 |
[*Note 5*: If such a dependent name is unqualified, it is looked up
|
| 3107 |
-
from
|
| 3108 |
-
[[temp.
|
| 3109 |
|
| 3110 |
[*Example 3*:
|
| 3111 |
|
| 3112 |
``` cpp
|
| 3113 |
template <int I, int J> void f(A<I+J>); // #1
|
|
@@ -3163,34 +3619,35 @@ declared with a *type-constraint*, and if either *template-head* has a
|
|
| 3163 |
corresponding *constraint-expression*s are equivalent. Two
|
| 3164 |
*template-parameter*s are *equivalent* under the following conditions:
|
| 3165 |
|
| 3166 |
- they declare template parameters of the same kind,
|
| 3167 |
- if either declares a template parameter pack, they both do,
|
| 3168 |
-
- if they declare
|
| 3169 |
types ignoring the use of *type-constraint*s for placeholder types,
|
| 3170 |
and
|
| 3171 |
-
- if they declare template template parameters, their template
|
| 3172 |
-
|
| 3173 |
|
| 3174 |
When determining whether types or *type-constraint*s are equivalent, the
|
| 3175 |
rules above are used to compare expressions involving template
|
| 3176 |
parameters. Two *template-head*s are *functionally equivalent* if they
|
| 3177 |
accept and are satisfied by [[temp.constr.constr]] the same set of
|
| 3178 |
template argument lists.
|
| 3179 |
|
| 3180 |
If the validity or meaning of the program depends on whether two
|
| 3181 |
constructs are equivalent, and they are functionally equivalent but not
|
| 3182 |
equivalent, the program is ill-formed, no diagnostic required.
|
| 3183 |
-
Furthermore, if two
|
| 3184 |
|
| 3185 |
-
-
|
| 3186 |
- have corresponding signatures [[basic.scope.scope]],
|
| 3187 |
-
- would declare the same entity
|
| 3188 |
-
|
| 3189 |
- accept and are satisfied by the same set of template argument lists,
|
| 3190 |
|
| 3191 |
-
the program is ill-formed, no diagnostic
|
|
|
|
| 3192 |
|
| 3193 |
[*Note 7*:
|
| 3194 |
|
| 3195 |
This rule guarantees that equivalent declarations will be linked with
|
| 3196 |
one another, while not requiring implementations to use heroic efforts
|
|
@@ -3240,20 +3697,24 @@ function type. The deduction process determines whether one of the
|
|
| 3240 |
templates is more specialized than the other. If so, the more
|
| 3241 |
specialized template is the one chosen by the partial ordering process.
|
| 3242 |
If both deductions succeed, the partial ordering selects the more
|
| 3243 |
constrained template (if one exists) as determined below.
|
| 3244 |
|
| 3245 |
-
To produce the transformed template, for each type,
|
| 3246 |
-
template template
|
| 3247 |
-
[[temp.variadic]] thereof) synthesize a unique
|
| 3248 |
-
|
| 3249 |
-
|
|
|
|
| 3250 |
|
| 3251 |
[*Note 1*: The type replacing the placeholder in the type of the value
|
| 3252 |
-
synthesized for a
|
| 3253 |
synthesized type. — *end note*]
|
| 3254 |
|
|
|
|
|
|
|
|
|
|
| 3255 |
Each function template M that is a member function is considered to have
|
| 3256 |
a new first parameter of type X(M), described below, inserted in its
|
| 3257 |
function parameter list. If exactly one of the function templates was
|
| 3258 |
considered by overload resolution via a rewritten candidate
|
| 3259 |
[[over.match.oper]] with a reversed order of parameters, then the order
|
|
@@ -3457,14 +3918,21 @@ void h() {
|
|
| 3457 |
A *template-declaration* in which the *declaration* is an
|
| 3458 |
*alias-declaration* [[dcl.pre]] declares the *identifier* to be an
|
| 3459 |
*alias template*. An alias template is a name for a family of types. The
|
| 3460 |
name of the alias template is a *template-name*.
|
| 3461 |
|
| 3462 |
-
|
| 3463 |
-
|
| 3464 |
-
*template-
|
| 3465 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3466 |
|
| 3467 |
[*Note 1*: An alias template name is never deduced. — *end note*]
|
| 3468 |
|
| 3469 |
[*Example 1*:
|
| 3470 |
|
|
@@ -3589,11 +4057,11 @@ specialized [[temp.expl.spec]], or partially specialized
|
|
| 3589 |
The *constraint-expression* of a *concept-definition* is an unevaluated
|
| 3590 |
operand [[expr.context]].
|
| 3591 |
|
| 3592 |
The first declared template parameter of a concept definition is its
|
| 3593 |
*prototype parameter*. A *type concept* is a concept whose prototype
|
| 3594 |
-
parameter is a type
|
| 3595 |
|
| 3596 |
## Name resolution <a id="temp.res">[[temp.res]]</a>
|
| 3597 |
|
| 3598 |
### General <a id="temp.res.general">[[temp.res.general]]</a>
|
| 3599 |
|
|
@@ -3682,11 +4150,11 @@ specialization [[temp.point]] but is not found by lookup for the
|
|
| 3682 |
specialization, the program is ill-formed, no diagnostic required.
|
| 3683 |
|
| 3684 |
``` bnf
|
| 3685 |
typename-specifier:
|
| 3686 |
typename nested-name-specifier identifier
|
| 3687 |
-
typename nested-name-specifier
|
| 3688 |
```
|
| 3689 |
|
| 3690 |
The component names of a *typename-specifier* are its *identifier* (if
|
| 3691 |
any) and those of its *nested-name-specifier* and *simple-template-id*
|
| 3692 |
(if any). A *typename-specifier* denotes the type or class template
|
|
@@ -3717,48 +4185,60 @@ void foo() {
|
|
| 3717 |
}
|
| 3718 |
```
|
| 3719 |
|
| 3720 |
— *end example*]
|
| 3721 |
|
| 3722 |
-
A
|
| 3723 |
-
if it is the terminal name of
|
| 3724 |
|
| 3725 |
-
- a *typename-specifier*, *nested-name-specifier*,
|
| 3726 |
-
*elaborated-type-specifier*, *class-or-decltype*,
|
|
|
|
|
|
|
| 3727 |
- a *type-specifier* of a
|
| 3728 |
- *new-type-id*,
|
| 3729 |
- *defining-type-id*,
|
| 3730 |
- *conversion-type-id*,
|
| 3731 |
- *trailing-return-type*,
|
| 3732 |
- default argument of a *type-parameter*, or
|
| 3733 |
- *type-id* of a `static_cast`, `const_cast`, `reinterpret_cast`, or
|
| 3734 |
`dynamic_cast`, or
|
| 3735 |
- a *decl-specifier* of the *decl-specifier-seq* of a
|
| 3736 |
-
- *simple-declaration* or
|
| 3737 |
- *member-declaration*,
|
| 3738 |
-
- *parameter-declaration* in a *member-declaration*,[^
|
| 3739 |
*parameter-declaration* appears in a default argument,
|
| 3740 |
- *parameter-declaration* in a *declarator* of a function or function
|
| 3741 |
template declaration whose *declarator-id* is qualified, unless that
|
| 3742 |
*parameter-declaration* appears in a default argument,
|
| 3743 |
- *parameter-declaration* in a *lambda-declarator* or
|
| 3744 |
*requirement-parameter-list*, unless that *parameter-declaration*
|
| 3745 |
appears in a default argument, or
|
| 3746 |
-
- *parameter-declaration* of a
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3747 |
|
| 3748 |
[*Example 5*:
|
| 3749 |
|
| 3750 |
``` cpp
|
| 3751 |
template<class T> T::R f(); // OK, return type of a function declaration at global scope
|
| 3752 |
template<class T> void f(T::R); // ill-formed, no diagnostic required: attempt to declare
|
| 3753 |
// a void variable template
|
|
|
|
| 3754 |
template<class T> struct S {
|
| 3755 |
using Ptr = PtrTraits<T>::Ptr; // OK, in a defining-type-id
|
|
|
|
| 3756 |
T::R f(T::P p) { // OK, class scope
|
| 3757 |
return static_cast<T::R>(p); // OK, type-id of a static_cast
|
| 3758 |
}
|
| 3759 |
auto g() -> S<T*>::Ptr; // OK, trailing-return-type
|
|
|
|
|
|
|
| 3760 |
};
|
| 3761 |
template<typename T> void f() {
|
| 3762 |
void (*pf)(T::X); // variable pf of type void* initialized with T::X
|
| 3763 |
void g(T::X); // error: T::X at block scope does not denote a type
|
| 3764 |
// (attempt to declare a void variable)
|
|
@@ -3793,33 +4273,45 @@ int main() {
|
|
| 3793 |
}
|
| 3794 |
```
|
| 3795 |
|
| 3796 |
— *end example*]
|
| 3797 |
|
| 3798 |
-
The validity of a
|
|
|
|
| 3799 |
|
| 3800 |
[*Note 3*: Knowing which names are type names allows the syntax of
|
| 3801 |
every template to be checked in this way. — *end note*]
|
| 3802 |
|
| 3803 |
-
The program is ill-formed, no diagnostic required, if
|
| 3804 |
|
| 3805 |
- no valid specialization, ignoring *static_assert-declaration*s that
|
| 3806 |
-
fail, can be generated for a
|
| 3807 |
-
if statement [[stmt.if]] within a
|
|
|
|
| 3808 |
instantiated, or
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3809 |
- any *constraint-expression* in the program, introduced or otherwise,
|
| 3810 |
has (in its normal form) an atomic constraint A where no satisfaction
|
| 3811 |
check of A could be well-formed and no satisfaction check of A is
|
| 3812 |
performed, or
|
| 3813 |
- every valid specialization of a variadic template requires an empty
|
| 3814 |
template parameter pack, or
|
| 3815 |
-
- a hypothetical instantiation of a
|
| 3816 |
-
definition would be ill-formed due to a construct
|
| 3817 |
-
|
|
|
|
| 3818 |
- the interpretation of such a construct in the hypothetical
|
| 3819 |
instantiation is different from the interpretation of the
|
| 3820 |
-
corresponding construct in any actual instantiation of the
|
|
|
|
| 3821 |
|
| 3822 |
[*Note 4*:
|
| 3823 |
|
| 3824 |
This can happen in situations including the following:
|
| 3825 |
|
|
@@ -3850,13 +4342,10 @@ This can happen in situations including the following:
|
|
| 3850 |
it names an explicit specialization that was not declared when the
|
| 3851 |
template was defined.
|
| 3852 |
|
| 3853 |
— *end note*]
|
| 3854 |
|
| 3855 |
-
Otherwise, no diagnostic shall be issued for a template for which a
|
| 3856 |
-
valid specialization can be generated.
|
| 3857 |
-
|
| 3858 |
[*Note 5*: If a template is instantiated, errors will be diagnosed
|
| 3859 |
according to the other rules in this document. Exactly when these errors
|
| 3860 |
are diagnosed is a quality of implementation issue. — *end note*]
|
| 3861 |
|
| 3862 |
[*Example 7*:
|
|
@@ -3893,12 +4382,12 @@ considered definitions [[temp.decls]]. — *end note*]
|
|
| 3893 |
### Locally declared names <a id="temp.local">[[temp.local]]</a>
|
| 3894 |
|
| 3895 |
Like normal (non-template) classes, class templates have an
|
| 3896 |
injected-class-name [[class.pre]]. The injected-class-name can be used
|
| 3897 |
as a *template-name* or a *type-name*. When it is used with a
|
| 3898 |
-
*template-argument-list*, as a *template-argument* for a template
|
| 3899 |
-
|
| 3900 |
*elaborated-type-specifier* of a friend class template declaration, it
|
| 3901 |
is a *template-name* that refers to the class template itself.
|
| 3902 |
Otherwise, it is a *type-name* equivalent to the *template-name*
|
| 3903 |
followed by the template argument list
|
| 3904 |
[[temp.decls.general]], [[temp.arg.general]] of the class template
|
|
@@ -3982,26 +4471,26 @@ template<class T> class X {
|
|
| 3982 |
};
|
| 3983 |
```
|
| 3984 |
|
| 3985 |
— *end example*]
|
| 3986 |
|
| 3987 |
-
The name of a
|
| 3988 |
-
declaration whose locus is contained by the scope to which the
|
| 3989 |
-
|
| 3990 |
|
| 3991 |
[*Example 5*:
|
| 3992 |
|
| 3993 |
``` cpp
|
| 3994 |
template<class T, int i> class Y {
|
| 3995 |
-
int T; // error: template
|
| 3996 |
void f() {
|
| 3997 |
-
char T; // error: template
|
| 3998 |
}
|
| 3999 |
friend void T(); // OK, no name bound
|
| 4000 |
};
|
| 4001 |
|
| 4002 |
-
template<class X> class X; // error: hidden by template
|
| 4003 |
```
|
| 4004 |
|
| 4005 |
— *end example*]
|
| 4006 |
|
| 4007 |
Unqualified name lookup considers the template parameter scope of a
|
|
@@ -4110,25 +4599,25 @@ A name or *template-id* refers to the *current instantiation* if it is
|
|
| 4110 |
class template, the name of the class template followed by the
|
| 4111 |
template argument list of its *template-head* [[temp.arg]] enclosed in
|
| 4112 |
`<>` (or an equivalent template alias specialization),
|
| 4113 |
- in the definition of a nested class of a class template, the name of
|
| 4114 |
the nested class referenced as a member of the current instantiation,
|
| 4115 |
-
or
|
| 4116 |
- in the definition of a class template partial specialization or a
|
| 4117 |
member of a class template partial specialization, the name of the
|
| 4118 |
class template followed by a template argument list equivalent to that
|
| 4119 |
of the partial specialization [[temp.spec.partial]] enclosed in `<>`
|
| 4120 |
-
(or an equivalent template alias specialization)
|
|
|
|
|
|
|
| 4121 |
|
| 4122 |
A template argument that is equivalent to a template parameter can be
|
| 4123 |
used in place of that template parameter in a reference to the current
|
| 4124 |
-
instantiation.
|
| 4125 |
-
|
| 4126 |
-
|
| 4127 |
-
|
| 4128 |
-
|
| 4129 |
-
template parameter if
|
| 4130 |
|
| 4131 |
- it has the same type as the template parameter (ignoring
|
| 4132 |
cv-qualification) and
|
| 4133 |
- its initializer consists of a single *identifier* that names the
|
| 4134 |
template parameter or, recursively, such a variable.
|
|
@@ -4240,11 +4729,11 @@ A qualified name [[basic.lookup.qual]] is dependent if
|
|
| 4240 |
- it is a *conversion-function-id* whose *conversion-type-id* is
|
| 4241 |
dependent, or
|
| 4242 |
- its lookup context is dependent and is not the current instantiation,
|
| 4243 |
or
|
| 4244 |
- its lookup context is the current instantiation and it is
|
| 4245 |
-
`operator=`,[^
|
| 4246 |
- its lookup context is the current instantiation and has at least one
|
| 4247 |
dependent base class, and qualified name lookup for the name finds
|
| 4248 |
nothing [[basic.lookup.qual]].
|
| 4249 |
|
| 4250 |
[*Example 4*:
|
|
@@ -4288,11 +4777,44 @@ struct C : A, T {
|
|
| 4288 |
int g() { return m; } // finds A::m in the template definition context
|
| 4289 |
};
|
| 4290 |
|
| 4291 |
template int C<B>::f(); // error: finds both A::m and B::m
|
| 4292 |
template int C<B>::g(); // OK, transformation to class member access syntax
|
| 4293 |
-
// does not occur in the template definition context; see~[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4294 |
```
|
| 4295 |
|
| 4296 |
— *end example*]
|
| 4297 |
|
| 4298 |
A type is dependent if it is
|
|
@@ -4306,16 +4828,21 @@ A type is dependent if it is
|
|
| 4306 |
- an array type whose element type is dependent or whose bound (if any)
|
| 4307 |
is value-dependent,
|
| 4308 |
- a function type whose parameters include one or more function
|
| 4309 |
parameter packs,
|
| 4310 |
- a function type whose exception specification is value-dependent,
|
|
|
|
|
|
|
| 4311 |
- denoted by a *simple-template-id* in which either the template name is
|
| 4312 |
-
a template parameter or any of the template arguments is
|
| 4313 |
-
|
| 4314 |
-
|
| 4315 |
- denoted by `decltype(`*expression*`)`, where *expression* is
|
| 4316 |
-
type-dependent [[temp.dep.expr]]
|
|
|
|
|
|
|
|
|
|
| 4317 |
|
| 4318 |
[*Note 3*: Because typedefs do not introduce new types, but instead
|
| 4319 |
simply refer to other types, a name that refers to a typedef that is a
|
| 4320 |
member of the current instantiation is dependent only if the type
|
| 4321 |
referred to is dependent. — *end note*]
|
|
@@ -4331,29 +4858,57 @@ dependent [[temp.dep.type]].
|
|
| 4331 |
An *id-expression* is type-dependent if it is a *template-id* that is
|
| 4332 |
not a concept-id and is dependent; or if its terminal name is
|
| 4333 |
|
| 4334 |
- associated by name lookup with one or more declarations declared with
|
| 4335 |
a dependent type,
|
| 4336 |
-
- associated by name lookup with a
|
| 4337 |
-
|
| 4338 |
-
[[dcl.spec.auto]],
|
| 4339 |
- associated by name lookup with a variable declared with a type that
|
| 4340 |
contains a placeholder type [[dcl.spec.auto]] where the initializer is
|
| 4341 |
type-dependent,
|
| 4342 |
- associated by name lookup with one or more declarations of member
|
| 4343 |
functions of a class that is the current instantiation declared with a
|
| 4344 |
return type that contains a placeholder type,
|
| 4345 |
- associated by name lookup with a structured binding declaration
|
| 4346 |
[[dcl.struct.bind]] whose *brace-or-equal-initializer* is
|
| 4347 |
type-dependent,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4348 |
- associated by name lookup with an entity captured by copy
|
| 4349 |
[[expr.prim.lambda.capture]] in a *lambda-expression* that has an
|
| 4350 |
explicit object parameter whose type is dependent [[dcl.fct]],
|
| 4351 |
- the *identifier* `__func__` [[dcl.fct.def.general]], where any
|
| 4352 |
enclosing function is a template, a member of a class template, or a
|
| 4353 |
generic lambda,
|
| 4354 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4355 |
- dependent
|
| 4356 |
|
| 4357 |
or if it names a dependent member of the current instantiation that is a
|
| 4358 |
static data member of type “array of unknown bound of `T`” for some `T`
|
| 4359 |
[[temp.static]]. Expressions of the following forms are type-dependent
|
|
@@ -4388,10 +4943,12 @@ typeid '(' expression ')'
|
|
| 4388 |
typeid '(' type-id ')'
|
| 4389 |
'::'ₒₚₜ delete cast-expression
|
| 4390 |
'::'ₒₚₜ delete '[' ']' cast-expression
|
| 4391 |
throw assignment-expressionₒₚₜ
|
| 4392 |
noexcept '(' expression ')'
|
|
|
|
|
|
|
| 4393 |
```
|
| 4394 |
|
| 4395 |
[*Note 1*: For the standard library macro `offsetof`, see
|
| 4396 |
[[support.types]]. — *end note*]
|
| 4397 |
|
|
@@ -4409,21 +4966,30 @@ always dependent. — *end note*]
|
|
| 4409 |
A *braced-init-list* is type-dependent if any element is type-dependent
|
| 4410 |
or is a pack expansion.
|
| 4411 |
|
| 4412 |
A *fold-expression* is type-dependent.
|
| 4413 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4414 |
#### Value-dependent expressions <a id="temp.dep.constexpr">[[temp.dep.constexpr]]</a>
|
| 4415 |
|
| 4416 |
Except as described below, an expression used in a context where a
|
| 4417 |
constant expression is required is value-dependent if any subexpression
|
| 4418 |
is value-dependent.
|
| 4419 |
|
| 4420 |
-
An *id-expression* is value-dependent if
|
| 4421 |
|
| 4422 |
-
- it is a concept-id and
|
|
|
|
| 4423 |
- it is type-dependent,
|
| 4424 |
-
- it is the name of a
|
|
|
|
|
|
|
| 4425 |
- it names a static data member that is a dependent member of the
|
| 4426 |
current instantiation and is not initialized in a *member-declarator*,
|
| 4427 |
- it names a static member function that is a dependent member of the
|
| 4428 |
current instantiation, or
|
| 4429 |
- it names a potentially-constant variable [[expr.const]] that is
|
|
@@ -4437,58 +5003,158 @@ dependent:
|
|
| 4437 |
sizeof unary-expression
|
| 4438 |
sizeof '(' type-id ')'
|
| 4439 |
typeid '(' expression ')'
|
| 4440 |
typeid '(' type-id ')'
|
| 4441 |
alignof '(' type-id ')'
|
| 4442 |
-
noexcept '(' expression ')'
|
| 4443 |
```
|
| 4444 |
|
| 4445 |
[*Note 1*: For the standard library macro `offsetof`, see
|
| 4446 |
[[support.types]]. — *end note*]
|
| 4447 |
|
| 4448 |
Expressions of the following form are value-dependent if either the
|
| 4449 |
-
*type-id*
|
| 4450 |
-
*cast-expression* is value-dependent
|
|
|
|
|
|
|
| 4451 |
|
| 4452 |
``` bnf
|
| 4453 |
simple-type-specifier '(' expression-listₒₚₜ ')'
|
|
|
|
|
|
|
|
|
|
| 4454 |
static_cast '<' type-id '>' '(' expression ')'
|
| 4455 |
const_cast '<' type-id '>' '(' expression ')'
|
| 4456 |
reinterpret_cast '<' type-id '>' '(' expression ')'
|
|
|
|
| 4457 |
'(' type-id ')' cast-expression
|
| 4458 |
```
|
| 4459 |
|
| 4460 |
Expressions of the following form are value-dependent:
|
| 4461 |
|
| 4462 |
``` bnf
|
| 4463 |
sizeof '...' '(' identifier ')'
|
| 4464 |
fold-expression
|
| 4465 |
```
|
| 4466 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4467 |
An expression of the form `&`*qualified-id* where the *qualified-id*
|
| 4468 |
names a dependent member of the current instantiation is
|
| 4469 |
value-dependent. An expression of the form `&`*cast-expression* is also
|
| 4470 |
value-dependent if evaluating *cast-expression* as a core constant
|
| 4471 |
expression [[expr.const]] succeeds and the result of the evaluation
|
| 4472 |
refers to a templated entity that is an object with static or thread
|
| 4473 |
storage duration or a member function.
|
| 4474 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4475 |
#### Dependent template arguments <a id="temp.dep.temp">[[temp.dep.temp]]</a>
|
| 4476 |
|
| 4477 |
A type *template-argument* is dependent if the type it specifies is
|
| 4478 |
dependent.
|
| 4479 |
|
| 4480 |
-
A
|
| 4481 |
the constant expression it specifies is value-dependent.
|
| 4482 |
|
| 4483 |
-
Furthermore, a
|
| 4484 |
-
corresponding
|
| 4485 |
type and the *template-argument* designates or points to a member of the
|
| 4486 |
current instantiation or a member of a dependent type.
|
| 4487 |
|
| 4488 |
-
A template
|
| 4489 |
-
|
|
|
|
|
|
|
| 4490 |
|
| 4491 |
### Dependent name resolution <a id="temp.dep.res">[[temp.dep.res]]</a>
|
| 4492 |
|
| 4493 |
#### Point of instantiation <a id="temp.point">[[temp.point]]</a>
|
| 4494 |
|
|
@@ -4558,10 +5224,16 @@ instantiation within a translation unit. A specialization for any
|
|
| 4558 |
template may have points of instantiation in multiple translation units.
|
| 4559 |
If two different points of instantiation give a template specialization
|
| 4560 |
different meanings according to the one-definition rule
|
| 4561 |
[[basic.def.odr]], the program is ill-formed, no diagnostic required.
|
| 4562 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4563 |
#### Candidate functions <a id="temp.dep.candidate">[[temp.dep.candidate]]</a>
|
| 4564 |
|
| 4565 |
If a dependent call [[temp.dep]] would be ill-formed or would find a
|
| 4566 |
better match had the lookup for its dependent name considered all the
|
| 4567 |
function declarations with external linkage introduced in the associated
|
|
@@ -4795,13 +5467,12 @@ variable template, a class template, a member of a class template, or a
|
|
| 4795 |
member template. An explicit specialization declaration is introduced by
|
| 4796 |
`template<>`. In an explicit specialization declaration for a variable
|
| 4797 |
template, a class template, a member of a class template, or a class
|
| 4798 |
member template, the variable or class that is explicitly specialized
|
| 4799 |
shall be specified with a *simple-template-id*. In the explicit
|
| 4800 |
-
specialization declaration for a function template
|
| 4801 |
-
|
| 4802 |
-
specified using a *template-id*.
|
| 4803 |
|
| 4804 |
[*Example 1*:
|
| 4805 |
|
| 4806 |
``` cpp
|
| 4807 |
template<class T = int> struct A {
|
|
@@ -5134,13 +5805,13 @@ void g(S<int>& sr) {
|
|
| 5134 |
};
|
| 5135 |
```
|
| 5136 |
|
| 5137 |
— *end example*]
|
| 5138 |
|
| 5139 |
-
If a function template
|
| 5140 |
-
|
| 5141 |
-
|
| 5142 |
|
| 5143 |
An implementation shall not implicitly instantiate a function template,
|
| 5144 |
a variable template, a member template, a non-virtual member function, a
|
| 5145 |
member class or static data member of a templated class, or a
|
| 5146 |
substatement of a constexpr if statement [[stmt.if]], unless such
|
|
@@ -5195,18 +5866,18 @@ void g(A a, A b, A c) {
|
|
| 5195 |
}
|
| 5196 |
```
|
| 5197 |
|
| 5198 |
— *end example*]
|
| 5199 |
|
| 5200 |
-
The *noexcept-specifier*
|
| 5201 |
-
|
| 5202 |
-
|
|
|
|
| 5203 |
but has not yet been instantiated, the dependent names are looked up,
|
| 5204 |
the semantics constraints are checked, and the instantiation of any
|
| 5205 |
-
template used in the
|
| 5206 |
-
|
| 5207 |
-
that point.
|
| 5208 |
|
| 5209 |
[*Note 6*: [[temp.point]] defines the point of instantiation of a
|
| 5210 |
template specialization. — *end note*]
|
| 5211 |
|
| 5212 |
There is an *implementation-defined* quantity that specifies the limit
|
|
@@ -5325,11 +5996,11 @@ instantiation is for a variable template specialization, the
|
|
| 5325 |
template<class T> class Array { void mf(); };
|
| 5326 |
template class Array<char>;
|
| 5327 |
template void Array<int>::mf();
|
| 5328 |
|
| 5329 |
template<class T> void sort(Array<T>& v) { ... }
|
| 5330 |
-
template void sort(Array<char>&); // argument is deduced here
|
| 5331 |
|
| 5332 |
namespace N {
|
| 5333 |
template<class T> void f(T&) { }
|
| 5334 |
}
|
| 5335 |
template void N::f<int>(int&);
|
|
@@ -5354,11 +6025,11 @@ The *declaration* in an *explicit-instantiation* and the *declaration*
|
|
| 5354 |
produced by the corresponding substitution into the templated function,
|
| 5355 |
variable, or class are two declarations of the same entity.
|
| 5356 |
|
| 5357 |
[*Note 1*:
|
| 5358 |
|
| 5359 |
-
These declarations
|
| 5360 |
[[basic.link]], except as specified in [[except.spec]].
|
| 5361 |
|
| 5362 |
[*Example 2*:
|
| 5363 |
|
| 5364 |
``` cpp
|
|
@@ -5366,11 +6037,11 @@ template<typename T> T var = {};
|
|
| 5366 |
template float var<float>; // OK, instantiated variable has type float
|
| 5367 |
template int var<int[16]>[]; // OK, absence of major array bound is permitted
|
| 5368 |
template int *var<int>; // error: instantiated variable has type int
|
| 5369 |
|
| 5370 |
template<typename T> auto av = T();
|
| 5371 |
-
template int av<int>; // OK, variable with type
|
| 5372 |
|
| 5373 |
template<typename T> auto f() {}
|
| 5374 |
template void f<int>(); // error: function with deduced return type
|
| 5375 |
// redeclared with non-deduced return type[dcl.spec.auto]
|
| 5376 |
```
|
|
@@ -5390,36 +6061,18 @@ that template, the explicit instantiation has no effect. Otherwise, for
|
|
| 5390 |
an explicit instantiation definition, the definition of a function
|
| 5391 |
template, a variable template, a member function template, or a member
|
| 5392 |
function or static data member of a class template shall be present in
|
| 5393 |
every translation unit in which it is explicitly instantiated.
|
| 5394 |
|
| 5395 |
-
|
| 5396 |
-
|
| 5397 |
-
|
| 5398 |
-
|
| 5399 |
-
|
| 5400 |
-
|
| 5401 |
-
|
| 5402 |
-
|
| 5403 |
-
``` cpp
|
| 5404 |
-
template<class T> class Array { ... };
|
| 5405 |
-
template<class T> void sort(Array<T>& v) { ... }
|
| 5406 |
-
|
| 5407 |
-
// instantiate sort(Array<int>&) -- template-argument deduced
|
| 5408 |
-
template void sort<>(Array<int>&);
|
| 5409 |
-
```
|
| 5410 |
-
|
| 5411 |
-
— *end example*]
|
| 5412 |
-
|
| 5413 |
-
[*Note 2*: An explicit instantiation of a constrained template is
|
| 5414 |
-
required to satisfy that template’s associated constraints
|
| 5415 |
-
[[temp.constr.decl]]. The satisfaction of constraints is determined when
|
| 5416 |
-
forming the template name of an explicit instantiation in which all
|
| 5417 |
-
template arguments are specified [[temp.names]], or, for explicit
|
| 5418 |
-
instantiations of function templates, during template argument deduction
|
| 5419 |
-
[[temp.deduct.decl]] when one or more trailing template arguments are
|
| 5420 |
-
left unspecified. — *end note*]
|
| 5421 |
|
| 5422 |
An explicit instantiation that names a class template specialization is
|
| 5423 |
also an explicit instantiation of the same kind (declaration or
|
| 5424 |
definition) of each of its direct non-template members that has not been
|
| 5425 |
previously explicitly specialized in the translation unit containing the
|
|
@@ -5459,11 +6112,11 @@ An explicit instantiation declaration shall not name a specialization of
|
|
| 5459 |
a template with internal linkage.
|
| 5460 |
|
| 5461 |
An explicit instantiation does not constitute a use of a default
|
| 5462 |
argument, so default argument instantiation is not done.
|
| 5463 |
|
| 5464 |
-
[*Example
|
| 5465 |
|
| 5466 |
``` cpp
|
| 5467 |
char* p = 0;
|
| 5468 |
template<class T> T g(T x = &p) { return x; }
|
| 5469 |
template int g<int>(int); // OK even though &p isn't an int.
|
|
@@ -5495,24 +6148,25 @@ explicit-specialization:
|
|
| 5495 |
[*Example 1*:
|
| 5496 |
|
| 5497 |
``` cpp
|
| 5498 |
template<class T> class stream;
|
| 5499 |
|
| 5500 |
-
template<> class stream<char> { ... };
|
| 5501 |
|
| 5502 |
template<class T> class Array { ... };
|
| 5503 |
template<class T> void sort(Array<T>& v) { ... }
|
| 5504 |
|
| 5505 |
-
template<> void sort<
|
|
|
|
| 5506 |
```
|
| 5507 |
|
| 5508 |
-
Given these declarations,
|
| 5509 |
-
of
|
| 5510 |
-
specializations instantiated from the class template. Similarly,
|
| 5511 |
-
|
| 5512 |
-
`Array<char*>`; other `Array`
|
| 5513 |
-
generated from the template.
|
| 5514 |
|
| 5515 |
— *end example*]
|
| 5516 |
|
| 5517 |
The *declaration* in an *explicit-specialization* shall not be an
|
| 5518 |
*export-declaration*. An explicit specialization shall not use a
|
|
@@ -5526,11 +6180,11 @@ An explicit specialization does not introduce a name
|
|
| 5526 |
[[basic.scope.scope]]. A declaration of a function template, class
|
| 5527 |
template, or variable template being explicitly specialized shall be
|
| 5528 |
reachable from the declaration of the explicit specialization.
|
| 5529 |
|
| 5530 |
[*Note 1*: A declaration, but not a definition of the template is
|
| 5531 |
-
|
| 5532 |
|
| 5533 |
The definition of a class or class template shall be reachable from the
|
| 5534 |
declaration of an explicit specialization for a member template of the
|
| 5535 |
class or class template.
|
| 5536 |
|
|
@@ -5676,13 +6330,14 @@ can affect whether a program is well-formed according to the relative
|
|
| 5676 |
positioning of the explicit specialization declarations and their points
|
| 5677 |
of instantiation in the translation unit as specified above and below.
|
| 5678 |
When writing a specialization, be careful about its location; or to make
|
| 5679 |
it compile will be such a trial as to kindle its self-immolation.
|
| 5680 |
|
| 5681 |
-
|
| 5682 |
-
|
| 5683 |
-
|
|
|
|
| 5684 |
[[basic.types]].
|
| 5685 |
|
| 5686 |
[*Example 5*:
|
| 5687 |
|
| 5688 |
``` cpp
|
|
@@ -5693,47 +6348,33 @@ X<int>* p; // OK, pointer to declared class
|
|
| 5693 |
X<int> x; // error: object of incomplete class X<int>
|
| 5694 |
```
|
| 5695 |
|
| 5696 |
— *end example*]
|
| 5697 |
|
| 5698 |
-
|
| 5699 |
-
|
| 5700 |
-
|
| 5701 |
-
|
| 5702 |
-
|
| 5703 |
-
|
| 5704 |
-
|
| 5705 |
-
|
| 5706 |
-
|
| 5707 |
-
|
| 5708 |
-
// explicit specialization for sort(Array<int>&)
|
| 5709 |
-
// with deduced template-argument of type int
|
| 5710 |
-
template<> void sort(Array<int>&);
|
| 5711 |
-
```
|
| 5712 |
-
|
| 5713 |
-
— *end example*]
|
| 5714 |
-
|
| 5715 |
-
[*Note 2*: An explicit specialization of a constrained template is
|
| 5716 |
-
required to satisfy that template’s associated constraints
|
| 5717 |
-
[[temp.constr.decl]]. The satisfaction of constraints is determined when
|
| 5718 |
-
forming the template name of an explicit specialization in which all
|
| 5719 |
-
template arguments are specified [[temp.names]], or, for explicit
|
| 5720 |
-
specializations of function templates, during template argument
|
| 5721 |
-
deduction [[temp.deduct.decl]] when one or more trailing template
|
| 5722 |
-
arguments are left unspecified. — *end note*]
|
| 5723 |
|
| 5724 |
A function with the same name as a template and a type that exactly
|
| 5725 |
matches that of a template specialization is not an explicit
|
| 5726 |
specialization [[temp.fct]].
|
| 5727 |
|
| 5728 |
Whether an explicit specialization of a function or variable template is
|
| 5729 |
inline, constexpr, constinit, or consteval is determined by the explicit
|
| 5730 |
specialization and is independent of those properties of the template.
|
| 5731 |
-
Similarly, attributes
|
| 5732 |
-
|
|
|
|
| 5733 |
|
| 5734 |
-
[*Example
|
| 5735 |
|
| 5736 |
``` cpp
|
| 5737 |
template<class T> void f(T) { ... }
|
| 5738 |
template<class T> inline T g(T) { ... }
|
| 5739 |
|
|
@@ -5752,11 +6393,11 @@ template<> void h<int>(int i) {
|
|
| 5752 |
An explicit specialization of a static data member of a template or an
|
| 5753 |
explicit specialization of a static data member template is a definition
|
| 5754 |
if the declaration includes an initializer; otherwise, it is a
|
| 5755 |
declaration.
|
| 5756 |
|
| 5757 |
-
[*Note
|
| 5758 |
|
| 5759 |
The definition of a static data member of a template for which
|
| 5760 |
default-initialization is desired can use functional cast notation
|
| 5761 |
[[expr.type.conv]]:
|
| 5762 |
|
|
@@ -5772,11 +6413,11 @@ A member or a member template of a class template may be explicitly
|
|
| 5772 |
specialized for a given implicit instantiation of the class template,
|
| 5773 |
even if the member or member template is defined in the class template
|
| 5774 |
definition. An explicit specialization of a member or member template is
|
| 5775 |
specified using the syntax for explicit specialization.
|
| 5776 |
|
| 5777 |
-
[*Example
|
| 5778 |
|
| 5779 |
``` cpp
|
| 5780 |
template<class T> struct A {
|
| 5781 |
void f(T);
|
| 5782 |
template<class X1> void g1(T, X1);
|
|
@@ -5808,11 +6449,11 @@ template<> void A<int>::h(int) { }
|
|
| 5808 |
A member or a member template may be nested within many enclosing class
|
| 5809 |
templates. In an explicit specialization for such a member, the member
|
| 5810 |
declaration shall be preceded by a `template<>` for each enclosing class
|
| 5811 |
template that is explicitly specialized.
|
| 5812 |
|
| 5813 |
-
[*Example
|
| 5814 |
|
| 5815 |
``` cpp
|
| 5816 |
template<class T1> class A {
|
| 5817 |
template<class T2> class B {
|
| 5818 |
void mf();
|
|
@@ -5834,11 +6475,11 @@ declaration, the keyword `template` followed by a
|
|
| 5834 |
*template-parameter-list* shall be provided instead of the `template<>`
|
| 5835 |
preceding the explicit specialization declaration of the member. The
|
| 5836 |
types of the *template-parameter*s in the *template-parameter-list*
|
| 5837 |
shall be the same as those specified in the primary template definition.
|
| 5838 |
|
| 5839 |
-
[*Example
|
| 5840 |
|
| 5841 |
``` cpp
|
| 5842 |
template <class T1> class A {
|
| 5843 |
template<class T2> class B {
|
| 5844 |
template<class T3> void mf1(T3);
|
|
@@ -5870,11 +6511,11 @@ definition for one of the following explicit specializations:
|
|
| 5870 |
|
| 5871 |
- the explicit specialization of a function template;
|
| 5872 |
- the explicit specialization of a member function template;
|
| 5873 |
- the explicit specialization of a member function of a class template
|
| 5874 |
where the class template specialization to which the member function
|
| 5875 |
-
specialization belongs is implicitly instantiated. \[*Note
|
| 5876 |
function arguments can be specified in the declaration or definition
|
| 5877 |
of a member function of a class template specialization that is
|
| 5878 |
explicitly specialized. — *end note*]
|
| 5879 |
|
| 5880 |
## Function template specializations <a id="temp.fct.spec">[[temp.fct.spec]]</a>
|
|
@@ -5999,14 +6640,14 @@ int l = f<>(1); // uses #1
|
|
| 5999 |
```
|
| 6000 |
|
| 6001 |
— *end note*]
|
| 6002 |
|
| 6003 |
Template arguments that are present shall be specified in the
|
| 6004 |
-
declaration order of their corresponding
|
| 6005 |
template argument list shall not specify more *template-argument*s than
|
| 6006 |
there are corresponding *template-parameter*s unless one of the
|
| 6007 |
-
*template-parameter*s
|
| 6008 |
|
| 6009 |
[*Example 3*:
|
| 6010 |
|
| 6011 |
``` cpp
|
| 6012 |
template<class X, class Y, class Z> X f(Y,Z);
|
|
@@ -6022,11 +6663,11 @@ void g() {
|
|
| 6022 |
|
| 6023 |
— *end example*]
|
| 6024 |
|
| 6025 |
Implicit conversions [[conv]] will be performed on a function argument
|
| 6026 |
to convert it to the type of the corresponding function parameter if the
|
| 6027 |
-
parameter type contains no
|
| 6028 |
template argument deduction.
|
| 6029 |
|
| 6030 |
[*Note 3*:
|
| 6031 |
|
| 6032 |
Template parameters do not participate in template argument deduction if
|
|
@@ -6096,13 +6737,14 @@ void g(double d) {
|
|
| 6096 |
```
|
| 6097 |
|
| 6098 |
— *end example*]
|
| 6099 |
|
| 6100 |
When an explicit template argument list is specified, if the given
|
| 6101 |
-
*template-id* is not valid
|
| 6102 |
-
|
| 6103 |
-
|
|
|
|
| 6104 |
|
| 6105 |
After this substitution is performed, the function parameter type
|
| 6106 |
adjustments described in [[dcl.fct]] are performed.
|
| 6107 |
|
| 6108 |
[*Example 2*: A parameter type of “`void (const int, int[5])`” becomes
|
|
@@ -6227,29 +6869,33 @@ when any template arguments that were deduced or obtained from default
|
|
| 6227 |
arguments are substituted.
|
| 6228 |
|
| 6229 |
The *deduction substitution loci* are
|
| 6230 |
|
| 6231 |
- the function type outside of the *noexcept-specifier*,
|
| 6232 |
-
- the *explicit-specifier*,
|
| 6233 |
-
- the template parameter declarations
|
|
|
|
|
|
|
| 6234 |
|
| 6235 |
The substitution occurs in all types and expressions that are used in
|
| 6236 |
the deduction substitution loci. The expressions include not only
|
| 6237 |
constant expressions such as those that appear in array bounds or as
|
| 6238 |
-
|
| 6239 |
non-constant expressions) inside `sizeof`, `decltype`, and other
|
| 6240 |
contexts that allow non-constant expressions. The substitution proceeds
|
| 6241 |
in lexical order and stops when a condition that causes deduction to
|
| 6242 |
fail is encountered. If substitution into different declarations of the
|
| 6243 |
same function template would cause template instantiations to occur in a
|
| 6244 |
different order or not at all, the program is ill-formed; no diagnostic
|
| 6245 |
required.
|
| 6246 |
|
| 6247 |
-
[*Note 4*: The equivalent substitution in exception specifications
|
| 6248 |
-
|
| 6249 |
-
|
| 6250 |
-
|
|
|
|
|
|
|
| 6251 |
|
| 6252 |
[*Example 6*:
|
| 6253 |
|
| 6254 |
``` cpp
|
| 6255 |
template <class T> struct A { using X = typename T::X; };
|
|
@@ -6287,13 +6933,12 @@ effects such as the instantiation of class template specializations
|
|
| 6287 |
and/or function template specializations, the generation of
|
| 6288 |
implicitly-defined functions, etc. Such effects are not in the
|
| 6289 |
“immediate context” and can result in the program being
|
| 6290 |
ill-formed. — *end note*]
|
| 6291 |
|
| 6292 |
-
|
| 6293 |
-
|
| 6294 |
-
purposes of template argument deduction.
|
| 6295 |
|
| 6296 |
[*Note 7*:
|
| 6297 |
|
| 6298 |
The intent is to avoid requiring implementations to deal with
|
| 6299 |
substitution failure involving arbitrary statements.
|
|
@@ -6378,11 +7023,12 @@ Type deduction can fail for the following reasons:
|
|
| 6378 |
*qualified-id* when that type does not contain the specified member,
|
| 6379 |
or
|
| 6380 |
- the specified member is not a type where a type is required, or
|
| 6381 |
- the specified member is not a template where a template is required,
|
| 6382 |
or
|
| 6383 |
-
- the specified member is not a non-type
|
|
|
|
| 6384 |
|
| 6385 |
\[*Example 11*:
|
| 6386 |
``` cpp
|
| 6387 |
template <int I> struct X { };
|
| 6388 |
template <template <class T> class> struct Z { };
|
|
@@ -6400,11 +7046,11 @@ Type deduction can fail for the following reasons:
|
|
| 6400 |
|
| 6401 |
int main() {
|
| 6402 |
// Deduction fails in each of these cases:
|
| 6403 |
f<A>(0); // A does not contain a member Y
|
| 6404 |
f<B>(0); // The Y member of B is not a type
|
| 6405 |
-
g<C>(0); // The N member of C is not a non-type
|
| 6406 |
h<D>(0); // The TT member of D is not a template
|
| 6407 |
}
|
| 6408 |
```
|
| 6409 |
|
| 6410 |
— *end example*]
|
|
@@ -6417,19 +7063,19 @@ Type deduction can fail for the following reasons:
|
|
| 6417 |
template <class T> int f(int T::*);
|
| 6418 |
int i = f<int>(0);
|
| 6419 |
```
|
| 6420 |
|
| 6421 |
— *end example*]
|
| 6422 |
-
- Attempting to give an invalid type to a
|
| 6423 |
\[*Example 13*:
|
| 6424 |
``` cpp
|
| 6425 |
template <class T, T> struct S {};
|
| 6426 |
template <class T> int f(S<T, T{}>*); // #1
|
| 6427 |
class X {
|
| 6428 |
int m;
|
| 6429 |
};
|
| 6430 |
-
int i0 = f<X>(0); // #1 uses a value of non-structural type X as a
|
| 6431 |
```
|
| 6432 |
|
| 6433 |
— *end example*]
|
| 6434 |
- Attempting to perform an invalid conversion in either a template
|
| 6435 |
argument expression, or an expression used in the function
|
|
@@ -6442,10 +7088,13 @@ Type deduction can fail for the following reasons:
|
|
| 6442 |
|
| 6443 |
— *end example*]
|
| 6444 |
- Attempting to create a function type in which a parameter has a type
|
| 6445 |
of `void`, or in which the return type is a function type or array
|
| 6446 |
type.
|
|
|
|
|
|
|
|
|
|
| 6447 |
|
| 6448 |
— *end note*]
|
| 6449 |
|
| 6450 |
[*Example 15*:
|
| 6451 |
|
|
@@ -6465,23 +7114,23 @@ int i2 = f<1>(0); // ambiguous; not narrowing
|
|
| 6465 |
— *end example*]
|
| 6466 |
|
| 6467 |
#### Deducing template arguments from a function call <a id="temp.deduct.call">[[temp.deduct.call]]</a>
|
| 6468 |
|
| 6469 |
Template argument deduction is done by comparing each function template
|
| 6470 |
-
parameter type (call it `P`) that contains
|
| 6471 |
participate in template argument deduction with the type of the
|
| 6472 |
corresponding argument of the call (call it `A`) as described below. If
|
| 6473 |
removing references and cv-qualifiers from `P` gives
|
| 6474 |
-
|
| 6475 |
-
and the argument is a non-empty initializer list
|
| 6476 |
-
deduction is performed instead for each element
|
| 6477 |
-
independently, taking `P`' as separate function
|
| 6478 |
-
`P`'_i and the iᵗʰ initializer element as the
|
| 6479 |
-
the `P`'`[N]` case, if `N` is a
|
| 6480 |
-
deduced from the length of the initializer
|
| 6481 |
-
initializer list argument causes the parameter to be
|
| 6482 |
-
non-deduced context [[temp.deduct.type]].
|
| 6483 |
|
| 6484 |
[*Example 1*:
|
| 6485 |
|
| 6486 |
``` cpp
|
| 6487 |
template<class T> void f(std::initializer_list<T>);
|
|
@@ -6619,19 +7268,20 @@ that allow a difference:
|
|
| 6619 |
transformed `A`.
|
| 6620 |
- The transformed `A` can be another pointer or pointer-to-member type
|
| 6621 |
that can be converted to the deduced `A` via a function pointer
|
| 6622 |
conversion [[conv.fctptr]] and/or qualification conversion
|
| 6623 |
[[conv.qual]].
|
| 6624 |
-
- If `P` is a class and `P` has the form *simple-template-id*
|
| 6625 |
-
|
| 6626 |
-
|
| 6627 |
-
|
| 6628 |
-
|
| 6629 |
-
|
| 6630 |
-
|
| 6631 |
-
|
| 6632 |
-
|
|
|
|
| 6633 |
\[*Example 5*:
|
| 6634 |
``` cpp
|
| 6635 |
template <typename... T> struct X;
|
| 6636 |
template <> struct X<> {};
|
| 6637 |
template <typename T, typename... Ts>
|
|
@@ -6650,11 +7300,11 @@ that allow a difference:
|
|
| 6650 |
|
| 6651 |
These alternatives are considered only if type deduction would otherwise
|
| 6652 |
fail. If they yield more than one possible deduced `A`, the type
|
| 6653 |
deduction fails.
|
| 6654 |
|
| 6655 |
-
[*Note 1*: If a
|
| 6656 |
parameters of a function template, or is used only in a non-deduced
|
| 6657 |
context, its corresponding *template-argument* cannot be deduced from a
|
| 6658 |
function call and the *template-argument* must be explicitly
|
| 6659 |
specified. — *end note*]
|
| 6660 |
|
|
@@ -6663,14 +7313,14 @@ pointer-to-member-function type:
|
|
| 6663 |
|
| 6664 |
- If the argument is an overload set containing one or more function
|
| 6665 |
templates, the parameter is treated as a non-deduced context.
|
| 6666 |
- If the argument is an overload set (not containing function
|
| 6667 |
templates), trial argument deduction is attempted using each of the
|
| 6668 |
-
members of the set
|
| 6669 |
-
|
| 6670 |
-
|
| 6671 |
-
|
| 6672 |
|
| 6673 |
[*Example 6*:
|
| 6674 |
|
| 6675 |
``` cpp
|
| 6676 |
// Only one function of an overload set matches the call so the function parameter is a deduced context.
|
|
@@ -6704,10 +7354,26 @@ template <class T> T g(T);
|
|
| 6704 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 6705 |
```
|
| 6706 |
|
| 6707 |
— *end example*]
|
| 6708 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6709 |
#### Deducing template arguments taking the address of a function template <a id="temp.deduct.funcaddr">[[temp.deduct.funcaddr]]</a>
|
| 6710 |
|
| 6711 |
Template arguments can be deduced from the type specified when taking
|
| 6712 |
the address of an overload set [[over.over]]. If there is a target, the
|
| 6713 |
function template’s function type and the target type are used as the
|
|
@@ -6787,11 +7453,11 @@ as the parameter template.
|
|
| 6787 |
|
| 6788 |
The types used to determine the ordering depend on the context in which
|
| 6789 |
the partial ordering is done:
|
| 6790 |
|
| 6791 |
- In the context of a function call, the types used are those function
|
| 6792 |
-
parameter types for which the function call has arguments.[^
|
| 6793 |
- In the context of a call to a conversion function, the return types of
|
| 6794 |
the conversion function templates are used.
|
| 6795 |
- In other contexts [[temp.func.order]] the function template’s function
|
| 6796 |
type is used.
|
| 6797 |
|
|
@@ -6914,13 +7580,13 @@ g(Tuple<int>()); // calls #3
|
|
| 6914 |
|
| 6915 |
Template arguments can be deduced in several different contexts, but in
|
| 6916 |
each case a type that is specified in terms of template parameters (call
|
| 6917 |
it `P`) is compared with an actual type (call it `A`), and an attempt is
|
| 6918 |
made to find template argument values (a type for a type parameter, a
|
| 6919 |
-
value for a
|
| 6920 |
-
that will make `P`, after substitution of the
|
| 6921 |
-
the deduced `A`), compatible with `A`.
|
| 6922 |
|
| 6923 |
In some cases, the deduction is done using a single set of types `P` and
|
| 6924 |
`A`, in other cases, there will be a set of corresponding types `P` and
|
| 6925 |
`A`. Type deduction is done independently for each `P/A` pair, and the
|
| 6926 |
deduced template argument values are then combined. If type deduction
|
|
@@ -6930,52 +7596,57 @@ pairs yield different deduced values, or if any template argument
|
|
| 6930 |
remains neither deduced nor explicitly specified, template argument
|
| 6931 |
deduction fails. The type of a type parameter is only deduced from an
|
| 6932 |
array bound if it is not otherwise deduced.
|
| 6933 |
|
| 6934 |
A given type `P` can be composed from a number of other types,
|
| 6935 |
-
templates, and
|
| 6936 |
|
| 6937 |
- A function type includes the types of each of the function parameters,
|
| 6938 |
the return type, and its exception specification.
|
| 6939 |
- A pointer-to-member type includes the type of the class object pointed
|
| 6940 |
to and the type of the member pointed to.
|
| 6941 |
- A type that is a specialization of a class template (e.g., `A<int>`)
|
| 6942 |
-
includes the types, templates, and
|
| 6943 |
-
template argument list of the specialization.
|
| 6944 |
- An array type includes the array element type and the value of the
|
| 6945 |
array bound.
|
| 6946 |
|
| 6947 |
-
In most cases, the types, templates, and
|
| 6948 |
-
to compose `P` participate in template argument
|
| 6949 |
-
may be used to determine the value of a
|
| 6950 |
-
argument deduction fails if the value so
|
| 6951 |
-
with the values determined elsewhere. In
|
| 6952 |
-
value does not participate in type
|
| 6953 |
-
values of template arguments that were
|
| 6954 |
-
explicitly specified. If a template
|
| 6955 |
-
non-deduced contexts and is not explicitly
|
| 6956 |
-
deduction fails.
|
| 6957 |
|
| 6958 |
-
[*Note 1*: Under [[temp.deduct.call]], if `P` contains no
|
| 6959 |
-
|
| 6960 |
-
|
| 6961 |
|
| 6962 |
The non-deduced contexts are:
|
| 6963 |
|
| 6964 |
- The *nested-name-specifier* of a type that was specified using a
|
| 6965 |
*qualified-id*.
|
|
|
|
|
|
|
| 6966 |
- The *expression* of a *decltype-specifier*.
|
| 6967 |
-
-
|
|
|
|
| 6968 |
subexpression references a template parameter.
|
| 6969 |
- A template parameter used in the parameter type of a function
|
| 6970 |
parameter that has a default argument that is being used in the call
|
| 6971 |
for which argument deduction is being done.
|
| 6972 |
- A function parameter for which the associated argument is an overload
|
| 6973 |
-
set
|
| 6974 |
-
-
|
|
|
|
| 6975 |
(resulting in an ambiguous deduction), or
|
| 6976 |
-
- no function
|
|
|
|
| 6977 |
- the overload set supplied as an argument contains one or more
|
| 6978 |
function templates.
|
| 6979 |
- A function parameter for which the associated argument is an
|
| 6980 |
initializer list [[dcl.init.list]] but the parameter does not have a
|
| 6981 |
type for which deduction from an initializer list is specified
|
|
@@ -7084,13 +7755,15 @@ void t() {
|
|
| 7084 |
}
|
| 7085 |
```
|
| 7086 |
|
| 7087 |
— *end example*]
|
| 7088 |
|
| 7089 |
-
A
|
| 7090 |
-
template
|
| 7091 |
-
|
|
|
|
|
|
|
| 7092 |
|
| 7093 |
``` cpp
|
| 7094 |
\opt{cv} T
|
| 7095 |
T*
|
| 7096 |
T&
|
|
@@ -7099,47 +7772,48 @@ T&&
|
|
| 7099 |
\opt{T}(\opt{T}) noexcept(\opt{i})
|
| 7100 |
\opt{T} \opt{T}::*
|
| 7101 |
\opt{TT}<T>
|
| 7102 |
\opt{TT}<i>
|
| 7103 |
\opt{TT}<TT>
|
|
|
|
| 7104 |
\opt{TT}<>
|
| 7105 |
```
|
| 7106 |
|
| 7107 |
where
|
| 7108 |
|
| 7109 |
- `\opt{T}` represents a type or parameter-type-list that either
|
| 7110 |
satisfies these rules recursively, is a non-deduced context in `P` or
|
| 7111 |
`A`, or is the same non-dependent type in `P` and `A`,
|
| 7112 |
-
- `\opt{TT}` represents either a class template or a template template
|
| 7113 |
-
parameter,
|
| 7114 |
- `\opt{i}` represents an expression that either is an `i`, is
|
| 7115 |
value-dependent in `P` or `A`, or has the same constant value in `P`
|
| 7116 |
-
and `A`,
|
|
|
|
|
|
|
| 7117 |
- `noexcept(\opt{i})` represents an exception specification
|
| 7118 |
[[except.spec]] in which the (possibly-implicit, see [[dcl.fct]])
|
| 7119 |
*noexcept-specifier*’s operand satisfies the rules for an `\opt{i}`
|
| 7120 |
above.
|
| 7121 |
|
| 7122 |
[*Note 2*: If a type matches such a form but contains no `T`s, `i`s, or
|
| 7123 |
`TT`s, deduction is not possible. — *end note*]
|
| 7124 |
|
| 7125 |
-
Similarly, `<
|
| 7126 |
-
argument contains
|
| 7127 |
-
|
| 7128 |
-
|
| 7129 |
|
| 7130 |
-
If `P` has a form that contains `<T>`
|
| 7131 |
-
the respective template argument list of `P` is
|
| 7132 |
-
corresponding argument Aᵢ of the corresponding
|
| 7133 |
-
`A`. If the template argument list of `P`
|
| 7134 |
-
is not the last template argument, the
|
| 7135 |
-
a non-deduced context. If `Pᵢ` is a
|
| 7136 |
-
`Pᵢ` is compared with each remaining
|
| 7137 |
-
list of `A`. Each comparison deduces
|
| 7138 |
-
positions in the template parameter
|
| 7139 |
-
partial ordering [[temp.deduct.partial]],
|
| 7140 |
-
expansion:
|
| 7141 |
|
| 7142 |
- if `P` does not contain a template argument corresponding to `Aᵢ` then
|
| 7143 |
`Aᵢ` is ignored;
|
| 7144 |
- otherwise, if `Pᵢ` is not a pack expansion, template argument
|
| 7145 |
deduction fails.
|
|
@@ -7240,11 +7914,11 @@ where type is `X<int>` and `T` is `char[6]`.
|
|
| 7240 |
— *end example*]
|
| 7241 |
|
| 7242 |
Template arguments cannot be deduced from function arguments involving
|
| 7243 |
constructs other than the ones specified above.
|
| 7244 |
|
| 7245 |
-
When the value of the argument corresponding to a
|
| 7246 |
parameter `P` that is declared with a dependent type is deduced from an
|
| 7247 |
expression, the template parameters in the type of `P` are deduced from
|
| 7248 |
the type of the value.
|
| 7249 |
|
| 7250 |
[*Example 8*:
|
|
@@ -7331,12 +8005,12 @@ void g() {
|
|
| 7331 |
|
| 7332 |
— *end note*]
|
| 7333 |
|
| 7334 |
[*Note 5*:
|
| 7335 |
|
| 7336 |
-
If, in the declaration of a function template with a
|
| 7337 |
-
parameter, the
|
| 7338 |
the function parameter list, the expression is a non-deduced context as
|
| 7339 |
specified above.
|
| 7340 |
|
| 7341 |
[*Example 12*:
|
| 7342 |
|
|
@@ -7377,13 +8051,14 @@ int x = deduce<77>(a.xm, 62, b.ym);
|
|
| 7377 |
|
| 7378 |
— *end note*]
|
| 7379 |
|
| 7380 |
If `P` has a form that contains `<i>`, and if the type of `i` differs
|
| 7381 |
from the type of the corresponding template parameter of the template
|
| 7382 |
-
named by the enclosing *simple-template-id*
|
| 7383 |
-
|
| 7384 |
-
|
|
|
|
| 7385 |
|
| 7386 |
If `P` has a form that includes `noexcept(i)` and the type of `i` is not
|
| 7387 |
`bool`, deduction fails.
|
| 7388 |
|
| 7389 |
[*Example 13*:
|
|
@@ -7443,11 +8118,11 @@ void g() {
|
|
| 7443 |
}
|
| 7444 |
```
|
| 7445 |
|
| 7446 |
— *end example*]
|
| 7447 |
|
| 7448 |
-
The *template-argument* corresponding to a template
|
| 7449 |
is deduced from the type of the *template-argument* of a class template
|
| 7450 |
specialization used in the argument list of a function call.
|
| 7451 |
|
| 7452 |
[*Example 16*:
|
| 7453 |
|
|
@@ -7515,23 +8190,26 @@ or implicitly using the operator notation), template argument deduction
|
|
| 7515 |
[[temp.deduct]] and checking of any explicit template arguments
|
| 7516 |
[[temp.arg]] are performed for each function template to find the
|
| 7517 |
template argument values (if any) that can be used with that function
|
| 7518 |
template to instantiate a function template specialization that can be
|
| 7519 |
invoked with the call arguments or, for conversion function templates,
|
| 7520 |
-
that can convert to the required type. For each function template
|
| 7521 |
-
|
| 7522 |
-
|
| 7523 |
-
|
| 7524 |
-
|
| 7525 |
-
|
| 7526 |
-
|
| 7527 |
-
|
| 7528 |
-
|
| 7529 |
-
|
| 7530 |
-
|
| 7531 |
-
|
| 7532 |
-
|
|
|
|
|
|
|
|
|
|
| 7533 |
|
| 7534 |
[*Example 1*:
|
| 7535 |
|
| 7536 |
``` cpp
|
| 7537 |
template<class T> T max(T a, T b) { return a>b?a:b; }
|
|
@@ -7556,11 +8234,11 @@ conversion of `char` to `int` for `c`.
|
|
| 7556 |
— *end example*]
|
| 7557 |
|
| 7558 |
[*Example 2*:
|
| 7559 |
|
| 7560 |
Here is an example involving conversions on a function argument involved
|
| 7561 |
-
in
|
| 7562 |
|
| 7563 |
``` cpp
|
| 7564 |
template<class T> struct B { ... };
|
| 7565 |
template<class T> struct D : public B<T> { ... };
|
| 7566 |
template<class T> void f(B<T>&);
|
|
@@ -7574,11 +8252,11 @@ void g(B<int>& bi, D<int>& di) {
|
|
| 7574 |
— *end example*]
|
| 7575 |
|
| 7576 |
[*Example 3*:
|
| 7577 |
|
| 7578 |
Here is an example involving conversions on a function argument not
|
| 7579 |
-
involved in
|
| 7580 |
|
| 7581 |
``` cpp
|
| 7582 |
template<class T> void f(T*,int); // #1
|
| 7583 |
template<class T> void f(T,char); // #2
|
| 7584 |
|
|
@@ -7622,10 +8300,11 @@ in some translation unit [[temp.pre]].
|
|
| 7622 |
[basic.lookup]: basic.md#basic.lookup
|
| 7623 |
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
| 7624 |
[basic.lookup.qual]: basic.md#basic.lookup.qual
|
| 7625 |
[basic.scope.namespace]: basic.md#basic.scope.namespace
|
| 7626 |
[basic.scope.scope]: basic.md#basic.scope.scope
|
|
|
|
| 7627 |
[basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
|
| 7628 |
[basic.types]: basic.md#basic.types
|
| 7629 |
[class.access]: class.md#class.access
|
| 7630 |
[class.base.init]: class.md#class.base.init
|
| 7631 |
[class.conv.fct]: class.md#class.conv.fct
|
|
@@ -7633,10 +8312,11 @@ in some translation unit [[temp.pre]].
|
|
| 7633 |
[class.default.ctor]: class.md#class.default.ctor
|
| 7634 |
[class.derived]: class.md#class.derived
|
| 7635 |
[class.dtor]: class.md#class.dtor
|
| 7636 |
[class.local]: class.md#class.local
|
| 7637 |
[class.mem]: class.md#class.mem
|
|
|
|
| 7638 |
[class.member.lookup]: basic.md#class.member.lookup
|
| 7639 |
[class.pre]: class.md#class.pre
|
| 7640 |
[class.qual]: basic.md#class.qual
|
| 7641 |
[class.temporary]: basic.md#class.temporary
|
| 7642 |
[conv]: expr.md#conv
|
|
@@ -7645,28 +8325,33 @@ in some translation unit [[temp.pre]].
|
|
| 7645 |
[conv.func]: expr.md#conv.func
|
| 7646 |
[conv.lval]: expr.md#conv.lval
|
| 7647 |
[conv.qual]: expr.md#conv.qual
|
| 7648 |
[dcl.align]: dcl.md#dcl.align
|
| 7649 |
[dcl.attr.grammar]: dcl.md#dcl.attr.grammar
|
|
|
|
|
|
|
|
|
|
| 7650 |
[dcl.decl]: dcl.md#dcl.decl
|
| 7651 |
[dcl.fct]: dcl.md#dcl.fct
|
| 7652 |
[dcl.fct.def.general]: dcl.md#dcl.fct.def.general
|
| 7653 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 7654 |
[dcl.init]: dcl.md#dcl.init
|
| 7655 |
[dcl.init.list]: dcl.md#dcl.init.list
|
| 7656 |
[dcl.meaning]: dcl.md#dcl.meaning
|
| 7657 |
[dcl.pre]: dcl.md#dcl.pre
|
| 7658 |
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
|
|
|
| 7659 |
[dcl.stc]: dcl.md#dcl.stc
|
| 7660 |
[dcl.struct.bind]: dcl.md#dcl.struct.bind
|
| 7661 |
[dcl.type.class.deduct]: dcl.md#dcl.type.class.deduct
|
| 7662 |
[dcl.type.elab]: dcl.md#dcl.type.elab
|
| 7663 |
[dcl.type.simple]: dcl.md#dcl.type.simple
|
| 7664 |
[depr.template.template]: future.md#depr.template.template
|
| 7665 |
[except.spec]: except.md#except.spec
|
| 7666 |
[expr.const]: expr.md#expr.const
|
| 7667 |
[expr.context]: expr.md#expr.context
|
|
|
|
| 7668 |
[expr.log.and]: expr.md#expr.log.and
|
| 7669 |
[expr.log.or]: expr.md#expr.log.or
|
| 7670 |
[expr.new]: expr.md#expr.new
|
| 7671 |
[expr.prim.fold]: expr.md#expr.prim.fold
|
| 7672 |
[expr.prim.id]: expr.md#expr.prim.id
|
|
@@ -7677,13 +8362,15 @@ in some translation unit [[temp.pre]].
|
|
| 7677 |
[expr.prim.this]: expr.md#expr.prim.this
|
| 7678 |
[expr.ref]: expr.md#expr.ref
|
| 7679 |
[expr.sizeof]: expr.md#expr.sizeof
|
| 7680 |
[expr.type.conv]: expr.md#expr.type.conv
|
| 7681 |
[expr.typeid]: expr.md#expr.typeid
|
|
|
|
| 7682 |
[expr.unary.op]: expr.md#expr.unary.op
|
| 7683 |
[implimits]: limits.md#implimits
|
| 7684 |
[intro.defs]: intro.md#intro.defs
|
|
|
|
| 7685 |
[intro.object]: basic.md#intro.object
|
| 7686 |
[lex.string]: lex.md#lex.string
|
| 7687 |
[module.unit]: module.md#module.unit
|
| 7688 |
[namespace.udecl]: dcl.md#namespace.udecl
|
| 7689 |
[over.match]: over.md#over.match
|
|
@@ -7692,10 +8379,11 @@ in some translation unit [[temp.pre]].
|
|
| 7692 |
[over.match.funcs]: over.md#over.match.funcs
|
| 7693 |
[over.match.oper]: over.md#over.match.oper
|
| 7694 |
[over.match.viable]: over.md#over.match.viable
|
| 7695 |
[over.over]: over.md#over.over
|
| 7696 |
[special]: class.md#special
|
|
|
|
| 7697 |
[stmt.if]: stmt.md#stmt.if
|
| 7698 |
[support.types]: support.md#support.types
|
| 7699 |
[temp]: #temp
|
| 7700 |
[temp.alias]: #temp.alias
|
| 7701 |
[temp.arg]: #temp.arg
|
|
@@ -7707,13 +8395,15 @@ in some translation unit [[temp.pre]].
|
|
| 7707 |
[temp.class]: #temp.class
|
| 7708 |
[temp.class.general]: #temp.class.general
|
| 7709 |
[temp.concept]: #temp.concept
|
| 7710 |
[temp.constr]: #temp.constr
|
| 7711 |
[temp.constr.atomic]: #temp.constr.atomic
|
|
|
|
| 7712 |
[temp.constr.constr]: #temp.constr.constr
|
| 7713 |
[temp.constr.constr.general]: #temp.constr.constr.general
|
| 7714 |
[temp.constr.decl]: #temp.constr.decl
|
|
|
|
| 7715 |
[temp.constr.general]: #temp.constr.general
|
| 7716 |
[temp.constr.normal]: #temp.constr.normal
|
| 7717 |
[temp.constr.op]: #temp.constr.op
|
| 7718 |
[temp.constr.order]: #temp.constr.order
|
| 7719 |
[temp.decls]: #temp.decls
|
|
@@ -7730,11 +8420,13 @@ in some translation unit [[temp.pre]].
|
|
| 7730 |
[temp.dep]: #temp.dep
|
| 7731 |
[temp.dep.candidate]: #temp.dep.candidate
|
| 7732 |
[temp.dep.constexpr]: #temp.dep.constexpr
|
| 7733 |
[temp.dep.expr]: #temp.dep.expr
|
| 7734 |
[temp.dep.general]: #temp.dep.general
|
|
|
|
| 7735 |
[temp.dep.res]: #temp.dep.res
|
|
|
|
| 7736 |
[temp.dep.temp]: #temp.dep.temp
|
| 7737 |
[temp.dep.type]: #temp.dep.type
|
| 7738 |
[temp.expl.spec]: #temp.expl.spec
|
| 7739 |
[temp.explicit]: #temp.explicit
|
| 7740 |
[temp.fct]: #temp.fct
|
|
@@ -7769,66 +8461,62 @@ in some translation unit [[temp.pre]].
|
|
| 7769 |
[temp.type]: #temp.type
|
| 7770 |
[temp.variadic]: #temp.variadic
|
| 7771 |
[term.incomplete.type]: basic.md#term.incomplete.type
|
| 7772 |
[term.odr.use]: basic.md#term.odr.use
|
| 7773 |
|
| 7774 |
-
[^1]:
|
| 7775 |
-
*template-argument*s are treated as types for descriptive purposes,
|
| 7776 |
-
the terms *non-type parameter* and *non-type argument* are used to
|
| 7777 |
-
refer to non-type, non-template parameters and arguments.
|
| 7778 |
-
|
| 7779 |
-
[^2]: A `>` that encloses the *type-id* of a `dynamic_cast`,
|
| 7780 |
`static_cast`, `reinterpret_cast` or `const_cast`, or which encloses
|
| 7781 |
-
the *template-argument*s of a subsequent *template-id*
|
| 7782 |
-
considered nested for the
|
|
|
|
| 7783 |
|
| 7784 |
-
[^
|
| 7785 |
because the form of the *template-parameter* determines the
|
| 7786 |
allowable forms of the *template-argument*.
|
| 7787 |
|
| 7788 |
-
[^
|
| 7789 |
-
disjunction of clauses where each clause is a conjunction of
|
| 7790 |
-
constraints. For atomic constraints A, B, and C,
|
| 7791 |
-
normal form of the constraint A ∧ (B ∨ C) is
|
| 7792 |
-
disjunctive clauses are (A ∧ B) and (A ∧ C).
|
| 7793 |
|
| 7794 |
-
[^
|
| 7795 |
-
conjunction of clauses where each clause is a disjunction of
|
| 7796 |
-
constraints. For atomic constraints A, B, and C,
|
| 7797 |
-
A ∧ (B ∨ C) is in conjunctive normal form. Its
|
| 7798 |
-
are A and (B ∨ C).
|
| 7799 |
|
| 7800 |
-
[^
|
| 7801 |
|
| 7802 |
-
[^
|
| 7803 |
|
| 7804 |
-
[^
|
| 7805 |
|
| 7806 |
-
[^
|
| 7807 |
guide overload resolution of function template specializations with
|
| 7808 |
the same name. If such a non-template function is odr-used
|
| 7809 |
[[term.odr.use]] in a program, it must be defined; it will not be
|
| 7810 |
implicitly instantiated using the function template definition.
|
| 7811 |
|
| 7812 |
-
[^
|
| 7813 |
|
| 7814 |
-
[^
|
| 7815 |
of assignment operators.
|
| 7816 |
|
| 7817 |
-
[^
|
| 7818 |
template used without a *template-argument-list*.
|
| 7819 |
|
| 7820 |
-
[^
|
| 7821 |
context; they only become arguments after a function has been
|
| 7822 |
selected.
|
| 7823 |
|
| 7824 |
-
[^
|
| 7825 |
-
|
| 7826 |
-
|
| 7827 |
-
|
| 7828 |
|
| 7829 |
-
[^
|
| 7830 |
template parameter types. The set of conversions allowed on deduced
|
| 7831 |
arguments is limited, because the argument deduction process
|
| 7832 |
produces function templates with parameters that either match the
|
| 7833 |
call arguments exactly or differ only in ways that can be bridged by
|
| 7834 |
the allowed limited conversions. Non-deduced arguments allow the
|
|
|
|
| 48 |
- declare or define a function, a class, or a variable, or
|
| 49 |
- define a member function, a member class, a member enumeration, or a
|
| 50 |
static data member of a class template or of a class nested within a
|
| 51 |
class template, or
|
| 52 |
- define a member template of a class or class template, or
|
| 53 |
+
- be a *friend-type-declaration*, or
|
| 54 |
- be a *deduction-guide*, or
|
| 55 |
- be an *alias-declaration*.
|
| 56 |
|
| 57 |
A *template-declaration* is a *declaration*. A declaration introduced by
|
| 58 |
a template declaration of a variable is a *variable template*. A
|
|
|
|
| 91 |
[*Note 3*: A class or variable template declaration of a
|
| 92 |
*simple-template-id* declares a partial specialization
|
| 93 |
[[temp.spec.partial]]. — *end note*]
|
| 94 |
|
| 95 |
In a *template-declaration*, explicit specialization, or explicit
|
| 96 |
+
instantiation, the *init-declarator-list* in the declaration shall
|
| 97 |
contain at most one declarator. When such a declaration is used to
|
| 98 |
declare a class template, no declarator is permitted.
|
| 99 |
|
| 100 |
A specialization (explicit or implicit) of one template is distinct from
|
| 101 |
all specializations of any other template. A template, an explicit
|
|
|
|
| 105 |
[*Note 4*: Default arguments for function templates and for member
|
| 106 |
functions of class templates are considered definitions for the purpose
|
| 107 |
of template instantiation [[temp.decls]] and must obey the
|
| 108 |
one-definition rule [[basic.def.odr]]. — *end note*]
|
| 109 |
|
| 110 |
+
[*Note 5*:
|
| 111 |
+
|
| 112 |
+
A template cannot have the same name as any other name bound in the same
|
| 113 |
+
scope [[basic.scope.scope]], except that a function template can share a
|
| 114 |
+
name with *using-declarator*s, a type, non-template functions
|
| 115 |
+
[[dcl.fct]] and/or function templates [[temp.over]]. Specializations,
|
| 116 |
+
including partial specializations [[temp.spec.partial]], do not
|
| 117 |
+
reintroduce or bind names. Their target scope is the target scope of the
|
| 118 |
+
primary template, so all specializations of a template belong to the
|
| 119 |
+
same scope as it does.
|
| 120 |
+
|
| 121 |
+
[*Example 2*:
|
| 122 |
+
|
| 123 |
+
``` cpp
|
| 124 |
+
void f() {}
|
| 125 |
+
class f {}; // OK
|
| 126 |
+
namespace N {
|
| 127 |
+
void f(int) {}
|
| 128 |
+
}
|
| 129 |
+
using N::f; // OK
|
| 130 |
+
template<typename> void f(long) {} // #1, OK
|
| 131 |
+
template<typename> void f(long) {} // error: redefinition of #1
|
| 132 |
+
template<typename> void f(long long) {} // OK
|
| 133 |
+
template<> void f<int>(long long) {} // OK, doesn't bind a name
|
| 134 |
+
```
|
| 135 |
+
|
| 136 |
+
— *end example*]
|
| 137 |
+
|
| 138 |
+
— *end note*]
|
| 139 |
|
| 140 |
An entity is *templated* if it is
|
| 141 |
|
| 142 |
- a template,
|
| 143 |
+
- an entity defined [[basic.def]] or created [[class.temporary]] within
|
| 144 |
+
the *compound-statement* of an *expansion-statement* [[stmt.expand]],
|
| 145 |
+
- an entity defined or created in a templated entity,
|
| 146 |
- a member of a templated entity,
|
| 147 |
- an enumerator for an enumeration that is a templated entity, or
|
| 148 |
- the closure type of a *lambda-expression* [[expr.prim.lambda.closure]]
|
| 149 |
appearing in the declaration of a templated entity.
|
| 150 |
|
|
|
|
| 170 |
|
| 171 |
The expression in a *requires-clause* uses a restricted grammar to avoid
|
| 172 |
ambiguities. Parentheses can be used to specify arbitrary expressions in
|
| 173 |
a *requires-clause*.
|
| 174 |
|
| 175 |
+
[*Example 3*:
|
| 176 |
|
| 177 |
``` cpp
|
| 178 |
template<int N> requires N == sizeof new unsigned short
|
| 179 |
int f(); // error: parentheses required around == expression
|
| 180 |
```
|
|
|
|
| 196 |
|
| 197 |
``` bnf
|
| 198 |
template-parameter:
|
| 199 |
type-parameter
|
| 200 |
parameter-declaration
|
| 201 |
+
type-tt-parameter
|
| 202 |
+
variable-tt-parameter
|
| 203 |
+
concept-tt-parameter
|
| 204 |
```
|
| 205 |
|
| 206 |
``` bnf
|
| 207 |
type-parameter:
|
| 208 |
type-parameter-key '...'ₒₚₜ identifierₒₚₜ
|
| 209 |
type-parameter-key identifierₒₚₜ '=' type-id
|
| 210 |
type-constraint '...'ₒₚₜ identifierₒₚₜ
|
| 211 |
type-constraint identifierₒₚₜ '=' type-id
|
|
|
|
|
|
|
| 212 |
```
|
| 213 |
|
| 214 |
``` bnf
|
| 215 |
type-parameter-key:
|
| 216 |
class
|
|
|
|
| 221 |
type-constraint:
|
| 222 |
nested-name-specifierₒₚₜ concept-name
|
| 223 |
nested-name-specifierₒₚₜ concept-name '<' template-argument-listₒₚₜ '>'
|
| 224 |
```
|
| 225 |
|
| 226 |
+
``` bnf
|
| 227 |
+
type-tt-parameter:
|
| 228 |
+
template-head type-parameter-key '...'ₒₚₜ identifierₒₚₜ
|
| 229 |
+
template-head type-parameter-key identifierₒₚₜ type-tt-parameter-default
|
| 230 |
+
```
|
| 231 |
+
|
| 232 |
+
``` bnf
|
| 233 |
+
type-tt-parameter-default:
|
| 234 |
+
'=' nested-name-specifierₒₚₜ template-name
|
| 235 |
+
'=' nested-name-specifier 'template' template-name
|
| 236 |
+
```
|
| 237 |
+
|
| 238 |
+
``` bnf
|
| 239 |
+
variable-tt-parameter:
|
| 240 |
+
template-head 'auto' '...'ₒₚₜ identifierₒₚₜ
|
| 241 |
+
template-head 'auto' identifierₒₚₜ '=' nested-name-specifierₒₚₜ template-name
|
| 242 |
+
```
|
| 243 |
+
|
| 244 |
+
``` bnf
|
| 245 |
+
concept-tt-parameter:
|
| 246 |
+
'template' '<' template-parameter-list '>' 'concept' '...'ₒₚₜ identifierₒₚₜ
|
| 247 |
+
'template' '<' template-parameter-list '>' 'concept' identifierₒₚₜ '=' nested-name-specifierₒₚₜ template-name
|
| 248 |
+
```
|
| 249 |
+
|
| 250 |
The component names of a *type-constraint* are its *concept-name* and
|
| 251 |
those of its *nested-name-specifier* (if any).
|
| 252 |
|
| 253 |
[*Note 1*: The `>` token following the *template-parameter-list* of a
|
| 254 |
+
*type-tt-parameter*, *variable-tt-parameter*, or *concept-tt-parameter*
|
| 255 |
+
can be the product of replacing a `>>` token by two consecutive `>`
|
| 256 |
+
tokens [[temp.names]]. — *end note*]
|
| 257 |
+
|
| 258 |
+
A template parameter is of one of the following kinds:
|
| 259 |
+
|
| 260 |
+
- A *type template parameter* is a template parameter introduced by a
|
| 261 |
+
*type-parameter*.
|
| 262 |
+
- A *constant template parameter* is a template parameter introduced by
|
| 263 |
+
a *parameter-declaration*.
|
| 264 |
+
- A *type template template parameter* is a template parameter
|
| 265 |
+
introduced by a *type-tt-parameter*.
|
| 266 |
+
- A *variable template template parameter* is a template parameter
|
| 267 |
+
introduced by a *variable-tt-parameter*.
|
| 268 |
+
- A *concept template parameter* is a template parameter introduced by a
|
| 269 |
+
*concept-tt-parameter*.
|
| 270 |
+
|
| 271 |
+
Type template template parameters, variable template template
|
| 272 |
+
parameters, and concept template parameters are collectively referred to
|
| 273 |
+
as *template template parameters*.
|
| 274 |
+
|
| 275 |
+
The *nested-name-specifier* of a *type-constraint*, if any, shall not be
|
| 276 |
+
dependent.
|
| 277 |
+
|
| 278 |
+
A concept template parameter shall not have associated constraints
|
| 279 |
+
[[temp.constr.decl]].
|
| 280 |
+
|
| 281 |
+
If a *template-parameter* is a *parameter-declaration* that declares a
|
| 282 |
+
pack [[dcl.fct]], or otherwise has an ellipsis prior to its optional
|
| 283 |
+
*identifier*, then the *template-parameter* declares a template
|
| 284 |
+
parameter pack [[temp.variadic]]. A template parameter pack that is a
|
| 285 |
+
*parameter-declaration* whose type contains one or more unexpanded packs
|
| 286 |
+
is a pack expansion. Similarly, a template parameter pack that is a
|
| 287 |
+
template template parameter with a *template-parameter-list* containing
|
| 288 |
+
one or more unexpanded packs is a pack expansion. A type parameter pack
|
| 289 |
+
with a *type-constraint* that contains an unexpanded parameter pack is a
|
| 290 |
+
pack expansion. A template parameter pack that is a pack expansion shall
|
| 291 |
+
not expand a template parameter pack declared in the same
|
| 292 |
+
*template-parameter-list*.
|
| 293 |
+
|
| 294 |
+
[*Example 1*:
|
| 295 |
+
|
| 296 |
+
``` cpp
|
| 297 |
+
template <class... Types> // Types is a template type parameter pack
|
| 298 |
+
class Tuple; // but not a pack expansion
|
| 299 |
+
|
| 300 |
+
template <class T, int... Dims> // Dims is a constant template parameter pack
|
| 301 |
+
struct multi_array; // but not a pack expansion
|
| 302 |
+
|
| 303 |
+
template <class... T>
|
| 304 |
+
struct value_holder {
|
| 305 |
+
template <T... Values> struct apply { }; // Values is a constant template parameter pack
|
| 306 |
+
}; // and a pack expansion
|
| 307 |
+
|
| 308 |
+
template <class... T, T... Values> // error: Values expands template type parameter
|
| 309 |
+
struct static_array; // pack T within the same template parameter list
|
| 310 |
+
```
|
| 311 |
+
|
| 312 |
+
— *end example*]
|
| 313 |
|
| 314 |
There is no semantic difference between `class` and `typename` in a
|
| 315 |
*type-parameter-key*. `typename` followed by an *unqualified-id* names a
|
| 316 |
template type parameter. `typename` followed by a *qualified-id* denotes
|
| 317 |
+
the type in a *parameter-declaration*. A *template-parameter* of the
|
| 318 |
+
form `class` *identifier* is a *type-parameter*.
|
| 319 |
|
| 320 |
+
[*Example 2*:
|
|
|
|
|
|
|
|
|
|
| 321 |
|
| 322 |
``` cpp
|
| 323 |
class T { ... };
|
| 324 |
int i;
|
| 325 |
|
| 326 |
template<class T, T i> void f(T t) {
|
| 327 |
+
T t1 = i; // template parameters T and i
|
| 328 |
::T t2 = ::i; // global namespace members T and i
|
| 329 |
}
|
| 330 |
```
|
| 331 |
|
| 332 |
+
Here, the template `f` has a type template parameter called `T`, rather
|
| 333 |
+
than an unnamed constant template parameter of class `T`.
|
| 334 |
|
| 335 |
— *end example*]
|
| 336 |
|
| 337 |
+
The *parameter-declaration* of a *template-parameter* shall not have a
|
| 338 |
+
*storage-class-specifier*. Types shall not be defined in a template
|
| 339 |
+
parameter declaration.
|
| 340 |
|
| 341 |
+
The *identifier* in a *template-parameter* denoting a type or template
|
| 342 |
+
is not looked up. An *identifier* that does not follow an ellipsis is
|
| 343 |
+
defined to be
|
|
|
|
|
|
|
| 344 |
|
| 345 |
+
- a *typedef-name* for a *type-parameter*,
|
| 346 |
+
- a *template-name* for a *variable-tt-parameter*,
|
| 347 |
+
- a *template-name* for a *type-tt-parameter*, or
|
| 348 |
+
- a *concept-name* for a *concept-tt-parameter*,
|
| 349 |
|
| 350 |
+
in the scope of the template declaration.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
|
| 352 |
A *type-constraint* `Q` that designates a concept `C` can be used to
|
| 353 |
constrain a contextually-determined type or template type parameter pack
|
| 354 |
`T` with a *constraint-expression* `E` defined as follows. If `Q` is of
|
| 355 |
+
the form `C<A₁, …, Aₙ>`, then let `E'` be `C<T, A₁, …, Aₙ>`. Otherwise,
|
| 356 |
let `E'` be `C<T>`. If `T` is not a pack, then `E` is `E'`, otherwise
|
| 357 |
`E` is `(E' && ...)`. This *constraint-expression* `E` is called the
|
| 358 |
*immediately-declared constraint* of `Q` for `T`. The concept designated
|
| 359 |
by a *type-constraint* shall be a type concept [[temp.concept]].
|
| 360 |
|
| 361 |
A *type-parameter* that starts with a *type-constraint* introduces the
|
| 362 |
immediately-declared constraint of the *type-constraint* for the
|
| 363 |
parameter.
|
| 364 |
|
| 365 |
+
[*Example 3*:
|
| 366 |
|
| 367 |
``` cpp
|
| 368 |
template<typename T> concept C1 = true;
|
| 369 |
template<typename... Ts> concept C2 = true;
|
| 370 |
template<typename T, typename U> concept C3 = true;
|
|
|
|
| 376 |
template<C3<int>... T> struct s5; // associates (C3<T, int> && ...)
|
| 377 |
```
|
| 378 |
|
| 379 |
— *end example*]
|
| 380 |
|
| 381 |
+
A constant template parameter shall have one of the following (possibly
|
| 382 |
+
cv-qualified) types:
|
| 383 |
|
| 384 |
- a structural type (see below),
|
| 385 |
- a type that contains a placeholder type [[dcl.spec.auto]], or
|
| 386 |
- a placeholder for a deduced class type [[dcl.type.class.deduct]].
|
| 387 |
|
|
|
|
| 393 |
- a scalar type, or
|
| 394 |
- an lvalue reference type, or
|
| 395 |
- a literal class type with the following properties:
|
| 396 |
- all base classes and non-static data members are public and
|
| 397 |
non-mutable and
|
| 398 |
+
- the types of all base classes and non-static data members are
|
| 399 |
+
structural types or (possibly multidimensional) arrays thereof.
|
| 400 |
|
| 401 |
+
An *id-expression* naming a constant template parameter of class type
|
| 402 |
`T` denotes a static storage duration object of type `const T`, known as
|
| 403 |
+
a *template parameter object*, which is template-argument-equivalent
|
| 404 |
+
[[temp.type]] to the corresponding template argument after it has been
|
| 405 |
+
converted to the type of the template parameter [[temp.arg.nontype]]. No
|
| 406 |
+
two template parameter objects are template-argument-equivalent.
|
|
|
|
|
|
|
| 407 |
|
| 408 |
+
[*Note 2*: If an *id-expression* names a non-reference constant
|
| 409 |
+
template parameter, then it is a prvalue if it has non-class type.
|
| 410 |
Otherwise, if it is of class type `T`, it is an lvalue and has type
|
| 411 |
`const T` [[expr.prim.id.unqual]]. — *end note*]
|
| 412 |
|
| 413 |
+
[*Example 4*:
|
| 414 |
|
| 415 |
``` cpp
|
| 416 |
using X = int;
|
| 417 |
struct A {};
|
| 418 |
template<const X& x, int i, A a> void f() {
|
| 419 |
+
i++; // error: change of template parameter value
|
| 420 |
|
| 421 |
&x; // OK
|
| 422 |
+
&i; // error: address of non-reference template parameter
|
| 423 |
&a; // OK
|
| 424 |
int& ri = i; // error: attempt to bind non-const reference to temporary
|
| 425 |
const int& cri = i; // OK, const reference binds to temporary
|
| 426 |
const A& ra = a; // OK, const reference binds to a template parameter object
|
| 427 |
}
|
| 428 |
```
|
| 429 |
|
| 430 |
— *end example*]
|
| 431 |
|
| 432 |
+
[*Note 3*:
|
| 433 |
|
| 434 |
+
A constant template parameter cannot be declared to have type cv `void`.
|
|
|
|
| 435 |
|
| 436 |
+
[*Example 5*:
|
| 437 |
|
| 438 |
``` cpp
|
| 439 |
template<void v> class X; // error
|
| 440 |
template<void* pv> class Y; // OK
|
| 441 |
```
|
| 442 |
|
| 443 |
— *end example*]
|
| 444 |
|
| 445 |
— *end note*]
|
| 446 |
|
| 447 |
+
A constant template parameter of type “array of `T`” or of function type
|
| 448 |
+
`T` is adjusted to be of type “pointer to `T`”.
|
| 449 |
|
| 450 |
+
[*Example 6*:
|
| 451 |
|
| 452 |
``` cpp
|
| 453 |
template<int* a> struct R { ... };
|
| 454 |
template<int b[5]> struct S { ... };
|
| 455 |
int p;
|
|
|
|
| 460 |
S<v> z; // OK due to both adjustment and conversion
|
| 461 |
```
|
| 462 |
|
| 463 |
— *end example*]
|
| 464 |
|
| 465 |
+
A constant template parameter declared with a type that contains a
|
| 466 |
placeholder type with a *type-constraint* introduces the
|
| 467 |
immediately-declared constraint of the *type-constraint* for the
|
| 468 |
invented type corresponding to the placeholder [[dcl.fct]].
|
| 469 |
|
| 470 |
+
A *default template argument* is a template argument [[temp.arg]]
|
| 471 |
+
specified after `=` in a *template-parameter*. A default template
|
| 472 |
+
argument may be specified for any kind of template parameter that is not
|
| 473 |
+
a template parameter pack [[temp.variadic]]. A default template argument
|
| 474 |
+
may be specified in a template declaration. A default template argument
|
| 475 |
+
shall not be specified in the *template-parameter-list*s of the
|
| 476 |
+
definition of a member of a class template that appears outside of the
|
| 477 |
+
member’s class. A default template argument shall not be specified in a
|
| 478 |
+
friend class template declaration. If a friend function template
|
| 479 |
+
declaration D specifies a default template argument, that declaration
|
| 480 |
+
shall be a definition and there shall be no other declaration of the
|
| 481 |
+
function template which is reachable from D or from which D is
|
| 482 |
+
reachable.
|
| 483 |
|
| 484 |
+
The set of default template arguments available for use is obtained by
|
| 485 |
merging the default arguments from all prior declarations of the
|
| 486 |
template in the same way default function arguments are
|
| 487 |
[[dcl.fct.default]].
|
| 488 |
|
| 489 |
+
[*Example 7*:
|
| 490 |
|
| 491 |
``` cpp
|
| 492 |
template<class T1, class T2 = int> class A;
|
| 493 |
template<class T1 = int, class T2> class A;
|
| 494 |
```
|
|
|
|
| 500 |
```
|
| 501 |
|
| 502 |
— *end example*]
|
| 503 |
|
| 504 |
If a *template-parameter* of a class template, variable template, or
|
| 505 |
+
alias template has a default template argument, each subsequent
|
| 506 |
+
*template-parameter* shall either have a default template argument
|
| 507 |
+
supplied or declare a template parameter pack. If a *template-parameter*
|
| 508 |
+
of a primary class template, primary variable template, or alias
|
| 509 |
+
template declares a template parameter pack, it shall be the last
|
| 510 |
+
*template-parameter*. If a *template-parameter* of a function template
|
| 511 |
+
declares a template parameter pack, it shall not be followed by another
|
| 512 |
+
*template-parameter* unless that template parameter is deducible from
|
| 513 |
+
the parameter-type-list [[dcl.fct]] of the function template or has a
|
| 514 |
+
default argument [[temp.deduct]]. A template parameter of a deduction
|
| 515 |
guide template [[temp.deduct.guide]] that does not have a default
|
| 516 |
argument shall be deducible from the parameter-type-list of the
|
| 517 |
deduction guide template.
|
| 518 |
|
| 519 |
+
[*Example 8*:
|
| 520 |
|
| 521 |
``` cpp
|
| 522 |
template<class T1 = int, class T2> class B; // error
|
| 523 |
|
| 524 |
// U can be neither deduced from the parameter-type-list nor specified
|
|
|
|
| 526 |
template<class... T, class U> void g() { } // error
|
| 527 |
```
|
| 528 |
|
| 529 |
— *end example*]
|
| 530 |
|
| 531 |
+
When parsing a default template argument for a constant template
|
| 532 |
+
parameter, the first non-nested `>` is taken as the end of the
|
| 533 |
+
*template-parameter-list* rather than a greater-than operator.
|
| 534 |
|
| 535 |
+
[*Example 9*:
|
| 536 |
|
| 537 |
``` cpp
|
| 538 |
template<int i = 3 > 4 > // syntax error
|
| 539 |
class X { ... };
|
| 540 |
|
|
|
|
| 543 |
```
|
| 544 |
|
| 545 |
— *end example*]
|
| 546 |
|
| 547 |
A *template-parameter* of a template *template-parameter* is permitted
|
| 548 |
+
to have a default template argument. When such default arguments are
|
| 549 |
specified, they apply to the template *template-parameter* in the scope
|
| 550 |
of the template *template-parameter*.
|
| 551 |
|
| 552 |
+
[*Example 10*:
|
| 553 |
|
| 554 |
``` cpp
|
| 555 |
template <template <class TT = float> class T> struct A {
|
| 556 |
inline void f();
|
| 557 |
inline void g();
|
|
|
|
| 564 |
}
|
| 565 |
```
|
| 566 |
|
| 567 |
— *end example*]
|
| 568 |
|
| 569 |
+
The associated constraints of a template template parameter shall not
|
| 570 |
+
contain a concept-dependent constraint [[temp.constr.concept]].
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 571 |
|
| 572 |
+
[*Example 11*:
|
| 573 |
|
| 574 |
``` cpp
|
| 575 |
+
template<
|
| 576 |
+
template<typename> concept C,
|
| 577 |
+
template<C> class TT // error: C forms a concept-dependent constraint
|
| 578 |
+
>
|
| 579 |
+
struct A {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 580 |
```
|
| 581 |
|
| 582 |
— *end example*]
|
| 583 |
|
| 584 |
## Names of template specializations <a id="temp.names">[[temp.names]]</a>
|
|
|
|
| 611 |
|
| 612 |
``` bnf
|
| 613 |
template-argument:
|
| 614 |
constant-expression
|
| 615 |
type-id
|
| 616 |
+
nested-name-specifierₒₚₜ template-name
|
| 617 |
+
nested-name-specifier 'template' template-name
|
| 618 |
+
braced-init-list
|
| 619 |
```
|
| 620 |
|
| 621 |
The component name of a *simple-template-id*, *template-id*, or
|
| 622 |
*template-name* is the first name in it.
|
| 623 |
|
| 624 |
A `<` is interpreted as the delimiter of a *template-argument-list* if
|
| 625 |
+
either
|
| 626 |
|
| 627 |
+
- it follows a *splice-specifier* that either
|
| 628 |
+
- appears in a type-only context or
|
| 629 |
+
- is preceded by `template` or `typename`, or
|
| 630 |
+
- it follows a name that is not a *conversion-function-id* and
|
| 631 |
- that follows the keyword `template` or a `~` after a
|
| 632 |
*nested-name-specifier* or in a class member access expression, or
|
| 633 |
- for which name lookup finds the injected-class-name of a class
|
| 634 |
template or finds any declaration of a template, or
|
| 635 |
+
- that is an unqualified name for which name lookup either finds one
|
| 636 |
+
or more functions or finds nothing, or
|
| 637 |
- that is a terminal name in a *using-declarator* [[namespace.udecl]],
|
| 638 |
+
in a *declarator-id* [[dcl.meaning]], or in a type-only context
|
| 639 |
+
other than a *nested-name-specifier* [[temp.res]].
|
| 640 |
|
| 641 |
[*Note 1*: If the name is an *identifier*, it is then interpreted as a
|
| 642 |
*template-name*. The keyword `template` is used to indicate that a
|
| 643 |
dependent qualified name [[temp.dep.type]] denotes a template where an
|
| 644 |
expression might appear. — *end note*]
|
|
|
|
| 653 |
template<class T> void f(T* p) {
|
| 654 |
T* p1 = p->alloc<200>(); // error: < means less than
|
| 655 |
T* p2 = p->template alloc<200>(); // OK, < starts template argument list
|
| 656 |
T::adjust<100>(); // error: < means less than
|
| 657 |
T::template adjust<100>(); // OK, < starts template argument list
|
| 658 |
+
|
| 659 |
+
static constexpr std::meta::info r = ^^T::adjust;
|
| 660 |
+
T* p3 = [:r:]<200>(); // error: < means less than
|
| 661 |
+
T* p4 = template [:r:]<200>(); // OK, < starts template argument list
|
| 662 |
+
}}
|
| 663 |
```
|
| 664 |
|
| 665 |
— *end example*]
|
| 666 |
|
| 667 |
+
When parsing a *template-argument-list*, the first non-nested `>`[^1]
|
| 668 |
|
| 669 |
is taken as the ending delimiter rather than a greater-than operator.
|
| 670 |
Similarly, the first non-nested `>>` is treated as two consecutive but
|
| 671 |
distinct `>` tokens, the first of which is taken as the end of the
|
| 672 |
+
*template-argument-list* and completes the *template-id* or
|
| 673 |
+
*splice-specialization-specifier*.
|
| 674 |
|
| 675 |
[*Note 2*: The second `>` token produced by this replacement rule can
|
| 676 |
+
terminate an enclosing *template-id* or
|
| 677 |
+
*splice-specialization-specifier* construct or it can be part of a
|
| 678 |
different construct (e.g., a cast). — *end note*]
|
| 679 |
|
| 680 |
[*Example 2*:
|
| 681 |
|
| 682 |
``` cpp
|
|
|
|
| 694 |
— *end example*]
|
| 695 |
|
| 696 |
The keyword `template` shall not appear immediately after a declarative
|
| 697 |
*nested-name-specifier* [[expr.prim.id.qual]].
|
| 698 |
|
| 699 |
+
The *constant-expression* of a *template-argument* shall not be an
|
| 700 |
+
unparenthesized *splice-expression*.
|
| 701 |
+
|
| 702 |
+
[*Example 3*:
|
| 703 |
+
|
| 704 |
+
``` cpp
|
| 705 |
+
template<int> struct S { };
|
| 706 |
+
constexpr int k = 5;
|
| 707 |
+
constexpr std::meta::info r = ^^k;
|
| 708 |
+
S<[:r:]> s1; // error: unparenthesized splice-expression used as template argument
|
| 709 |
+
S<([:r:])> s2; // OK
|
| 710 |
+
S<[:r:] + 1> s3; // OK
|
| 711 |
+
```
|
| 712 |
+
|
| 713 |
+
— *end example*]
|
| 714 |
+
|
| 715 |
A name prefixed by the keyword `template` shall be followed by a
|
| 716 |
template argument list or refer to a class template or an alias
|
| 717 |
template. The latter case is deprecated [[depr.template.template]]. The
|
| 718 |
keyword `template` shall not appear immediately before a `~` token (as
|
| 719 |
to name a destructor).
|
| 720 |
|
| 721 |
[*Note 3*: The keyword `template` cannot be applied to non-template
|
| 722 |
members of class templates. — *end note*]
|
| 723 |
|
| 724 |
[*Note 4*: As is the case with the `typename` prefix, the `template`
|
| 725 |
+
prefix is well-formed even when lookup for the name would already find a
|
| 726 |
template. — *end note*]
|
| 727 |
|
| 728 |
+
[*Example 4*:
|
| 729 |
|
| 730 |
``` cpp
|
| 731 |
template <class T> struct A {
|
| 732 |
void f(int);
|
| 733 |
template <class U> void f(U);
|
|
|
|
| 748 |
D<B<int> > db;
|
| 749 |
```
|
| 750 |
|
| 751 |
— *end example*]
|
| 752 |
|
| 753 |
+
A *template-id* or *splice-specialization-specifier* is *valid* if
|
| 754 |
|
| 755 |
- there are at most as many arguments as there are parameters or a
|
| 756 |
parameter is a template parameter pack [[temp.variadic]],
|
| 757 |
- there is an argument for each non-deducible non-pack parameter that
|
| 758 |
does not have a default *template-argument*,
|
| 759 |
+
- each *template-argument* matches the corresponding template parameter
|
| 760 |
+
[[temp.arg]],
|
| 761 |
- substitution of each template argument into the following template
|
| 762 |
parameters (if any) succeeds, and
|
| 763 |
+
- if the *template-id* or *splice-specialization-specifier* is
|
| 764 |
+
non-dependent, the associated constraints are satisfied as specified
|
| 765 |
+
in the next paragraph.
|
| 766 |
|
| 767 |
+
A *simple-template-id* or *splice-specialization-specifier* shall be
|
| 768 |
+
valid unless its respective *template-name* or *splice-specifier* names
|
| 769 |
+
or designates a function template [[temp.deduct]].
|
| 770 |
|
| 771 |
+
[*Example 5*:
|
| 772 |
|
| 773 |
``` cpp
|
| 774 |
template<class T, T::type n = 0> class X;
|
| 775 |
struct S {
|
| 776 |
using type = int;
|
|
|
|
| 782 |
using T5 = X<S>; // OK
|
| 783 |
```
|
| 784 |
|
| 785 |
— *end example*]
|
| 786 |
|
| 787 |
+
When the *template-name* of a *simple-template-id* or the
|
| 788 |
+
*splice-specifier* of a *splice-specialization-specifier* designates a
|
| 789 |
+
constrained non-function template or a constrained template template
|
| 790 |
+
parameter, and all *template-argument*s in the *simple-template-id* or
|
| 791 |
+
*splice-specialization-specifier* are non-dependent [[temp.dep.temp]],
|
| 792 |
+
the associated constraints [[temp.constr.decl]] of the constrained
|
| 793 |
+
template shall be satisfied [[temp.constr.constr]].
|
| 794 |
|
| 795 |
+
[*Example 6*:
|
| 796 |
|
| 797 |
``` cpp
|
| 798 |
template<typename T> concept C1 = sizeof(T) != sizeof(int);
|
| 799 |
|
| 800 |
template<C1 T> struct S1 { };
|
|
|
|
| 836 |
[*Note 5*: Since a *constraint-expression* is an unevaluated operand, a
|
| 837 |
concept-id appearing in a *constraint-expression* is not evaluated
|
| 838 |
except as necessary to determine whether the normalized constraints are
|
| 839 |
satisfied. — *end note*]
|
| 840 |
|
| 841 |
+
[*Example 7*:
|
| 842 |
|
| 843 |
``` cpp
|
| 844 |
template<typename T> concept C = true;
|
| 845 |
static_assert(C<int>); // OK
|
| 846 |
```
|
|
|
|
| 849 |
|
| 850 |
## Template arguments <a id="temp.arg">[[temp.arg]]</a>
|
| 851 |
|
| 852 |
### General <a id="temp.arg.general">[[temp.arg.general]]</a>
|
| 853 |
|
| 854 |
+
The type and form of each *template-argument* specified in a
|
| 855 |
+
*template-id* or in a *splice-specialization-specifier* shall match the
|
| 856 |
+
type and form specified for the corresponding parameter declared by the
|
| 857 |
+
template in its *template-parameter-list*. When the parameter declared
|
| 858 |
+
by the template is a template parameter pack [[temp.variadic]], it will
|
| 859 |
+
correspond to zero or more *template-argument*s.
|
|
|
|
|
|
|
| 860 |
|
| 861 |
[*Example 1*:
|
| 862 |
|
| 863 |
``` cpp
|
| 864 |
template<class T> class Array {
|
|
|
|
| 890 |
argument is a pack expansion whose pattern is the name of the template
|
| 891 |
parameter pack.
|
| 892 |
|
| 893 |
In a *template-argument*, an ambiguity between a *type-id* and an
|
| 894 |
expression is resolved to a *type-id*, regardless of the form of the
|
| 895 |
+
corresponding *template-parameter*.[^2]
|
| 896 |
|
| 897 |
[*Example 2*:
|
| 898 |
|
| 899 |
``` cpp
|
| 900 |
template<class T> void f();
|
|
|
|
| 906 |
```
|
| 907 |
|
| 908 |
— *end example*]
|
| 909 |
|
| 910 |
[*Note 1*: Names used in a *template-argument* are subject to access
|
| 911 |
+
control where they appear. Because a template parameter is not a class
|
| 912 |
+
member, no access control applies where the template parameter is
|
| 913 |
+
used. — *end note*]
|
| 914 |
|
| 915 |
[*Example 3*:
|
| 916 |
|
| 917 |
``` cpp
|
| 918 |
template<class T> class X {
|
|
|
|
| 930 |
X<Y::S> y; // error: S not accessible
|
| 931 |
```
|
| 932 |
|
| 933 |
— *end example*]
|
| 934 |
|
| 935 |
+
For a template argument that is a class type or a class template, the
|
| 936 |
template definition has no special access rights to the members of the
|
| 937 |
+
template argument.
|
| 938 |
|
| 939 |
[*Example 4*:
|
| 940 |
|
| 941 |
``` cpp
|
| 942 |
template <template <class TT> class T> class A {
|
|
|
|
| 951 |
A<B> b; // error: A has no access to B::S
|
| 952 |
```
|
| 953 |
|
| 954 |
— *end example*]
|
| 955 |
|
| 956 |
+
When template argument packs or default template arguments are used, a
|
| 957 |
*template-argument* list can be empty. In that case the empty `<>`
|
| 958 |
brackets shall still be used as the *template-argument-list*.
|
| 959 |
|
| 960 |
[*Example 5*:
|
| 961 |
|
|
|
|
| 986 |
}
|
| 987 |
```
|
| 988 |
|
| 989 |
— *end example*]
|
| 990 |
|
| 991 |
+
If the use of a template argument gives rise to an ill-formed construct
|
| 992 |
+
in the instantiation of a template specialization, the program is
|
| 993 |
+
ill-formed.
|
| 994 |
|
| 995 |
When name lookup for the component name of a *template-id* finds an
|
| 996 |
overload set, both non-template functions in the overload set and
|
| 997 |
function templates in the overload set for which the
|
| 998 |
*template-argument*s do not match the *template-parameter*s are ignored.
|
| 999 |
|
| 1000 |
[*Note 2*: If none of the function templates have matching
|
| 1001 |
*template-parameter*s, the program is ill-formed. — *end note*]
|
| 1002 |
|
| 1003 |
+
When a *simple-template-id* or *splice-specialization-specifier* does
|
| 1004 |
+
not designate a function, a default *template-argument* is implicitly
|
| 1005 |
+
instantiated [[temp.inst]] when the value of that default argument is
|
| 1006 |
+
needed.
|
| 1007 |
|
| 1008 |
[*Example 7*:
|
| 1009 |
|
| 1010 |
``` cpp
|
| 1011 |
template<typename T, typename U = int> struct S { };
|
|
|
|
| 1018 |
— *end example*]
|
| 1019 |
|
| 1020 |
A *template-argument* followed by an ellipsis is a pack expansion
|
| 1021 |
[[temp.variadic]].
|
| 1022 |
|
| 1023 |
+
### Type template arguments <a id="temp.arg.type">[[temp.arg.type]]</a>
|
| 1024 |
|
| 1025 |
+
A *template-argument* for a type template parameter shall be a
|
| 1026 |
+
*type-id*.
|
| 1027 |
|
| 1028 |
[*Example 1*:
|
| 1029 |
|
| 1030 |
``` cpp
|
| 1031 |
template <class T> class X { };
|
|
|
|
| 1049 |
— *end example*]
|
| 1050 |
|
| 1051 |
[*Note 1*: A template type argument can be an incomplete type
|
| 1052 |
[[term.incomplete.type]]. — *end note*]
|
| 1053 |
|
| 1054 |
+
### Constant template arguments <a id="temp.arg.nontype">[[temp.arg.nontype]]</a>
|
| 1055 |
|
| 1056 |
+
A template argument E for a constant template parameter with declared
|
| 1057 |
+
type `T` shall be such that the invented declaration
|
|
|
|
|
|
|
| 1058 |
|
| 1059 |
``` cpp
|
| 1060 |
+
T x = E ;
|
| 1061 |
```
|
| 1062 |
|
| 1063 |
+
satisfies the semantic constraints for the definition of a `constexpr`
|
| 1064 |
+
variable with static storage duration [[dcl.constexpr]]. If `T` contains
|
| 1065 |
+
a placeholder type [[dcl.spec.auto]] or a placeholder for a deduced
|
| 1066 |
+
class type [[dcl.type.class.deduct]], the type of the parameter is
|
| 1067 |
+
deduced from the above declaration.
|
| 1068 |
|
| 1069 |
+
[*Note 1*: E is a *template-argument* or (for a default template
|
| 1070 |
+
argument) an *initializer-clause*. — *end note*]
|
|
|
|
| 1071 |
|
| 1072 |
+
If the parameter type thus deduced is not permitted for a constant
|
| 1073 |
+
template parameter [[temp.param]], the program is ill-formed.
|
|
|
|
| 1074 |
|
| 1075 |
+
The value of a constant template parameter P of (possibly deduced) type
|
| 1076 |
+
`T` is determined from its template argument A as follows. If `T` is not
|
| 1077 |
+
a class type and A is not a *braced-init-list*, A shall be a converted
|
| 1078 |
+
constant expression [[expr.const]] of type `T`; the value of P is A (as
|
| 1079 |
+
converted).
|
| 1080 |
+
|
| 1081 |
+
Otherwise, a temporary variable
|
| 1082 |
+
|
| 1083 |
+
``` cpp
|
| 1084 |
+
constexpr T v = A;
|
| 1085 |
+
```
|
| 1086 |
+
|
| 1087 |
+
is introduced. The lifetime of `v` ends immediately after initializing
|
| 1088 |
+
it and any template parameter object (see below). For each such
|
| 1089 |
+
variable, the *id-expression* `v` is termed a *candidate initializer*.
|
| 1090 |
+
|
| 1091 |
+
If `T` is a class type, a template parameter object [[temp.param]]
|
| 1092 |
+
exists that is constructed so as to be template-argument-equivalent to
|
| 1093 |
+
`v`; P denotes that template parameter object. P is copy-initialized
|
| 1094 |
+
from an unspecified candidate initializer that is
|
| 1095 |
+
template-argument-equivalent to `v`. If, for the initialization from any
|
| 1096 |
+
candidate initializer,
|
| 1097 |
+
|
| 1098 |
+
- the initialization would be ill-formed, or
|
| 1099 |
+
- the full-expression of an invented *init-declarator* for the
|
| 1100 |
+
initialization would not be a constant expression when interpreted as
|
| 1101 |
+
a *constant-expression* [[expr.const]], or
|
| 1102 |
+
- the initialization would cause P to not be
|
| 1103 |
+
template-argument-equivalent [[temp.type]] to `v`,
|
| 1104 |
+
|
| 1105 |
+
the program is ill-formed.
|
| 1106 |
+
|
| 1107 |
+
Otherwise, the value of P is that of v.
|
| 1108 |
+
|
| 1109 |
+
For a constant template parameter of reference or pointer type, or for
|
| 1110 |
+
each non-static data member of reference or pointer type in a constant
|
| 1111 |
+
template parameter of class type or subobject thereof, the reference or
|
| 1112 |
+
pointer value shall not refer or point to (respectively):
|
| 1113 |
|
| 1114 |
- a temporary object [[class.temporary]],
|
| 1115 |
- a string literal object [[lex.string]],
|
| 1116 |
- the result of a `typeid` expression [[expr.typeid]],
|
| 1117 |
- a predefined `__func__` variable [[dcl.fct.def.general]], or
|
| 1118 |
- a subobject [[intro.object]] of one of the above.
|
| 1119 |
|
| 1120 |
[*Example 1*:
|
| 1121 |
|
| 1122 |
+
``` cpp
|
| 1123 |
+
template <int& r> class A{};
|
| 1124 |
+
extern int x;
|
| 1125 |
+
A<x> a; // OK
|
| 1126 |
+
void f(int p) {
|
| 1127 |
+
constexpr int& r = p; // OK
|
| 1128 |
+
A<r> a; // error: a static constexpr int& variable cannot be initialized to refer to p here
|
| 1129 |
+
}
|
| 1130 |
+
```
|
| 1131 |
+
|
| 1132 |
+
— *end example*]
|
| 1133 |
+
|
| 1134 |
+
[*Example 2*:
|
| 1135 |
+
|
| 1136 |
``` cpp
|
| 1137 |
template<const int* pci> struct X { ... };
|
| 1138 |
int ai[10];
|
| 1139 |
X<ai> xi; // array to pointer and qualification conversions
|
| 1140 |
|
|
|
|
| 1157 |
template<auto n> struct B { ... };
|
| 1158 |
B<5> b1; // OK, template parameter type is int
|
| 1159 |
B<'a'> b2; // OK, template parameter type is char
|
| 1160 |
B<2.5> b3; // OK, template parameter type is double
|
| 1161 |
B<void(0)> b4; // error: template parameter type cannot be void
|
| 1162 |
+
|
| 1163 |
+
template<int i> struct C { /* ... */ };
|
| 1164 |
+
C<{ 42 }> c1; // OK
|
| 1165 |
+
|
| 1166 |
+
struct J1 {
|
| 1167 |
+
J1 *self = this;
|
| 1168 |
+
};
|
| 1169 |
+
B<J1{}> j1; // error: initialization of template parameter object is not a constant expression
|
| 1170 |
+
|
| 1171 |
+
struct J2 {
|
| 1172 |
+
J2 *self = this;
|
| 1173 |
+
constexpr J2() {}
|
| 1174 |
+
constexpr J2(const J2&) {}
|
| 1175 |
+
};
|
| 1176 |
+
B<J2{}> j2; // error: template parameter object not template-argument-equivalent to introduced temporary
|
| 1177 |
```
|
| 1178 |
|
| 1179 |
— *end example*]
|
| 1180 |
|
| 1181 |
[*Note 2*:
|
| 1182 |
|
| 1183 |
A *string-literal* [[lex.string]] is not an acceptable
|
| 1184 |
+
*template-argument* for a constant template parameter of non-class type.
|
| 1185 |
|
| 1186 |
+
[*Example 3*:
|
| 1187 |
|
| 1188 |
``` cpp
|
| 1189 |
template<class T, T p> class X {
|
| 1190 |
...
|
| 1191 |
};
|
|
|
|
| 1208 |
— *end note*]
|
| 1209 |
|
| 1210 |
[*Note 3*:
|
| 1211 |
|
| 1212 |
A temporary object is not an acceptable *template-argument* when the
|
| 1213 |
+
corresponding template parameter has reference type.
|
| 1214 |
|
| 1215 |
+
[*Example 4*:
|
| 1216 |
|
| 1217 |
``` cpp
|
| 1218 |
template<const int& CRI> struct B { ... };
|
| 1219 |
|
| 1220 |
B<1> b1; // error: temporary would be required for template argument
|
|
|
|
| 1233 |
|
| 1234 |
— *end note*]
|
| 1235 |
|
| 1236 |
### Template template arguments <a id="temp.arg.template">[[temp.arg.template]]</a>
|
| 1237 |
|
| 1238 |
+
A *template-argument* for a template template parameter shall be the
|
| 1239 |
+
name of a template. For a *type-tt-parameter*, the name shall denote a
|
| 1240 |
+
class template or alias template. For a *variable-tt-parameter*, the
|
| 1241 |
+
name shall denote a variable template. For a *concept-tt-parameter*, the
|
| 1242 |
+
name shall denote a concept. Only primary templates are considered when
|
| 1243 |
+
matching the template template argument with the corresponding
|
| 1244 |
+
parameter; partial specializations are not considered even if their
|
| 1245 |
+
parameter lists match that of the template template parameter.
|
| 1246 |
|
| 1247 |
Any partial specializations [[temp.spec.partial]] associated with the
|
| 1248 |
primary template are considered when a specialization based on the
|
| 1249 |
+
template template parameter is instantiated. If a specialization is not
|
| 1250 |
+
reachable from the point of instantiation, and it would have been
|
| 1251 |
selected had it been reachable, the program is ill-formed, no diagnostic
|
| 1252 |
required.
|
| 1253 |
|
| 1254 |
[*Example 1*:
|
| 1255 |
|
|
|
|
| 1268 |
// V<int*> within C<A> uses the partial specialization, so c.z.x has type long
|
| 1269 |
```
|
| 1270 |
|
| 1271 |
— *end example*]
|
| 1272 |
|
| 1273 |
+
A template template parameter `P` and a *template-argument* `A` are
|
| 1274 |
+
*compatible* if
|
| 1275 |
+
|
| 1276 |
+
- `A` denotes a class template or an alias template and `P` is a type
|
| 1277 |
+
template template parameter,
|
| 1278 |
+
- `A` denotes a variable template and `P` is a variable template
|
| 1279 |
+
template parameter, or
|
| 1280 |
+
- `A` denotes a concept and `P` is a concept template parameter.
|
| 1281 |
+
|
| 1282 |
+
A template *template-argument* `A` matches a template template parameter
|
| 1283 |
+
`P` when `A` and `P` are compatible and `P` is at least as specialized
|
| 1284 |
+
as `A`, ignoring constraints on `A` if `P` is unconstrained. If `P`
|
| 1285 |
+
contains a template parameter pack, then `A` also matches `P` if each of
|
| 1286 |
+
`A`’s template parameters matches the corresponding template parameter
|
| 1287 |
+
declared in the *template-head* of `P`. Two template parameters match if
|
| 1288 |
+
they are of the same kind, for constant template parameters, their types
|
| 1289 |
+
are equivalent [[temp.over.link]], and for template template parameters,
|
| 1290 |
+
each of their corresponding template parameters matches, recursively.
|
| 1291 |
+
When `P`’s *template-head* contains a *template-parameter* that declares
|
| 1292 |
+
a template parameter pack [[temp.variadic]], the template parameter pack
|
| 1293 |
+
will match zero or more template parameters or template parameter packs
|
| 1294 |
+
declared in the *template-head* of `A` with the same type and form as
|
| 1295 |
+
the template parameter pack declared in `P` (ignoring whether those
|
| 1296 |
+
template parameters are template parameter packs).
|
| 1297 |
|
| 1298 |
[*Example 2*:
|
| 1299 |
|
| 1300 |
``` cpp
|
| 1301 |
template<class T> class A { ... };
|
|
|
|
| 1357 |
S<Z> s3; // OK, P is at least as specialized as Z
|
| 1358 |
```
|
| 1359 |
|
| 1360 |
— *end example*]
|
| 1361 |
|
| 1362 |
+
A template template parameter `P` is at least as specialized as a
|
| 1363 |
template *template-argument* `A` if, given the following rewrite to two
|
| 1364 |
function templates, the function template corresponding to `P` is at
|
| 1365 |
least as specialized as the function template corresponding to `A`
|
| 1366 |
according to the partial ordering rules for function templates
|
| 1367 |
[[temp.func.order]]. Given an invented class template `X` with the
|
|
|
|
| 1371 |
- Each of the two function templates has the same template parameters
|
| 1372 |
and *requires-clause* (if any), respectively, as `P` or `A`.
|
| 1373 |
- Each function template has a single function parameter whose type is a
|
| 1374 |
specialization of `X` with template arguments corresponding to the
|
| 1375 |
template parameters from the respective function template where, for
|
| 1376 |
+
each *template-parameter* `PP` in the *template-head* of the function
|
| 1377 |
+
template, a corresponding *template-argument* `AA` is formed. If `PP`
|
| 1378 |
declares a template parameter pack, then `AA` is the pack expansion
|
| 1379 |
+
`PP...` [[temp.variadic]]; otherwise, `AA` is an *id-expression*
|
| 1380 |
+
denoting `PP`.
|
| 1381 |
|
| 1382 |
If the rewrite produces an invalid type, then `P` is not at least as
|
| 1383 |
specialized as `A`.
|
| 1384 |
|
| 1385 |
## Template constraints <a id="temp.constr">[[temp.constr]]</a>
|
|
|
|
| 1396 |
|
| 1397 |
#### General <a id="temp.constr.constr.general">[[temp.constr.constr.general]]</a>
|
| 1398 |
|
| 1399 |
A *constraint* is a sequence of logical operations and operands that
|
| 1400 |
specifies requirements on template arguments. The operands of a logical
|
| 1401 |
+
operation are constraints. There are five different kinds of
|
| 1402 |
constraints:
|
| 1403 |
|
| 1404 |
- conjunctions [[temp.constr.op]],
|
| 1405 |
+
- disjunctions [[temp.constr.op]],
|
| 1406 |
+
- atomic constraints [[temp.constr.atomic]],
|
| 1407 |
+
- concept-dependent constraints [[temp.constr.concept]], and
|
| 1408 |
+
- fold expanded constraints [[temp.constr.fold]].
|
| 1409 |
|
| 1410 |
In order for a constrained template to be instantiated [[temp.spec]],
|
| 1411 |
its associated constraints [[temp.constr.decl]] shall be satisfied as
|
| 1412 |
described in the following subclauses.
|
| 1413 |
|
|
|
|
| 1586 |
|
| 1587 |
— *end note*]
|
| 1588 |
|
| 1589 |
To determine if an atomic constraint is *satisfied*, the parameter
|
| 1590 |
mapping and template arguments are first substituted into its
|
| 1591 |
+
expression. If substitution results in an invalid type or expression in
|
| 1592 |
+
the immediate context of the atomic constraint [[temp.deduct.general]],
|
| 1593 |
the constraint is not satisfied. Otherwise, the lvalue-to-rvalue
|
| 1594 |
conversion [[conv.lval]] is performed if necessary, and `E` shall be a
|
| 1595 |
constant expression of type `bool`. The constraint is satisfied if and
|
| 1596 |
only if evaluation of `E` results in `true`. If, at different points in
|
| 1597 |
the program, the satisfaction result is different for identical atomic
|
|
|
|
| 1618 |
// call is ill-formed even though #2 is a better match
|
| 1619 |
```
|
| 1620 |
|
| 1621 |
— *end example*]
|
| 1622 |
|
| 1623 |
+
#### Concept-dependent constraints <a id="temp.constr.concept">[[temp.constr.concept]]</a>
|
| 1624 |
+
|
| 1625 |
+
A *concept-dependent constraint* `CD` is an atomic constraint whose
|
| 1626 |
+
expression is a concept-id `CI` whose *concept-name* names a dependent
|
| 1627 |
+
concept named `C`.
|
| 1628 |
+
|
| 1629 |
+
To determine if `CD` is *satisfied*, the parameter mapping and template
|
| 1630 |
+
arguments are first substituted into `C`. If substitution results in an
|
| 1631 |
+
invalid concept-id in the immediate context of the constraint
|
| 1632 |
+
[[temp.deduct.general]], the constraint is not satisfied. Otherwise, let
|
| 1633 |
+
`CI'` be the normal form [[temp.constr.normal]] of the concept-id after
|
| 1634 |
+
substitution of `C`.
|
| 1635 |
+
|
| 1636 |
+
[*Note 1*: Normalization of `CI` might be ill-formed; no diagnostic is
|
| 1637 |
+
required. — *end note*]
|
| 1638 |
+
|
| 1639 |
+
To form `CI''`, each appearance of `C`'s template parameters in the
|
| 1640 |
+
parameter mappings of the atomic constraints (including
|
| 1641 |
+
concept-dependent constraints) in `CI'` is substituted with their
|
| 1642 |
+
respective arguments from the parameter mapping of `CD` and the
|
| 1643 |
+
arguments of `CI`.
|
| 1644 |
+
|
| 1645 |
+
`CD` is satisfied if `CI''` is satisfied.
|
| 1646 |
+
|
| 1647 |
+
[*Note 2*: Checking whether `CI''` is satisfied can lead to further
|
| 1648 |
+
normalization of concept-dependent constraints. — *end note*]
|
| 1649 |
+
|
| 1650 |
+
[*Example 1*:
|
| 1651 |
+
|
| 1652 |
+
``` cpp
|
| 1653 |
+
template<typename>
|
| 1654 |
+
concept C = true;
|
| 1655 |
+
|
| 1656 |
+
template<typename T, template<typename> concept CC>
|
| 1657 |
+
concept D = CC<T>;
|
| 1658 |
+
|
| 1659 |
+
template<typename U,
|
| 1660 |
+
template<typename> concept CT,
|
| 1661 |
+
template<typename, template<typename> concept> concept CU>
|
| 1662 |
+
int f() requires CU<U, CT>;
|
| 1663 |
+
int i = f<int, C, D>();
|
| 1664 |
+
```
|
| 1665 |
+
|
| 1666 |
+
In this example, the associated constraints of `f` consist of a
|
| 1667 |
+
concept-dependent constraint whose expression is the concept-id
|
| 1668 |
+
`CU<U, CT>` with the mapping `U` ↦ `U`, `CT` ↦ `CT`, `CU` ↦ `CU`. The
|
| 1669 |
+
result of substituting `D` into this expression is `D<U, CT>`. We
|
| 1670 |
+
consider the normal form of the resulting concept-id, which is `CC<T>`
|
| 1671 |
+
with the mapping `T` ↦ `U`, `CC` ↦ `CT`. By recursion, `C` is
|
| 1672 |
+
substituted into `CC<T>`, and the result is normalized to the atomic
|
| 1673 |
+
constraint `true`, which is satisfied.
|
| 1674 |
+
|
| 1675 |
+
— *end example*]
|
| 1676 |
+
|
| 1677 |
+
#### Fold expanded constraint <a id="temp.constr.fold">[[temp.constr.fold]]</a>
|
| 1678 |
+
|
| 1679 |
+
A *fold expanded constraint* is formed from a constraint C and a
|
| 1680 |
+
*fold-operator* which can either be `&&` or `||`. A fold expanded
|
| 1681 |
+
constraint is a pack expansion [[temp.variadic]]. Let N be the number of
|
| 1682 |
+
elements in the pack expansion parameters [[temp.variadic]].
|
| 1683 |
+
|
| 1684 |
+
A fold expanded constraint whose *fold-operator* is `&&` is satisfied if
|
| 1685 |
+
it is a valid pack expansion and if N = 0 or if for each i where
|
| 1686 |
+
0 ≤ i < N in increasing order, C is satisfied when replacing each pack
|
| 1687 |
+
expansion parameter with the corresponding iᵗʰ element. No substitution
|
| 1688 |
+
takes place for any i greater than the smallest i for which the
|
| 1689 |
+
constraint is not satisfied.
|
| 1690 |
+
|
| 1691 |
+
A fold expanded constraint whose *fold-operator* is `||` is satisfied if
|
| 1692 |
+
it is a valid pack expansion, N > 0, and if for i where 0 ≤ i < N in
|
| 1693 |
+
increasing order, there is a smallest i for which C is satisfied when
|
| 1694 |
+
replacing each pack expansion parameter with the corresponding iᵗʰ
|
| 1695 |
+
element. No substitution takes place for any i greater than the smallest
|
| 1696 |
+
i for which the constraint is satisfied.
|
| 1697 |
+
|
| 1698 |
+
[*Note 1*: If the pack expansion expands packs of different size, then
|
| 1699 |
+
it is invalid and the fold expanded constraint is not
|
| 1700 |
+
satisfied. — *end note*]
|
| 1701 |
+
|
| 1702 |
+
Two fold expanded constraints are *compatible for subsumption* if their
|
| 1703 |
+
respective constraints both contain an equivalent unexpanded pack
|
| 1704 |
+
[[temp.over.link]].
|
| 1705 |
+
|
| 1706 |
### Constrained declarations <a id="temp.constr.decl">[[temp.constr.decl]]</a>
|
| 1707 |
|
| 1708 |
A template declaration [[temp.pre]] or templated function declaration
|
| 1709 |
[[dcl.fct]] can be constrained by the use of a *requires-clause*. This
|
| 1710 |
allows the specification of constraints for that declaration as an
|
|
|
|
| 1815 |
- The normal form of an expression `( E )` is the normal form of `E`.
|
| 1816 |
- The normal form of an expression `E1 || E2` is the disjunction
|
| 1817 |
[[temp.constr.op]] of the normal forms of `E1` and `E2`.
|
| 1818 |
- The normal form of an expression `E1 && E2` is the conjunction of the
|
| 1819 |
normal forms of `E1` and `E2`.
|
| 1820 |
+
- For a concept-id `C<A₁, A₂, …, Aₙ>` termed `CI`:
|
| 1821 |
+
- If `C` names a dependent concept, the normal form of `CI` is a
|
| 1822 |
+
concept-dependent constraint whose concept-id is `CI` and whose
|
| 1823 |
+
parameter mapping is the identity mapping.
|
| 1824 |
+
- Otherwise, to form `CE`, any non-dependent concept template argument
|
| 1825 |
+
`Aᵢ` is substituted into the *constraint-expression* of `C`. If any
|
| 1826 |
+
such substitution results in an invalid concept-id, the program is
|
| 1827 |
+
ill-formed; no diagnostic is required. The normal form of `CI` is
|
| 1828 |
+
the result of substituting, in the normal form `N` of `CE`,
|
| 1829 |
+
appearances of `C`'s template parameters in the parameter mappings
|
| 1830 |
+
of the atomic constraints in `N` with their respective arguments
|
| 1831 |
+
from `C`. If any such substitution results in an invalid type or
|
| 1832 |
+
expression, the program is ill-formed; no diagnostic is required.
|
| 1833 |
+
|
| 1834 |
\[*Example 1*:
|
| 1835 |
``` cpp
|
| 1836 |
template<typename T> concept A = T::value || true;
|
| 1837 |
template<typename U> concept B = A<U*>;
|
| 1838 |
template<typename V> concept C = B<V&>;
|
|
|
|
| 1843 |
mapping), despite the expression `T::value` being ill-formed for a
|
| 1844 |
pointer type `T`. Normalization of `C`'s *constraint-expression*
|
| 1845 |
results in the program being ill-formed, because it would form the
|
| 1846 |
invalid type `V&*` in the parameter mapping.
|
| 1847 |
— *end example*]
|
| 1848 |
+
- For a *fold-operator* `Op` [[expr.prim.fold]] that is either `&&` or
|
| 1849 |
+
`||`:
|
| 1850 |
+
- The normal form of an expression `( ... Op E )` is the normal form
|
| 1851 |
+
of `( E Op ... )`.
|
| 1852 |
+
- The normal form of an expression `( E1 Op ... Op E2 )` is the normal
|
| 1853 |
+
form of
|
| 1854 |
+
- `( E1 Op ... ) Op E2` if `E1` contains an unexpanded pack, or
|
| 1855 |
+
- `E1 Op ( E2 Op ... )` otherwise.
|
| 1856 |
+
- The normal form of an expression `F` of the form `( E Op ... )` is
|
| 1857 |
+
as follows:
|
| 1858 |
+
If `E` contains an unexpanded concept template parameter pack, it
|
| 1859 |
+
shall not contain an unexpanded template parameter pack of another
|
| 1860 |
+
kind. Let `E'` be the normal form of `E`.
|
| 1861 |
+
- If `E` contains an unexpanded concept template parameter pack `Pₖ`
|
| 1862 |
+
that has corresponding template arguments in the parameter mapping
|
| 1863 |
+
of any atomic constraint (including concept-dependent constraints)
|
| 1864 |
+
of `E'`, the number of arguments specified for all such `Pₖ` shall
|
| 1865 |
+
be the same number N. The normal form of `F` is the normal form of
|
| 1866 |
+
`E₀ Op … Op E_{N-1}` after substituting in `Eᵢ` the respective iᵗʰ
|
| 1867 |
+
concept argument of each `Pₖ`. If any such substitution results in
|
| 1868 |
+
an invalid type or expression, the program is ill-formed; no
|
| 1869 |
+
diagnostic is required.
|
| 1870 |
+
- Otherwise, the normal form of `F` is a fold expanded constraint
|
| 1871 |
+
[[temp.constr.fold]] whose constraint is `E'` and whose
|
| 1872 |
+
*fold-operator* is `Op`.
|
| 1873 |
- The normal form of any other expression `E` is the atomic constraint
|
| 1874 |
whose expression is `E` and whose parameter mapping is the identity
|
| 1875 |
mapping.
|
| 1876 |
|
| 1877 |
The process of obtaining the normal form of a *constraint-expression* is
|
|
|
|
| 1902 |
The associated constraints of \#3 are `requires (T x) { ++x; }` (with
|
| 1903 |
mapping `T` ↦ `U`).
|
| 1904 |
|
| 1905 |
— *end example*]
|
| 1906 |
|
| 1907 |
+
[*Example 3*:
|
| 1908 |
+
|
| 1909 |
+
``` cpp
|
| 1910 |
+
template<typename T>
|
| 1911 |
+
concept C = true;
|
| 1912 |
+
template<typename T, template<typename> concept CT>
|
| 1913 |
+
concept CC = CT<T>;
|
| 1914 |
+
|
| 1915 |
+
template<typename U,
|
| 1916 |
+
template<typename, template<typename> concept> concept CT>
|
| 1917 |
+
void f() requires CT<U*, C>;
|
| 1918 |
+
template<typename U>
|
| 1919 |
+
void g() requires CC<U*, C>;
|
| 1920 |
+
```
|
| 1921 |
+
|
| 1922 |
+
The normal form of the associated constraints of `f` is the
|
| 1923 |
+
concept-dependent constraint `CT<T, C>`.
|
| 1924 |
+
The normal form of the associated constraints of `g` is the atomic
|
| 1925 |
+
constraint `true`.
|
| 1926 |
+
|
| 1927 |
+
— *end example*]
|
| 1928 |
+
|
| 1929 |
+
[*Example 4*:
|
| 1930 |
+
|
| 1931 |
+
``` cpp
|
| 1932 |
+
template<typename T>
|
| 1933 |
+
concept A = true;
|
| 1934 |
+
template<typename T>
|
| 1935 |
+
concept B = A<T> && true; // B subsumes A
|
| 1936 |
+
template<typename T>
|
| 1937 |
+
concept C = true;
|
| 1938 |
+
template<typename T>
|
| 1939 |
+
concept D = C<T> && true; // D subsumes C
|
| 1940 |
+
|
| 1941 |
+
template<typename T, template<typename> concept... CTs>
|
| 1942 |
+
concept all_of = (CTs<T> && ...);
|
| 1943 |
+
|
| 1944 |
+
template<typename T> requires all_of<T, A, C>
|
| 1945 |
+
constexpr int f(T) { return 1; } // #1
|
| 1946 |
+
template<typename T> requires all_of<T, B, D>
|
| 1947 |
+
constexpr int f(T) { return 2; } // #2
|
| 1948 |
+
|
| 1949 |
+
static_assert(f(1) == 2); // ok
|
| 1950 |
+
```
|
| 1951 |
+
|
| 1952 |
+
The normal form of `all_of<T, A, C>` is the conjunction of the normal
|
| 1953 |
+
forms of `A<T>` and `C<T>`.
|
| 1954 |
+
Similarly, the normal form of `all_of<T, B, D>` is the conjunction of
|
| 1955 |
+
the normal forms of `B<T>` and `D<T>`.
|
| 1956 |
+
\#2 therefore is more constrained than \#1.
|
| 1957 |
+
|
| 1958 |
+
— *end example*]
|
| 1959 |
+
|
| 1960 |
+
[*Example 5*:
|
| 1961 |
+
|
| 1962 |
+
``` cpp
|
| 1963 |
+
template<typename T, template<typename> concept>
|
| 1964 |
+
struct wrapper {};
|
| 1965 |
+
|
| 1966 |
+
template<typename... T, template<typename> concept... CTs>
|
| 1967 |
+
int f(wrapper<T, CTs>...) requires (CTs<T> && ...); // error: fold expression contains
|
| 1968 |
+
// different kinds of template parameters
|
| 1969 |
+
```
|
| 1970 |
+
|
| 1971 |
+
— *end example*]
|
| 1972 |
+
|
| 1973 |
### Partial ordering by constraints <a id="temp.constr.order">[[temp.constr.order]]</a>
|
| 1974 |
|
| 1975 |
A constraint P *subsumes* a constraint Q if and only if, for every
|
| 1976 |
+
disjunctive clause Pᵢ in the disjunctive normal form[^3]
|
| 1977 |
|
| 1978 |
of P, Pᵢ subsumes every conjunctive clause Qⱼ in the conjunctive normal
|
| 1979 |
+
form[^4]
|
| 1980 |
|
| 1981 |
of Q, where
|
| 1982 |
|
| 1983 |
- a disjunctive clause Pᵢ subsumes a conjunctive clause Qⱼ if and only
|
| 1984 |
if there exists an atomic constraint Pᵢₐ in Pᵢ for which there exists
|
| 1985 |
an atomic constraint $Q_{jb}$ in Qⱼ such that Pᵢₐ subsumes $Q_{jb}$,
|
|
|
|
| 1986 |
- an atomic constraint A subsumes another atomic constraint B if and
|
| 1987 |
only if A and B are identical using the rules described in
|
| 1988 |
+
[[temp.constr.atomic]], and
|
| 1989 |
+
- a fold expanded constraint A subsumes another fold expanded constraint
|
| 1990 |
+
B if they are compatible for subsumption, have the same
|
| 1991 |
+
*fold-operator*, and the constraint of A subsumes that of B.
|
| 1992 |
|
| 1993 |
[*Example 1*: Let A and B be atomic constraints [[temp.constr.atomic]].
|
| 1994 |
The constraint A ∧ B subsumes A, but A does not subsume A ∧ B. The
|
| 1995 |
constraint A subsumes A ∨ B, but A ∨ B does not subsume A. Also note
|
| 1996 |
that every constraint subsumes itself. — *end example*]
|
|
|
|
| 2008 |
[[temp.spec.partial.order]], and
|
| 2009 |
- the partial ordering of function templates [[temp.func.order]].
|
| 2010 |
|
| 2011 |
— *end note*]
|
| 2012 |
|
| 2013 |
+
The associated constraints `C` of a declaration `D` are *eligible for
|
| 2014 |
+
subsumption* unless `C` contains a concept-dependent constraint.
|
| 2015 |
+
|
| 2016 |
A declaration `D1` is *at least as constrained* as a declaration `D2` if
|
| 2017 |
|
| 2018 |
- `D1` and `D2` are both constrained declarations and `D1`’s associated
|
| 2019 |
+
constraints are eligible for subsumption and subsume those of `D2`; or
|
| 2020 |
- `D2` has no associated constraints.
|
| 2021 |
|
| 2022 |
A declaration `D1` is *more constrained* than another declaration `D2`
|
| 2023 |
when `D1` is at least as constrained as `D2`, and `D2` is not at least
|
| 2024 |
as constrained as `D1`.
|
|
|
|
| 2040 |
g(0); // selects #4
|
| 2041 |
```
|
| 2042 |
|
| 2043 |
— *end example*]
|
| 2044 |
|
| 2045 |
+
[*Example 3*:
|
| 2046 |
+
|
| 2047 |
+
``` cpp
|
| 2048 |
+
template<template<typename T> concept CT, typename T>
|
| 2049 |
+
struct S {};
|
| 2050 |
+
template<typename T>
|
| 2051 |
+
concept A = true;
|
| 2052 |
+
|
| 2053 |
+
template<template<typename T> concept X, typename T>
|
| 2054 |
+
int f(S<X, T>) requires A<T> { return 42; } // #1
|
| 2055 |
+
template<template<typename T> concept X, typename T>
|
| 2056 |
+
int f(S<X, T>) requires X<T> { return 43; } // #2
|
| 2057 |
+
|
| 2058 |
+
f(S<A, int>{}); // ok, select #1 because #2 is not eligible for subsumption
|
| 2059 |
+
```
|
| 2060 |
+
|
| 2061 |
+
— *end example*]
|
| 2062 |
+
|
| 2063 |
+
A non-template function `F1` is *more partial-ordering-constrained* than
|
| 2064 |
+
a non-template function `F2` if
|
| 2065 |
+
|
| 2066 |
+
- they have the same non-object-parameter-type-lists [[dcl.fct]], and
|
| 2067 |
+
- if they are member functions, both are direct members of the same
|
| 2068 |
+
class, and
|
| 2069 |
+
- if both are non-static member functions, they have the same types for
|
| 2070 |
+
their object parameters, and
|
| 2071 |
+
- the declaration of `F1` is more constrained than the declaration of
|
| 2072 |
+
`F2`.
|
| 2073 |
+
|
| 2074 |
## Type equivalence <a id="temp.type">[[temp.type]]</a>
|
| 2075 |
|
| 2076 |
Two *template-id*s are the same if
|
| 2077 |
|
| 2078 |
- their *template-name*s, *operator-function-id*s, or
|
| 2079 |
*literal-operator-id*s refer to the same template, and
|
| 2080 |
- their corresponding type *template-argument*s are the same type, and
|
| 2081 |
+
- the template parameter values determined by their corresponding
|
| 2082 |
+
constant template arguments [[temp.arg.nontype]] are
|
| 2083 |
+
template-argument-equivalent (see below), and
|
| 2084 |
- their corresponding template *template-argument*s refer to the same
|
| 2085 |
template.
|
| 2086 |
|
| 2087 |
Two *template-id*s that are the same refer to the same class, function,
|
| 2088 |
or variable.
|
|
|
|
| 2091 |
type and
|
| 2092 |
|
| 2093 |
- they are of integral type and their values are the same, or
|
| 2094 |
- they are of floating-point type and their values are identical, or
|
| 2095 |
- they are of type `std::nullptr_t`, or
|
| 2096 |
+
- they are of type `std::meta::info` and their values compare equal
|
| 2097 |
+
[[expr.eq]], or
|
| 2098 |
+
- they are of enumeration type and their values are the same,[^5] or
|
| 2099 |
- they are of pointer type and they have the same pointer value, or
|
| 2100 |
- they are of pointer-to-member type and they refer to the same class
|
| 2101 |
member or are both the null member pointer value, or
|
| 2102 |
- they are of reference type and they refer to the same object or
|
| 2103 |
function, or
|
| 2104 |
- they are of array type and their corresponding elements are
|
| 2105 |
+
template-argument-equivalent,[^6] or
|
| 2106 |
- they are of union type and either they both have no active member or
|
| 2107 |
they have the same active member and their active members are
|
| 2108 |
template-argument-equivalent, or
|
| 2109 |
+
- they are of a closure type [[expr.prim.lambda.closure]], or
|
| 2110 |
- they are of class type and their corresponding direct subobjects and
|
| 2111 |
reference members are template-argument-equivalent.
|
| 2112 |
|
| 2113 |
[*Example 1*:
|
| 2114 |
|
|
|
|
| 2149 |
[[temp.over.link]].
|
| 2150 |
|
| 2151 |
[*Note 1*: However, such a type might be aliased, e.g., by a
|
| 2152 |
*typedef-name*. — *end note*]
|
| 2153 |
|
| 2154 |
+
For a type template parameter pack `T`, `T...[`*constant-expression*`]`
|
| 2155 |
+
denotes a unique dependent type.
|
| 2156 |
+
|
| 2157 |
+
If the *constant-expression* of a *pack-index-specifier* is
|
| 2158 |
+
value-dependent, two such *pack-index-specifier*s refer to the same type
|
| 2159 |
+
only if their *constant-expression*s are equivalent [[temp.over.link]].
|
| 2160 |
+
Otherwise, two such *pack-index-specifier*s refer to the same type only
|
| 2161 |
+
if their indexes have the same value.
|
| 2162 |
+
|
| 2163 |
## Template declarations <a id="temp.decls">[[temp.decls]]</a>
|
| 2164 |
|
| 2165 |
### General <a id="temp.decls.general">[[temp.decls.general]]</a>
|
| 2166 |
|
| 2167 |
The template parameters of a template are specified in the angle bracket
|
|
|
|
| 2182 |
*type-constraint*, *requires-clause*, or *noexcept-specifier* is a
|
| 2183 |
separate definition which is unrelated to the templated function
|
| 2184 |
definition or to any other default arguments, *type-constraint*s,
|
| 2185 |
*requires-clause*s, or *noexcept-specifier*s. For the purpose of
|
| 2186 |
instantiation, the substatements of a constexpr if statement [[stmt.if]]
|
| 2187 |
+
are considered definitions. For the purpose of name lookup and
|
| 2188 |
+
instantiation, the *compound-statement* of an *expansion-statement* is
|
| 2189 |
+
considered a template definition.
|
| 2190 |
|
| 2191 |
Because an *alias-declaration* cannot declare a *template-id*, it is not
|
| 2192 |
possible to partially or explicitly specialize an alias template.
|
| 2193 |
|
| 2194 |
### Class templates <a id="temp.class">[[temp.class]]</a>
|
|
|
|
| 2352 |
|
| 2353 |
— *end example*]
|
| 2354 |
|
| 2355 |
#### Deduction guides <a id="temp.deduct.guide">[[temp.deduct.guide]]</a>
|
| 2356 |
|
| 2357 |
+
Deduction guides are used when a *template-name* or
|
| 2358 |
+
*splice-type-specifier* appears as a type specifier for a deduced class
|
| 2359 |
+
type [[dcl.type.class.deduct]]. Deduction guides are not found by name
|
| 2360 |
+
lookup. Instead, when performing class template argument deduction
|
| 2361 |
+
[[over.match.class.deduct]], all reachable deduction guides declared for
|
| 2362 |
+
the class template are considered.
|
| 2363 |
|
| 2364 |
``` bnf
|
| 2365 |
deduction-guide:
|
| 2366 |
+
explicit-specifierₒₚₜ template-name '(' parameter-declaration-clause ')' '->' simple-template-id requires-clauseₒₚₜ ';'
|
| 2367 |
```
|
| 2368 |
|
| 2369 |
[*Example 1*:
|
| 2370 |
|
| 2371 |
``` cpp
|
|
|
|
| 2384 |
```
|
| 2385 |
|
| 2386 |
— *end example*]
|
| 2387 |
|
| 2388 |
The same restrictions apply to the *parameter-declaration-clause* of a
|
| 2389 |
+
deduction guide as in a function declaration [[dcl.fct]], except that a
|
| 2390 |
+
generic parameter type placeholder [[dcl.spec.auto]] shall not appear in
|
| 2391 |
+
the *parameter-declaration-clause* of a deduction guide. The
|
| 2392 |
*simple-template-id* shall name a class template specialization. The
|
| 2393 |
*template-name* shall be the same *identifier* as the *template-name* of
|
| 2394 |
the *simple-template-id*. A *deduction-guide* shall inhabit the scope to
|
| 2395 |
which the corresponding class template belongs and, for a member class
|
| 2396 |
template, have the same access. Two deduction guide declarations for the
|
|
|
|
| 2467 |
|
| 2468 |
``` cpp
|
| 2469 |
template<class T> struct A {
|
| 2470 |
enum E : T;
|
| 2471 |
};
|
|
|
|
| 2472 |
template<class T> enum A<T>::E : T { e1, e2 };
|
| 2473 |
A<int>::E e = A<int>::e1;
|
| 2474 |
```
|
| 2475 |
|
| 2476 |
— *end example*]
|
|
|
|
| 2579 |
|
| 2580 |
— *end example*]
|
| 2581 |
|
| 2582 |
[*Note 1*:
|
| 2583 |
|
| 2584 |
+
A specialization of a conversion function template is named in the same
|
| 2585 |
+
way as a non-template conversion function that converts to the same type
|
| 2586 |
+
[[class.conv.fct]].
|
| 2587 |
|
| 2588 |
[*Example 6*:
|
| 2589 |
|
| 2590 |
``` cpp
|
| 2591 |
struct A {
|
|
|
|
| 2602 |
}
|
| 2603 |
```
|
| 2604 |
|
| 2605 |
— *end example*]
|
| 2606 |
|
| 2607 |
+
An expression designating a particular specialization of a conversion
|
| 2608 |
+
function template can only be formed with a *splice-expression*. There
|
| 2609 |
+
is no analogous syntax to form a *template-id* [[temp.names]] for such a
|
| 2610 |
+
function by providing an explicit template argument list
|
| 2611 |
+
[[temp.arg.explicit]].
|
| 2612 |
|
| 2613 |
— *end note*]
|
| 2614 |
|
| 2615 |
### Variadic templates <a id="temp.variadic">[[temp.variadic]]</a>
|
| 2616 |
|
|
|
|
| 2663 |
foo(1); // xs contains one init-capture
|
| 2664 |
```
|
| 2665 |
|
| 2666 |
— *end example*]
|
| 2667 |
|
| 2668 |
+
A *structured binding pack* is an *sb-identifier* that introduces zero
|
| 2669 |
+
or more structured bindings [[dcl.struct.bind]].
|
| 2670 |
+
|
| 2671 |
+
[*Example 4*:
|
| 2672 |
+
|
| 2673 |
+
``` cpp
|
| 2674 |
+
auto foo() -> int(&)[2];
|
| 2675 |
+
|
| 2676 |
+
template <class T>
|
| 2677 |
+
void g() {
|
| 2678 |
+
auto [...a] = foo(); // a is a structured binding pack containing two elements
|
| 2679 |
+
auto [b, c, ...d] = foo(); // d is a structured binding pack containing zero elements
|
| 2680 |
+
}
|
| 2681 |
+
```
|
| 2682 |
+
|
| 2683 |
+
— *end example*]
|
| 2684 |
+
|
| 2685 |
+
A *pack* is a template parameter pack, a function parameter pack, an
|
| 2686 |
+
*init-capture* pack, or a structured binding pack. The number of
|
| 2687 |
+
elements of a template parameter pack or a function parameter pack is
|
| 2688 |
+
the number of arguments provided for the parameter pack. The number of
|
| 2689 |
+
elements of an *init-capture* pack is the number of elements in the pack
|
| 2690 |
+
expansion of its *initializer*.
|
| 2691 |
|
| 2692 |
A *pack expansion* consists of a *pattern* and an ellipsis, the
|
| 2693 |
instantiation of which produces zero or more instantiations of the
|
| 2694 |
pattern in a list (described below). The form of the pattern depends on
|
| 2695 |
the context in which the expansion occurs. Pack expansions can occur in
|
|
|
|
| 2697 |
|
| 2698 |
- In a function parameter pack [[dcl.fct]]; the pattern is the
|
| 2699 |
*parameter-declaration* without the ellipsis.
|
| 2700 |
- In a *using-declaration* [[namespace.udecl]]; the pattern is a
|
| 2701 |
*using-declarator*.
|
| 2702 |
+
- In a *friend-type-declaration* [[class.mem.general]]; the pattern is a
|
| 2703 |
+
*friend-type-specifier*.
|
| 2704 |
- In a template parameter pack that is a pack expansion [[temp.param]]:
|
| 2705 |
- if the template parameter pack is a *parameter-declaration*; the
|
| 2706 |
pattern is the *parameter-declaration* without the ellipsis;
|
| 2707 |
- if the template parameter pack is a *type-parameter*; the pattern is
|
| 2708 |
+
the corresponding *type-parameter* without the ellipsis;
|
| 2709 |
+
- if the template parameter pack is a template template parameter; the
|
| 2710 |
+
pattern is the corresponding *type-tt-parameter*,
|
| 2711 |
+
*variable-tt-parameter*, or *concept-tt-parameter* without the
|
| 2712 |
+
ellipsis.
|
| 2713 |
- In an *initializer-list* [[dcl.init]]; the pattern is an
|
| 2714 |
*initializer-clause*.
|
| 2715 |
- In a *base-specifier-list* [[class.derived]]; the pattern is a
|
| 2716 |
*base-specifier*.
|
| 2717 |
- In a *mem-initializer-list* [[class.base.init]] for a
|
|
|
|
| 2719 |
pattern is the *mem-initializer*.
|
| 2720 |
- In a *template-argument-list* [[temp.arg]]; the pattern is a
|
| 2721 |
*template-argument*.
|
| 2722 |
- In an *attribute-list* [[dcl.attr.grammar]]; the pattern is an
|
| 2723 |
*attribute*.
|
| 2724 |
+
- In an *annotation-list* [[dcl.attr.grammar]]; the pattern is an
|
| 2725 |
+
*annotation*.
|
| 2726 |
- In an *alignment-specifier* [[dcl.align]]; the pattern is the
|
| 2727 |
*alignment-specifier* without the ellipsis.
|
| 2728 |
- In a *capture-list* [[expr.prim.lambda.capture]]; the pattern is the
|
| 2729 |
*capture* without the ellipsis.
|
| 2730 |
- In a `sizeof...` expression [[expr.sizeof]]; the pattern is an
|
| 2731 |
*identifier*.
|
| 2732 |
+
- In a *pack-index-expression*; the pattern is an *identifier*.
|
| 2733 |
+
- In a *pack-index-specifier*; the pattern is a *typedef-name*.
|
| 2734 |
- In a *fold-expression* [[expr.prim.fold]]; the pattern is the
|
| 2735 |
*cast-expression* that contains an unexpanded pack.
|
| 2736 |
+
- In a fold expanded constraint [[temp.constr.fold]]; the pattern is the
|
| 2737 |
+
constraint of that fold expanded constraint.
|
| 2738 |
|
| 2739 |
+
[*Example 5*:
|
| 2740 |
|
| 2741 |
``` cpp
|
| 2742 |
template<class ... Types> void f(Types ... rest);
|
| 2743 |
template<class ... Types> void g(Types ... rest) {
|
| 2744 |
f(&rest ...); // ``&rest ...'' is a pack expansion; ``&rest'' is its pattern
|
|
|
|
| 2758 |
nested pack expansion; such packs are called *unexpanded packs* in the
|
| 2759 |
pattern. All of the packs expanded by a pack expansion shall have the
|
| 2760 |
same number of arguments specified. An appearance of a name of a pack
|
| 2761 |
that is not expanded is ill-formed.
|
| 2762 |
|
| 2763 |
+
[*Example 6*:
|
| 2764 |
|
| 2765 |
``` cpp
|
| 2766 |
template<typename...> struct Tuple {};
|
| 2767 |
template<typename T1, typename T2> struct Pair {};
|
| 2768 |
|
|
|
|
| 2794 |
expansion parameters. Each `Eᵢ` is generated by instantiating the
|
| 2795 |
pattern and replacing each pack expansion parameter with its iᵗʰ
|
| 2796 |
element. Such an element, in the context of the instantiation, is
|
| 2797 |
interpreted as follows:
|
| 2798 |
|
| 2799 |
+
- if the pack is a template parameter pack, the element is
|
| 2800 |
+
- a *typedef-name* for a type template parameter pack,
|
| 2801 |
+
- an *id-expression* for a constant template parameter pack, or
|
| 2802 |
+
- a *template-name* for a template template parameter pack
|
| 2803 |
+
|
| 2804 |
+
designating the iᵗʰ corresponding type, constant, or template template
|
| 2805 |
+
argument;
|
| 2806 |
- if the pack is a function parameter pack, the element is an
|
| 2807 |
*id-expression* designating the iᵗʰ function parameter that resulted
|
| 2808 |
from instantiation of the function parameter pack declaration;
|
|
|
|
| 2809 |
- if the pack is an *init-capture* pack, the element is an
|
| 2810 |
*id-expression* designating the variable introduced by the iᵗʰ
|
| 2811 |
*init-capture* that resulted from instantiation of the *init-capture*
|
| 2812 |
+
pack declaration; otherwise
|
| 2813 |
+
- if the pack is a structured binding pack, the element is an
|
| 2814 |
+
*id-expression* designating the $i^\textrm{th}$ structured binding in
|
| 2815 |
+
the pack that resulted from the structured binding declaration.
|
| 2816 |
|
| 2817 |
When N is zero, the instantiation of a pack expansion does not alter the
|
| 2818 |
syntactic interpretation of the enclosing construct, even in cases where
|
| 2819 |
omitting the pack expansion entirely would otherwise be ill-formed or
|
| 2820 |
would result in an ambiguity in the grammar.
|
| 2821 |
|
| 2822 |
The instantiation of a `sizeof...` expression [[expr.sizeof]] produces
|
| 2823 |
an integral constant with value N.
|
| 2824 |
|
| 2825 |
+
When instantiating a *pack-index-expression* P, let K be the index of P.
|
| 2826 |
+
The instantiation of P is the *id-expression* `E_K`.
|
| 2827 |
+
|
| 2828 |
+
When instantiating a *pack-index-specifier* P, let K be the index of P.
|
| 2829 |
+
The instantiation of P is the *typedef-name* `E_K`.
|
| 2830 |
+
|
| 2831 |
+
The instantiation of an *alignment-specifier* with an ellipsis produces
|
| 2832 |
+
`E₁` `E₂` … `E_N`.
|
| 2833 |
+
|
| 2834 |
The instantiation of a *fold-expression* [[expr.prim.fold]] produces:
|
| 2835 |
|
| 2836 |
- `(` `((`E₁ *op* E₂`)` *op* ⋯`)` *op* $\mathtt{E}_N$ `)` for a unary
|
| 2837 |
left fold,
|
| 2838 |
- `(` E₁ *op* `(`⋯ *op* `(`$\mathtt{E}_{N-1}$ *op* $\mathtt{E}_N$`))`
|
|
|
|
| 2844 |
|
| 2845 |
In each case, *op* is the *fold-operator*. For a binary fold, E is
|
| 2846 |
generated by instantiating the *cast-expression* that did not contain an
|
| 2847 |
unexpanded pack.
|
| 2848 |
|
| 2849 |
+
[*Example 7*:
|
| 2850 |
|
| 2851 |
``` cpp
|
| 2852 |
template<typename ...Args>
|
| 2853 |
bool all(Args ...args) { return (... && args); }
|
| 2854 |
|
|
|
|
| 2871 |
| `&&` | `true` |
|
| 2872 |
| `||` | `false` |
|
| 2873 |
| `,` | `void()` |
|
| 2874 |
|
| 2875 |
|
| 2876 |
+
A fold expanded constraint is not instantiated [[temp.constr.fold]].
|
| 2877 |
+
|
| 2878 |
The instantiation of any other pack expansion produces a list of
|
| 2879 |
elements `E₁`, `E₂`, …, `E_N`.
|
| 2880 |
|
| 2881 |
[*Note 1*: The variety of list varies with the context:
|
| 2882 |
*expression-list*, *base-specifier-list*, *template-argument-list*,
|
| 2883 |
etc. — *end note*]
|
| 2884 |
|
| 2885 |
When N is zero, the instantiation of the expansion produces an empty
|
| 2886 |
list.
|
| 2887 |
|
| 2888 |
+
[*Example 8*:
|
| 2889 |
|
| 2890 |
``` cpp
|
| 2891 |
template<class... T> struct X : T... { };
|
| 2892 |
template<class... T> void f(T... values) {
|
| 2893 |
X<T...> x(values...);
|
|
|
|
| 3169 |
// which refers to the primary template
|
| 3170 |
```
|
| 3171 |
|
| 3172 |
— *end example*]
|
| 3173 |
|
| 3174 |
+
A constant template argument is non-specialized if it is the name of a
|
| 3175 |
+
constant template parameter. All other constant template arguments are
|
| 3176 |
+
specialized.
|
| 3177 |
|
| 3178 |
Within the argument list of a partial specialization, the following
|
| 3179 |
restrictions apply:
|
| 3180 |
|
| 3181 |
- The type of a template parameter corresponding to a specialized
|
| 3182 |
+
constant template argument shall not be dependent on a parameter of
|
| 3183 |
+
the partial specialization.
|
| 3184 |
\[*Example 5*:
|
| 3185 |
``` cpp
|
| 3186 |
template <class T, T t> struct C {};
|
| 3187 |
template <class T> struct C<T, 1>; // error
|
| 3188 |
|
|
|
|
| 3193 |
|
| 3194 |
— *end example*]
|
| 3195 |
- The partial specialization shall be more specialized than the primary
|
| 3196 |
template [[temp.spec.partial.order]].
|
| 3197 |
- The template parameter list of a partial specialization shall not
|
| 3198 |
+
contain default template argument values.[^7]
|
| 3199 |
- An argument shall not contain an unexpanded pack. If an argument is a
|
| 3200 |
pack expansion [[temp.variadic]], it shall be the last argument in the
|
| 3201 |
template argument list.
|
| 3202 |
|
| 3203 |
The usual access checking rules do not apply to non-dependent names used
|
|
|
|
| 3457 |
scope. — *end note*]
|
| 3458 |
|
| 3459 |
A non-template function is not related to a function template (i.e., it
|
| 3460 |
is never considered to be a specialization), even if it has the same
|
| 3461 |
name and type as a potentially generated function template
|
| 3462 |
+
specialization.[^8]
|
| 3463 |
|
| 3464 |
#### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
|
| 3465 |
|
| 3466 |
It is possible to overload function templates so that two different
|
| 3467 |
function template specializations have the same type.
|
|
|
|
| 3527 |
template <int I, int J> A<I-J> f(A<I>, A<J>); // different from #1
|
| 3528 |
```
|
| 3529 |
|
| 3530 |
— *end example*]
|
| 3531 |
|
| 3532 |
+
[*Note 2*: Most expressions that use template parameters use constant
|
| 3533 |
template parameters, but it is possible for an expression to reference a
|
| 3534 |
type parameter. For example, a template type parameter can be used in
|
| 3535 |
the `sizeof` operator. — *end note*]
|
| 3536 |
|
| 3537 |
Two expressions involving template parameters are considered
|
|
|
|
| 3558 |
|
| 3559 |
For determining whether two dependent names [[temp.dep]] are equivalent,
|
| 3560 |
only the name itself is considered, not the result of name lookup.
|
| 3561 |
|
| 3562 |
[*Note 5*: If such a dependent name is unqualified, it is looked up
|
| 3563 |
+
from a first declaration of the function template
|
| 3564 |
+
[[temp.res.general]]. — *end note*]
|
| 3565 |
|
| 3566 |
[*Example 3*:
|
| 3567 |
|
| 3568 |
``` cpp
|
| 3569 |
template <int I, int J> void f(A<I+J>); // #1
|
|
|
|
| 3619 |
corresponding *constraint-expression*s are equivalent. Two
|
| 3620 |
*template-parameter*s are *equivalent* under the following conditions:
|
| 3621 |
|
| 3622 |
- they declare template parameters of the same kind,
|
| 3623 |
- if either declares a template parameter pack, they both do,
|
| 3624 |
+
- if they declare constant template parameters, they have equivalent
|
| 3625 |
types ignoring the use of *type-constraint*s for placeholder types,
|
| 3626 |
and
|
| 3627 |
+
- if they declare template template parameters, their *template-head*s
|
| 3628 |
+
are equivalent.
|
| 3629 |
|
| 3630 |
When determining whether types or *type-constraint*s are equivalent, the
|
| 3631 |
rules above are used to compare expressions involving template
|
| 3632 |
parameters. Two *template-head*s are *functionally equivalent* if they
|
| 3633 |
accept and are satisfied by [[temp.constr.constr]] the same set of
|
| 3634 |
template argument lists.
|
| 3635 |
|
| 3636 |
If the validity or meaning of the program depends on whether two
|
| 3637 |
constructs are equivalent, and they are functionally equivalent but not
|
| 3638 |
equivalent, the program is ill-formed, no diagnostic required.
|
| 3639 |
+
Furthermore, if two declarations A and B of function templates
|
| 3640 |
|
| 3641 |
+
- introduce the same name,
|
| 3642 |
- have corresponding signatures [[basic.scope.scope]],
|
| 3643 |
+
- would declare the same entity, when considering A and B to correspond
|
| 3644 |
+
in that determination [[basic.link]], and
|
| 3645 |
- accept and are satisfied by the same set of template argument lists,
|
| 3646 |
|
| 3647 |
+
but do not correspond, the program is ill-formed, no diagnostic
|
| 3648 |
+
required.
|
| 3649 |
|
| 3650 |
[*Note 7*:
|
| 3651 |
|
| 3652 |
This rule guarantees that equivalent declarations will be linked with
|
| 3653 |
one another, while not requiring implementations to use heroic efforts
|
|
|
|
| 3697 |
templates is more specialized than the other. If so, the more
|
| 3698 |
specialized template is the one chosen by the partial ordering process.
|
| 3699 |
If both deductions succeed, the partial ordering selects the more
|
| 3700 |
constrained template (if one exists) as determined below.
|
| 3701 |
|
| 3702 |
+
To produce the transformed template, for each type, constant, type
|
| 3703 |
+
template, variable template, or concept template parameter (including
|
| 3704 |
+
template parameter packs [[temp.variadic]] thereof) synthesize a unique
|
| 3705 |
+
type, value, class template, variable template, or concept,
|
| 3706 |
+
respectively, and substitute it for each occurrence of that parameter in
|
| 3707 |
+
the function type of the template.
|
| 3708 |
|
| 3709 |
[*Note 1*: The type replacing the placeholder in the type of the value
|
| 3710 |
+
synthesized for a constant template parameter is also a unique
|
| 3711 |
synthesized type. — *end note*]
|
| 3712 |
|
| 3713 |
+
A synthesized template has the same *template-head* as its corresponding
|
| 3714 |
+
template template parameter.
|
| 3715 |
+
|
| 3716 |
Each function template M that is a member function is considered to have
|
| 3717 |
a new first parameter of type X(M), described below, inserted in its
|
| 3718 |
function parameter list. If exactly one of the function templates was
|
| 3719 |
considered by overload resolution via a rewritten candidate
|
| 3720 |
[[over.match.oper]] with a reversed order of parameters, then the order
|
|
|
|
| 3918 |
A *template-declaration* in which the *declaration* is an
|
| 3919 |
*alias-declaration* [[dcl.pre]] declares the *identifier* to be an
|
| 3920 |
*alias template*. An alias template is a name for a family of types. The
|
| 3921 |
name of the alias template is a *template-name*.
|
| 3922 |
|
| 3923 |
+
A
|
| 3924 |
+
|
| 3925 |
+
- *template-id* that is not the operand of a *reflect-expression* or
|
| 3926 |
+
|
| 3927 |
+
-
|
| 3928 |
+
|
| 3929 |
+
that designates the specialization of an alias template is equivalent to
|
| 3930 |
+
the associated type obtained by substitution of its *template-argument*s
|
| 3931 |
+
for the *template-parameter*s in the *defining-type-id* of the alias
|
| 3932 |
+
template. Any other *template-id* that names a specialization of an
|
| 3933 |
+
alias template is a *typedef-name* for a type alias.
|
| 3934 |
|
| 3935 |
[*Note 1*: An alias template name is never deduced. — *end note*]
|
| 3936 |
|
| 3937 |
[*Example 1*:
|
| 3938 |
|
|
|
|
| 4057 |
The *constraint-expression* of a *concept-definition* is an unevaluated
|
| 4058 |
operand [[expr.context]].
|
| 4059 |
|
| 4060 |
The first declared template parameter of a concept definition is its
|
| 4061 |
*prototype parameter*. A *type concept* is a concept whose prototype
|
| 4062 |
+
parameter is a type template parameter.
|
| 4063 |
|
| 4064 |
## Name resolution <a id="temp.res">[[temp.res]]</a>
|
| 4065 |
|
| 4066 |
### General <a id="temp.res.general">[[temp.res.general]]</a>
|
| 4067 |
|
|
|
|
| 4150 |
specialization, the program is ill-formed, no diagnostic required.
|
| 4151 |
|
| 4152 |
``` bnf
|
| 4153 |
typename-specifier:
|
| 4154 |
typename nested-name-specifier identifier
|
| 4155 |
+
typename nested-name-specifier templateₒₚₜ simple-template-id
|
| 4156 |
```
|
| 4157 |
|
| 4158 |
The component names of a *typename-specifier* are its *identifier* (if
|
| 4159 |
any) and those of its *nested-name-specifier* and *simple-template-id*
|
| 4160 |
(if any). A *typename-specifier* denotes the type or class template
|
|
|
|
| 4185 |
}
|
| 4186 |
```
|
| 4187 |
|
| 4188 |
— *end example*]
|
| 4189 |
|
| 4190 |
+
A *type-only context* is defined as follows: A qualified or unqualified
|
| 4191 |
+
name is said to be in a type-only context if it is the terminal name of
|
| 4192 |
|
| 4193 |
+
- a *typename-specifier*, *type-requirement*, *nested-name-specifier*,
|
| 4194 |
+
*elaborated-type-specifier*, *class-or-decltype*,
|
| 4195 |
+
*using-enum-declarator*, or
|
| 4196 |
+
- a *simple-type-specifier* of a *friend-type-specifier*, or
|
| 4197 |
- a *type-specifier* of a
|
| 4198 |
- *new-type-id*,
|
| 4199 |
- *defining-type-id*,
|
| 4200 |
- *conversion-type-id*,
|
| 4201 |
- *trailing-return-type*,
|
| 4202 |
- default argument of a *type-parameter*, or
|
| 4203 |
- *type-id* of a `static_cast`, `const_cast`, `reinterpret_cast`, or
|
| 4204 |
`dynamic_cast`, or
|
| 4205 |
- a *decl-specifier* of the *decl-specifier-seq* of a
|
| 4206 |
+
- *simple-declaration* or *function-definition* in namespace scope,
|
| 4207 |
- *member-declaration*,
|
| 4208 |
+
- *parameter-declaration* in a *member-declaration*,[^9] unless that
|
| 4209 |
*parameter-declaration* appears in a default argument,
|
| 4210 |
- *parameter-declaration* in a *declarator* of a function or function
|
| 4211 |
template declaration whose *declarator-id* is qualified, unless that
|
| 4212 |
*parameter-declaration* appears in a default argument,
|
| 4213 |
- *parameter-declaration* in a *lambda-declarator* or
|
| 4214 |
*requirement-parameter-list*, unless that *parameter-declaration*
|
| 4215 |
appears in a default argument, or
|
| 4216 |
+
- *parameter-declaration* of a *template-parameter* (which necessarily
|
| 4217 |
+
declares a constant template parameter).
|
| 4218 |
+
|
| 4219 |
+
A *splice-specifier* or *splice-specialization-specifier*
|
| 4220 |
+
[[basic.splice]] is said to be in a type-only context if a hypothetical
|
| 4221 |
+
qualified name appearing in the same position would be in a type-only
|
| 4222 |
+
context.
|
| 4223 |
|
| 4224 |
[*Example 5*:
|
| 4225 |
|
| 4226 |
``` cpp
|
| 4227 |
template<class T> T::R f(); // OK, return type of a function declaration at global scope
|
| 4228 |
template<class T> void f(T::R); // ill-formed, no diagnostic required: attempt to declare
|
| 4229 |
// a void variable template
|
| 4230 |
+
enum class Enum { A, B, C };
|
| 4231 |
template<class T> struct S {
|
| 4232 |
using Ptr = PtrTraits<T>::Ptr; // OK, in a defining-type-id
|
| 4233 |
+
using Alias = [:^^int:]; // OK, in a defining-type-id
|
| 4234 |
T::R f(T::P p) { // OK, class scope
|
| 4235 |
return static_cast<T::R>(p); // OK, type-id of a static_cast
|
| 4236 |
}
|
| 4237 |
auto g() -> S<T*>::Ptr; // OK, trailing-return-type
|
| 4238 |
+
auto h() -> [:^^S:]<T*>; // OK, trailing-return-type
|
| 4239 |
+
using enum [:^^Enum:]; // OK, using-enum-declarator
|
| 4240 |
};
|
| 4241 |
template<typename T> void f() {
|
| 4242 |
void (*pf)(T::X); // variable pf of type void* initialized with T::X
|
| 4243 |
void g(T::X); // error: T::X at block scope does not denote a type
|
| 4244 |
// (attempt to declare a void variable)
|
|
|
|
| 4273 |
}
|
| 4274 |
```
|
| 4275 |
|
| 4276 |
— *end example*]
|
| 4277 |
|
| 4278 |
+
The validity of a templated entity may be checked prior to any
|
| 4279 |
+
instantiation.
|
| 4280 |
|
| 4281 |
[*Note 3*: Knowing which names are type names allows the syntax of
|
| 4282 |
every template to be checked in this way. — *end note*]
|
| 4283 |
|
| 4284 |
+
The program is ill-formed, no diagnostic required, if
|
| 4285 |
|
| 4286 |
- no valid specialization, ignoring *static_assert-declaration*s that
|
| 4287 |
+
fail [[dcl.pre]], can be generated for a templated entity or a
|
| 4288 |
+
substatement of a constexpr if statement [[stmt.if]] within a
|
| 4289 |
+
templated entity and the innermost enclosing template is not
|
| 4290 |
instantiated, or
|
| 4291 |
+
- no valid specialization, ignoring *static_assert-declaration*s that
|
| 4292 |
+
fail, can be generated for the *compound-statement* of an
|
| 4293 |
+
*expansion-statement* and there is no instantiation of it, or
|
| 4294 |
+
- no valid specialization, ignoring *static_assert-declaration*s that
|
| 4295 |
+
fail, can be generated for a default *template-argument* and the
|
| 4296 |
+
default *template-argument* is not used in any instantiation, or
|
| 4297 |
+
- no specialization of an alias template [[temp.alias]] is valid and no
|
| 4298 |
+
specialization of the alias template is named in the program, or
|
| 4299 |
- any *constraint-expression* in the program, introduced or otherwise,
|
| 4300 |
has (in its normal form) an atomic constraint A where no satisfaction
|
| 4301 |
check of A could be well-formed and no satisfaction check of A is
|
| 4302 |
performed, or
|
| 4303 |
- every valid specialization of a variadic template requires an empty
|
| 4304 |
template parameter pack, or
|
| 4305 |
+
- a hypothetical instantiation of a templated entity immediately
|
| 4306 |
+
following its definition would be ill-formed due to a construct (other
|
| 4307 |
+
than a *static_assert-declaration* that fails) that does not depend on
|
| 4308 |
+
a template parameter, or
|
| 4309 |
- the interpretation of such a construct in the hypothetical
|
| 4310 |
instantiation is different from the interpretation of the
|
| 4311 |
+
corresponding construct in any actual instantiation of the templated
|
| 4312 |
+
entity.
|
| 4313 |
|
| 4314 |
[*Note 4*:
|
| 4315 |
|
| 4316 |
This can happen in situations including the following:
|
| 4317 |
|
|
|
|
| 4342 |
it names an explicit specialization that was not declared when the
|
| 4343 |
template was defined.
|
| 4344 |
|
| 4345 |
— *end note*]
|
| 4346 |
|
|
|
|
|
|
|
|
|
|
| 4347 |
[*Note 5*: If a template is instantiated, errors will be diagnosed
|
| 4348 |
according to the other rules in this document. Exactly when these errors
|
| 4349 |
are diagnosed is a quality of implementation issue. — *end note*]
|
| 4350 |
|
| 4351 |
[*Example 7*:
|
|
|
|
| 4382 |
### Locally declared names <a id="temp.local">[[temp.local]]</a>
|
| 4383 |
|
| 4384 |
Like normal (non-template) classes, class templates have an
|
| 4385 |
injected-class-name [[class.pre]]. The injected-class-name can be used
|
| 4386 |
as a *template-name* or a *type-name*. When it is used with a
|
| 4387 |
+
*template-argument-list*, as a *template-argument* for a type template
|
| 4388 |
+
template parameter, or as the final identifier in the
|
| 4389 |
*elaborated-type-specifier* of a friend class template declaration, it
|
| 4390 |
is a *template-name* that refers to the class template itself.
|
| 4391 |
Otherwise, it is a *type-name* equivalent to the *template-name*
|
| 4392 |
followed by the template argument list
|
| 4393 |
[[temp.decls.general]], [[temp.arg.general]] of the class template
|
|
|
|
| 4471 |
};
|
| 4472 |
```
|
| 4473 |
|
| 4474 |
— *end example*]
|
| 4475 |
|
| 4476 |
+
The name of a template parameter shall not be bound to any following
|
| 4477 |
+
declaration whose locus is contained by the scope to which the template
|
| 4478 |
+
parameter belongs.
|
| 4479 |
|
| 4480 |
[*Example 5*:
|
| 4481 |
|
| 4482 |
``` cpp
|
| 4483 |
template<class T, int i> class Y {
|
| 4484 |
+
int T; // error: template parameter hidden
|
| 4485 |
void f() {
|
| 4486 |
+
char T; // error: template parameter hidden
|
| 4487 |
}
|
| 4488 |
friend void T(); // OK, no name bound
|
| 4489 |
};
|
| 4490 |
|
| 4491 |
+
template<class X> class X; // error: hidden by template parameter
|
| 4492 |
```
|
| 4493 |
|
| 4494 |
— *end example*]
|
| 4495 |
|
| 4496 |
Unqualified name lookup considers the template parameter scope of a
|
|
|
|
| 4599 |
class template, the name of the class template followed by the
|
| 4600 |
template argument list of its *template-head* [[temp.arg]] enclosed in
|
| 4601 |
`<>` (or an equivalent template alias specialization),
|
| 4602 |
- in the definition of a nested class of a class template, the name of
|
| 4603 |
the nested class referenced as a member of the current instantiation,
|
|
|
|
| 4604 |
- in the definition of a class template partial specialization or a
|
| 4605 |
member of a class template partial specialization, the name of the
|
| 4606 |
class template followed by a template argument list equivalent to that
|
| 4607 |
of the partial specialization [[temp.spec.partial]] enclosed in `<>`
|
| 4608 |
+
(or an equivalent template alias specialization), or
|
| 4609 |
+
- in the definition of a templated function, the name of a local class
|
| 4610 |
+
[[class.local]].
|
| 4611 |
|
| 4612 |
A template argument that is equivalent to a template parameter can be
|
| 4613 |
used in place of that template parameter in a reference to the current
|
| 4614 |
+
instantiation. A template argument is equivalent to a type template
|
| 4615 |
+
parameter if it denotes the same type. A template argument is equivalent
|
| 4616 |
+
to a constant template parameter if it is an *identifier* that names a
|
| 4617 |
+
variable that is equivalent to the template parameter. A variable is
|
| 4618 |
+
equivalent to a template parameter if
|
|
|
|
| 4619 |
|
| 4620 |
- it has the same type as the template parameter (ignoring
|
| 4621 |
cv-qualification) and
|
| 4622 |
- its initializer consists of a single *identifier* that names the
|
| 4623 |
template parameter or, recursively, such a variable.
|
|
|
|
| 4729 |
- it is a *conversion-function-id* whose *conversion-type-id* is
|
| 4730 |
dependent, or
|
| 4731 |
- its lookup context is dependent and is not the current instantiation,
|
| 4732 |
or
|
| 4733 |
- its lookup context is the current instantiation and it is
|
| 4734 |
+
`operator=`,[^10] or
|
| 4735 |
- its lookup context is the current instantiation and has at least one
|
| 4736 |
dependent base class, and qualified name lookup for the name finds
|
| 4737 |
nothing [[basic.lookup.qual]].
|
| 4738 |
|
| 4739 |
[*Example 4*:
|
|
|
|
| 4777 |
int g() { return m; } // finds A::m in the template definition context
|
| 4778 |
};
|
| 4779 |
|
| 4780 |
template int C<B>::f(); // error: finds both A::m and B::m
|
| 4781 |
template int C<B>::g(); // OK, transformation to class member access syntax
|
| 4782 |
+
// does not occur in the template definition context; see~[expr.prim.id.general]
|
| 4783 |
+
```
|
| 4784 |
+
|
| 4785 |
+
— *end example*]
|
| 4786 |
+
|
| 4787 |
+
An initializer is dependent if any constituent expression
|
| 4788 |
+
[[intro.execution]] of the initializer is type-dependent. A placeholder
|
| 4789 |
+
type [[dcl.spec.auto.general]] is dependent if it designates a type
|
| 4790 |
+
deduced from a dependent initializer.
|
| 4791 |
+
|
| 4792 |
+
A placeholder for a deduced class type [[dcl.type.class.deduct]] is
|
| 4793 |
+
dependent if
|
| 4794 |
+
|
| 4795 |
+
- it has a dependent initializer, or
|
| 4796 |
+
- it refers to an alias template that is a member of the current
|
| 4797 |
+
instantiation and whose *defining-type-id* is dependent after class
|
| 4798 |
+
template argument deduction [[over.match.class.deduct]] and
|
| 4799 |
+
substitution [[temp.alias]].
|
| 4800 |
+
|
| 4801 |
+
[*Example 6*:
|
| 4802 |
+
|
| 4803 |
+
``` cpp
|
| 4804 |
+
template<class T, class V>
|
| 4805 |
+
struct S { S(T); };
|
| 4806 |
+
|
| 4807 |
+
template<class U>
|
| 4808 |
+
struct A {
|
| 4809 |
+
template<class T> using X = S<T, U>;
|
| 4810 |
+
template<class T> using Y = S<T, int>;
|
| 4811 |
+
void f() {
|
| 4812 |
+
new X(1); // dependent
|
| 4813 |
+
new Y(1); // not dependent
|
| 4814 |
+
}
|
| 4815 |
+
};
|
| 4816 |
```
|
| 4817 |
|
| 4818 |
— *end example*]
|
| 4819 |
|
| 4820 |
A type is dependent if it is
|
|
|
|
| 4828 |
- an array type whose element type is dependent or whose bound (if any)
|
| 4829 |
is value-dependent,
|
| 4830 |
- a function type whose parameters include one or more function
|
| 4831 |
parameter packs,
|
| 4832 |
- a function type whose exception specification is value-dependent,
|
| 4833 |
+
- denoted by a dependent placeholder type,
|
| 4834 |
+
- denoted by a dependent placeholder for a deduced class type,
|
| 4835 |
- denoted by a *simple-template-id* in which either the template name is
|
| 4836 |
+
a template parameter or any of the template arguments is dependent
|
| 4837 |
+
[[temp.dep.temp]],[^11]
|
| 4838 |
+
- a *pack-index-specifier*,
|
| 4839 |
- denoted by `decltype(`*expression*`)`, where *expression* is
|
| 4840 |
+
type-dependent [[temp.dep.expr]], or
|
| 4841 |
+
- denoted by a *splice-type-specifier* in which either the
|
| 4842 |
+
*splice-specifier* or *splice-specialization-specifier* is dependent
|
| 4843 |
+
[[temp.dep.splice]].
|
| 4844 |
|
| 4845 |
[*Note 3*: Because typedefs do not introduce new types, but instead
|
| 4846 |
simply refer to other types, a name that refers to a typedef that is a
|
| 4847 |
member of the current instantiation is dependent only if the type
|
| 4848 |
referred to is dependent. — *end note*]
|
|
|
|
| 4858 |
An *id-expression* is type-dependent if it is a *template-id* that is
|
| 4859 |
not a concept-id and is dependent; or if its terminal name is
|
| 4860 |
|
| 4861 |
- associated by name lookup with one or more declarations declared with
|
| 4862 |
a dependent type,
|
| 4863 |
+
- associated by name lookup with a constant template parameter declared
|
| 4864 |
+
with a type that contains a placeholder type [[dcl.spec.auto]],
|
|
|
|
| 4865 |
- associated by name lookup with a variable declared with a type that
|
| 4866 |
contains a placeholder type [[dcl.spec.auto]] where the initializer is
|
| 4867 |
type-dependent,
|
| 4868 |
- associated by name lookup with one or more declarations of member
|
| 4869 |
functions of a class that is the current instantiation declared with a
|
| 4870 |
return type that contains a placeholder type,
|
| 4871 |
- associated by name lookup with a structured binding declaration
|
| 4872 |
[[dcl.struct.bind]] whose *brace-or-equal-initializer* is
|
| 4873 |
type-dependent,
|
| 4874 |
+
- associated by name lookup with a pack,
|
| 4875 |
+
\[*Example 1*:
|
| 4876 |
+
``` cpp
|
| 4877 |
+
struct C { };
|
| 4878 |
+
|
| 4879 |
+
void g(...); // #1
|
| 4880 |
+
|
| 4881 |
+
template <typename T>
|
| 4882 |
+
void f() {
|
| 4883 |
+
C arr[1];
|
| 4884 |
+
auto [...e] = arr;
|
| 4885 |
+
g(e...); // calls #2
|
| 4886 |
+
}
|
| 4887 |
+
|
| 4888 |
+
void g(C); // #2
|
| 4889 |
+
|
| 4890 |
+
int main() {
|
| 4891 |
+
f<int>();
|
| 4892 |
+
}
|
| 4893 |
+
```
|
| 4894 |
+
|
| 4895 |
+
— *end example*]
|
| 4896 |
- associated by name lookup with an entity captured by copy
|
| 4897 |
[[expr.prim.lambda.capture]] in a *lambda-expression* that has an
|
| 4898 |
explicit object parameter whose type is dependent [[dcl.fct]],
|
| 4899 |
- the *identifier* `__func__` [[dcl.fct.def.general]], where any
|
| 4900 |
enclosing function is a template, a member of a class template, or a
|
| 4901 |
generic lambda,
|
| 4902 |
+
- associated by name lookup with a result binding [[dcl.contract.res]]
|
| 4903 |
+
of a function whose return type is dependent,
|
| 4904 |
+
- a *conversion-function-id* that specifies a dependent type,
|
| 4905 |
+
- a name N introduced by the *for-range-declaration* of an
|
| 4906 |
+
*expansion-statement* S if the type specified for N contains a
|
| 4907 |
+
placeholder type and either
|
| 4908 |
+
- the *expansion-initializer* of S is type-dependent or
|
| 4909 |
+
- S is not an iterating expansion statement, or
|
| 4910 |
- dependent
|
| 4911 |
|
| 4912 |
or if it names a dependent member of the current instantiation that is a
|
| 4913 |
static data member of type “array of unknown bound of `T`” for some `T`
|
| 4914 |
[[temp.static]]. Expressions of the following forms are type-dependent
|
|
|
|
| 4943 |
typeid '(' type-id ')'
|
| 4944 |
'::'ₒₚₜ delete cast-expression
|
| 4945 |
'::'ₒₚₜ delete '[' ']' cast-expression
|
| 4946 |
throw assignment-expressionₒₚₜ
|
| 4947 |
noexcept '(' expression ')'
|
| 4948 |
+
requires-expression
|
| 4949 |
+
reflect-expression
|
| 4950 |
```
|
| 4951 |
|
| 4952 |
[*Note 1*: For the standard library macro `offsetof`, see
|
| 4953 |
[[support.types]]. — *end note*]
|
| 4954 |
|
|
|
|
| 4966 |
A *braced-init-list* is type-dependent if any element is type-dependent
|
| 4967 |
or is a pack expansion.
|
| 4968 |
|
| 4969 |
A *fold-expression* is type-dependent.
|
| 4970 |
|
| 4971 |
+
A *pack-index-expression* is type-dependent if its *id-expression* is
|
| 4972 |
+
type-dependent.
|
| 4973 |
+
|
| 4974 |
+
A *splice-expression* is type-dependent if its *splice-specifier* or
|
| 4975 |
+
*splice-specialization-specifier* is dependent [[temp.dep.splice]].
|
| 4976 |
+
|
| 4977 |
#### Value-dependent expressions <a id="temp.dep.constexpr">[[temp.dep.constexpr]]</a>
|
| 4978 |
|
| 4979 |
Except as described below, an expression used in a context where a
|
| 4980 |
constant expression is required is value-dependent if any subexpression
|
| 4981 |
is value-dependent.
|
| 4982 |
|
| 4983 |
+
An *id-expression* is value-dependent if
|
| 4984 |
|
| 4985 |
+
- it is a concept-id and its *concept-name* is dependent or any of its
|
| 4986 |
+
arguments are dependent [[temp.dep.temp]],
|
| 4987 |
- it is type-dependent,
|
| 4988 |
+
- it is the name of a constant template parameter,
|
| 4989 |
+
- it is a name introduced by the *for-range-declaration* of an
|
| 4990 |
+
*expansion-statement* [[stmt.expand]],
|
| 4991 |
- it names a static data member that is a dependent member of the
|
| 4992 |
current instantiation and is not initialized in a *member-declarator*,
|
| 4993 |
- it names a static member function that is a dependent member of the
|
| 4994 |
current instantiation, or
|
| 4995 |
- it names a potentially-constant variable [[expr.const]] that is
|
|
|
|
| 5003 |
sizeof unary-expression
|
| 5004 |
sizeof '(' type-id ')'
|
| 5005 |
typeid '(' expression ')'
|
| 5006 |
typeid '(' type-id ')'
|
| 5007 |
alignof '(' type-id ')'
|
|
|
|
| 5008 |
```
|
| 5009 |
|
| 5010 |
[*Note 1*: For the standard library macro `offsetof`, see
|
| 5011 |
[[support.types]]. — *end note*]
|
| 5012 |
|
| 5013 |
Expressions of the following form are value-dependent if either the
|
| 5014 |
+
*type-id*, *simple-type-specifier*, or *typename-specifier* is dependent
|
| 5015 |
+
or the *expression* or *cast-expression* is value-dependent or any
|
| 5016 |
+
*expression* in the *expression-list* is value-dependent or any
|
| 5017 |
+
*assignment-expression* in the *braced-init-list* is value-dependent:
|
| 5018 |
|
| 5019 |
``` bnf
|
| 5020 |
simple-type-specifier '(' expression-listₒₚₜ ')'
|
| 5021 |
+
typename-specifier '(' expression-listₒₚₜ ')'
|
| 5022 |
+
simple-type-specifier braced-init-list
|
| 5023 |
+
typename-specifier braced-init-list
|
| 5024 |
static_cast '<' type-id '>' '(' expression ')'
|
| 5025 |
const_cast '<' type-id '>' '(' expression ')'
|
| 5026 |
reinterpret_cast '<' type-id '>' '(' expression ')'
|
| 5027 |
+
dynamic_cast '<' type-id '>' '(' expression ')'
|
| 5028 |
'(' type-id ')' cast-expression
|
| 5029 |
```
|
| 5030 |
|
| 5031 |
Expressions of the following form are value-dependent:
|
| 5032 |
|
| 5033 |
``` bnf
|
| 5034 |
sizeof '...' '(' identifier ')'
|
| 5035 |
fold-expression
|
| 5036 |
```
|
| 5037 |
|
| 5038 |
+
unless the *identifier* is a structured binding pack whose initializer
|
| 5039 |
+
is not dependent.
|
| 5040 |
+
|
| 5041 |
+
A *noexcept-expression* [[expr.unary.noexcept]] is value-dependent if
|
| 5042 |
+
its *expression* involves a template parameter.
|
| 5043 |
+
|
| 5044 |
An expression of the form `&`*qualified-id* where the *qualified-id*
|
| 5045 |
names a dependent member of the current instantiation is
|
| 5046 |
value-dependent. An expression of the form `&`*cast-expression* is also
|
| 5047 |
value-dependent if evaluating *cast-expression* as a core constant
|
| 5048 |
expression [[expr.const]] succeeds and the result of the evaluation
|
| 5049 |
refers to a templated entity that is an object with static or thread
|
| 5050 |
storage duration or a member function.
|
| 5051 |
|
| 5052 |
+
A *reflect-expression* is value-dependent if
|
| 5053 |
+
|
| 5054 |
+
- it is of the form `^^ reflection-name` and the *reflection-name*
|
| 5055 |
+
- is a dependent qualified name,
|
| 5056 |
+
- is a dependent *namespace-name*,
|
| 5057 |
+
- is the name of a template parameter, or
|
| 5058 |
+
- names a dependent member of the current instantiation
|
| 5059 |
+
[[temp.dep.type]],
|
| 5060 |
+
- it is of the form `^^ type-id` and the *type-id* denotes a dependent
|
| 5061 |
+
type, or
|
| 5062 |
+
- it is of the form `^^ id-expression` and the *id-expression* is
|
| 5063 |
+
value-dependent.
|
| 5064 |
+
|
| 5065 |
+
A *splice-expression* is value-dependent if its *splice-specifier* or
|
| 5066 |
+
*splice-specialization-specifier* is dependent [[temp.dep.splice]].
|
| 5067 |
+
|
| 5068 |
+
#### Dependent splice specifiers <a id="temp.dep.splice">[[temp.dep.splice]]</a>
|
| 5069 |
+
|
| 5070 |
+
A *splice-specifier* is dependent if its converted *constant-expression*
|
| 5071 |
+
is value-dependent. A *splice-specialization-specifier* is dependent if
|
| 5072 |
+
its *splice-specifier* is dependent or if any of its template arguments
|
| 5073 |
+
are dependent. A *splice-scope-specifier* is dependent if its
|
| 5074 |
+
*splice-specifier* or *splice-specialization-specifier* is dependent.
|
| 5075 |
+
|
| 5076 |
+
[*Example 1*:
|
| 5077 |
+
|
| 5078 |
+
``` cpp
|
| 5079 |
+
template<auto T, auto NS>
|
| 5080 |
+
void fn() {
|
| 5081 |
+
using a = [:T:]<1>; // [:T:]<1> is dependent because [:T:] is dependent
|
| 5082 |
+
static_assert([:NS:]::template TCls<1>::v == a::v); // [:NS:] is dependent
|
| 5083 |
+
}
|
| 5084 |
+
|
| 5085 |
+
namespace N {
|
| 5086 |
+
template <auto V> struct TCls { static constexpr int v = V; };
|
| 5087 |
+
}
|
| 5088 |
+
|
| 5089 |
+
int main() {
|
| 5090 |
+
fn<^^N::TCls, ^^N>();
|
| 5091 |
+
}
|
| 5092 |
+
```
|
| 5093 |
+
|
| 5094 |
+
— *end example*]
|
| 5095 |
+
|
| 5096 |
+
[*Example 2*:
|
| 5097 |
+
|
| 5098 |
+
``` cpp
|
| 5099 |
+
template<template<class> class X>
|
| 5100 |
+
struct S {
|
| 5101 |
+
[:^^X:]<int, float> m;
|
| 5102 |
+
};
|
| 5103 |
+
|
| 5104 |
+
template<class> struct V1 {};
|
| 5105 |
+
template<class, class = int> struct V2 {};
|
| 5106 |
+
|
| 5107 |
+
S<V1> s1; // error: V1<int, float> has too many template arguments
|
| 5108 |
+
S<V2> s2; // OK
|
| 5109 |
+
```
|
| 5110 |
+
|
| 5111 |
+
— *end example*]
|
| 5112 |
+
|
| 5113 |
+
#### Dependent namespaces <a id="temp.dep.namespace">[[temp.dep.namespace]]</a>
|
| 5114 |
+
|
| 5115 |
+
A namespace alias is dependent if it is introduced by a
|
| 5116 |
+
*namespace-alias-definition* whose *qualified-namespace-specifier* (if
|
| 5117 |
+
any) is a dependent qualified name or whose *splice-specifier* (if any)
|
| 5118 |
+
is dependent. A *namespace-name* is dependent if it names a dependent
|
| 5119 |
+
namespace alias.
|
| 5120 |
+
|
| 5121 |
+
[*Example 1*:
|
| 5122 |
+
|
| 5123 |
+
``` cpp
|
| 5124 |
+
template<std::meta::info R>
|
| 5125 |
+
int fn() {
|
| 5126 |
+
namespace Alias = [:R:]; // [:R:] is dependent
|
| 5127 |
+
return typename Alias::T{}; // Alias is dependent
|
| 5128 |
+
}
|
| 5129 |
+
|
| 5130 |
+
namespace NS {
|
| 5131 |
+
using T = int;
|
| 5132 |
+
}
|
| 5133 |
+
|
| 5134 |
+
int a = fn<^^NS>();
|
| 5135 |
+
```
|
| 5136 |
+
|
| 5137 |
+
— *end example*]
|
| 5138 |
+
|
| 5139 |
#### Dependent template arguments <a id="temp.dep.temp">[[temp.dep.temp]]</a>
|
| 5140 |
|
| 5141 |
A type *template-argument* is dependent if the type it specifies is
|
| 5142 |
dependent.
|
| 5143 |
|
| 5144 |
+
A constant *template-argument* is dependent if its type is dependent or
|
| 5145 |
the constant expression it specifies is value-dependent.
|
| 5146 |
|
| 5147 |
+
Furthermore, a constant *template-argument* is dependent if the
|
| 5148 |
+
corresponding constant template parameter is of reference or pointer
|
| 5149 |
type and the *template-argument* designates or points to a member of the
|
| 5150 |
current instantiation or a member of a dependent type.
|
| 5151 |
|
| 5152 |
+
A template argument is also dependent if it is a pack expansion.
|
| 5153 |
+
|
| 5154 |
+
A template template parameter is dependent if it names a template
|
| 5155 |
+
parameter or its terminal name is dependent.
|
| 5156 |
|
| 5157 |
### Dependent name resolution <a id="temp.dep.res">[[temp.dep.res]]</a>
|
| 5158 |
|
| 5159 |
#### Point of instantiation <a id="temp.point">[[temp.point]]</a>
|
| 5160 |
|
|
|
|
| 5224 |
template may have points of instantiation in multiple translation units.
|
| 5225 |
If two different points of instantiation give a template specialization
|
| 5226 |
different meanings according to the one-definition rule
|
| 5227 |
[[basic.def.odr]], the program is ill-formed, no diagnostic required.
|
| 5228 |
|
| 5229 |
+
For the *compound-statement* of an *expansion-statement*
|
| 5230 |
+
[[stmt.expand]], the point of instantiation is the point of
|
| 5231 |
+
instantiation of its enclosing templated entity, if any. Otherwise, it
|
| 5232 |
+
immediately follows the namespace-scope declaration or definition that
|
| 5233 |
+
contains the expansion statement.
|
| 5234 |
+
|
| 5235 |
#### Candidate functions <a id="temp.dep.candidate">[[temp.dep.candidate]]</a>
|
| 5236 |
|
| 5237 |
If a dependent call [[temp.dep]] would be ill-formed or would find a
|
| 5238 |
better match had the lookup for its dependent name considered all the
|
| 5239 |
function declarations with external linkage introduced in the associated
|
|
|
|
| 5467 |
member template. An explicit specialization declaration is introduced by
|
| 5468 |
`template<>`. In an explicit specialization declaration for a variable
|
| 5469 |
template, a class template, a member of a class template, or a class
|
| 5470 |
member template, the variable or class that is explicitly specialized
|
| 5471 |
shall be specified with a *simple-template-id*. In the explicit
|
| 5472 |
+
specialization declaration for a function template, the function
|
| 5473 |
+
explicitly specialized may be specified using a *template-id*.
|
|
|
|
| 5474 |
|
| 5475 |
[*Example 1*:
|
| 5476 |
|
| 5477 |
``` cpp
|
| 5478 |
template<class T = int> struct A {
|
|
|
|
| 5805 |
};
|
| 5806 |
```
|
| 5807 |
|
| 5808 |
— *end example*]
|
| 5809 |
|
| 5810 |
+
If a function template specialization is used in a way that involves
|
| 5811 |
+
overload resolution, a declaration of the specialization is implicitly
|
| 5812 |
+
instantiated [[temp.over]].
|
| 5813 |
|
| 5814 |
An implementation shall not implicitly instantiate a function template,
|
| 5815 |
a variable template, a member template, a non-virtual member function, a
|
| 5816 |
member class or static data member of a templated class, or a
|
| 5817 |
substatement of a constexpr if statement [[stmt.if]], unless such
|
|
|
|
| 5866 |
}
|
| 5867 |
```
|
| 5868 |
|
| 5869 |
— *end example*]
|
| 5870 |
|
| 5871 |
+
The *noexcept-specifier* and *function-contract-specifier*s of a
|
| 5872 |
+
function template specialization are not instantiated along with the
|
| 5873 |
+
function declaration; they are instantiated when needed
|
| 5874 |
+
[[except.spec]], [[dcl.contract.func]]. If such a specifier is needed
|
| 5875 |
but has not yet been instantiated, the dependent names are looked up,
|
| 5876 |
the semantics constraints are checked, and the instantiation of any
|
| 5877 |
+
template used in the specifier is done as if it were being done as part
|
| 5878 |
+
of instantiating the declaration of the specialization at that point.
|
|
|
|
| 5879 |
|
| 5880 |
[*Note 6*: [[temp.point]] defines the point of instantiation of a
|
| 5881 |
template specialization. — *end note*]
|
| 5882 |
|
| 5883 |
There is an *implementation-defined* quantity that specifies the limit
|
|
|
|
| 5996 |
template<class T> class Array { void mf(); };
|
| 5997 |
template class Array<char>;
|
| 5998 |
template void Array<int>::mf();
|
| 5999 |
|
| 6000 |
template<class T> void sort(Array<T>& v) { ... }
|
| 6001 |
+
template void sort(Array<char>&); // argument is deduced here[temp.arg.explicit]
|
| 6002 |
|
| 6003 |
namespace N {
|
| 6004 |
template<class T> void f(T&) { }
|
| 6005 |
}
|
| 6006 |
template void N::f<int>(int&);
|
|
|
|
| 6025 |
produced by the corresponding substitution into the templated function,
|
| 6026 |
variable, or class are two declarations of the same entity.
|
| 6027 |
|
| 6028 |
[*Note 1*:
|
| 6029 |
|
| 6030 |
+
These declarations need to have matching types as specified in
|
| 6031 |
[[basic.link]], except as specified in [[except.spec]].
|
| 6032 |
|
| 6033 |
[*Example 2*:
|
| 6034 |
|
| 6035 |
``` cpp
|
|
|
|
| 6037 |
template float var<float>; // OK, instantiated variable has type float
|
| 6038 |
template int var<int[16]>[]; // OK, absence of major array bound is permitted
|
| 6039 |
template int *var<int>; // error: instantiated variable has type int
|
| 6040 |
|
| 6041 |
template<typename T> auto av = T();
|
| 6042 |
+
template int av<int>; // OK, variable with type auto can be redeclared with type int
|
| 6043 |
|
| 6044 |
template<typename T> auto f() {}
|
| 6045 |
template void f<int>(); // error: function with deduced return type
|
| 6046 |
// redeclared with non-deduced return type[dcl.spec.auto]
|
| 6047 |
```
|
|
|
|
| 6061 |
an explicit instantiation definition, the definition of a function
|
| 6062 |
template, a variable template, a member function template, or a member
|
| 6063 |
function or static data member of a class template shall be present in
|
| 6064 |
every translation unit in which it is explicitly instantiated.
|
| 6065 |
|
| 6066 |
+
[*Note 2*: An explicit instantiation of a constrained template needs to
|
| 6067 |
+
satisfy that template’s associated constraints [[temp.constr.decl]]. The
|
| 6068 |
+
satisfaction of constraints is determined when forming the template name
|
| 6069 |
+
of an explicit instantiation in which all template arguments are
|
| 6070 |
+
specified [[temp.names]], or, for explicit instantiations of function
|
| 6071 |
+
templates, during template argument deduction [[temp.deduct.decl]] when
|
| 6072 |
+
one or more trailing template arguments are left
|
| 6073 |
+
unspecified. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6074 |
|
| 6075 |
An explicit instantiation that names a class template specialization is
|
| 6076 |
also an explicit instantiation of the same kind (declaration or
|
| 6077 |
definition) of each of its direct non-template members that has not been
|
| 6078 |
previously explicitly specialized in the translation unit containing the
|
|
|
|
| 6112 |
a template with internal linkage.
|
| 6113 |
|
| 6114 |
An explicit instantiation does not constitute a use of a default
|
| 6115 |
argument, so default argument instantiation is not done.
|
| 6116 |
|
| 6117 |
+
[*Example 3*:
|
| 6118 |
|
| 6119 |
``` cpp
|
| 6120 |
char* p = 0;
|
| 6121 |
template<class T> T g(T x = &p) { return x; }
|
| 6122 |
template int g<int>(int); // OK even though &p isn't an int.
|
|
|
|
| 6148 |
[*Example 1*:
|
| 6149 |
|
| 6150 |
``` cpp
|
| 6151 |
template<class T> class stream;
|
| 6152 |
|
| 6153 |
+
template<> class stream<char> { ... }; // #1
|
| 6154 |
|
| 6155 |
template<class T> class Array { ... };
|
| 6156 |
template<class T> void sort(Array<T>& v) { ... }
|
| 6157 |
|
| 6158 |
+
template<> void sort<int>(Array<int>&); // #2
|
| 6159 |
+
template<> void sort(Array<char*>&); // #3 template argument is deduced[temp.arg.explicit]
|
| 6160 |
```
|
| 6161 |
|
| 6162 |
+
Given these declarations, \#1 will be used as the definition of streams
|
| 6163 |
+
of `char`s; other streams will be handled by class template
|
| 6164 |
+
specializations instantiated from the class template. Similarly, \#2
|
| 6165 |
+
will be used as the sort function for arguments of type `Array<int>` and
|
| 6166 |
+
\#3 will be used for arguments of type `Array<char*>`; other `Array`
|
| 6167 |
+
types will be sorted by functions generated from the function template.
|
| 6168 |
|
| 6169 |
— *end example*]
|
| 6170 |
|
| 6171 |
The *declaration* in an *explicit-specialization* shall not be an
|
| 6172 |
*export-declaration*. An explicit specialization shall not use a
|
|
|
|
| 6180 |
[[basic.scope.scope]]. A declaration of a function template, class
|
| 6181 |
template, or variable template being explicitly specialized shall be
|
| 6182 |
reachable from the declaration of the explicit specialization.
|
| 6183 |
|
| 6184 |
[*Note 1*: A declaration, but not a definition of the template is
|
| 6185 |
+
needed. — *end note*]
|
| 6186 |
|
| 6187 |
The definition of a class or class template shall be reachable from the
|
| 6188 |
declaration of an explicit specialization for a member template of the
|
| 6189 |
class or class template.
|
| 6190 |
|
|
|
|
| 6330 |
positioning of the explicit specialization declarations and their points
|
| 6331 |
of instantiation in the translation unit as specified above and below.
|
| 6332 |
When writing a specialization, be careful about its location; or to make
|
| 6333 |
it compile will be such a trial as to kindle its self-immolation.
|
| 6334 |
|
| 6335 |
+
[*Note 2*:
|
| 6336 |
+
|
| 6337 |
+
A class template explicit specialization that has been declared but not
|
| 6338 |
+
defined can be used exactly like other incompletely-defined classes
|
| 6339 |
[[basic.types]].
|
| 6340 |
|
| 6341 |
[*Example 5*:
|
| 6342 |
|
| 6343 |
``` cpp
|
|
|
|
| 6348 |
X<int> x; // error: object of incomplete class X<int>
|
| 6349 |
```
|
| 6350 |
|
| 6351 |
— *end example*]
|
| 6352 |
|
| 6353 |
+
— *end note*]
|
| 6354 |
+
|
| 6355 |
+
[*Note 3*: An explicit specialization of a constrained template needs
|
| 6356 |
+
to satisfy that template’s associated constraints [[temp.constr.decl]].
|
| 6357 |
+
The satisfaction of constraints is determined when forming the template
|
| 6358 |
+
name of an explicit specialization in which all template arguments are
|
| 6359 |
+
specified [[temp.names]], or, for explicit specializations of function
|
| 6360 |
+
templates, during template argument deduction [[temp.deduct.decl]] when
|
| 6361 |
+
one or more trailing template arguments are left
|
| 6362 |
+
unspecified. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6363 |
|
| 6364 |
A function with the same name as a template and a type that exactly
|
| 6365 |
matches that of a template specialization is not an explicit
|
| 6366 |
specialization [[temp.fct]].
|
| 6367 |
|
| 6368 |
Whether an explicit specialization of a function or variable template is
|
| 6369 |
inline, constexpr, constinit, or consteval is determined by the explicit
|
| 6370 |
specialization and is independent of those properties of the template.
|
| 6371 |
+
Similarly, attributes and *function-contract-specifier*s appearing in
|
| 6372 |
+
the declaration of a template have no effect on an explicit
|
| 6373 |
+
specialization of that template.
|
| 6374 |
|
| 6375 |
+
[*Example 6*:
|
| 6376 |
|
| 6377 |
``` cpp
|
| 6378 |
template<class T> void f(T) { ... }
|
| 6379 |
template<class T> inline T g(T) { ... }
|
| 6380 |
|
|
|
|
| 6393 |
An explicit specialization of a static data member of a template or an
|
| 6394 |
explicit specialization of a static data member template is a definition
|
| 6395 |
if the declaration includes an initializer; otherwise, it is a
|
| 6396 |
declaration.
|
| 6397 |
|
| 6398 |
+
[*Note 4*:
|
| 6399 |
|
| 6400 |
The definition of a static data member of a template for which
|
| 6401 |
default-initialization is desired can use functional cast notation
|
| 6402 |
[[expr.type.conv]]:
|
| 6403 |
|
|
|
|
| 6413 |
specialized for a given implicit instantiation of the class template,
|
| 6414 |
even if the member or member template is defined in the class template
|
| 6415 |
definition. An explicit specialization of a member or member template is
|
| 6416 |
specified using the syntax for explicit specialization.
|
| 6417 |
|
| 6418 |
+
[*Example 7*:
|
| 6419 |
|
| 6420 |
``` cpp
|
| 6421 |
template<class T> struct A {
|
| 6422 |
void f(T);
|
| 6423 |
template<class X1> void g1(T, X1);
|
|
|
|
| 6449 |
A member or a member template may be nested within many enclosing class
|
| 6450 |
templates. In an explicit specialization for such a member, the member
|
| 6451 |
declaration shall be preceded by a `template<>` for each enclosing class
|
| 6452 |
template that is explicitly specialized.
|
| 6453 |
|
| 6454 |
+
[*Example 8*:
|
| 6455 |
|
| 6456 |
``` cpp
|
| 6457 |
template<class T1> class A {
|
| 6458 |
template<class T2> class B {
|
| 6459 |
void mf();
|
|
|
|
| 6475 |
*template-parameter-list* shall be provided instead of the `template<>`
|
| 6476 |
preceding the explicit specialization declaration of the member. The
|
| 6477 |
types of the *template-parameter*s in the *template-parameter-list*
|
| 6478 |
shall be the same as those specified in the primary template definition.
|
| 6479 |
|
| 6480 |
+
[*Example 9*:
|
| 6481 |
|
| 6482 |
``` cpp
|
| 6483 |
template <class T1> class A {
|
| 6484 |
template<class T2> class B {
|
| 6485 |
template<class T3> void mf1(T3);
|
|
|
|
| 6511 |
|
| 6512 |
- the explicit specialization of a function template;
|
| 6513 |
- the explicit specialization of a member function template;
|
| 6514 |
- the explicit specialization of a member function of a class template
|
| 6515 |
where the class template specialization to which the member function
|
| 6516 |
+
specialization belongs is implicitly instantiated. \[*Note 5*: Default
|
| 6517 |
function arguments can be specified in the declaration or definition
|
| 6518 |
of a member function of a class template specialization that is
|
| 6519 |
explicitly specialized. — *end note*]
|
| 6520 |
|
| 6521 |
## Function template specializations <a id="temp.fct.spec">[[temp.fct.spec]]</a>
|
|
|
|
| 6640 |
```
|
| 6641 |
|
| 6642 |
— *end note*]
|
| 6643 |
|
| 6644 |
Template arguments that are present shall be specified in the
|
| 6645 |
+
declaration order of their corresponding template parameters. The
|
| 6646 |
template argument list shall not specify more *template-argument*s than
|
| 6647 |
there are corresponding *template-parameter*s unless one of the
|
| 6648 |
+
*template-parameter*s declares a template parameter pack.
|
| 6649 |
|
| 6650 |
[*Example 3*:
|
| 6651 |
|
| 6652 |
``` cpp
|
| 6653 |
template<class X, class Y, class Z> X f(Y,Z);
|
|
|
|
| 6663 |
|
| 6664 |
— *end example*]
|
| 6665 |
|
| 6666 |
Implicit conversions [[conv]] will be performed on a function argument
|
| 6667 |
to convert it to the type of the corresponding function parameter if the
|
| 6668 |
+
parameter type contains no template parameters that participate in
|
| 6669 |
template argument deduction.
|
| 6670 |
|
| 6671 |
[*Note 3*:
|
| 6672 |
|
| 6673 |
Template parameters do not participate in template argument deduction if
|
|
|
|
| 6737 |
```
|
| 6738 |
|
| 6739 |
— *end example*]
|
| 6740 |
|
| 6741 |
When an explicit template argument list is specified, if the given
|
| 6742 |
+
*template-id* or *splice-specialization-specifier* is not valid
|
| 6743 |
+
[[temp.names]], type deduction fails. Otherwise, the specified template
|
| 6744 |
+
argument values are substituted for the corresponding template
|
| 6745 |
+
parameters as specified below.
|
| 6746 |
|
| 6747 |
After this substitution is performed, the function parameter type
|
| 6748 |
adjustments described in [[dcl.fct]] are performed.
|
| 6749 |
|
| 6750 |
[*Example 2*: A parameter type of “`void (const int, int[5])`” becomes
|
|
|
|
| 6869 |
arguments are substituted.
|
| 6870 |
|
| 6871 |
The *deduction substitution loci* are
|
| 6872 |
|
| 6873 |
- the function type outside of the *noexcept-specifier*,
|
| 6874 |
+
- the *explicit-specifier*,
|
| 6875 |
+
- the template parameter declarations, and
|
| 6876 |
+
- the template argument list of a partial specialization
|
| 6877 |
+
[[temp.spec.partial.general]].
|
| 6878 |
|
| 6879 |
The substitution occurs in all types and expressions that are used in
|
| 6880 |
the deduction substitution loci. The expressions include not only
|
| 6881 |
constant expressions such as those that appear in array bounds or as
|
| 6882 |
+
constant template arguments but also general expressions (i.e.,
|
| 6883 |
non-constant expressions) inside `sizeof`, `decltype`, and other
|
| 6884 |
contexts that allow non-constant expressions. The substitution proceeds
|
| 6885 |
in lexical order and stops when a condition that causes deduction to
|
| 6886 |
fail is encountered. If substitution into different declarations of the
|
| 6887 |
same function template would cause template instantiations to occur in a
|
| 6888 |
different order or not at all, the program is ill-formed; no diagnostic
|
| 6889 |
required.
|
| 6890 |
|
| 6891 |
+
[*Note 4*: The equivalent substitution in exception specifications
|
| 6892 |
+
[[except.spec]] and function contract assertions [[dcl.contract.func]]
|
| 6893 |
+
is done only when the *noexcept-specifier* or
|
| 6894 |
+
*function-contract-specifier*, respectively, is instantiated, at which
|
| 6895 |
+
point a program is ill-formed if the substitution results in an invalid
|
| 6896 |
+
type or expression. — *end note*]
|
| 6897 |
|
| 6898 |
[*Example 6*:
|
| 6899 |
|
| 6900 |
``` cpp
|
| 6901 |
template <class T> struct A { using X = typename T::X; };
|
|
|
|
| 6933 |
and/or function template specializations, the generation of
|
| 6934 |
implicitly-defined functions, etc. Such effects are not in the
|
| 6935 |
“immediate context” and can result in the program being
|
| 6936 |
ill-formed. — *end note*]
|
| 6937 |
|
| 6938 |
+
When substituting into a *lambda-expression*, substitution into its body
|
| 6939 |
+
is not in the immediate context.
|
|
|
|
| 6940 |
|
| 6941 |
[*Note 7*:
|
| 6942 |
|
| 6943 |
The intent is to avoid requiring implementations to deal with
|
| 6944 |
substitution failure involving arbitrary statements.
|
|
|
|
| 7023 |
*qualified-id* when that type does not contain the specified member,
|
| 7024 |
or
|
| 7025 |
- the specified member is not a type where a type is required, or
|
| 7026 |
- the specified member is not a template where a template is required,
|
| 7027 |
or
|
| 7028 |
+
- the specified member is not a non-type, non-template where a
|
| 7029 |
+
non-type, non-template is required.
|
| 7030 |
|
| 7031 |
\[*Example 11*:
|
| 7032 |
``` cpp
|
| 7033 |
template <int I> struct X { };
|
| 7034 |
template <template <class T> class> struct Z { };
|
|
|
|
| 7046 |
|
| 7047 |
int main() {
|
| 7048 |
// Deduction fails in each of these cases:
|
| 7049 |
f<A>(0); // A does not contain a member Y
|
| 7050 |
f<B>(0); // The Y member of B is not a type
|
| 7051 |
+
g<C>(0); // The N member of C is not a non-type, non-template name
|
| 7052 |
h<D>(0); // The TT member of D is not a template
|
| 7053 |
}
|
| 7054 |
```
|
| 7055 |
|
| 7056 |
— *end example*]
|
|
|
|
| 7063 |
template <class T> int f(int T::*);
|
| 7064 |
int i = f<int>(0);
|
| 7065 |
```
|
| 7066 |
|
| 7067 |
— *end example*]
|
| 7068 |
+
- Attempting to give an invalid type to a constant template parameter.
|
| 7069 |
\[*Example 13*:
|
| 7070 |
``` cpp
|
| 7071 |
template <class T, T> struct S {};
|
| 7072 |
template <class T> int f(S<T, T{}>*); // #1
|
| 7073 |
class X {
|
| 7074 |
int m;
|
| 7075 |
};
|
| 7076 |
+
int i0 = f<X>(0); // #1 uses a value of non-structural type X as a constant template argument
|
| 7077 |
```
|
| 7078 |
|
| 7079 |
— *end example*]
|
| 7080 |
- Attempting to perform an invalid conversion in either a template
|
| 7081 |
argument expression, or an expression used in the function
|
|
|
|
| 7088 |
|
| 7089 |
— *end example*]
|
| 7090 |
- Attempting to create a function type in which a parameter has a type
|
| 7091 |
of `void`, or in which the return type is a function type or array
|
| 7092 |
type.
|
| 7093 |
+
- Attempting to give to an explicit object parameter of a lambda’s
|
| 7094 |
+
function call operator a type not permitted for such
|
| 7095 |
+
[[expr.prim.lambda.closure]].
|
| 7096 |
|
| 7097 |
— *end note*]
|
| 7098 |
|
| 7099 |
[*Example 15*:
|
| 7100 |
|
|
|
|
| 7114 |
— *end example*]
|
| 7115 |
|
| 7116 |
#### Deducing template arguments from a function call <a id="temp.deduct.call">[[temp.deduct.call]]</a>
|
| 7117 |
|
| 7118 |
Template argument deduction is done by comparing each function template
|
| 7119 |
+
parameter type (call it `P`) that contains template parameters that
|
| 7120 |
participate in template argument deduction with the type of the
|
| 7121 |
corresponding argument of the call (call it `A`) as described below. If
|
| 7122 |
removing references and cv-qualifiers from `P` gives
|
| 7123 |
+
$\tcode{std::initializer_list<P}^{\prime}\tcode{>}$ or `P`'`[N]` for
|
| 7124 |
+
some `P`' and `N` and the argument is a non-empty initializer list
|
| 7125 |
+
[[dcl.init.list]], then deduction is performed instead for each element
|
| 7126 |
+
of the initializer list independently, taking `P`' as separate function
|
| 7127 |
+
template parameter types `P`'_i and the iᵗʰ initializer element as the
|
| 7128 |
+
corresponding argument. In the `P`'`[N]` case, if `N` is a constant
|
| 7129 |
+
template parameter, `N` is deduced from the length of the initializer
|
| 7130 |
+
list. Otherwise, an initializer list argument causes the parameter to be
|
| 7131 |
+
considered a non-deduced context [[temp.deduct.type]].
|
| 7132 |
|
| 7133 |
[*Example 1*:
|
| 7134 |
|
| 7135 |
``` cpp
|
| 7136 |
template<class T> void f(std::initializer_list<T>);
|
|
|
|
| 7268 |
transformed `A`.
|
| 7269 |
- The transformed `A` can be another pointer or pointer-to-member type
|
| 7270 |
that can be converted to the deduced `A` via a function pointer
|
| 7271 |
conversion [[conv.fctptr]] and/or qualification conversion
|
| 7272 |
[[conv.qual]].
|
| 7273 |
+
- If `P` is a class and `P` has the form *simple-template-id* or
|
| 7274 |
+
`typename`ₒₚₜ *splice-specialization-specifier*, then the transformed
|
| 7275 |
+
`A` can be a derived class `D` of the deduced `A`. Likewise, if `P` is
|
| 7276 |
+
a pointer to a class of the form *simple-template-id* or
|
| 7277 |
+
`typename`ₒₚₜ *splice-specialization-specifier*, the transformed `A`
|
| 7278 |
+
can be a pointer to a derived class `D` of the class pointed to by the
|
| 7279 |
+
deduced `A`. However, if there is a class `C` that is a (direct or
|
| 7280 |
+
indirect) base class of `D` and derived (directly or indirectly) from
|
| 7281 |
+
a class `B` and that would be a valid deduced `A`, the deduced `A`
|
| 7282 |
+
cannot be `B` or pointer to `B`, respectively.
|
| 7283 |
\[*Example 5*:
|
| 7284 |
``` cpp
|
| 7285 |
template <typename... T> struct X;
|
| 7286 |
template <> struct X<> {};
|
| 7287 |
template <typename T, typename... Ts>
|
|
|
|
| 7300 |
|
| 7301 |
These alternatives are considered only if type deduction would otherwise
|
| 7302 |
fail. If they yield more than one possible deduced `A`, the type
|
| 7303 |
deduction fails.
|
| 7304 |
|
| 7305 |
+
[*Note 1*: If a template parameter is not used in any of the function
|
| 7306 |
parameters of a function template, or is used only in a non-deduced
|
| 7307 |
context, its corresponding *template-argument* cannot be deduced from a
|
| 7308 |
function call and the *template-argument* must be explicitly
|
| 7309 |
specified. — *end note*]
|
| 7310 |
|
|
|
|
| 7313 |
|
| 7314 |
- If the argument is an overload set containing one or more function
|
| 7315 |
templates, the parameter is treated as a non-deduced context.
|
| 7316 |
- If the argument is an overload set (not containing function
|
| 7317 |
templates), trial argument deduction is attempted using each of the
|
| 7318 |
+
members of the set whose associated constraints [[temp.constr.constr]]
|
| 7319 |
+
are satisfied. If all successful deductions yield the same deduced
|
| 7320 |
+
`A`, that deduced `A` is the result of deduction; otherwise, the
|
| 7321 |
+
parameter is treated as a non-deduced context.
|
| 7322 |
|
| 7323 |
[*Example 6*:
|
| 7324 |
|
| 7325 |
``` cpp
|
| 7326 |
// Only one function of an overload set matches the call so the function parameter is a deduced context.
|
|
|
|
| 7354 |
int i = f(1, g); // calls f(int, int (*)(int))
|
| 7355 |
```
|
| 7356 |
|
| 7357 |
— *end example*]
|
| 7358 |
|
| 7359 |
+
[*Example 9*:
|
| 7360 |
+
|
| 7361 |
+
``` cpp
|
| 7362 |
+
// All arguments for placeholder type deduction[dcl.type.auto.deduct] yield the same deduced type.
|
| 7363 |
+
template<bool B> struct X {
|
| 7364 |
+
static void f(short) requires B; // #1
|
| 7365 |
+
static void f(short); // #2
|
| 7366 |
+
};
|
| 7367 |
+
void test() {
|
| 7368 |
+
auto x = &X<true>::f; // OK, deduces void(*)(short), selects #1
|
| 7369 |
+
auto y = &X<false>::f; // OK, deduces void(*)(short), selects #2
|
| 7370 |
+
}
|
| 7371 |
+
```
|
| 7372 |
+
|
| 7373 |
+
— *end example*]
|
| 7374 |
+
|
| 7375 |
#### Deducing template arguments taking the address of a function template <a id="temp.deduct.funcaddr">[[temp.deduct.funcaddr]]</a>
|
| 7376 |
|
| 7377 |
Template arguments can be deduced from the type specified when taking
|
| 7378 |
the address of an overload set [[over.over]]. If there is a target, the
|
| 7379 |
function template’s function type and the target type are used as the
|
|
|
|
| 7453 |
|
| 7454 |
The types used to determine the ordering depend on the context in which
|
| 7455 |
the partial ordering is done:
|
| 7456 |
|
| 7457 |
- In the context of a function call, the types used are those function
|
| 7458 |
+
parameter types for which the function call has arguments.[^12]
|
| 7459 |
- In the context of a call to a conversion function, the return types of
|
| 7460 |
the conversion function templates are used.
|
| 7461 |
- In other contexts [[temp.func.order]] the function template’s function
|
| 7462 |
type is used.
|
| 7463 |
|
|
|
|
| 7580 |
|
| 7581 |
Template arguments can be deduced in several different contexts, but in
|
| 7582 |
each case a type that is specified in terms of template parameters (call
|
| 7583 |
it `P`) is compared with an actual type (call it `A`), and an attempt is
|
| 7584 |
made to find template argument values (a type for a type parameter, a
|
| 7585 |
+
value for a constant template parameter, or a template for a template
|
| 7586 |
+
template parameter) that will make `P`, after substitution of the
|
| 7587 |
+
deduced values (call it the deduced `A`), compatible with `A`.
|
| 7588 |
|
| 7589 |
In some cases, the deduction is done using a single set of types `P` and
|
| 7590 |
`A`, in other cases, there will be a set of corresponding types `P` and
|
| 7591 |
`A`. Type deduction is done independently for each `P/A` pair, and the
|
| 7592 |
deduced template argument values are then combined. If type deduction
|
|
|
|
| 7596 |
remains neither deduced nor explicitly specified, template argument
|
| 7597 |
deduction fails. The type of a type parameter is only deduced from an
|
| 7598 |
array bound if it is not otherwise deduced.
|
| 7599 |
|
| 7600 |
A given type `P` can be composed from a number of other types,
|
| 7601 |
+
templates, and constant template argument values:
|
| 7602 |
|
| 7603 |
- A function type includes the types of each of the function parameters,
|
| 7604 |
the return type, and its exception specification.
|
| 7605 |
- A pointer-to-member type includes the type of the class object pointed
|
| 7606 |
to and the type of the member pointed to.
|
| 7607 |
- A type that is a specialization of a class template (e.g., `A<int>`)
|
| 7608 |
+
includes the types, templates, and constant template argument values
|
| 7609 |
+
referenced by the template argument list of the specialization.
|
| 7610 |
- An array type includes the array element type and the value of the
|
| 7611 |
array bound.
|
| 7612 |
|
| 7613 |
+
In most cases, the types, templates, and constant template argument
|
| 7614 |
+
values that are used to compose `P` participate in template argument
|
| 7615 |
+
deduction. That is, they may be used to determine the value of a
|
| 7616 |
+
template argument, and template argument deduction fails if the value so
|
| 7617 |
+
determined is not consistent with the values determined elsewhere. In
|
| 7618 |
+
certain contexts, however, the value does not participate in type
|
| 7619 |
+
deduction, but instead uses the values of template arguments that were
|
| 7620 |
+
either deduced elsewhere or explicitly specified. If a template
|
| 7621 |
+
parameter is used only in non-deduced contexts and is not explicitly
|
| 7622 |
+
specified, template argument deduction fails.
|
| 7623 |
|
| 7624 |
+
[*Note 1*: Under [[temp.deduct.call]], if `P` contains no template
|
| 7625 |
+
parameters that appear in deduced contexts, no deduction is done, so `P`
|
| 7626 |
+
and `A` need not have the same form. — *end note*]
|
| 7627 |
|
| 7628 |
The non-deduced contexts are:
|
| 7629 |
|
| 7630 |
- The *nested-name-specifier* of a type that was specified using a
|
| 7631 |
*qualified-id*.
|
| 7632 |
+
- A *pack-index-specifier* or a *pack-index-expression*.
|
| 7633 |
+
- A *type-constraint*.
|
| 7634 |
- The *expression* of a *decltype-specifier*.
|
| 7635 |
+
- The *constant-expression* of a *splice-specifier*.
|
| 7636 |
+
- A constant template argument or an array bound in which a
|
| 7637 |
subexpression references a template parameter.
|
| 7638 |
- A template parameter used in the parameter type of a function
|
| 7639 |
parameter that has a default argument that is being used in the call
|
| 7640 |
for which argument deduction is being done.
|
| 7641 |
- A function parameter for which the associated argument is an overload
|
| 7642 |
+
set such that one or more of the following apply:
|
| 7643 |
+
- functions whose associated constraints are satisfied and that do not
|
| 7644 |
+
all have the same function type match the function parameter type
|
| 7645 |
(resulting in an ambiguous deduction), or
|
| 7646 |
+
- no function whose associated constraints are satisfied matches the
|
| 7647 |
+
function parameter type, or
|
| 7648 |
- the overload set supplied as an argument contains one or more
|
| 7649 |
function templates.
|
| 7650 |
- A function parameter for which the associated argument is an
|
| 7651 |
initializer list [[dcl.init.list]] but the parameter does not have a
|
| 7652 |
type for which deduction from an initializer list is specified
|
|
|
|
| 7755 |
}
|
| 7756 |
```
|
| 7757 |
|
| 7758 |
— *end example*]
|
| 7759 |
|
| 7760 |
+
A type template argument `T`, a constant template argument `i`, a
|
| 7761 |
+
template template argument `TT` denoting a class template or an alias
|
| 7762 |
+
template, or a template template argument `VV` denoting a variable
|
| 7763 |
+
template or a concept can be deduced if `P` and `A` have one of the
|
| 7764 |
+
following forms:
|
| 7765 |
|
| 7766 |
``` cpp
|
| 7767 |
\opt{cv} T
|
| 7768 |
T*
|
| 7769 |
T&
|
|
|
|
| 7772 |
\opt{T}(\opt{T}) noexcept(\opt{i})
|
| 7773 |
\opt{T} \opt{T}::*
|
| 7774 |
\opt{TT}<T>
|
| 7775 |
\opt{TT}<i>
|
| 7776 |
\opt{TT}<TT>
|
| 7777 |
+
\opt{TT}<VV>
|
| 7778 |
\opt{TT}<>
|
| 7779 |
```
|
| 7780 |
|
| 7781 |
where
|
| 7782 |
|
| 7783 |
- `\opt{T}` represents a type or parameter-type-list that either
|
| 7784 |
satisfies these rules recursively, is a non-deduced context in `P` or
|
| 7785 |
`A`, or is the same non-dependent type in `P` and `A`,
|
|
|
|
|
|
|
| 7786 |
- `\opt{i}` represents an expression that either is an `i`, is
|
| 7787 |
value-dependent in `P` or `A`, or has the same constant value in `P`
|
| 7788 |
+
and `A`,
|
| 7789 |
+
- `\opt{TT}` represents either a class template or a template template
|
| 7790 |
+
parameter, and
|
| 7791 |
- `noexcept(\opt{i})` represents an exception specification
|
| 7792 |
[[except.spec]] in which the (possibly-implicit, see [[dcl.fct]])
|
| 7793 |
*noexcept-specifier*’s operand satisfies the rules for an `\opt{i}`
|
| 7794 |
above.
|
| 7795 |
|
| 7796 |
[*Note 2*: If a type matches such a form but contains no `T`s, `i`s, or
|
| 7797 |
`TT`s, deduction is not possible. — *end note*]
|
| 7798 |
|
| 7799 |
+
Similarly, `<X>` represents template argument lists where at least one
|
| 7800 |
+
argument contains an X, where X is one of `T`, `i`, `TT`, or `VV`; and
|
| 7801 |
+
`<>` represents template argument lists where no argument contains a
|
| 7802 |
+
`T`, an `i`, a `TT`, or a `VV`.
|
| 7803 |
|
| 7804 |
+
If `P` has a form that contains `<T>`, `<i>`, `<TT>`, or `<VV>`, then
|
| 7805 |
+
each argument Pᵢ of the respective template argument list of `P` is
|
| 7806 |
+
compared with the corresponding argument Aᵢ of the corresponding
|
| 7807 |
+
template argument list of `A`. If the template argument list of `P`
|
| 7808 |
+
contains a pack expansion that is not the last template argument, the
|
| 7809 |
+
entire template argument list is a non-deduced context. If `Pᵢ` is a
|
| 7810 |
+
pack expansion, then the pattern of `Pᵢ` is compared with each remaining
|
| 7811 |
+
argument in the template argument list of `A`. Each comparison deduces
|
| 7812 |
+
template arguments for subsequent positions in the template parameter
|
| 7813 |
+
packs expanded by `Pᵢ`. During partial ordering [[temp.deduct.partial]],
|
| 7814 |
+
if `Aᵢ` was originally a pack expansion:
|
| 7815 |
|
| 7816 |
- if `P` does not contain a template argument corresponding to `Aᵢ` then
|
| 7817 |
`Aᵢ` is ignored;
|
| 7818 |
- otherwise, if `Pᵢ` is not a pack expansion, template argument
|
| 7819 |
deduction fails.
|
|
|
|
| 7914 |
— *end example*]
|
| 7915 |
|
| 7916 |
Template arguments cannot be deduced from function arguments involving
|
| 7917 |
constructs other than the ones specified above.
|
| 7918 |
|
| 7919 |
+
When the value of the argument corresponding to a constant template
|
| 7920 |
parameter `P` that is declared with a dependent type is deduced from an
|
| 7921 |
expression, the template parameters in the type of `P` are deduced from
|
| 7922 |
the type of the value.
|
| 7923 |
|
| 7924 |
[*Example 8*:
|
|
|
|
| 8005 |
|
| 8006 |
— *end note*]
|
| 8007 |
|
| 8008 |
[*Note 5*:
|
| 8009 |
|
| 8010 |
+
If, in the declaration of a function template with a constant template
|
| 8011 |
+
parameter, the constant template parameter is used in a subexpression in
|
| 8012 |
the function parameter list, the expression is a non-deduced context as
|
| 8013 |
specified above.
|
| 8014 |
|
| 8015 |
[*Example 12*:
|
| 8016 |
|
|
|
|
| 8051 |
|
| 8052 |
— *end note*]
|
| 8053 |
|
| 8054 |
If `P` has a form that contains `<i>`, and if the type of `i` differs
|
| 8055 |
from the type of the corresponding template parameter of the template
|
| 8056 |
+
named by the enclosing *simple-template-id* or
|
| 8057 |
+
*splice-specialization-specifier*, deduction fails. If `P` has a form
|
| 8058 |
+
that contains `[i]`, and if the type of `i` is not an integral type,
|
| 8059 |
+
deduction fails.[^13]
|
| 8060 |
|
| 8061 |
If `P` has a form that includes `noexcept(i)` and the type of `i` is not
|
| 8062 |
`bool`, deduction fails.
|
| 8063 |
|
| 8064 |
[*Example 13*:
|
|
|
|
| 8118 |
}
|
| 8119 |
```
|
| 8120 |
|
| 8121 |
— *end example*]
|
| 8122 |
|
| 8123 |
+
The *template-argument* corresponding to a template template parameter
|
| 8124 |
is deduced from the type of the *template-argument* of a class template
|
| 8125 |
specialization used in the argument list of a function call.
|
| 8126 |
|
| 8127 |
[*Example 16*:
|
| 8128 |
|
|
|
|
| 8190 |
[[temp.deduct]] and checking of any explicit template arguments
|
| 8191 |
[[temp.arg]] are performed for each function template to find the
|
| 8192 |
template argument values (if any) that can be used with that function
|
| 8193 |
template to instantiate a function template specialization that can be
|
| 8194 |
invoked with the call arguments or, for conversion function templates,
|
| 8195 |
+
that can convert to the required type. For each function template:
|
| 8196 |
+
|
| 8197 |
+
- If the argument deduction and checking succeeds, the
|
| 8198 |
+
*template-argument*s (deduced and/or explicit) are used to synthesize
|
| 8199 |
+
the declaration of a single function template specialization which is
|
| 8200 |
+
added to the candidate functions set to be used in overload
|
| 8201 |
+
resolution.
|
| 8202 |
+
- If the argument deduction fails or the synthesized function template
|
| 8203 |
+
specialization would be ill-formed, no such function is added to the
|
| 8204 |
+
set of candidate functions for that template.
|
| 8205 |
+
|
| 8206 |
+
The complete set of candidate functions includes all the synthesized
|
| 8207 |
+
declarations and all of the non-template functions found by name lookup.
|
| 8208 |
+
The synthesized declarations are treated like any other functions in the
|
| 8209 |
+
remainder of overload resolution, except as explicitly noted in
|
| 8210 |
+
[[over.match.best]].[^14]
|
| 8211 |
|
| 8212 |
[*Example 1*:
|
| 8213 |
|
| 8214 |
``` cpp
|
| 8215 |
template<class T> T max(T a, T b) { return a>b?a:b; }
|
|
|
|
| 8234 |
— *end example*]
|
| 8235 |
|
| 8236 |
[*Example 2*:
|
| 8237 |
|
| 8238 |
Here is an example involving conversions on a function argument involved
|
| 8239 |
+
in template argument deduction:
|
| 8240 |
|
| 8241 |
``` cpp
|
| 8242 |
template<class T> struct B { ... };
|
| 8243 |
template<class T> struct D : public B<T> { ... };
|
| 8244 |
template<class T> void f(B<T>&);
|
|
|
|
| 8252 |
— *end example*]
|
| 8253 |
|
| 8254 |
[*Example 3*:
|
| 8255 |
|
| 8256 |
Here is an example involving conversions on a function argument not
|
| 8257 |
+
involved in template argument deduction:
|
| 8258 |
|
| 8259 |
``` cpp
|
| 8260 |
template<class T> void f(T*,int); // #1
|
| 8261 |
template<class T> void f(T,char); // #2
|
| 8262 |
|
|
|
|
| 8300 |
[basic.lookup]: basic.md#basic.lookup
|
| 8301 |
[basic.lookup.argdep]: basic.md#basic.lookup.argdep
|
| 8302 |
[basic.lookup.qual]: basic.md#basic.lookup.qual
|
| 8303 |
[basic.scope.namespace]: basic.md#basic.scope.namespace
|
| 8304 |
[basic.scope.scope]: basic.md#basic.scope.scope
|
| 8305 |
+
[basic.splice]: basic.md#basic.splice
|
| 8306 |
[basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
|
| 8307 |
[basic.types]: basic.md#basic.types
|
| 8308 |
[class.access]: class.md#class.access
|
| 8309 |
[class.base.init]: class.md#class.base.init
|
| 8310 |
[class.conv.fct]: class.md#class.conv.fct
|
|
|
|
| 8312 |
[class.default.ctor]: class.md#class.default.ctor
|
| 8313 |
[class.derived]: class.md#class.derived
|
| 8314 |
[class.dtor]: class.md#class.dtor
|
| 8315 |
[class.local]: class.md#class.local
|
| 8316 |
[class.mem]: class.md#class.mem
|
| 8317 |
+
[class.mem.general]: class.md#class.mem.general
|
| 8318 |
[class.member.lookup]: basic.md#class.member.lookup
|
| 8319 |
[class.pre]: class.md#class.pre
|
| 8320 |
[class.qual]: basic.md#class.qual
|
| 8321 |
[class.temporary]: basic.md#class.temporary
|
| 8322 |
[conv]: expr.md#conv
|
|
|
|
| 8325 |
[conv.func]: expr.md#conv.func
|
| 8326 |
[conv.lval]: expr.md#conv.lval
|
| 8327 |
[conv.qual]: expr.md#conv.qual
|
| 8328 |
[dcl.align]: dcl.md#dcl.align
|
| 8329 |
[dcl.attr.grammar]: dcl.md#dcl.attr.grammar
|
| 8330 |
+
[dcl.constexpr]: dcl.md#dcl.constexpr
|
| 8331 |
+
[dcl.contract.func]: dcl.md#dcl.contract.func
|
| 8332 |
+
[dcl.contract.res]: dcl.md#dcl.contract.res
|
| 8333 |
[dcl.decl]: dcl.md#dcl.decl
|
| 8334 |
[dcl.fct]: dcl.md#dcl.fct
|
| 8335 |
[dcl.fct.def.general]: dcl.md#dcl.fct.def.general
|
| 8336 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 8337 |
[dcl.init]: dcl.md#dcl.init
|
| 8338 |
[dcl.init.list]: dcl.md#dcl.init.list
|
| 8339 |
[dcl.meaning]: dcl.md#dcl.meaning
|
| 8340 |
[dcl.pre]: dcl.md#dcl.pre
|
| 8341 |
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
| 8342 |
+
[dcl.spec.auto.general]: dcl.md#dcl.spec.auto.general
|
| 8343 |
[dcl.stc]: dcl.md#dcl.stc
|
| 8344 |
[dcl.struct.bind]: dcl.md#dcl.struct.bind
|
| 8345 |
[dcl.type.class.deduct]: dcl.md#dcl.type.class.deduct
|
| 8346 |
[dcl.type.elab]: dcl.md#dcl.type.elab
|
| 8347 |
[dcl.type.simple]: dcl.md#dcl.type.simple
|
| 8348 |
[depr.template.template]: future.md#depr.template.template
|
| 8349 |
[except.spec]: except.md#except.spec
|
| 8350 |
[expr.const]: expr.md#expr.const
|
| 8351 |
[expr.context]: expr.md#expr.context
|
| 8352 |
+
[expr.eq]: expr.md#expr.eq
|
| 8353 |
[expr.log.and]: expr.md#expr.log.and
|
| 8354 |
[expr.log.or]: expr.md#expr.log.or
|
| 8355 |
[expr.new]: expr.md#expr.new
|
| 8356 |
[expr.prim.fold]: expr.md#expr.prim.fold
|
| 8357 |
[expr.prim.id]: expr.md#expr.prim.id
|
|
|
|
| 8362 |
[expr.prim.this]: expr.md#expr.prim.this
|
| 8363 |
[expr.ref]: expr.md#expr.ref
|
| 8364 |
[expr.sizeof]: expr.md#expr.sizeof
|
| 8365 |
[expr.type.conv]: expr.md#expr.type.conv
|
| 8366 |
[expr.typeid]: expr.md#expr.typeid
|
| 8367 |
+
[expr.unary.noexcept]: expr.md#expr.unary.noexcept
|
| 8368 |
[expr.unary.op]: expr.md#expr.unary.op
|
| 8369 |
[implimits]: limits.md#implimits
|
| 8370 |
[intro.defs]: intro.md#intro.defs
|
| 8371 |
+
[intro.execution]: basic.md#intro.execution
|
| 8372 |
[intro.object]: basic.md#intro.object
|
| 8373 |
[lex.string]: lex.md#lex.string
|
| 8374 |
[module.unit]: module.md#module.unit
|
| 8375 |
[namespace.udecl]: dcl.md#namespace.udecl
|
| 8376 |
[over.match]: over.md#over.match
|
|
|
|
| 8379 |
[over.match.funcs]: over.md#over.match.funcs
|
| 8380 |
[over.match.oper]: over.md#over.match.oper
|
| 8381 |
[over.match.viable]: over.md#over.match.viable
|
| 8382 |
[over.over]: over.md#over.over
|
| 8383 |
[special]: class.md#special
|
| 8384 |
+
[stmt.expand]: stmt.md#stmt.expand
|
| 8385 |
[stmt.if]: stmt.md#stmt.if
|
| 8386 |
[support.types]: support.md#support.types
|
| 8387 |
[temp]: #temp
|
| 8388 |
[temp.alias]: #temp.alias
|
| 8389 |
[temp.arg]: #temp.arg
|
|
|
|
| 8395 |
[temp.class]: #temp.class
|
| 8396 |
[temp.class.general]: #temp.class.general
|
| 8397 |
[temp.concept]: #temp.concept
|
| 8398 |
[temp.constr]: #temp.constr
|
| 8399 |
[temp.constr.atomic]: #temp.constr.atomic
|
| 8400 |
+
[temp.constr.concept]: #temp.constr.concept
|
| 8401 |
[temp.constr.constr]: #temp.constr.constr
|
| 8402 |
[temp.constr.constr.general]: #temp.constr.constr.general
|
| 8403 |
[temp.constr.decl]: #temp.constr.decl
|
| 8404 |
+
[temp.constr.fold]: #temp.constr.fold
|
| 8405 |
[temp.constr.general]: #temp.constr.general
|
| 8406 |
[temp.constr.normal]: #temp.constr.normal
|
| 8407 |
[temp.constr.op]: #temp.constr.op
|
| 8408 |
[temp.constr.order]: #temp.constr.order
|
| 8409 |
[temp.decls]: #temp.decls
|
|
|
|
| 8420 |
[temp.dep]: #temp.dep
|
| 8421 |
[temp.dep.candidate]: #temp.dep.candidate
|
| 8422 |
[temp.dep.constexpr]: #temp.dep.constexpr
|
| 8423 |
[temp.dep.expr]: #temp.dep.expr
|
| 8424 |
[temp.dep.general]: #temp.dep.general
|
| 8425 |
+
[temp.dep.namespace]: #temp.dep.namespace
|
| 8426 |
[temp.dep.res]: #temp.dep.res
|
| 8427 |
+
[temp.dep.splice]: #temp.dep.splice
|
| 8428 |
[temp.dep.temp]: #temp.dep.temp
|
| 8429 |
[temp.dep.type]: #temp.dep.type
|
| 8430 |
[temp.expl.spec]: #temp.expl.spec
|
| 8431 |
[temp.explicit]: #temp.explicit
|
| 8432 |
[temp.fct]: #temp.fct
|
|
|
|
| 8461 |
[temp.type]: #temp.type
|
| 8462 |
[temp.variadic]: #temp.variadic
|
| 8463 |
[term.incomplete.type]: basic.md#term.incomplete.type
|
| 8464 |
[term.odr.use]: basic.md#term.odr.use
|
| 8465 |
|
| 8466 |
+
[^1]: A `>` that encloses the *type-id* of a `dynamic_cast`,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8467 |
`static_cast`, `reinterpret_cast` or `const_cast`, or which encloses
|
| 8468 |
+
the *template-argument*s of a subsequent *template-id* or
|
| 8469 |
+
*splice-specialization-specifier*, is considered nested for the
|
| 8470 |
+
purpose of this description.
|
| 8471 |
|
| 8472 |
+
[^2]: There is no such ambiguity in a default *template-argument*
|
| 8473 |
because the form of the *template-parameter* determines the
|
| 8474 |
allowable forms of the *template-argument*.
|
| 8475 |
|
| 8476 |
+
[^3]: A constraint is in disjunctive normal form when it is a
|
| 8477 |
+
disjunction of clauses where each clause is a conjunction of fold
|
| 8478 |
+
expanded or atomic constraints. For atomic constraints A, B, and C,
|
| 8479 |
+
the disjunctive normal form of the constraint A ∧ (B ∨ C) is
|
| 8480 |
+
(A ∧ B) ∨ (A ∧ C). Its disjunctive clauses are (A ∧ B) and (A ∧ C).
|
| 8481 |
|
| 8482 |
+
[^4]: A constraint is in conjunctive normal form when it is a
|
| 8483 |
+
conjunction of clauses where each clause is a disjunction of fold
|
| 8484 |
+
expanded or atomic constraints. For atomic constraints A, B, and C,
|
| 8485 |
+
the constraint A ∧ (B ∨ C) is in conjunctive normal form. Its
|
| 8486 |
+
conjunctive clauses are A and (B ∨ C).
|
| 8487 |
|
| 8488 |
+
[^5]: The identity of enumerators is not preserved.
|
| 8489 |
|
| 8490 |
+
[^6]: An array as a *template-parameter* decays to a pointer.
|
| 8491 |
|
| 8492 |
+
[^7]: There is no context in which they would be used.
|
| 8493 |
|
| 8494 |
+
[^8]: That is, declarations of non-template functions do not merely
|
| 8495 |
guide overload resolution of function template specializations with
|
| 8496 |
the same name. If such a non-template function is odr-used
|
| 8497 |
[[term.odr.use]] in a program, it must be defined; it will not be
|
| 8498 |
implicitly instantiated using the function template definition.
|
| 8499 |
|
| 8500 |
+
[^9]: This includes friend function declarations.
|
| 8501 |
|
| 8502 |
+
[^10]: Every instantiation of a class template declares a different set
|
| 8503 |
of assignment operators.
|
| 8504 |
|
| 8505 |
+
[^11]: This includes an injected-class-name [[class.pre]] of a class
|
| 8506 |
template used without a *template-argument-list*.
|
| 8507 |
|
| 8508 |
+
[^12]: Default arguments are not considered to be arguments in this
|
| 8509 |
context; they only become arguments after a function has been
|
| 8510 |
selected.
|
| 8511 |
|
| 8512 |
+
[^13]: Although the *template-argument* corresponding to a template
|
| 8513 |
+
parameter of type `bool` can be deduced from an array bound, the
|
| 8514 |
+
resulting value will always be `true` because the array bound will
|
| 8515 |
+
be nonzero.
|
| 8516 |
|
| 8517 |
+
[^14]: The parameters of function template specializations contain no
|
| 8518 |
template parameter types. The set of conversions allowed on deduced
|
| 8519 |
arguments is limited, because the argument deduction process
|
| 8520 |
produces function templates with parameters that either match the
|
| 8521 |
call arguments exactly or differ only in ways that can be bridged by
|
| 8522 |
the allowed limited conversions. Non-deduced arguments allow the
|