|
68 | 68 | \end{bnf} |
69 | 69 |
|
70 | 70 | \begin{bnf} |
71 | | -\nontermdef{attributed-identifier}\br |
72 | | - identifier \opt{attribute-specifier-seq} |
| 71 | +\nontermdef{sb-identifier}\br |
| 72 | + \opt{\terminal{...}} identifier \opt{attribute-specifier-seq} |
73 | 73 | \end{bnf} |
74 | 74 |
|
75 | 75 | \begin{bnf} |
76 | | -\nontermdef{attributed-identifier-list}\br |
77 | | - attributed-identifier\br |
78 | | - attributed-identifier-list \terminal{,} attributed-identifier |
| 76 | +\nontermdef{sb-identifier-list}\br |
| 77 | + sb-identifier\br |
| 78 | + sb-identifier-list \terminal{,} sb-identifier |
79 | 79 | \end{bnf} |
80 | 80 |
|
81 | 81 | \begin{bnf} |
82 | 82 | \nontermdef{structured-binding-declaration}\br |
83 | | - \opt{attribute-specifier-seq} decl-specifier-seq \opt{ref-qualifier} \terminal{[} attributed-identifier-list \terminal{]} |
| 83 | + \opt{attribute-specifier-seq} decl-specifier-seq \opt{ref-qualifier} \terminal{[} sb-identifier-list \terminal{]} |
84 | 84 | \end{bnf} |
85 | 85 |
|
86 | 86 | \begin{bnf} |
|
218 | 218 | \tcode{thread_local}, |
219 | 219 | \tcode{auto}\iref{dcl.spec.auto}, or |
220 | 220 | a \grammarterm{cv-qualifier}. |
| 221 | +The declaration shall contain at most one \grammarterm{sb-identifier} |
| 222 | +whose \grammarterm{identifier} is preceded by an ellipsis. |
| 223 | +If the declaration contains any such \grammarterm{sb-identifier}, |
| 224 | +it shall declare a templated entity\iref{temp.pre}. |
221 | 225 | \begin{example} |
222 | 226 | \begin{codeblock} |
223 | 227 | template<class T> concept C = true; |
|
7040 | 7044 |
|
7041 | 7045 | \pnum |
7042 | 7046 | A structured binding declaration introduces the \grammarterm{identifier}{s} |
7043 | | -$\tcode{v}_0$, $\tcode{v}_1$, $\tcode{v}_2, \dotsc$ |
| 7047 | +$\tcode{v}_0$, $\tcode{v}_1$, $\tcode{v}_2, \dotsc, \tcode{v}_{N-1}$ |
7044 | 7048 | of the |
7045 | | -\grammarterm{attributed-identifier-list} as names |
7046 | | -of \defn{structured binding}{s}. |
| 7049 | +\grammarterm{sb-identifier-list} as names. |
| 7050 | +An \grammarterm{sb-identifier} that contains an ellipsis |
| 7051 | +introduces a structured binding pack\iref{temp.variadic}. |
| 7052 | +A \defn{structured binding} is either |
| 7053 | +an \grammarterm{sb-identifier} that does not contain an ellipsis or |
| 7054 | +an element of a structured binding pack. |
7047 | 7055 | The optional \grammarterm{attribute-specifier-seq} of |
7048 | | -an \grammarterm{attributed-identifier} |
7049 | | -appertains to the structured binding so introduced. |
| 7056 | +an \grammarterm{sb-identifier} |
| 7057 | +appertains to the associated structured bindings. |
7050 | 7058 | Let \cv{} denote the \grammarterm{cv-qualifier}{s} in |
7051 | 7059 | the \grammarterm{decl-specifier-seq} and |
7052 | 7060 | \placeholder{S} consist of |
|
7080 | 7088 | \tcode{E} is never a reference type\iref{expr.prop}. |
7081 | 7089 | \end{note} |
7082 | 7090 |
|
| 7091 | +\pnum |
| 7092 | +The \defn{structured binding size} of \tcode{E}, as defined below, |
| 7093 | +is the number of structured bindings |
| 7094 | +that need to be introduced by the structured binding declaration. |
| 7095 | +If there is no structured binding pack, |
| 7096 | +then the number of elements in the \grammarterm{sb-identifier-list} |
| 7097 | +shall be equal to the structured binding size of \tcode{E}. |
| 7098 | +Otherwise, the number of non-pack elements shall be no more than |
| 7099 | +the structured binding size of \tcode{E}; |
| 7100 | +the number of elements of the structured binding pack is |
| 7101 | +the structured binding size of \tcode{E} less |
| 7102 | +the number of non-pack elements in the\grammarterm{sb-identifier-list}. |
| 7103 | + |
| 7104 | +\pnum |
| 7105 | +Let $\textrm{SB}_i$ denote |
| 7106 | +the $i^\textrm{th}$ structured binding in the structured binding declaration |
| 7107 | +after expanding the structured binding pack, if any. |
| 7108 | +\begin{note} |
| 7109 | +If there is no structured binding pack, |
| 7110 | +then $\textrm{SB}_i$ denotes $\tcode{v}_i$. |
| 7111 | +\end{note} |
| 7112 | +\begin{example} |
| 7113 | +\begin{codeblock} |
| 7114 | +struct C { int x, y, z; }; |
| 7115 | + |
| 7116 | +template<class T> |
| 7117 | +void now_i_know_my() { |
| 7118 | + 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} |
| 7119 | + 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$ |
| 7120 | + 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} |
| 7121 | + auto [h, i, j, ...k] = C(); // OK, the pack \tcode{k} is empty |
| 7122 | + auto [l, m, n, o, ...p] = C(); // error: structured binding size is too small |
| 7123 | +} |
| 7124 | +\end{codeblock} |
| 7125 | +\end{example} |
| 7126 | + |
7083 | 7127 | \pnum |
7084 | 7128 | If a structured binding declaration appears as a \grammarterm{condition}, |
7085 | 7129 | the decision variable\iref{stmt.pre} of the condition is \exposid{e}. |
|
7090 | 7134 | the program is ill-formed. |
7091 | 7135 |
|
7092 | 7136 | \pnum |
7093 | | -If \tcode{E} is an array type with element type \tcode{T}, the number |
7094 | | -of elements in the \grammarterm{attributed-identifier-list} shall be equal to the |
7095 | | -number of elements of \tcode{E}. Each $\tcode{v}_i$ is the name of an |
| 7137 | +If \tcode{E} is an array type with element type \tcode{T}, |
| 7138 | +the structured binding size of \tcode{E} is equal to the |
| 7139 | +number of elements of \tcode{E}. |
| 7140 | +Each $\textrm{SB}_i$ is the name of an |
7096 | 7141 | lvalue that refers to the element $i$ of the array and whose type |
7097 | 7142 | is \tcode{T}; the referenced type is \tcode{T}. |
7098 | 7143 | \begin{note} |
|
7103 | 7148 | auto f() -> int(&)[2]; |
7104 | 7149 | auto [ x, y ] = f(); // \tcode{x} and \tcode{y} refer to elements in a copy of the array return value |
7105 | 7150 | auto& [ xr, yr ] = f(); // \tcode{xr} and \tcode{yr} refer to elements in the array referred to by \tcode{f}'s return value |
| 7151 | + |
| 7152 | +auto g() -> int(&)[4]; |
| 7153 | + |
| 7154 | +template<size_t N> |
| 7155 | +void h(int (&arr)[N]) { |
| 7156 | + auto [a, ...b, c] = arr; // \tcode{a} names the first element of the array, \tcode{b} is a pack referring to the second and |
| 7157 | + // third elements, and \tcode{c} names the fourth element |
| 7158 | + auto& [...e] = arr; // \tcode{e} is a pack referring to the four elements of the array |
| 7159 | +} |
| 7160 | + |
| 7161 | +void call_h() { |
| 7162 | + h(g()); |
| 7163 | +} |
7106 | 7164 | \end{codeblock} |
7107 | 7165 | \end{example} |
7108 | 7166 |
|
|
7113 | 7171 | the expression \tcode{std::tuple_size<E>::value} |
7114 | 7172 | shall be a well-formed integral constant expression |
7115 | 7173 | and |
7116 | | -the number of elements in |
7117 | | -the \grammarterm{attributed-identifier-list} shall be equal to the value of that |
| 7174 | +the structured binding size of \tcode{E} is equal to the value of that |
7118 | 7175 | expression. |
7119 | 7176 | Let \tcode{i} be an index prvalue of type \tcode{std::size_t} |
7120 | 7177 | corresponding to $\tcode{v}_i$. |
|
7144 | 7201 | \placeholder{S} \terminal{U$_i$ r$_i$ =} initializer \terminal{;} |
7145 | 7202 | \end{ncbnf} |
7146 | 7203 |
|
7147 | | -Each $\tcode{v}_i$ is the name of an lvalue of type $\tcode{T}_i$ |
| 7204 | +Each $\textrm{SB}_i$ is the name of an lvalue of type $\tcode{T}_i$ |
7148 | 7205 | that refers to the object bound to $\tcode{r}_i$; |
7149 | 7206 | the referenced type is $\tcode{T}_i$. |
7150 | 7207 | The initialization of \exposid{e} and |
|
7162 | 7219 | well-formed when named as \tcode{\exposidnc{e}.\placeholder{name}} |
7163 | 7220 | in the context of the structured binding, |
7164 | 7221 | \tcode{E} shall not have an anonymous union member, and |
7165 | | -the number of elements in the \grammarterm{attributed-identifier-list} shall be |
| 7222 | +the structured binding size of \tcode{E} is |
7166 | 7223 | equal to the number of non-static data members of \tcode{E}. |
7167 | 7224 | Designating the non-static data members of \tcode{E} as |
7168 | 7225 | $\tcode{m}_0$, $\tcode{m}_1$, $\tcode{m}_2, \dotsc$ |
|
0 commit comments