tmp/tmpwnsh8lcm/{from.md → to.md}
RENAMED
|
@@ -22,84 +22,103 @@ more function arguments.
|
|
| 22 |
[*Example 2*:
|
| 23 |
|
| 24 |
``` cpp
|
| 25 |
template<class ... Types> void f(Types ... args);
|
| 26 |
|
| 27 |
-
f(); //
|
| 28 |
-
f(1); //
|
| 29 |
-
f(2, 1.0); //
|
| 30 |
```
|
| 31 |
|
| 32 |
— *end example*]
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
A *pack expansion* consists of a *pattern* and an ellipsis, the
|
| 38 |
instantiation of which produces zero or more instantiations of the
|
| 39 |
pattern in a list (described below). The form of the pattern depends on
|
| 40 |
the context in which the expansion occurs. Pack expansions can occur in
|
| 41 |
the following contexts:
|
| 42 |
|
| 43 |
-
- In a function parameter pack
|
| 44 |
*parameter-declaration* without the ellipsis.
|
| 45 |
-
- In a *using-declaration*
|
| 46 |
*using-declarator*.
|
| 47 |
-
- In a template parameter pack that is a pack expansion
|
| 48 |
-
[[temp.param]]):
|
| 49 |
- if the template parameter pack is a *parameter-declaration*; the
|
| 50 |
pattern is the *parameter-declaration* without the ellipsis;
|
| 51 |
-
- if the template parameter pack is a *type-parameter*
|
| 52 |
-
*
|
| 53 |
-
|
| 54 |
-
- In an *initializer-list* ([[dcl.init]]); the pattern is an
|
| 55 |
*initializer-clause*.
|
| 56 |
-
- In a *base-specifier-list*
|
| 57 |
-
|
| 58 |
-
- In a *mem-initializer-list*
|
| 59 |
*mem-initializer* whose *mem-initializer-id* denotes a base class; the
|
| 60 |
pattern is the *mem-initializer*.
|
| 61 |
-
- In a *template-argument-list*
|
| 62 |
*template-argument*.
|
| 63 |
-
- In an *attribute-list*
|
| 64 |
*attribute*.
|
| 65 |
-
- In an *alignment-specifier*
|
| 66 |
*alignment-specifier* without the ellipsis.
|
| 67 |
-
- In a *capture-list*
|
| 68 |
-
*capture*.
|
| 69 |
-
- In a `sizeof...` expression
|
| 70 |
*identifier*.
|
| 71 |
-
- In a *fold-expression*
|
| 72 |
-
*cast-expression* that contains an unexpanded
|
| 73 |
|
| 74 |
-
[*Example
|
| 75 |
|
| 76 |
``` cpp
|
| 77 |
template<class ... Types> void f(Types ... rest);
|
| 78 |
template<class ... Types> void g(Types ... rest) {
|
| 79 |
f(&rest ...); // ``&rest ...'' is a pack expansion; ``&rest'' is its pattern
|
| 80 |
}
|
| 81 |
```
|
| 82 |
|
| 83 |
— *end example*]
|
| 84 |
|
| 85 |
-
For the purpose of determining whether a
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
the pattern in which it appears.
|
| 89 |
|
| 90 |
-
A
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
parameter pack that is not expanded is ill-formed.
|
| 99 |
|
| 100 |
-
[*Example
|
| 101 |
|
| 102 |
``` cpp
|
| 103 |
template<typename...> struct Tuple {};
|
| 104 |
template<typename T1, typename T2> struct Pair {};
|
| 105 |
|
|
@@ -115,36 +134,40 @@ typedef zip<short>::with<unsigned short, unsigned>::type T2;
|
|
| 115 |
// error: different number of arguments specified for Args1 and Args2
|
| 116 |
|
| 117 |
template<class ... Args>
|
| 118 |
void g(Args ... args) { // OK: Args is expanded by the function parameter pack args
|
| 119 |
f(const_cast<const Args*>(&args)...); // OK: ``Args'' and ``args'' are expanded
|
| 120 |
-
f(5 ...); // error: pattern does not contain any
|
| 121 |
-
f(args); // error:
|
| 122 |
f(h(args ...) + args ...); // OK: first ``args'' expanded within h,
|
| 123 |
// second ``args'' expanded within f
|
| 124 |
}
|
| 125 |
```
|
| 126 |
|
| 127 |
— *end example*]
|
| 128 |
|
| 129 |
The instantiation of a pack expansion that is neither a `sizeof...`
|
| 130 |
-
expression nor a *fold-expression* produces a list
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
|
| 137 |
- if the pack is a template parameter pack, the element is a template
|
| 138 |
-
parameter
|
| 139 |
-
|
| 140 |
-
otherwise,
|
| 141 |
- if the pack is a function parameter pack, the element is an
|
| 142 |
-
*id-expression* designating the function parameter that resulted
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
All of the Eᵢ become
|
| 146 |
|
| 147 |
[*Note 1*: The variety of list varies with the context:
|
| 148 |
*expression-list*, *base-specifier-list*, *template-argument-list*,
|
| 149 |
etc. — *end note*]
|
| 150 |
|
|
@@ -152,11 +175,11 @@ When N is zero, the instantiation of the expansion produces an empty
|
|
| 152 |
list. Such an instantiation does not alter the syntactic interpretation
|
| 153 |
of the enclosing construct, even in cases where omitting the list
|
| 154 |
entirely would otherwise be ill-formed or would result in an ambiguity
|
| 155 |
in the grammar.
|
| 156 |
|
| 157 |
-
[*Example
|
| 158 |
|
| 159 |
``` cpp
|
| 160 |
template<class... T> struct X : T... { };
|
| 161 |
template<class... T> void f(T... values) {
|
| 162 |
X<T...> x(values...);
|
|
@@ -166,13 +189,13 @@ template void f<>(); // OK: X<> has no base classes
|
|
| 166 |
// x is a variable of type X<> that is value-initialized
|
| 167 |
```
|
| 168 |
|
| 169 |
— *end example*]
|
| 170 |
|
| 171 |
-
The instantiation of a `sizeof...` expression
|
| 172 |
-
|
| 173 |
-
|
| 174 |
|
| 175 |
The instantiation of a *fold-expression* produces:
|
| 176 |
|
| 177 |
- `((`E₁ *op* E₂`)` *op* ⋯`)` *op* $\mathtt{E}_N$ for a unary left fold,
|
| 178 |
- E₁ *op* `(`⋯ *op* `(`$\mathtt{E}_{N-1}$ *op* $\mathtt{E}_N$`))` for a
|
|
@@ -183,15 +206,15 @@ The instantiation of a *fold-expression* produces:
|
|
| 183 |
E`)))` for a binary right fold.
|
| 184 |
|
| 185 |
In each case, *op* is the *fold-operator*, N is the number of elements
|
| 186 |
in the pack expansion parameters, and each Eᵢ is generated by
|
| 187 |
instantiating the pattern and replacing each pack expansion parameter
|
| 188 |
-
with its
|
| 189 |
instantiating the *cast-expression* that did not contain an unexpanded
|
| 190 |
-
|
| 191 |
|
| 192 |
-
[*Example
|
| 193 |
|
| 194 |
``` cpp
|
| 195 |
template<typename ...Args>
|
| 196 |
bool all(Args ...args) { return (... && args); }
|
| 197 |
|
|
@@ -202,17 +225,17 @@ Within the instantiation of `all`, the returned expression expands to
|
|
| 202 |
`((true && true) && true) && false`, which evaluates to `false`.
|
| 203 |
|
| 204 |
— *end example*]
|
| 205 |
|
| 206 |
If N is zero for a unary fold-expression, the value of the expression is
|
| 207 |
-
shown in
|
| 208 |
-
|
| 209 |
|
| 210 |
-
**Table: Value of folding empty sequences** <a id="
|
| 211 |
|
| 212 |
-
| Operator | Value when
|
| 213 |
-
| -------- | ------------------------
|
| 214 |
| `&&` | `true` |
|
| 215 |
| `||` | `false` |
|
| 216 |
| `,` | `void()` |
|
| 217 |
|
| 218 |
|
|
|
|
| 22 |
[*Example 2*:
|
| 23 |
|
| 24 |
``` cpp
|
| 25 |
template<class ... Types> void f(Types ... args);
|
| 26 |
|
| 27 |
+
f(); // args contains no arguments
|
| 28 |
+
f(1); // args contains one argument: int
|
| 29 |
+
f(2, 1.0); // args contains two arguments: int and double
|
| 30 |
```
|
| 31 |
|
| 32 |
— *end example*]
|
| 33 |
|
| 34 |
+
An **init-capture* pack* is a lambda capture that introduces an
|
| 35 |
+
*init-capture* for each of the elements in the pack expansion of its
|
| 36 |
+
*initializer*.
|
| 37 |
+
|
| 38 |
+
[*Example 3*:
|
| 39 |
+
|
| 40 |
+
``` cpp
|
| 41 |
+
template <typename... Args>
|
| 42 |
+
void foo(Args... args) {
|
| 43 |
+
[...xs=args]{
|
| 44 |
+
bar(xs...); // xs is an init-capture pack
|
| 45 |
+
};
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
foo(); // xs contains zero init-captures
|
| 49 |
+
foo(1); // xs contains one init-capture
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
— *end example*]
|
| 53 |
+
|
| 54 |
+
A *pack* is a template parameter pack, a function parameter pack, or an
|
| 55 |
+
*init-capture* pack. The number of elements of a template parameter pack
|
| 56 |
+
or a function parameter pack is the number of arguments provided for the
|
| 57 |
+
parameter pack. The number of elements of an *init-capture* pack is the
|
| 58 |
+
number of elements in the pack expansion of its *initializer*.
|
| 59 |
|
| 60 |
A *pack expansion* consists of a *pattern* and an ellipsis, the
|
| 61 |
instantiation of which produces zero or more instantiations of the
|
| 62 |
pattern in a list (described below). The form of the pattern depends on
|
| 63 |
the context in which the expansion occurs. Pack expansions can occur in
|
| 64 |
the following contexts:
|
| 65 |
|
| 66 |
+
- In a function parameter pack [[dcl.fct]]; the pattern is the
|
| 67 |
*parameter-declaration* without the ellipsis.
|
| 68 |
+
- In a *using-declaration* [[namespace.udecl]]; the pattern is a
|
| 69 |
*using-declarator*.
|
| 70 |
+
- In a template parameter pack that is a pack expansion [[temp.param]]:
|
|
|
|
| 71 |
- if the template parameter pack is a *parameter-declaration*; the
|
| 72 |
pattern is the *parameter-declaration* without the ellipsis;
|
| 73 |
+
- if the template parameter pack is a *type-parameter*; the pattern is
|
| 74 |
+
the corresponding *type-parameter* without the ellipsis.
|
| 75 |
+
- In an *initializer-list* [[dcl.init]]; the pattern is an
|
|
|
|
| 76 |
*initializer-clause*.
|
| 77 |
+
- In a *base-specifier-list* [[class.derived]]; the pattern is a
|
| 78 |
+
*base-specifier*.
|
| 79 |
+
- In a *mem-initializer-list* [[class.base.init]] for a
|
| 80 |
*mem-initializer* whose *mem-initializer-id* denotes a base class; the
|
| 81 |
pattern is the *mem-initializer*.
|
| 82 |
+
- In a *template-argument-list* [[temp.arg]]; the pattern is a
|
| 83 |
*template-argument*.
|
| 84 |
+
- In an *attribute-list* [[dcl.attr.grammar]]; the pattern is an
|
| 85 |
*attribute*.
|
| 86 |
+
- In an *alignment-specifier* [[dcl.align]]; the pattern is the
|
| 87 |
*alignment-specifier* without the ellipsis.
|
| 88 |
+
- In a *capture-list* [[expr.prim.lambda.capture]]; the pattern is the
|
| 89 |
+
*capture* without the ellipsis.
|
| 90 |
+
- In a `sizeof...` expression [[expr.sizeof]]; the pattern is an
|
| 91 |
*identifier*.
|
| 92 |
+
- In a *fold-expression* [[expr.prim.fold]]; the pattern is the
|
| 93 |
+
*cast-expression* that contains an unexpanded pack.
|
| 94 |
|
| 95 |
+
[*Example 4*:
|
| 96 |
|
| 97 |
``` cpp
|
| 98 |
template<class ... Types> void f(Types ... rest);
|
| 99 |
template<class ... Types> void g(Types ... rest) {
|
| 100 |
f(&rest ...); // ``&rest ...'' is a pack expansion; ``&rest'' is its pattern
|
| 101 |
}
|
| 102 |
```
|
| 103 |
|
| 104 |
— *end example*]
|
| 105 |
|
| 106 |
+
For the purpose of determining whether a pack satisfies a rule regarding
|
| 107 |
+
entities other than packs, the pack is considered to be the entity that
|
| 108 |
+
would result from an instantiation of the pattern in which it appears.
|
|
|
|
| 109 |
|
| 110 |
+
A pack whose name appears within the pattern of a pack expansion is
|
| 111 |
+
expanded by that pack expansion. An appearance of the name of a pack is
|
| 112 |
+
only expanded by the innermost enclosing pack expansion. The pattern of
|
| 113 |
+
a pack expansion shall name one or more packs that are not expanded by a
|
| 114 |
+
nested pack expansion; such packs are called *unexpanded packs* in the
|
| 115 |
+
pattern. All of the packs expanded by a pack expansion shall have the
|
| 116 |
+
same number of arguments specified. An appearance of a name of a pack
|
| 117 |
+
that is not expanded is ill-formed.
|
|
|
|
| 118 |
|
| 119 |
+
[*Example 5*:
|
| 120 |
|
| 121 |
``` cpp
|
| 122 |
template<typename...> struct Tuple {};
|
| 123 |
template<typename T1, typename T2> struct Pair {};
|
| 124 |
|
|
|
|
| 134 |
// error: different number of arguments specified for Args1 and Args2
|
| 135 |
|
| 136 |
template<class ... Args>
|
| 137 |
void g(Args ... args) { // OK: Args is expanded by the function parameter pack args
|
| 138 |
f(const_cast<const Args*>(&args)...); // OK: ``Args'' and ``args'' are expanded
|
| 139 |
+
f(5 ...); // error: pattern does not contain any packs
|
| 140 |
+
f(args); // error: pack ``args'' is not expanded
|
| 141 |
f(h(args ...) + args ...); // OK: first ``args'' expanded within h,
|
| 142 |
// second ``args'' expanded within f
|
| 143 |
}
|
| 144 |
```
|
| 145 |
|
| 146 |
— *end example*]
|
| 147 |
|
| 148 |
The instantiation of a pack expansion that is neither a `sizeof...`
|
| 149 |
+
expression nor a *fold-expression* produces a list of elements E₁, E₂,
|
| 150 |
+
⋯, $\mathtt{E}_N$, where N is the number of elements in the pack
|
| 151 |
+
expansion parameters. Each Eᵢ is generated by instantiating the pattern
|
| 152 |
+
and replacing each pack expansion parameter with its iᵗʰ element. Such
|
| 153 |
+
an element, in the context of the instantiation, is interpreted as
|
| 154 |
+
follows:
|
| 155 |
|
| 156 |
- if the pack is a template parameter pack, the element is a template
|
| 157 |
+
parameter [[temp.param]] of the corresponding kind (type or non-type)
|
| 158 |
+
designating the iᵗʰ corresponding type or value template argument;
|
|
|
|
| 159 |
- if the pack is a function parameter pack, the element is an
|
| 160 |
+
*id-expression* designating the iᵗʰ function parameter that resulted
|
| 161 |
+
from instantiation of the function parameter pack declaration;
|
| 162 |
+
otherwise
|
| 163 |
+
- if the pack is an *init-capture* pack, the element is an
|
| 164 |
+
*id-expression* designating the variable introduced by the iᵗʰ
|
| 165 |
+
*init-capture* that resulted from instantiation of the *init-capture*
|
| 166 |
+
pack.
|
| 167 |
|
| 168 |
+
All of the Eᵢ become items in the enclosing list.
|
| 169 |
|
| 170 |
[*Note 1*: The variety of list varies with the context:
|
| 171 |
*expression-list*, *base-specifier-list*, *template-argument-list*,
|
| 172 |
etc. — *end note*]
|
| 173 |
|
|
|
|
| 175 |
list. Such an instantiation does not alter the syntactic interpretation
|
| 176 |
of the enclosing construct, even in cases where omitting the list
|
| 177 |
entirely would otherwise be ill-formed or would result in an ambiguity
|
| 178 |
in the grammar.
|
| 179 |
|
| 180 |
+
[*Example 6*:
|
| 181 |
|
| 182 |
``` cpp
|
| 183 |
template<class... T> struct X : T... { };
|
| 184 |
template<class... T> void f(T... values) {
|
| 185 |
X<T...> x(values...);
|
|
|
|
| 189 |
// x is a variable of type X<> that is value-initialized
|
| 190 |
```
|
| 191 |
|
| 192 |
— *end example*]
|
| 193 |
|
| 194 |
+
The instantiation of a `sizeof...` expression [[expr.sizeof]] produces
|
| 195 |
+
an integral constant containing the number of elements in the pack it
|
| 196 |
+
expands.
|
| 197 |
|
| 198 |
The instantiation of a *fold-expression* produces:
|
| 199 |
|
| 200 |
- `((`E₁ *op* E₂`)` *op* ⋯`)` *op* $\mathtt{E}_N$ for a unary left fold,
|
| 201 |
- E₁ *op* `(`⋯ *op* `(`$\mathtt{E}_{N-1}$ *op* $\mathtt{E}_N$`))` for a
|
|
|
|
| 206 |
E`)))` for a binary right fold.
|
| 207 |
|
| 208 |
In each case, *op* is the *fold-operator*, N is the number of elements
|
| 209 |
in the pack expansion parameters, and each Eᵢ is generated by
|
| 210 |
instantiating the pattern and replacing each pack expansion parameter
|
| 211 |
+
with its iᵗʰ element. For a binary fold-expression, E is generated by
|
| 212 |
instantiating the *cast-expression* that did not contain an unexpanded
|
| 213 |
+
pack.
|
| 214 |
|
| 215 |
+
[*Example 7*:
|
| 216 |
|
| 217 |
``` cpp
|
| 218 |
template<typename ...Args>
|
| 219 |
bool all(Args ...args) { return (... && args); }
|
| 220 |
|
|
|
|
| 225 |
`((true && true) && true) && false`, which evaluates to `false`.
|
| 226 |
|
| 227 |
— *end example*]
|
| 228 |
|
| 229 |
If N is zero for a unary fold-expression, the value of the expression is
|
| 230 |
+
shown in [[temp.fold.empty]]; if the operator is not listed in
|
| 231 |
+
[[temp.fold.empty]], the instantiation is ill-formed.
|
| 232 |
|
| 233 |
+
**Table: Value of folding empty sequences** <a id="temp.fold.empty">[temp.fold.empty]</a>
|
| 234 |
|
| 235 |
+
| Operator | Value when pack is empty |
|
| 236 |
+
| -------- | ------------------------ |
|
| 237 |
| `&&` | `true` |
|
| 238 |
| `||` | `false` |
|
| 239 |
| `,` | `void()` |
|
| 240 |
|
| 241 |
|