|
4960 | 4960 | \item
|
4961 | 4961 | If the initializer list is a brace-enclosed \grammarterm{initializer-list},
|
4962 | 4962 | the explicitly initialized elements of the aggregate
|
4963 |
| -are the first $n$ elements of the aggregate, |
4964 |
| -where $n$ is the number of elements in the initializer list. |
| 4963 | +are those for which an element of the initializer list |
| 4964 | +appertains to the aggregate element or to a subobject thereof (see below). |
4965 | 4965 | \item
|
4966 | 4966 | Otherwise, the initializer list must be \tcode{\{\}},
|
4967 | 4967 | and there are no explicitly initialized elements.
|
|
4992 | 4992 | initializes \tcode{c.a} with 1 and \tcode{c.x} with 3.
|
4993 | 4993 | \end{example}
|
4994 | 4994 | \item
|
4995 |
| -Otherwise, the element is copy-initialized |
4996 |
| -from the corresponding \grammarterm{initializer-clause} |
4997 |
| -or is initialized with the \grammarterm{brace-or-equal-initializer} |
| 4995 | +Otherwise, if the initializer list is |
| 4996 | +a brace-enclosed \grammarterm{designated-initializer-list}, |
| 4997 | +the element is initialized with the \grammarterm{brace-or-equal-initializer} |
4998 | 4998 | of the corresponding \grammarterm{designated-initializer-clause}.
|
4999 | 4999 | If that initializer is of the form
|
5000 |
| -\grammarterm{assignment-expression} or |
5001 | 5000 | \tcode{= }\grammarterm{assignment-expression}
|
5002 | 5001 | and
|
5003 | 5002 | a narrowing conversion\iref{dcl.init.list} is required
|
5004 | 5003 | to convert the expression, the program is ill-formed.
|
5005 | 5004 | \begin{note}
|
5006 |
| -If the initialization is by \grammarterm{designated-initializer-clause}, |
5007 |
| -its form determines whether copy-initialization or direct-initialization |
5008 |
| -is performed. |
| 5005 | +The form of the initializer determines |
| 5006 | +whether copy-initialization or direct-initialization is performed. |
5009 | 5007 | \end{note}
|
| 5008 | +\item |
| 5009 | +Otherwise, |
| 5010 | +the initializer list is a brace-enclosed \grammarterm{initializer-list}. |
| 5011 | +If an \grammarterm{initializer-clause} appertains to the aggregate element, |
| 5012 | +then the aggregate element is copy-initialized from the \grammarterm{initializer-clause}. |
| 5013 | +Otherwise, |
| 5014 | +the aggregate element is copy-initialized |
| 5015 | +from a brace-enclosed \grammarterm{initializer-list} |
| 5016 | +consisting of all of the \grammarterm{initializer-clause}s |
| 5017 | +that appertain to subobjects of the aggregate element, |
| 5018 | +in the order of appearance. |
5010 | 5019 | \begin{note}
|
5011 | 5020 | If an initializer is itself an initializer list,
|
5012 | 5021 | the element is list-initialized, which will result in a recursive application
|
|
5096 | 5105 | with the value of an expression of the form
|
5097 | 5106 | \tcode{int\{\}}
|
5098 | 5107 | (that is, \tcode{0}), and \tcode{ss.d} with the value of \tcode{ss.b[ss.a]}
|
5099 |
| -(that is, \tcode{'s'}), and in |
5100 |
| -\begin{codeblock} |
5101 |
| -struct X { int i, j, k = 42; }; |
5102 |
| -X a[] = { 1, 2, 3, 4, 5, 6 }; |
5103 |
| -X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } }; |
5104 |
| -\end{codeblock} |
5105 |
| -\tcode{a} and \tcode{b} have the same value |
| 5108 | +(that is, \tcode{'s'}). |
5106 | 5109 |
|
5107 | 5110 | \begin{codeblock}
|
5108 | 5111 | struct A {
|
|
5144 | 5147 | \end{note}
|
5145 | 5148 |
|
5146 | 5149 | \pnum
|
5147 |
| -An array of unknown bound initialized with a |
5148 |
| -brace-enclosed |
5149 |
| -\grammarterm{initializer-list} |
5150 |
| -containing |
5151 |
| -\tcode{n} |
5152 |
| -\grammarterm{initializer-clause}{s} |
5153 |
| -is defined as having |
5154 |
| -\tcode{n} |
5155 |
| -elements\iref{dcl.array}. |
| 5150 | +The number of elements\iref{dcl.array} in an array of unknown bound |
| 5151 | +initialized with a brace-enclosed \grammarterm{initializer-list} |
| 5152 | +is the number of explicitly initialized elements of the array. |
5156 | 5153 | \begin{example}
|
5157 | 5154 | \begin{codeblock}
|
5158 | 5155 | int x[] = { 1, 3, 5 };
|
|
5162 | 5159 | as a one-dimensional array that has three elements
|
5163 | 5160 | since no size was specified and there are three initializers.
|
5164 | 5161 | \end{example}
|
| 5162 | +\begin{example} |
| 5163 | +In |
| 5164 | +\begin{codeblock} |
| 5165 | +struct X { int i, j, k; }; |
| 5166 | +X a[] = { 1, 2, 3, 4, 5, 6 }; |
| 5167 | +X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } }; |
| 5168 | +\end{codeblock} |
| 5169 | +\tcode{a} and \tcode{b} have the same value. |
| 5170 | +\end{example} |
5165 | 5171 | An array of unknown bound shall not be initialized with
|
5166 | 5172 | an empty \grammarterm{braced-init-list} \tcode{\{\}}.
|
5167 | 5173 | \begin{footnote}
|
|
5209 | 5215 | \end{example}
|
5210 | 5216 | \end{note}
|
5211 | 5217 |
|
5212 |
| -\pnum |
5213 |
| -An |
5214 |
| -\grammarterm{initializer-list} |
5215 |
| -is ill-formed if the number of |
5216 |
| -\grammarterm{initializer-clause}{s} |
5217 |
| -exceeds the number of elements of the aggregate. |
5218 |
| -\begin{example} |
5219 |
| -\begin{codeblock} |
5220 |
| -char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error |
5221 |
| -\end{codeblock} |
5222 |
| -is ill-formed. |
5223 |
| -\end{example} |
5224 |
| - |
5225 | 5218 | \pnum
|
5226 | 5219 | If a member has a default member initializer
|
5227 | 5220 | and a potentially-evaluated subexpression thereof is an aggregate
|
|
5243 | 5236 | \end{codeblock}
|
5244 | 5237 | \end{example}
|
5245 | 5238 |
|
5246 |
| -\pnum |
5247 |
| -If an aggregate class \tcode{C} contains a subaggregate element |
5248 |
| -\tcode{e} with no elements, |
5249 |
| -the \grammarterm{initializer-clause} for \tcode{e} shall not be |
5250 |
| -omitted from an \grammarterm{initializer-list} for an object of type |
5251 |
| -\tcode{C} unless the \grammarterm{initializer-clause}{s} for all |
5252 |
| -elements of \tcode{C} following \tcode{e} are also omitted. |
5253 |
| -\begin{example} |
5254 |
| -\begin{codeblock} |
5255 |
| -struct S { } s; |
5256 |
| -struct A { |
5257 |
| - S s1; |
5258 |
| - int i1; |
5259 |
| - S s2; |
5260 |
| - int i2; |
5261 |
| - S s3; |
5262 |
| - int i3; |
5263 |
| -} a = { |
5264 |
| - { }, // Required initialization |
5265 |
| - 0, |
5266 |
| - s, // Required initialization |
5267 |
| - 0 |
5268 |
| -}; // Initialization not required for \tcode{A::s3} because \tcode{A::i3} is also not initialized |
5269 |
| -\end{codeblock} |
5270 |
| -\end{example} |
5271 |
| - |
5272 | 5239 | \pnum
|
5273 | 5240 | When initializing a multidimensional array,
|
5274 | 5241 | the
|
|
5306 | 5273 | \end{example}
|
5307 | 5274 |
|
5308 | 5275 | \pnum
|
5309 |
| -Braces can be elided in an |
5310 |
| -\grammarterm{initializer-list} |
5311 |
| -as follows. |
5312 |
| -If the |
5313 |
| -\grammarterm{initializer-list} |
5314 |
| -begins with a left brace, |
5315 |
| -then the succeeding comma-separated list of |
5316 |
| -\grammarterm{initializer-clause}{s} |
5317 |
| -initializes the elements of a subaggregate; |
5318 |
| -it is erroneous for there to be more |
5319 |
| -\grammarterm{initializer-clause}{s} |
5320 |
| -than elements. |
5321 |
| -If, however, the |
5322 |
| -\grammarterm{initializer-list} |
5323 |
| -for a subaggregate does not begin with a left brace, |
5324 |
| -then only enough |
5325 |
| -\grammarterm{initializer-clause}{s} |
5326 |
| -from the list are taken to initialize the elements of the subaggregate; |
5327 |
| -any remaining |
5328 |
| -\grammarterm{initializer-clause}{s} |
5329 |
| -are left to initialize the next element of the aggregate |
5330 |
| -of which the current subaggregate is an element. |
| 5276 | +Each \grammarterm{initializer-clause} in |
| 5277 | +a brace-enclosed \grammarterm{initializer-list} |
| 5278 | +is said to \defn{appertain} |
| 5279 | +to an element of the aggregate being initialized or |
| 5280 | +to an element of one of its subaggregates. |
| 5281 | +Considering the sequence of \grammarterm{initializer-clause}s, |
| 5282 | +and the sequence of aggregate elements |
| 5283 | +initially formed as the sequence of elements of the aggregate being initialized |
| 5284 | +and potentially modified as described below, |
| 5285 | +each \grammarterm{initializer-clause} appertains to |
| 5286 | +the corresponding aggregate element if |
| 5287 | +\begin{itemize} |
| 5288 | +\item |
| 5289 | +the aggregate element is not an aggregate, or |
| 5290 | +\item |
| 5291 | +the \grammarterm{initializer-clause} begins with a left brace, or |
| 5292 | +\item |
| 5293 | +the \grammarterm{initializer-clause} is an expression and |
| 5294 | +an implicit conversion sequence can be formed |
| 5295 | +that converts the expression to the type of the aggregate element, or |
| 5296 | +\item |
| 5297 | +the aggregate element is an aggregate that itself has no aggregate elements. |
| 5298 | +\end{itemize} |
| 5299 | +Otherwise, |
| 5300 | +the aggregate element is an aggregate and |
| 5301 | +that subaggregate is replaced in the list of aggregate elements by |
| 5302 | +the sequence of its own aggregate elements, and |
| 5303 | +the appertainment analysis resumes with |
| 5304 | +the first such element and the same \grammarterm{initializer-clause}. |
| 5305 | +\begin{note} |
| 5306 | +These rules apply recursively to the aggregate's subaggregates. |
| 5307 | +\begin{example} |
| 5308 | +In |
| 5309 | +\begin{codeblock} |
| 5310 | +struct S1 { int a, b; }; |
| 5311 | +struct S2 { S1 s, t; }; |
| 5312 | + |
| 5313 | +S2 x[2] = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 5314 | +S2 y[2] = { |
| 5315 | + { |
| 5316 | + { 1, 2 }, |
| 5317 | + { 3, 4 } |
| 5318 | + }, |
| 5319 | + { |
| 5320 | + { 5, 6 }, |
| 5321 | + { 7, 8 } |
| 5322 | + } |
| 5323 | +}; |
| 5324 | +\end{codeblock} |
| 5325 | +\tcode{x} and \tcode{y} have the same value. |
| 5326 | +\end{example} |
| 5327 | +\end{note} |
| 5328 | + |
| 5329 | +This process continues |
| 5330 | +until all \grammarterm{initializer-clause}s have been exhausted. |
| 5331 | + |
| 5332 | +If any \grammarterm{initializer-clause} remains |
| 5333 | +that does not appertain to |
| 5334 | +an element of the aggregate or one of its subaggregates, |
| 5335 | +the program is ill-formed. |
| 5336 | +\begin{example} |
| 5337 | +\begin{codeblock} |
| 5338 | +char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error: too many initializers |
| 5339 | +\end{codeblock} |
| 5340 | +\end{example} |
| 5341 | + |
| 5342 | +\pnum |
5331 | 5343 | \begin{example}
|
5332 | 5344 | \begin{codeblock}
|
5333 | 5345 | float y[4][3] = {
|
|
5382 | 5394 | \end{example}
|
5383 | 5395 |
|
5384 | 5396 | \pnum
|
5385 |
| -All implicit type conversions\iref{conv} are considered when |
5386 |
| -initializing the element with an \grammarterm{assignment-expression}. |
5387 |
| -If the |
5388 |
| -\grammarterm{assignment-expression} |
5389 |
| -can initialize an element, the element is initialized. |
5390 |
| -Otherwise, if the element is itself a subaggregate, |
5391 |
| -brace elision is assumed and the |
5392 |
| -\grammarterm{assignment-expression} |
5393 |
| -is considered for the initialization of the first element of the subaggregate. |
5394 | 5397 | \begin{note}
|
5395 |
| -As specified above, brace elision cannot apply to |
5396 |
| -subaggregates with no elements; an |
5397 |
| -\grammarterm{initializer-clause} for the entire subobject is needed. |
| 5398 | +The initializer for an empty subaggregate is required |
| 5399 | +if any initializers are provided for subsequent elements. |
| 5400 | +\begin{example} |
| 5401 | +\begin{codeblock} |
| 5402 | +struct S { } s; |
| 5403 | +struct A { |
| 5404 | + S s1; |
| 5405 | + int i1; |
| 5406 | + S s2; |
| 5407 | + int i2; |
| 5408 | + S s3; |
| 5409 | + int i3; |
| 5410 | +} a = { |
| 5411 | + { }, // Required initialization |
| 5412 | + 0, |
| 5413 | + s, // Required initialization |
| 5414 | + 0 |
| 5415 | +}; // Initialization not required for \tcode{A::s3} because \tcode{A::i3} is also not initialized |
| 5416 | +\end{codeblock} |
| 5417 | +\end{example} |
5398 | 5418 | \end{note}
|
5399 | 5419 |
|
| 5420 | +\pnum |
5400 | 5421 | \begin{example}
|
5401 | 5422 | \begin{codeblock}
|
5402 | 5423 | struct A {
|
|
5410 | 5431 | A a;
|
5411 | 5432 | B b = { 4, a, a };
|
5412 | 5433 | \end{codeblock}
|
5413 |
| - |
5414 | 5434 | Braces are elided around the
|
5415 | 5435 | \grammarterm{initializer-clause}
|
5416 | 5436 | for
|
|
0 commit comments