|
4997 | 4997 | \item
|
4998 | 4998 | If the initializer list is a brace-enclosed \grammarterm{initializer-list},
|
4999 | 4999 | the explicitly initialized elements of the aggregate
|
5000 |
| -are the first $n$ elements of the aggregate, |
5001 |
| -where $n$ is the number of elements in the initializer list. |
| 5000 | +are those for which an element of the initializer list |
| 5001 | +appertains to the aggregate element or to a subobject thereof (see below). |
5002 | 5002 | \item
|
5003 | 5003 | Otherwise, the initializer list must be \tcode{\{\}},
|
5004 | 5004 | and there are no explicitly initialized elements.
|
|
5029 | 5029 | initializes \tcode{c.a} with 1 and \tcode{c.x} with 3.
|
5030 | 5030 | \end{example}
|
5031 | 5031 | \item
|
5032 |
| -Otherwise, the element is copy-initialized |
5033 |
| -from the corresponding \grammarterm{initializer-clause} |
5034 |
| -or is initialized with the \grammarterm{brace-or-equal-initializer} |
| 5032 | +Otherwise, if the initializer list is |
| 5033 | +a brace-enclosed \grammarterm{designated-initializer-list}, |
| 5034 | +the element is initialized with the \grammarterm{brace-or-equal-initializer} |
5035 | 5035 | of the corresponding \grammarterm{designated-initializer-clause}.
|
5036 | 5036 | If that initializer is of the form
|
5037 |
| -\grammarterm{assignment-expression} or |
5038 | 5037 | \tcode{= }\grammarterm{assignment-expression}
|
5039 | 5038 | and
|
5040 | 5039 | a narrowing conversion\iref{dcl.init.list} is required
|
5041 | 5040 | to convert the expression, the program is ill-formed.
|
5042 | 5041 | \begin{note}
|
5043 |
| -If the initialization is by \grammarterm{designated-initializer-clause}, |
5044 |
| -its form determines whether copy-initialization or direct-initialization |
5045 |
| -is performed. |
| 5042 | +The form of the initializer determines |
| 5043 | +whether copy-initialization or direct-initialization is performed. |
5046 | 5044 | \end{note}
|
| 5045 | +\item |
| 5046 | +Otherwise, |
| 5047 | +the initializer list is a brace-enclosed \grammarterm{initializer-list}. |
| 5048 | +If an \grammarterm{initializer-clause} appertains to the aggregate element, |
| 5049 | +then the aggregate element is copy-initialized from the \grammarterm{initializer-clause}. |
| 5050 | +Otherwise, |
| 5051 | +the aggregate element is copy-initialized |
| 5052 | +from a brace-enclosed \grammarterm{initializer-list} |
| 5053 | +consisting of all of the \grammarterm{initializer-clause}s |
| 5054 | +that appertain to subobjects of the aggregate element, |
| 5055 | +in the order of appearance. |
5047 | 5056 | \begin{note}
|
5048 | 5057 | If an initializer is itself an initializer list,
|
5049 | 5058 | the element is list-initialized, which will result in a recursive application
|
|
5133 | 5142 | with the value of an expression of the form
|
5134 | 5143 | \tcode{int\{\}}
|
5135 | 5144 | (that is, \tcode{0}), and \tcode{ss.d} with the value of \tcode{ss.b[ss.a]}
|
5136 |
| -(that is, \tcode{'s'}), and in |
5137 |
| -\begin{codeblock} |
5138 |
| -struct X { int i, j, k = 42; }; |
5139 |
| -X a[] = { 1, 2, 3, 4, 5, 6 }; |
5140 |
| -X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } }; |
5141 |
| -\end{codeblock} |
5142 |
| -\tcode{a} and \tcode{b} have the same value |
| 5145 | +(that is, \tcode{'s'}). |
5143 | 5146 |
|
5144 | 5147 | \begin{codeblock}
|
5145 | 5148 | struct A {
|
|
5181 | 5184 | \end{note}
|
5182 | 5185 |
|
5183 | 5186 | \pnum
|
5184 |
| -An array of unknown bound initialized with a |
5185 |
| -brace-enclosed |
5186 |
| -\grammarterm{initializer-list} |
5187 |
| -containing |
5188 |
| -\tcode{n} |
5189 |
| -\grammarterm{initializer-clause}{s} |
5190 |
| -is defined as having |
5191 |
| -\tcode{n} |
5192 |
| -elements\iref{dcl.array}. |
| 5187 | +The number of elements\iref{dcl.array} in an array of unknown bound |
| 5188 | +initialized with a brace-enclosed \grammarterm{initializer-list} |
| 5189 | +is the number of explicitly initialized elements of the array. |
5193 | 5190 | \begin{example}
|
5194 | 5191 | \begin{codeblock}
|
5195 | 5192 | int x[] = { 1, 3, 5 };
|
|
5199 | 5196 | as a one-dimensional array that has three elements
|
5200 | 5197 | since no size was specified and there are three initializers.
|
5201 | 5198 | \end{example}
|
| 5199 | +\begin{example} |
| 5200 | +In |
| 5201 | +\begin{codeblock} |
| 5202 | +struct X { int i, j, k; }; |
| 5203 | +X a[] = { 1, 2, 3, 4, 5, 6 }; |
| 5204 | +X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } }; |
| 5205 | +\end{codeblock} |
| 5206 | +\tcode{a} and \tcode{b} have the same value. |
| 5207 | +\end{example} |
5202 | 5208 | An array of unknown bound shall not be initialized with
|
5203 | 5209 | an empty \grammarterm{braced-init-list} \tcode{\{\}}.
|
5204 | 5210 | \begin{footnote}
|
|
5246 | 5252 | \end{example}
|
5247 | 5253 | \end{note}
|
5248 | 5254 |
|
5249 |
| -\pnum |
5250 |
| -An |
5251 |
| -\grammarterm{initializer-list} |
5252 |
| -is ill-formed if the number of |
5253 |
| -\grammarterm{initializer-clause}{s} |
5254 |
| -exceeds the number of elements of the aggregate. |
5255 |
| -\begin{example} |
5256 |
| -\begin{codeblock} |
5257 |
| -char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error |
5258 |
| -\end{codeblock} |
5259 |
| -is ill-formed. |
5260 |
| -\end{example} |
5261 |
| - |
5262 | 5255 | \pnum
|
5263 | 5256 | If a member has a default member initializer
|
5264 | 5257 | and a potentially-evaluated subexpression thereof is an aggregate
|
|
5280 | 5273 | \end{codeblock}
|
5281 | 5274 | \end{example}
|
5282 | 5275 |
|
5283 |
| -\pnum |
5284 |
| -If an aggregate class \tcode{C} contains a subaggregate element |
5285 |
| -\tcode{e} with no elements, |
5286 |
| -the \grammarterm{initializer-clause} for \tcode{e} shall not be |
5287 |
| -omitted from an \grammarterm{initializer-list} for an object of type |
5288 |
| -\tcode{C} unless the \grammarterm{initializer-clause}{s} for all |
5289 |
| -elements of \tcode{C} following \tcode{e} are also omitted. |
5290 |
| -\begin{example} |
5291 |
| -\begin{codeblock} |
5292 |
| -struct S { } s; |
5293 |
| -struct A { |
5294 |
| - S s1; |
5295 |
| - int i1; |
5296 |
| - S s2; |
5297 |
| - int i2; |
5298 |
| - S s3; |
5299 |
| - int i3; |
5300 |
| -} a = { |
5301 |
| - { }, // Required initialization |
5302 |
| - 0, |
5303 |
| - s, // Required initialization |
5304 |
| - 0 |
5305 |
| -}; // Initialization not required for \tcode{A::s3} because \tcode{A::i3} is also not initialized |
5306 |
| -\end{codeblock} |
5307 |
| -\end{example} |
5308 |
| - |
5309 | 5276 | \pnum
|
5310 | 5277 | When initializing a multidimensional array,
|
5311 | 5278 | the
|
|
5343 | 5310 | \end{example}
|
5344 | 5311 |
|
5345 | 5312 | \pnum
|
5346 |
| -Braces can be elided in an |
5347 |
| -\grammarterm{initializer-list} |
5348 |
| -as follows. |
5349 |
| -If the |
5350 |
| -\grammarterm{initializer-list} |
5351 |
| -begins with a left brace, |
5352 |
| -then the succeeding comma-separated list of |
5353 |
| -\grammarterm{initializer-clause}{s} |
5354 |
| -initializes the elements of a subaggregate; |
5355 |
| -it is erroneous for there to be more |
5356 |
| -\grammarterm{initializer-clause}{s} |
5357 |
| -than elements. |
5358 |
| -If, however, the |
5359 |
| -\grammarterm{initializer-list} |
5360 |
| -for a subaggregate does not begin with a left brace, |
5361 |
| -then only enough |
5362 |
| -\grammarterm{initializer-clause}{s} |
5363 |
| -from the list are taken to initialize the elements of the subaggregate; |
5364 |
| -any remaining |
5365 |
| -\grammarterm{initializer-clause}{s} |
5366 |
| -are left to initialize the next element of the aggregate |
5367 |
| -of which the current subaggregate is an element. |
| 5313 | +Each \grammarterm{initializer-clause} in |
| 5314 | +a brace-enclosed \grammarterm{initializer-list} |
| 5315 | +is said to \defn{appertain} |
| 5316 | +to an element of the aggregate being initialized or |
| 5317 | +to an element of one of its subaggregates. |
| 5318 | +Considering the sequence of \grammarterm{initializer-clause}s, |
| 5319 | +and the sequence of aggregate elements |
| 5320 | +initially formed as the sequence of elements of the aggregate being initialized |
| 5321 | +and potentially modified as described below, |
| 5322 | +each \grammarterm{initializer-clause} appertains to |
| 5323 | +the corresponding aggregate element if |
| 5324 | +\begin{itemize} |
| 5325 | +\item |
| 5326 | +the aggregate element is not an aggregate, or |
| 5327 | +\item |
| 5328 | +the \grammarterm{initializer-clause} begins with a left brace, or |
| 5329 | +\item |
| 5330 | +the \grammarterm{initializer-clause} is an expression and |
| 5331 | +an implicit conversion sequence can be formed |
| 5332 | +that converts the expression to the type of the aggregate element, or |
| 5333 | +\item |
| 5334 | +the aggregate element is an aggregate that itself has no aggregate elements. |
| 5335 | +\end{itemize} |
| 5336 | +Otherwise, |
| 5337 | +the aggregate element is an aggregate and |
| 5338 | +that subaggregate is replaced in the list of aggregate elements by |
| 5339 | +the sequence of its own aggregate elements, and |
| 5340 | +the appertainment analysis resumes with |
| 5341 | +the first such element and the same \grammarterm{initializer-clause}. |
| 5342 | +\begin{note} |
| 5343 | +These rules apply recursively to the aggregate's subaggregates. |
| 5344 | +\begin{example} |
| 5345 | +In |
| 5346 | +\begin{codeblock} |
| 5347 | +struct S1 { int a, b; }; |
| 5348 | +struct S2 { S1 s, t; }; |
| 5349 | + |
| 5350 | +S2 x[2] = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 5351 | +S2 y[2] = { |
| 5352 | + { |
| 5353 | + { 1, 2 }, |
| 5354 | + { 3, 4 } |
| 5355 | + }, |
| 5356 | + { |
| 5357 | + { 5, 6 }, |
| 5358 | + { 7, 8 } |
| 5359 | + } |
| 5360 | +}; |
| 5361 | +\end{codeblock} |
| 5362 | +\tcode{x} and \tcode{y} have the same value. |
| 5363 | +\end{example} |
| 5364 | +\end{note} |
| 5365 | +This process continues |
| 5366 | +until all \grammarterm{initializer-clause}s have been exhausted. |
| 5367 | +If any \grammarterm{initializer-clause} remains |
| 5368 | +that does not appertain to |
| 5369 | +an element of the aggregate or one of its subaggregates, |
| 5370 | +the program is ill-formed. |
| 5371 | +\begin{example} |
| 5372 | +\begin{codeblock} |
| 5373 | +char cv[4] = { 'a', 's', 'd', 'f', 0 }; // error: too many initializers |
| 5374 | +\end{codeblock} |
| 5375 | +\end{example} |
| 5376 | + |
| 5377 | +\pnum |
5368 | 5378 | \begin{example}
|
5369 | 5379 | \begin{codeblock}
|
5370 | 5380 | float y[4][3] = {
|
|
5419 | 5429 | \end{example}
|
5420 | 5430 |
|
5421 | 5431 | \pnum
|
5422 |
| -All implicit type conversions\iref{conv} are considered when |
5423 |
| -initializing the element with an \grammarterm{assignment-expression}. |
5424 |
| -If the |
5425 |
| -\grammarterm{assignment-expression} |
5426 |
| -can initialize an element, the element is initialized. |
5427 |
| -Otherwise, if the element is itself a subaggregate, |
5428 |
| -brace elision is assumed and the |
5429 |
| -\grammarterm{assignment-expression} |
5430 |
| -is considered for the initialization of the first element of the subaggregate. |
5431 | 5432 | \begin{note}
|
5432 |
| -As specified above, brace elision cannot apply to |
5433 |
| -subaggregates with no elements; an |
5434 |
| -\grammarterm{initializer-clause} for the entire subobject is needed. |
| 5433 | +The initializer for an empty subaggregate is required |
| 5434 | +if any initializers are provided for subsequent elements. |
| 5435 | +\begin{example} |
| 5436 | +\begin{codeblock} |
| 5437 | +struct S { } s; |
| 5438 | +struct A { |
| 5439 | + S s1; |
| 5440 | + int i1; |
| 5441 | + S s2; |
| 5442 | + int i2; |
| 5443 | + S s3; |
| 5444 | + int i3; |
| 5445 | +} a = { |
| 5446 | + { }, // Required initialization |
| 5447 | + 0, |
| 5448 | + s, // Required initialization |
| 5449 | + 0 |
| 5450 | +}; // Initialization not required for \tcode{A::s3} because \tcode{A::i3} is also not initialized |
| 5451 | +\end{codeblock} |
| 5452 | +\end{example} |
5435 | 5453 | \end{note}
|
5436 | 5454 |
|
| 5455 | +\pnum |
5437 | 5456 | \begin{example}
|
5438 | 5457 | \begin{codeblock}
|
5439 | 5458 | struct A {
|
|
5447 | 5466 | A a;
|
5448 | 5467 | B b = { 4, a, a };
|
5449 | 5468 | \end{codeblock}
|
5450 |
| - |
5451 | 5469 | Braces are elided around the
|
5452 | 5470 | \grammarterm{initializer-clause}
|
5453 | 5471 | for
|
|
0 commit comments