Skip to content

Commit 3dabca0

Browse files
committed
P3106R1 Clarifying rules for brace elision in aggregate initialization
1 parent 9ec133c commit 3dabca0

File tree

1 file changed

+119
-99
lines changed

1 file changed

+119
-99
lines changed

source/declarations.tex

Lines changed: 119 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -4960,8 +4960,8 @@
49604960
\item
49614961
If the initializer list is a brace-enclosed \grammarterm{initializer-list},
49624962
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).
49654965
\item
49664966
Otherwise, the initializer list must be \tcode{\{\}},
49674967
and there are no explicitly initialized elements.
@@ -4992,21 +4992,30 @@
49924992
initializes \tcode{c.a} with 1 and \tcode{c.x} with 3.
49934993
\end{example}
49944994
\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}
49984998
of the corresponding \grammarterm{designated-initializer-clause}.
49994999
If that initializer is of the form
5000-
\grammarterm{assignment-expression} or
50015000
\tcode{= }\grammarterm{assignment-expression}
50025001
and
50035002
a narrowing conversion\iref{dcl.init.list} is required
50045003
to convert the expression, the program is ill-formed.
50055004
\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.
50095007
\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.
50105019
\begin{note}
50115020
If an initializer is itself an initializer list,
50125021
the element is list-initialized, which will result in a recursive application
@@ -5096,13 +5105,7 @@
50965105
with the value of an expression of the form
50975106
\tcode{int\{\}}
50985107
(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'}).
51065109

51075110
\begin{codeblock}
51085111
struct A {
@@ -5144,15 +5147,9 @@
51445147
\end{note}
51455148

51465149
\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.
51565153
\begin{example}
51575154
\begin{codeblock}
51585155
int x[] = { 1, 3, 5 };
@@ -5162,6 +5159,15 @@
51625159
as a one-dimensional array that has three elements
51635160
since no size was specified and there are three initializers.
51645161
\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}
51655171
An array of unknown bound shall not be initialized with
51665172
an empty \grammarterm{braced-init-list} \tcode{\{\}}.
51675173
\begin{footnote}
@@ -5209,19 +5215,6 @@
52095215
\end{example}
52105216
\end{note}
52115217

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-
52255218
\pnum
52265219
If a member has a default member initializer
52275220
and a potentially-evaluated subexpression thereof is an aggregate
@@ -5243,32 +5236,6 @@
52435236
\end{codeblock}
52445237
\end{example}
52455238

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-
52725239
\pnum
52735240
When initializing a multidimensional array,
52745241
the
@@ -5306,28 +5273,73 @@
53065273
\end{example}
53075274

53085275
\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
53315343
\begin{example}
53325344
\begin{codeblock}
53335345
float y[4][3] = {
@@ -5382,21 +5394,30 @@
53825394
\end{example}
53835395

53845396
\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.
53945397
\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}
53985418
\end{note}
53995419

5420+
\pnum
54005421
\begin{example}
54015422
\begin{codeblock}
54025423
struct A {
@@ -5410,7 +5431,6 @@
54105431
A a;
54115432
B b = { 4, a, a };
54125433
\end{codeblock}
5413-
54145434
Braces are elided around the
54155435
\grammarterm{initializer-clause}
54165436
for

0 commit comments

Comments
 (0)