- tmp/tmpqltf1yce/{from.md → to.md} +124 -61
tmp/tmpqltf1yce/{from.md → to.md}
RENAMED
|
@@ -20,11 +20,13 @@ class templates are considered definitions; each default argument,
|
|
| 20 |
*type-constraint*, *requires-clause*, or *noexcept-specifier* is a
|
| 21 |
separate definition which is unrelated to the templated function
|
| 22 |
definition or to any other default arguments, *type-constraint*s,
|
| 23 |
*requires-clause*s, or *noexcept-specifier*s. For the purpose of
|
| 24 |
instantiation, the substatements of a constexpr if statement [[stmt.if]]
|
| 25 |
-
are considered definitions.
|
|
|
|
|
|
|
| 26 |
|
| 27 |
Because an *alias-declaration* cannot declare a *template-id*, it is not
|
| 28 |
possible to partially or explicitly specialize an alias template.
|
| 29 |
|
| 30 |
### Class templates <a id="temp.class">[[temp.class]]</a>
|
|
@@ -188,19 +190,20 @@ v2[3] = dcomplex(7,8); // Array<dcomplex>::operator[]
|
|
| 188 |
|
| 189 |
— *end example*]
|
| 190 |
|
| 191 |
#### Deduction guides <a id="temp.deduct.guide">[[temp.deduct.guide]]</a>
|
| 192 |
|
| 193 |
-
Deduction guides are used when a *template-name*
|
| 194 |
-
specifier
|
| 195 |
-
guides are not found by name
|
| 196 |
-
|
| 197 |
-
deduction guides declared for
|
|
|
|
| 198 |
|
| 199 |
``` bnf
|
| 200 |
deduction-guide:
|
| 201 |
-
explicit-specifierₒₚₜ template-name '(' parameter-declaration-clause ')' '->' simple-template-id ';'
|
| 202 |
```
|
| 203 |
|
| 204 |
[*Example 1*:
|
| 205 |
|
| 206 |
``` cpp
|
|
@@ -219,11 +222,13 @@ S x{A()}; // x is of type S<short, int>
|
|
| 219 |
```
|
| 220 |
|
| 221 |
— *end example*]
|
| 222 |
|
| 223 |
The same restrictions apply to the *parameter-declaration-clause* of a
|
| 224 |
-
deduction guide as in a function declaration [[dcl.fct]]
|
|
|
|
|
|
|
| 225 |
*simple-template-id* shall name a class template specialization. The
|
| 226 |
*template-name* shall be the same *identifier* as the *template-name* of
|
| 227 |
the *simple-template-id*. A *deduction-guide* shall inhabit the scope to
|
| 228 |
which the corresponding class template belongs and, for a member class
|
| 229 |
template, have the same access. Two deduction guide declarations for the
|
|
@@ -300,11 +305,10 @@ class template definition.
|
|
| 300 |
|
| 301 |
``` cpp
|
| 302 |
template<class T> struct A {
|
| 303 |
enum E : T;
|
| 304 |
};
|
| 305 |
-
A<int> a;
|
| 306 |
template<class T> enum A<T>::E : T { e1, e2 };
|
| 307 |
A<int>::E e = A<int>::e1;
|
| 308 |
```
|
| 309 |
|
| 310 |
— *end example*]
|
|
@@ -413,13 +417,13 @@ class D : public B {
|
|
| 413 |
|
| 414 |
— *end example*]
|
| 415 |
|
| 416 |
[*Note 1*:
|
| 417 |
|
| 418 |
-
A specialization of a conversion function template is
|
| 419 |
-
|
| 420 |
-
|
| 421 |
|
| 422 |
[*Example 6*:
|
| 423 |
|
| 424 |
``` cpp
|
| 425 |
struct A {
|
|
@@ -436,13 +440,15 @@ int main() {
|
|
| 436 |
}
|
| 437 |
```
|
| 438 |
|
| 439 |
— *end example*]
|
| 440 |
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
|
|
|
|
|
|
| 444 |
|
| 445 |
— *end note*]
|
| 446 |
|
| 447 |
### Variadic templates <a id="temp.variadic">[[temp.variadic]]</a>
|
| 448 |
|
|
@@ -495,15 +501,33 @@ foo(); // xs contains zero init-captures
|
|
| 495 |
foo(1); // xs contains one init-capture
|
| 496 |
```
|
| 497 |
|
| 498 |
— *end example*]
|
| 499 |
|
| 500 |
-
A *pack* is
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 505 |
|
| 506 |
A *pack expansion* consists of a *pattern* and an ellipsis, the
|
| 507 |
instantiation of which produces zero or more instantiations of the
|
| 508 |
pattern in a list (described below). The form of the pattern depends on
|
| 509 |
the context in which the expansion occurs. Pack expansions can occur in
|
|
@@ -511,15 +535,21 @@ the following contexts:
|
|
| 511 |
|
| 512 |
- In a function parameter pack [[dcl.fct]]; the pattern is the
|
| 513 |
*parameter-declaration* without the ellipsis.
|
| 514 |
- In a *using-declaration* [[namespace.udecl]]; the pattern is a
|
| 515 |
*using-declarator*.
|
|
|
|
|
|
|
| 516 |
- In a template parameter pack that is a pack expansion [[temp.param]]:
|
| 517 |
- if the template parameter pack is a *parameter-declaration*; the
|
| 518 |
pattern is the *parameter-declaration* without the ellipsis;
|
| 519 |
- if the template parameter pack is a *type-parameter*; the pattern is
|
| 520 |
-
the corresponding *type-parameter* without the ellipsis
|
|
|
|
|
|
|
|
|
|
|
|
|
| 521 |
- In an *initializer-list* [[dcl.init]]; the pattern is an
|
| 522 |
*initializer-clause*.
|
| 523 |
- In a *base-specifier-list* [[class.derived]]; the pattern is a
|
| 524 |
*base-specifier*.
|
| 525 |
- In a *mem-initializer-list* [[class.base.init]] for a
|
|
@@ -527,20 +557,26 @@ the following contexts:
|
|
| 527 |
pattern is the *mem-initializer*.
|
| 528 |
- In a *template-argument-list* [[temp.arg]]; the pattern is a
|
| 529 |
*template-argument*.
|
| 530 |
- In an *attribute-list* [[dcl.attr.grammar]]; the pattern is an
|
| 531 |
*attribute*.
|
|
|
|
|
|
|
| 532 |
- In an *alignment-specifier* [[dcl.align]]; the pattern is the
|
| 533 |
*alignment-specifier* without the ellipsis.
|
| 534 |
- In a *capture-list* [[expr.prim.lambda.capture]]; the pattern is the
|
| 535 |
*capture* without the ellipsis.
|
| 536 |
- In a `sizeof...` expression [[expr.sizeof]]; the pattern is an
|
| 537 |
*identifier*.
|
|
|
|
|
|
|
| 538 |
- In a *fold-expression* [[expr.prim.fold]]; the pattern is the
|
| 539 |
*cast-expression* that contains an unexpanded pack.
|
|
|
|
|
|
|
| 540 |
|
| 541 |
-
[*Example
|
| 542 |
|
| 543 |
``` cpp
|
| 544 |
template<class ... Types> void f(Types ... rest);
|
| 545 |
template<class ... Types> void g(Types ... rest) {
|
| 546 |
f(&rest ...); // ``&rest ...'' is a pack expansion; ``&rest'' is its pattern
|
|
@@ -560,11 +596,11 @@ a pack expansion shall name one or more packs that are not expanded by a
|
|
| 560 |
nested pack expansion; such packs are called *unexpanded packs* in the
|
| 561 |
pattern. All of the packs expanded by a pack expansion shall have the
|
| 562 |
same number of arguments specified. An appearance of a name of a pack
|
| 563 |
that is not expanded is ill-formed.
|
| 564 |
|
| 565 |
-
[*Example
|
| 566 |
|
| 567 |
``` cpp
|
| 568 |
template<typename...> struct Tuple {};
|
| 569 |
template<typename T1, typename T2> struct Pair {};
|
| 570 |
|
|
@@ -596,33 +632,45 @@ The instantiation of a pack expansion considers items
|
|
| 596 |
expansion parameters. Each `Eᵢ` is generated by instantiating the
|
| 597 |
pattern and replacing each pack expansion parameter with its iᵗʰ
|
| 598 |
element. Such an element, in the context of the instantiation, is
|
| 599 |
interpreted as follows:
|
| 600 |
|
| 601 |
-
- if the pack is a template parameter pack, the element is
|
| 602 |
-
*
|
| 603 |
-
*
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
|
|
|
| 607 |
- if the pack is a function parameter pack, the element is an
|
| 608 |
*id-expression* designating the iᵗʰ function parameter that resulted
|
| 609 |
from instantiation of the function parameter pack declaration;
|
| 610 |
-
otherwise
|
| 611 |
- if the pack is an *init-capture* pack, the element is an
|
| 612 |
*id-expression* designating the variable introduced by the iᵗʰ
|
| 613 |
*init-capture* that resulted from instantiation of the *init-capture*
|
| 614 |
-
pack declaration
|
|
|
|
|
|
|
|
|
|
| 615 |
|
| 616 |
When N is zero, the instantiation of a pack expansion does not alter the
|
| 617 |
syntactic interpretation of the enclosing construct, even in cases where
|
| 618 |
omitting the pack expansion entirely would otherwise be ill-formed or
|
| 619 |
would result in an ambiguity in the grammar.
|
| 620 |
|
| 621 |
The instantiation of a `sizeof...` expression [[expr.sizeof]] produces
|
| 622 |
an integral constant with value N.
|
| 623 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 624 |
The instantiation of a *fold-expression* [[expr.prim.fold]] produces:
|
| 625 |
|
| 626 |
- `(` `((`E₁ *op* E₂`)` *op* ⋯`)` *op* $\mathtt{E}_N$ `)` for a unary
|
| 627 |
left fold,
|
| 628 |
- `(` E₁ *op* `(`⋯ *op* `(`$\mathtt{E}_{N-1}$ *op* $\mathtt{E}_N$`))`
|
|
@@ -634,11 +682,11 @@ The instantiation of a *fold-expression* [[expr.prim.fold]] produces:
|
|
| 634 |
|
| 635 |
In each case, *op* is the *fold-operator*. For a binary fold, E is
|
| 636 |
generated by instantiating the *cast-expression* that did not contain an
|
| 637 |
unexpanded pack.
|
| 638 |
|
| 639 |
-
[*Example
|
| 640 |
|
| 641 |
``` cpp
|
| 642 |
template<typename ...Args>
|
| 643 |
bool all(Args ...args) { return (... && args); }
|
| 644 |
|
|
@@ -661,21 +709,23 @@ If N is zero for a unary fold, the value of the expression is shown in
|
|
| 661 |
| `&&` | `true` |
|
| 662 |
| `||` | `false` |
|
| 663 |
| `,` | `void()` |
|
| 664 |
|
| 665 |
|
|
|
|
|
|
|
| 666 |
The instantiation of any other pack expansion produces a list of
|
| 667 |
elements `E₁`, `E₂`, …, `E_N`.
|
| 668 |
|
| 669 |
[*Note 1*: The variety of list varies with the context:
|
| 670 |
*expression-list*, *base-specifier-list*, *template-argument-list*,
|
| 671 |
etc. — *end note*]
|
| 672 |
|
| 673 |
When N is zero, the instantiation of the expansion produces an empty
|
| 674 |
list.
|
| 675 |
|
| 676 |
-
[*Example
|
| 677 |
|
| 678 |
``` cpp
|
| 679 |
template<class... T> struct X : T... { };
|
| 680 |
template<class... T> void f(T... values) {
|
| 681 |
X<T...> x(values...);
|
|
@@ -957,19 +1007,20 @@ A<int,int*> a; // uses the partial specialization, which is found through t
|
|
| 957 |
// which refers to the primary template
|
| 958 |
```
|
| 959 |
|
| 960 |
— *end example*]
|
| 961 |
|
| 962 |
-
A
|
| 963 |
-
parameter. All other
|
|
|
|
| 964 |
|
| 965 |
Within the argument list of a partial specialization, the following
|
| 966 |
restrictions apply:
|
| 967 |
|
| 968 |
- The type of a template parameter corresponding to a specialized
|
| 969 |
-
|
| 970 |
-
specialization.
|
| 971 |
\[*Example 5*:
|
| 972 |
``` cpp
|
| 973 |
template <class T, T t> struct C {};
|
| 974 |
template <class T> struct C<T, 1>; // error
|
| 975 |
|
|
@@ -980,11 +1031,11 @@ restrictions apply:
|
|
| 980 |
|
| 981 |
— *end example*]
|
| 982 |
- The partial specialization shall be more specialized than the primary
|
| 983 |
template [[temp.spec.partial.order]].
|
| 984 |
- The template parameter list of a partial specialization shall not
|
| 985 |
-
contain default template argument values.[^
|
| 986 |
- An argument shall not contain an unexpanded pack. If an argument is a
|
| 987 |
pack expansion [[temp.variadic]], it shall be the last argument in the
|
| 988 |
template argument list.
|
| 989 |
|
| 990 |
The usual access checking rules do not apply to non-dependent names used
|
|
@@ -1244,11 +1295,11 @@ templates and non-template functions [[dcl.fct]] in the same
|
|
| 1244 |
scope. — *end note*]
|
| 1245 |
|
| 1246 |
A non-template function is not related to a function template (i.e., it
|
| 1247 |
is never considered to be a specialization), even if it has the same
|
| 1248 |
name and type as a potentially generated function template
|
| 1249 |
-
specialization.[^
|
| 1250 |
|
| 1251 |
#### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
|
| 1252 |
|
| 1253 |
It is possible to overload function templates so that two different
|
| 1254 |
function template specializations have the same type.
|
|
@@ -1314,11 +1365,11 @@ template <int K, int L> A<K+L> f(A<K>, A<L>); // same as #1
|
|
| 1314 |
template <int I, int J> A<I-J> f(A<I>, A<J>); // different from #1
|
| 1315 |
```
|
| 1316 |
|
| 1317 |
— *end example*]
|
| 1318 |
|
| 1319 |
-
[*Note 2*: Most expressions that use template parameters use
|
| 1320 |
template parameters, but it is possible for an expression to reference a
|
| 1321 |
type parameter. For example, a template type parameter can be used in
|
| 1322 |
the `sizeof` operator. — *end note*]
|
| 1323 |
|
| 1324 |
Two expressions involving template parameters are considered
|
|
@@ -1345,12 +1396,12 @@ signature of a function template with external linkage. — *end note*]
|
|
| 1345 |
|
| 1346 |
For determining whether two dependent names [[temp.dep]] are equivalent,
|
| 1347 |
only the name itself is considered, not the result of name lookup.
|
| 1348 |
|
| 1349 |
[*Note 5*: If such a dependent name is unqualified, it is looked up
|
| 1350 |
-
from
|
| 1351 |
-
[[temp.
|
| 1352 |
|
| 1353 |
[*Example 3*:
|
| 1354 |
|
| 1355 |
``` cpp
|
| 1356 |
template <int I, int J> void f(A<I+J>); // #1
|
|
@@ -1406,34 +1457,35 @@ declared with a *type-constraint*, and if either *template-head* has a
|
|
| 1406 |
corresponding *constraint-expression*s are equivalent. Two
|
| 1407 |
*template-parameter*s are *equivalent* under the following conditions:
|
| 1408 |
|
| 1409 |
- they declare template parameters of the same kind,
|
| 1410 |
- if either declares a template parameter pack, they both do,
|
| 1411 |
-
- if they declare
|
| 1412 |
types ignoring the use of *type-constraint*s for placeholder types,
|
| 1413 |
and
|
| 1414 |
-
- if they declare template template parameters, their template
|
| 1415 |
-
|
| 1416 |
|
| 1417 |
When determining whether types or *type-constraint*s are equivalent, the
|
| 1418 |
rules above are used to compare expressions involving template
|
| 1419 |
parameters. Two *template-head*s are *functionally equivalent* if they
|
| 1420 |
accept and are satisfied by [[temp.constr.constr]] the same set of
|
| 1421 |
template argument lists.
|
| 1422 |
|
| 1423 |
If the validity or meaning of the program depends on whether two
|
| 1424 |
constructs are equivalent, and they are functionally equivalent but not
|
| 1425 |
equivalent, the program is ill-formed, no diagnostic required.
|
| 1426 |
-
Furthermore, if two
|
| 1427 |
|
| 1428 |
-
-
|
| 1429 |
- have corresponding signatures [[basic.scope.scope]],
|
| 1430 |
-
- would declare the same entity
|
| 1431 |
-
|
| 1432 |
- accept and are satisfied by the same set of template argument lists,
|
| 1433 |
|
| 1434 |
-
the program is ill-formed, no diagnostic
|
|
|
|
| 1435 |
|
| 1436 |
[*Note 7*:
|
| 1437 |
|
| 1438 |
This rule guarantees that equivalent declarations will be linked with
|
| 1439 |
one another, while not requiring implementations to use heroic efforts
|
|
@@ -1483,20 +1535,24 @@ function type. The deduction process determines whether one of the
|
|
| 1483 |
templates is more specialized than the other. If so, the more
|
| 1484 |
specialized template is the one chosen by the partial ordering process.
|
| 1485 |
If both deductions succeed, the partial ordering selects the more
|
| 1486 |
constrained template (if one exists) as determined below.
|
| 1487 |
|
| 1488 |
-
To produce the transformed template, for each type,
|
| 1489 |
-
template template
|
| 1490 |
-
[[temp.variadic]] thereof) synthesize a unique
|
| 1491 |
-
|
| 1492 |
-
|
|
|
|
| 1493 |
|
| 1494 |
[*Note 1*: The type replacing the placeholder in the type of the value
|
| 1495 |
-
synthesized for a
|
| 1496 |
synthesized type. — *end note*]
|
| 1497 |
|
|
|
|
|
|
|
|
|
|
| 1498 |
Each function template M that is a member function is considered to have
|
| 1499 |
a new first parameter of type X(M), described below, inserted in its
|
| 1500 |
function parameter list. If exactly one of the function templates was
|
| 1501 |
considered by overload resolution via a rewritten candidate
|
| 1502 |
[[over.match.oper]] with a reversed order of parameters, then the order
|
|
@@ -1700,14 +1756,21 @@ void h() {
|
|
| 1700 |
A *template-declaration* in which the *declaration* is an
|
| 1701 |
*alias-declaration* [[dcl.pre]] declares the *identifier* to be an
|
| 1702 |
*alias template*. An alias template is a name for a family of types. The
|
| 1703 |
name of the alias template is a *template-name*.
|
| 1704 |
|
| 1705 |
-
|
| 1706 |
-
|
| 1707 |
-
*template-
|
| 1708 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1709 |
|
| 1710 |
[*Note 1*: An alias template name is never deduced. — *end note*]
|
| 1711 |
|
| 1712 |
[*Example 1*:
|
| 1713 |
|
|
@@ -1832,7 +1895,7 @@ specialized [[temp.expl.spec]], or partially specialized
|
|
| 1832 |
The *constraint-expression* of a *concept-definition* is an unevaluated
|
| 1833 |
operand [[expr.context]].
|
| 1834 |
|
| 1835 |
The first declared template parameter of a concept definition is its
|
| 1836 |
*prototype parameter*. A *type concept* is a concept whose prototype
|
| 1837 |
-
parameter is a type
|
| 1838 |
|
|
|
|
| 20 |
*type-constraint*, *requires-clause*, or *noexcept-specifier* is a
|
| 21 |
separate definition which is unrelated to the templated function
|
| 22 |
definition or to any other default arguments, *type-constraint*s,
|
| 23 |
*requires-clause*s, or *noexcept-specifier*s. For the purpose of
|
| 24 |
instantiation, the substatements of a constexpr if statement [[stmt.if]]
|
| 25 |
+
are considered definitions. For the purpose of name lookup and
|
| 26 |
+
instantiation, the *compound-statement* of an *expansion-statement* is
|
| 27 |
+
considered a template definition.
|
| 28 |
|
| 29 |
Because an *alias-declaration* cannot declare a *template-id*, it is not
|
| 30 |
possible to partially or explicitly specialize an alias template.
|
| 31 |
|
| 32 |
### Class templates <a id="temp.class">[[temp.class]]</a>
|
|
|
|
| 190 |
|
| 191 |
— *end example*]
|
| 192 |
|
| 193 |
#### Deduction guides <a id="temp.deduct.guide">[[temp.deduct.guide]]</a>
|
| 194 |
|
| 195 |
+
Deduction guides are used when a *template-name* or
|
| 196 |
+
*splice-type-specifier* appears as a type specifier for a deduced class
|
| 197 |
+
type [[dcl.type.class.deduct]]. Deduction guides are not found by name
|
| 198 |
+
lookup. Instead, when performing class template argument deduction
|
| 199 |
+
[[over.match.class.deduct]], all reachable deduction guides declared for
|
| 200 |
+
the class template are considered.
|
| 201 |
|
| 202 |
``` bnf
|
| 203 |
deduction-guide:
|
| 204 |
+
explicit-specifierₒₚₜ template-name '(' parameter-declaration-clause ')' '->' simple-template-id requires-clauseₒₚₜ ';'
|
| 205 |
```
|
| 206 |
|
| 207 |
[*Example 1*:
|
| 208 |
|
| 209 |
``` cpp
|
|
|
|
| 222 |
```
|
| 223 |
|
| 224 |
— *end example*]
|
| 225 |
|
| 226 |
The same restrictions apply to the *parameter-declaration-clause* of a
|
| 227 |
+
deduction guide as in a function declaration [[dcl.fct]], except that a
|
| 228 |
+
generic parameter type placeholder [[dcl.spec.auto]] shall not appear in
|
| 229 |
+
the *parameter-declaration-clause* of a deduction guide. The
|
| 230 |
*simple-template-id* shall name a class template specialization. The
|
| 231 |
*template-name* shall be the same *identifier* as the *template-name* of
|
| 232 |
the *simple-template-id*. A *deduction-guide* shall inhabit the scope to
|
| 233 |
which the corresponding class template belongs and, for a member class
|
| 234 |
template, have the same access. Two deduction guide declarations for the
|
|
|
|
| 305 |
|
| 306 |
``` cpp
|
| 307 |
template<class T> struct A {
|
| 308 |
enum E : T;
|
| 309 |
};
|
|
|
|
| 310 |
template<class T> enum A<T>::E : T { e1, e2 };
|
| 311 |
A<int>::E e = A<int>::e1;
|
| 312 |
```
|
| 313 |
|
| 314 |
— *end example*]
|
|
|
|
| 417 |
|
| 418 |
— *end example*]
|
| 419 |
|
| 420 |
[*Note 1*:
|
| 421 |
|
| 422 |
+
A specialization of a conversion function template is named in the same
|
| 423 |
+
way as a non-template conversion function that converts to the same type
|
| 424 |
+
[[class.conv.fct]].
|
| 425 |
|
| 426 |
[*Example 6*:
|
| 427 |
|
| 428 |
``` cpp
|
| 429 |
struct A {
|
|
|
|
| 440 |
}
|
| 441 |
```
|
| 442 |
|
| 443 |
— *end example*]
|
| 444 |
|
| 445 |
+
An expression designating a particular specialization of a conversion
|
| 446 |
+
function template can only be formed with a *splice-expression*. There
|
| 447 |
+
is no analogous syntax to form a *template-id* [[temp.names]] for such a
|
| 448 |
+
function by providing an explicit template argument list
|
| 449 |
+
[[temp.arg.explicit]].
|
| 450 |
|
| 451 |
— *end note*]
|
| 452 |
|
| 453 |
### Variadic templates <a id="temp.variadic">[[temp.variadic]]</a>
|
| 454 |
|
|
|
|
| 501 |
foo(1); // xs contains one init-capture
|
| 502 |
```
|
| 503 |
|
| 504 |
— *end example*]
|
| 505 |
|
| 506 |
+
A *structured binding pack* is an *sb-identifier* that introduces zero
|
| 507 |
+
or more structured bindings [[dcl.struct.bind]].
|
| 508 |
+
|
| 509 |
+
[*Example 4*:
|
| 510 |
+
|
| 511 |
+
``` cpp
|
| 512 |
+
auto foo() -> int(&)[2];
|
| 513 |
+
|
| 514 |
+
template <class T>
|
| 515 |
+
void g() {
|
| 516 |
+
auto [...a] = foo(); // a is a structured binding pack containing two elements
|
| 517 |
+
auto [b, c, ...d] = foo(); // d is a structured binding pack containing zero elements
|
| 518 |
+
}
|
| 519 |
+
```
|
| 520 |
+
|
| 521 |
+
— *end example*]
|
| 522 |
+
|
| 523 |
+
A *pack* is a template parameter pack, a function parameter pack, an
|
| 524 |
+
*init-capture* pack, or a structured binding pack. The number of
|
| 525 |
+
elements of a template parameter pack or a function parameter pack is
|
| 526 |
+
the number of arguments provided for the parameter pack. The number of
|
| 527 |
+
elements of an *init-capture* pack is the number of elements in the pack
|
| 528 |
+
expansion of its *initializer*.
|
| 529 |
|
| 530 |
A *pack expansion* consists of a *pattern* and an ellipsis, the
|
| 531 |
instantiation of which produces zero or more instantiations of the
|
| 532 |
pattern in a list (described below). The form of the pattern depends on
|
| 533 |
the context in which the expansion occurs. Pack expansions can occur in
|
|
|
|
| 535 |
|
| 536 |
- In a function parameter pack [[dcl.fct]]; the pattern is the
|
| 537 |
*parameter-declaration* without the ellipsis.
|
| 538 |
- In a *using-declaration* [[namespace.udecl]]; the pattern is a
|
| 539 |
*using-declarator*.
|
| 540 |
+
- In a *friend-type-declaration* [[class.mem.general]]; the pattern is a
|
| 541 |
+
*friend-type-specifier*.
|
| 542 |
- In a template parameter pack that is a pack expansion [[temp.param]]:
|
| 543 |
- if the template parameter pack is a *parameter-declaration*; the
|
| 544 |
pattern is the *parameter-declaration* without the ellipsis;
|
| 545 |
- if the template parameter pack is a *type-parameter*; the pattern is
|
| 546 |
+
the corresponding *type-parameter* without the ellipsis;
|
| 547 |
+
- if the template parameter pack is a template template parameter; the
|
| 548 |
+
pattern is the corresponding *type-tt-parameter*,
|
| 549 |
+
*variable-tt-parameter*, or *concept-tt-parameter* without the
|
| 550 |
+
ellipsis.
|
| 551 |
- In an *initializer-list* [[dcl.init]]; the pattern is an
|
| 552 |
*initializer-clause*.
|
| 553 |
- In a *base-specifier-list* [[class.derived]]; the pattern is a
|
| 554 |
*base-specifier*.
|
| 555 |
- In a *mem-initializer-list* [[class.base.init]] for a
|
|
|
|
| 557 |
pattern is the *mem-initializer*.
|
| 558 |
- In a *template-argument-list* [[temp.arg]]; the pattern is a
|
| 559 |
*template-argument*.
|
| 560 |
- In an *attribute-list* [[dcl.attr.grammar]]; the pattern is an
|
| 561 |
*attribute*.
|
| 562 |
+
- In an *annotation-list* [[dcl.attr.grammar]]; the pattern is an
|
| 563 |
+
*annotation*.
|
| 564 |
- In an *alignment-specifier* [[dcl.align]]; the pattern is the
|
| 565 |
*alignment-specifier* without the ellipsis.
|
| 566 |
- In a *capture-list* [[expr.prim.lambda.capture]]; the pattern is the
|
| 567 |
*capture* without the ellipsis.
|
| 568 |
- In a `sizeof...` expression [[expr.sizeof]]; the pattern is an
|
| 569 |
*identifier*.
|
| 570 |
+
- In a *pack-index-expression*; the pattern is an *identifier*.
|
| 571 |
+
- In a *pack-index-specifier*; the pattern is a *typedef-name*.
|
| 572 |
- In a *fold-expression* [[expr.prim.fold]]; the pattern is the
|
| 573 |
*cast-expression* that contains an unexpanded pack.
|
| 574 |
+
- In a fold expanded constraint [[temp.constr.fold]]; the pattern is the
|
| 575 |
+
constraint of that fold expanded constraint.
|
| 576 |
|
| 577 |
+
[*Example 5*:
|
| 578 |
|
| 579 |
``` cpp
|
| 580 |
template<class ... Types> void f(Types ... rest);
|
| 581 |
template<class ... Types> void g(Types ... rest) {
|
| 582 |
f(&rest ...); // ``&rest ...'' is a pack expansion; ``&rest'' is its pattern
|
|
|
|
| 596 |
nested pack expansion; such packs are called *unexpanded packs* in the
|
| 597 |
pattern. All of the packs expanded by a pack expansion shall have the
|
| 598 |
same number of arguments specified. An appearance of a name of a pack
|
| 599 |
that is not expanded is ill-formed.
|
| 600 |
|
| 601 |
+
[*Example 6*:
|
| 602 |
|
| 603 |
``` cpp
|
| 604 |
template<typename...> struct Tuple {};
|
| 605 |
template<typename T1, typename T2> struct Pair {};
|
| 606 |
|
|
|
|
| 632 |
expansion parameters. Each `Eᵢ` is generated by instantiating the
|
| 633 |
pattern and replacing each pack expansion parameter with its iᵗʰ
|
| 634 |
element. Such an element, in the context of the instantiation, is
|
| 635 |
interpreted as follows:
|
| 636 |
|
| 637 |
+
- if the pack is a template parameter pack, the element is
|
| 638 |
+
- a *typedef-name* for a type template parameter pack,
|
| 639 |
+
- an *id-expression* for a constant template parameter pack, or
|
| 640 |
+
- a *template-name* for a template template parameter pack
|
| 641 |
+
|
| 642 |
+
designating the iᵗʰ corresponding type, constant, or template template
|
| 643 |
+
argument;
|
| 644 |
- if the pack is a function parameter pack, the element is an
|
| 645 |
*id-expression* designating the iᵗʰ function parameter that resulted
|
| 646 |
from instantiation of the function parameter pack declaration;
|
|
|
|
| 647 |
- if the pack is an *init-capture* pack, the element is an
|
| 648 |
*id-expression* designating the variable introduced by the iᵗʰ
|
| 649 |
*init-capture* that resulted from instantiation of the *init-capture*
|
| 650 |
+
pack declaration; otherwise
|
| 651 |
+
- if the pack is a structured binding pack, the element is an
|
| 652 |
+
*id-expression* designating the $i^\textrm{th}$ structured binding in
|
| 653 |
+
the pack that resulted from the structured binding declaration.
|
| 654 |
|
| 655 |
When N is zero, the instantiation of a pack expansion does not alter the
|
| 656 |
syntactic interpretation of the enclosing construct, even in cases where
|
| 657 |
omitting the pack expansion entirely would otherwise be ill-formed or
|
| 658 |
would result in an ambiguity in the grammar.
|
| 659 |
|
| 660 |
The instantiation of a `sizeof...` expression [[expr.sizeof]] produces
|
| 661 |
an integral constant with value N.
|
| 662 |
|
| 663 |
+
When instantiating a *pack-index-expression* P, let K be the index of P.
|
| 664 |
+
The instantiation of P is the *id-expression* `E_K`.
|
| 665 |
+
|
| 666 |
+
When instantiating a *pack-index-specifier* P, let K be the index of P.
|
| 667 |
+
The instantiation of P is the *typedef-name* `E_K`.
|
| 668 |
+
|
| 669 |
+
The instantiation of an *alignment-specifier* with an ellipsis produces
|
| 670 |
+
`E₁` `E₂` … `E_N`.
|
| 671 |
+
|
| 672 |
The instantiation of a *fold-expression* [[expr.prim.fold]] produces:
|
| 673 |
|
| 674 |
- `(` `((`E₁ *op* E₂`)` *op* ⋯`)` *op* $\mathtt{E}_N$ `)` for a unary
|
| 675 |
left fold,
|
| 676 |
- `(` E₁ *op* `(`⋯ *op* `(`$\mathtt{E}_{N-1}$ *op* $\mathtt{E}_N$`))`
|
|
|
|
| 682 |
|
| 683 |
In each case, *op* is the *fold-operator*. For a binary fold, E is
|
| 684 |
generated by instantiating the *cast-expression* that did not contain an
|
| 685 |
unexpanded pack.
|
| 686 |
|
| 687 |
+
[*Example 7*:
|
| 688 |
|
| 689 |
``` cpp
|
| 690 |
template<typename ...Args>
|
| 691 |
bool all(Args ...args) { return (... && args); }
|
| 692 |
|
|
|
|
| 709 |
| `&&` | `true` |
|
| 710 |
| `||` | `false` |
|
| 711 |
| `,` | `void()` |
|
| 712 |
|
| 713 |
|
| 714 |
+
A fold expanded constraint is not instantiated [[temp.constr.fold]].
|
| 715 |
+
|
| 716 |
The instantiation of any other pack expansion produces a list of
|
| 717 |
elements `E₁`, `E₂`, …, `E_N`.
|
| 718 |
|
| 719 |
[*Note 1*: The variety of list varies with the context:
|
| 720 |
*expression-list*, *base-specifier-list*, *template-argument-list*,
|
| 721 |
etc. — *end note*]
|
| 722 |
|
| 723 |
When N is zero, the instantiation of the expansion produces an empty
|
| 724 |
list.
|
| 725 |
|
| 726 |
+
[*Example 8*:
|
| 727 |
|
| 728 |
``` cpp
|
| 729 |
template<class... T> struct X : T... { };
|
| 730 |
template<class... T> void f(T... values) {
|
| 731 |
X<T...> x(values...);
|
|
|
|
| 1007 |
// which refers to the primary template
|
| 1008 |
```
|
| 1009 |
|
| 1010 |
— *end example*]
|
| 1011 |
|
| 1012 |
+
A constant template argument is non-specialized if it is the name of a
|
| 1013 |
+
constant template parameter. All other constant template arguments are
|
| 1014 |
+
specialized.
|
| 1015 |
|
| 1016 |
Within the argument list of a partial specialization, the following
|
| 1017 |
restrictions apply:
|
| 1018 |
|
| 1019 |
- The type of a template parameter corresponding to a specialized
|
| 1020 |
+
constant template argument shall not be dependent on a parameter of
|
| 1021 |
+
the partial specialization.
|
| 1022 |
\[*Example 5*:
|
| 1023 |
``` cpp
|
| 1024 |
template <class T, T t> struct C {};
|
| 1025 |
template <class T> struct C<T, 1>; // error
|
| 1026 |
|
|
|
|
| 1031 |
|
| 1032 |
— *end example*]
|
| 1033 |
- The partial specialization shall be more specialized than the primary
|
| 1034 |
template [[temp.spec.partial.order]].
|
| 1035 |
- The template parameter list of a partial specialization shall not
|
| 1036 |
+
contain default template argument values.[^7]
|
| 1037 |
- An argument shall not contain an unexpanded pack. If an argument is a
|
| 1038 |
pack expansion [[temp.variadic]], it shall be the last argument in the
|
| 1039 |
template argument list.
|
| 1040 |
|
| 1041 |
The usual access checking rules do not apply to non-dependent names used
|
|
|
|
| 1295 |
scope. — *end note*]
|
| 1296 |
|
| 1297 |
A non-template function is not related to a function template (i.e., it
|
| 1298 |
is never considered to be a specialization), even if it has the same
|
| 1299 |
name and type as a potentially generated function template
|
| 1300 |
+
specialization.[^8]
|
| 1301 |
|
| 1302 |
#### Function template overloading <a id="temp.over.link">[[temp.over.link]]</a>
|
| 1303 |
|
| 1304 |
It is possible to overload function templates so that two different
|
| 1305 |
function template specializations have the same type.
|
|
|
|
| 1365 |
template <int I, int J> A<I-J> f(A<I>, A<J>); // different from #1
|
| 1366 |
```
|
| 1367 |
|
| 1368 |
— *end example*]
|
| 1369 |
|
| 1370 |
+
[*Note 2*: Most expressions that use template parameters use constant
|
| 1371 |
template parameters, but it is possible for an expression to reference a
|
| 1372 |
type parameter. For example, a template type parameter can be used in
|
| 1373 |
the `sizeof` operator. — *end note*]
|
| 1374 |
|
| 1375 |
Two expressions involving template parameters are considered
|
|
|
|
| 1396 |
|
| 1397 |
For determining whether two dependent names [[temp.dep]] are equivalent,
|
| 1398 |
only the name itself is considered, not the result of name lookup.
|
| 1399 |
|
| 1400 |
[*Note 5*: If such a dependent name is unqualified, it is looked up
|
| 1401 |
+
from a first declaration of the function template
|
| 1402 |
+
[[temp.res.general]]. — *end note*]
|
| 1403 |
|
| 1404 |
[*Example 3*:
|
| 1405 |
|
| 1406 |
``` cpp
|
| 1407 |
template <int I, int J> void f(A<I+J>); // #1
|
|
|
|
| 1457 |
corresponding *constraint-expression*s are equivalent. Two
|
| 1458 |
*template-parameter*s are *equivalent* under the following conditions:
|
| 1459 |
|
| 1460 |
- they declare template parameters of the same kind,
|
| 1461 |
- if either declares a template parameter pack, they both do,
|
| 1462 |
+
- if they declare constant template parameters, they have equivalent
|
| 1463 |
types ignoring the use of *type-constraint*s for placeholder types,
|
| 1464 |
and
|
| 1465 |
+
- if they declare template template parameters, their *template-head*s
|
| 1466 |
+
are equivalent.
|
| 1467 |
|
| 1468 |
When determining whether types or *type-constraint*s are equivalent, the
|
| 1469 |
rules above are used to compare expressions involving template
|
| 1470 |
parameters. Two *template-head*s are *functionally equivalent* if they
|
| 1471 |
accept and are satisfied by [[temp.constr.constr]] the same set of
|
| 1472 |
template argument lists.
|
| 1473 |
|
| 1474 |
If the validity or meaning of the program depends on whether two
|
| 1475 |
constructs are equivalent, and they are functionally equivalent but not
|
| 1476 |
equivalent, the program is ill-formed, no diagnostic required.
|
| 1477 |
+
Furthermore, if two declarations A and B of function templates
|
| 1478 |
|
| 1479 |
+
- introduce the same name,
|
| 1480 |
- have corresponding signatures [[basic.scope.scope]],
|
| 1481 |
+
- would declare the same entity, when considering A and B to correspond
|
| 1482 |
+
in that determination [[basic.link]], and
|
| 1483 |
- accept and are satisfied by the same set of template argument lists,
|
| 1484 |
|
| 1485 |
+
but do not correspond, the program is ill-formed, no diagnostic
|
| 1486 |
+
required.
|
| 1487 |
|
| 1488 |
[*Note 7*:
|
| 1489 |
|
| 1490 |
This rule guarantees that equivalent declarations will be linked with
|
| 1491 |
one another, while not requiring implementations to use heroic efforts
|
|
|
|
| 1535 |
templates is more specialized than the other. If so, the more
|
| 1536 |
specialized template is the one chosen by the partial ordering process.
|
| 1537 |
If both deductions succeed, the partial ordering selects the more
|
| 1538 |
constrained template (if one exists) as determined below.
|
| 1539 |
|
| 1540 |
+
To produce the transformed template, for each type, constant, type
|
| 1541 |
+
template, variable template, or concept template parameter (including
|
| 1542 |
+
template parameter packs [[temp.variadic]] thereof) synthesize a unique
|
| 1543 |
+
type, value, class template, variable template, or concept,
|
| 1544 |
+
respectively, and substitute it for each occurrence of that parameter in
|
| 1545 |
+
the function type of the template.
|
| 1546 |
|
| 1547 |
[*Note 1*: The type replacing the placeholder in the type of the value
|
| 1548 |
+
synthesized for a constant template parameter is also a unique
|
| 1549 |
synthesized type. — *end note*]
|
| 1550 |
|
| 1551 |
+
A synthesized template has the same *template-head* as its corresponding
|
| 1552 |
+
template template parameter.
|
| 1553 |
+
|
| 1554 |
Each function template M that is a member function is considered to have
|
| 1555 |
a new first parameter of type X(M), described below, inserted in its
|
| 1556 |
function parameter list. If exactly one of the function templates was
|
| 1557 |
considered by overload resolution via a rewritten candidate
|
| 1558 |
[[over.match.oper]] with a reversed order of parameters, then the order
|
|
|
|
| 1756 |
A *template-declaration* in which the *declaration* is an
|
| 1757 |
*alias-declaration* [[dcl.pre]] declares the *identifier* to be an
|
| 1758 |
*alias template*. An alias template is a name for a family of types. The
|
| 1759 |
name of the alias template is a *template-name*.
|
| 1760 |
|
| 1761 |
+
A
|
| 1762 |
+
|
| 1763 |
+
- *template-id* that is not the operand of a *reflect-expression* or
|
| 1764 |
+
|
| 1765 |
+
-
|
| 1766 |
+
|
| 1767 |
+
that designates the specialization of an alias template is equivalent to
|
| 1768 |
+
the associated type obtained by substitution of its *template-argument*s
|
| 1769 |
+
for the *template-parameter*s in the *defining-type-id* of the alias
|
| 1770 |
+
template. Any other *template-id* that names a specialization of an
|
| 1771 |
+
alias template is a *typedef-name* for a type alias.
|
| 1772 |
|
| 1773 |
[*Note 1*: An alias template name is never deduced. — *end note*]
|
| 1774 |
|
| 1775 |
[*Example 1*:
|
| 1776 |
|
|
|
|
| 1895 |
The *constraint-expression* of a *concept-definition* is an unevaluated
|
| 1896 |
operand [[expr.context]].
|
| 1897 |
|
| 1898 |
The first declared template parameter of a concept definition is its
|
| 1899 |
*prototype parameter*. A *type concept* is a concept whose prototype
|
| 1900 |
+
parameter is a type template parameter.
|
| 1901 |
|