|
69 | 69 | \end{bnf} |
70 | 70 |
|
71 | 71 | \begin{bnf} |
72 | | -\nontermdef{attributed-identifier}\br |
73 | | - identifier \opt{attribute-specifier-seq} |
| 72 | +\nontermdef{sb-identifier}\br |
| 73 | + \opt{\terminal{...}} identifier \opt{attribute-specifier-seq} |
74 | 74 | \end{bnf} |
75 | 75 |
|
76 | 76 | \begin{bnf} |
77 | | -\nontermdef{attributed-identifier-list}\br |
78 | | - attributed-identifier\br |
79 | | - attributed-identifier-list \terminal{,} attributed-identifier |
| 77 | +\nontermdef{sb-identifier-list}\br |
| 78 | + sb-identifier\br |
| 79 | + sb-identifier-list \terminal{,} sb-identifier |
80 | 80 | \end{bnf} |
81 | 81 |
|
82 | 82 | \begin{bnf} |
83 | 83 | \nontermdef{structured-binding-declaration}\br |
84 | | - \opt{attribute-specifier-seq} decl-specifier-seq \opt{ref-qualifier} \terminal{[} attributed-identifier-list \terminal{]} |
| 84 | + \opt{attribute-specifier-seq} decl-specifier-seq \opt{ref-qualifier} \terminal{[} sb-identifier-list \terminal{]} |
85 | 85 | \end{bnf} |
86 | 86 |
|
87 | 87 | \begin{bnf} |
|
217 | 217 | \tcode{thread_local}, |
218 | 218 | \tcode{auto}\iref{dcl.spec.auto}, or |
219 | 219 | a \grammarterm{cv-qualifier}. |
| 220 | +The declaration shall contain at most one \grammarterm{sb-identifier} |
| 221 | +whose \grammarterm{identifier} is preceded by an ellipsis. |
| 222 | +If the declaration contains any such \grammarterm{sb-identifier}, |
| 223 | +it shall declare a templated entity\iref{temp.pre}. |
220 | 224 | \begin{example} |
221 | 225 | \begin{codeblock} |
222 | 226 | template<class T> concept C = true; |
|
7019 | 7023 |
|
7020 | 7024 | \pnum |
7021 | 7025 | A structured binding declaration introduces the \grammarterm{identifier}{s} |
7022 | | -$\tcode{v}_0$, $\tcode{v}_1$, $\tcode{v}_2, \dotsc$ |
| 7026 | +$\tcode{v}_0$, $\tcode{v}_1$, $\tcode{v}_2, \dotsc, \tcode{v}_{N-1}$ |
7023 | 7027 | of the |
7024 | | -\grammarterm{attributed-identifier-list} as names |
7025 | | -of \defn{structured binding}{s}. |
| 7028 | +\grammarterm{sb-identifier-list} as names. |
| 7029 | +An \grammarterm{sb-identifier} that contains an ellipsis |
| 7030 | +introduces a structured binding pack\iref{temp.variadic}. |
| 7031 | +A \defn{structured binding} is either |
| 7032 | +an \grammarterm{sb-identifier} that does not contain an ellipsis or |
| 7033 | +an element of a structured binding pack. |
7026 | 7034 | The optional \grammarterm{attribute-specifier-seq} of |
7027 | | -an \grammarterm{attributed-identifier} |
7028 | | -appertains to the structured binding so introduced. |
| 7035 | +an \grammarterm{sb-identifier} |
| 7036 | +appertains to the associated structured bindings. |
7029 | 7037 | Let \cv{} denote the \grammarterm{cv-qualifier}{s} in |
7030 | 7038 | the \grammarterm{decl-specifier-seq} and |
7031 | 7039 | \placeholder{S} consist of the \grammarterm{storage-class-specifier}{s} of |
|
7057 | 7065 | \tcode{E} is never a reference type\iref{expr.prop}. |
7058 | 7066 | \end{note} |
7059 | 7067 |
|
| 7068 | +\pnum |
| 7069 | +The \defn{structured binding size} of \tcode{E}, as defined below, |
| 7070 | +is the number of structured bindings |
| 7071 | +that need to be introduced by the structured binding declaration. |
| 7072 | +If there is no structured binding pack, |
| 7073 | +then the number of elements in the \grammarterm{sb-identifier-list} |
| 7074 | +shall be equal to the structured binding size of \tcode{E}. |
| 7075 | +Otherwise, the number of non-pack elements shall be no more than |
| 7076 | +the structured binding size of \tcode{E}; |
| 7077 | +the number of elements of the structured binding pack is |
| 7078 | +the structured binding size of \tcode{E} less |
| 7079 | +the number of non-pack elements in the\grammarterm{sb-identifier-list}. |
| 7080 | + |
| 7081 | +\pnum |
| 7082 | +Let $\textrm{SB}_i$ denote |
| 7083 | +the $i^\textrm{th}$ structured binding in the structured binding declaration |
| 7084 | +after expanding the structured binding pack, if any. |
| 7085 | +\begin{note} |
| 7086 | +If there is no structured binding pack, |
| 7087 | +then $\textrm{SB}_i$ denotes $\tcode{v}_i$. |
| 7088 | +\end{note} |
| 7089 | +\begin{example} |
| 7090 | +\begin{codeblock} |
| 7091 | +struct C { int x, y, z; }; |
| 7092 | + |
| 7093 | +template<class T> |
| 7094 | +void now_i_know_my() { |
| 7095 | + auto [a, b, c] = C(); // OK, $\textrm{SB}_0$ is \tcode{a}, $\textrm{SB}_1$ is \tcode{b}, and $\textrm{SB}_2$ is \tcode{c} |
| 7096 | + auto [d, ...e] = C(); // OK, $\textrm{SB}_0$ is \tcode{d}, the pack \tcode{e} $(\tcode{v}_1)$ contains two structured bindings: $\textrm{SB}_1$ and $\textrm{SB}_2$ |
| 7097 | + auto [...f, g] = C(); // OK, the pack \tcode{f} $(\tcode{v}_0)$ contains two structured bindings: $\textrm{SB}_0$ and $\textrm{SB}_1$, and $\textrm{SB}_2$ is \tcode{g} |
| 7098 | + auto [h, i, j, ...k] = C(); // OK, the pack \tcode{k} is empty |
| 7099 | + auto [l, m, n, o, ...p] = C(); // error: structured binding size is too small |
| 7100 | +} |
| 7101 | +\end{codeblock} |
| 7102 | +\end{example} |
| 7103 | + |
7060 | 7104 | \pnum |
7061 | 7105 | If a structured binding declaration appears as a \grammarterm{condition}, |
7062 | 7106 | the decision variable\iref{stmt.pre} of the condition is \exposid{e}. |
|
7067 | 7111 | the program is ill-formed. |
7068 | 7112 |
|
7069 | 7113 | \pnum |
7070 | | -If \tcode{E} is an array type with element type \tcode{T}, the number |
7071 | | -of elements in the \grammarterm{attributed-identifier-list} shall be equal to the |
7072 | | -number of elements of \tcode{E}. Each $\tcode{v}_i$ is the name of an |
| 7114 | +If \tcode{E} is an array type with element type \tcode{T}, |
| 7115 | +the structured binding size of \tcode{E} is equal to the |
| 7116 | +number of elements of \tcode{E}. |
| 7117 | +Each $\textrm{SB}_i$ is the name of an |
7073 | 7118 | lvalue that refers to the element $i$ of the array and whose type |
7074 | 7119 | is \tcode{T}; the referenced type is \tcode{T}. |
7075 | 7120 | \begin{note} |
|
7080 | 7125 | auto f() -> int(&)[2]; |
7081 | 7126 | auto [ x, y ] = f(); // \tcode{x} and \tcode{y} refer to elements in a copy of the array return value |
7082 | 7127 | auto& [ xr, yr ] = f(); // \tcode{xr} and \tcode{yr} refer to elements in the array referred to by \tcode{f}'s return value |
| 7128 | + |
| 7129 | +auto g() -> int(&)[4]; |
| 7130 | + |
| 7131 | +template<size_t N> |
| 7132 | +void h(int (&arr)[N]) { |
| 7133 | + auto [a, ...b, c] = arr; // \tcode{a} names the first element of the array, \tcode{b} is a pack referring to the second and |
| 7134 | + // third elements, and \tcode{c} names the fourth element |
| 7135 | + auto& [...e] = arr; // \tcode{e} is a pack referring to the four elements of the array |
| 7136 | +} |
| 7137 | + |
| 7138 | +void call_h() { |
| 7139 | + h(g()); |
| 7140 | +} |
7083 | 7141 | \end{codeblock} |
7084 | 7142 | \end{example} |
7085 | 7143 |
|
|
7090 | 7148 | the expression \tcode{std::tuple_size<E>::value} |
7091 | 7149 | shall be a well-formed integral constant expression |
7092 | 7150 | and |
7093 | | -the number of elements in |
7094 | | -the \grammarterm{attributed-identifier-list} shall be equal to the value of that |
| 7151 | +the structured binding size of \tcode{E} is equal to the value of that |
7095 | 7152 | expression. |
7096 | 7153 | Let \tcode{i} be an index prvalue of type \tcode{std::size_t} |
7097 | 7154 | corresponding to $\tcode{v}_i$. |
|
7121 | 7178 | \placeholder{S} \terminal{U$_i$ r$_i$ =} initializer \terminal{;} |
7122 | 7179 | \end{ncbnf} |
7123 | 7180 |
|
7124 | | -Each $\tcode{v}_i$ is the name of an lvalue of type $\tcode{T}_i$ |
| 7181 | +Each $\textrm{SB}_i$ is the name of an lvalue of type $\tcode{T}_i$ |
7125 | 7182 | that refers to the object bound to $\tcode{r}_i$; |
7126 | 7183 | the referenced type is $\tcode{T}_i$. |
7127 | 7184 | The initialization of \exposid{e} and |
|
7139 | 7196 | well-formed when named as \tcode{\exposidnc{e}.\placeholder{name}} |
7140 | 7197 | in the context of the structured binding, |
7141 | 7198 | \tcode{E} shall not have an anonymous union member, and |
7142 | | -the number of elements in the \grammarterm{attributed-identifier-list} shall be |
| 7199 | +the structured binding size of \tcode{E} is |
7143 | 7200 | equal to the number of non-static data members of \tcode{E}. |
7144 | 7201 | Designating the non-static data members of \tcode{E} as |
7145 | 7202 | $\tcode{m}_0$, $\tcode{m}_1$, $\tcode{m}_2, \dotsc$ |
|
0 commit comments