Skip to content

Commit 9dc5067

Browse files
jensmaurertkoeppe
authored andcommitted
P3106R1 Clarifying rules for brace elision in aggregate initialization
1 parent fe11b0a commit 9dc5067

File tree

1 file changed

+117
-99
lines changed

1 file changed

+117
-99
lines changed

source/declarations.tex

Lines changed: 117 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -4997,8 +4997,8 @@
49974997
\item
49984998
If the initializer list is a brace-enclosed \grammarterm{initializer-list},
49994999
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).
50025002
\item
50035003
Otherwise, the initializer list must be \tcode{\{\}},
50045004
and there are no explicitly initialized elements.
@@ -5029,21 +5029,30 @@
50295029
initializes \tcode{c.a} with 1 and \tcode{c.x} with 3.
50305030
\end{example}
50315031
\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}
50355035
of the corresponding \grammarterm{designated-initializer-clause}.
50365036
If that initializer is of the form
5037-
\grammarterm{assignment-expression} or
50385037
\tcode{= }\grammarterm{assignment-expression}
50395038
and
50405039
a narrowing conversion\iref{dcl.init.list} is required
50415040
to convert the expression, the program is ill-formed.
50425041
\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.
50465044
\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.
50475056
\begin{note}
50485057
If an initializer is itself an initializer list,
50495058
the element is list-initialized, which will result in a recursive application
@@ -5133,13 +5142,7 @@
51335142
with the value of an expression of the form
51345143
\tcode{int\{\}}
51355144
(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'}).
51435146

51445147
\begin{codeblock}
51455148
struct A {
@@ -5181,15 +5184,9 @@
51815184
\end{note}
51825185

51835186
\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.
51935190
\begin{example}
51945191
\begin{codeblock}
51955192
int x[] = { 1, 3, 5 };
@@ -5199,6 +5196,15 @@
51995196
as a one-dimensional array that has three elements
52005197
since no size was specified and there are three initializers.
52015198
\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}
52025208
An array of unknown bound shall not be initialized with
52035209
an empty \grammarterm{braced-init-list} \tcode{\{\}}.
52045210
\begin{footnote}
@@ -5246,19 +5252,6 @@
52465252
\end{example}
52475253
\end{note}
52485254

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-
52625255
\pnum
52635256
If a member has a default member initializer
52645257
and a potentially-evaluated subexpression thereof is an aggregate
@@ -5280,32 +5273,6 @@
52805273
\end{codeblock}
52815274
\end{example}
52825275

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-
53095276
\pnum
53105277
When initializing a multidimensional array,
53115278
the
@@ -5343,28 +5310,71 @@
53435310
\end{example}
53445311

53455312
\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
53685378
\begin{example}
53695379
\begin{codeblock}
53705380
float y[4][3] = {
@@ -5419,21 +5429,30 @@
54195429
\end{example}
54205430

54215431
\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.
54315432
\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}
54355453
\end{note}
54365454

5455+
\pnum
54375456
\begin{example}
54385457
\begin{codeblock}
54395458
struct A {
@@ -5447,7 +5466,6 @@
54475466
A a;
54485467
B b = { 4, a, a };
54495468
\end{codeblock}
5450-
54515469
Braces are elided around the
54525470
\grammarterm{initializer-clause}
54535471
for

0 commit comments

Comments
 (0)