Skip to content

Commit 8de0452

Browse files
committed
Merge 2017-07 CWG Motion 8
2 parents 93655d6 + 2442418 commit 8de0452

File tree

3 files changed

+252
-70
lines changed

3 files changed

+252
-70
lines changed

source/compatibility.tex

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,43 @@
600600
\howwide
601601
Common in old programs, but already known to be obsolescent.
602602

603+
\ref{dcl.init.aggr}
604+
\change
605+
In \Cpp, designated initialization support is restricted
606+
compared to the corresponding functionality in C.
607+
In \Cpp,
608+
designators for non-static data members
609+
must be specified in declaration order,
610+
designators for array elements and nested designators
611+
are not supported,
612+
and
613+
designated and non-designated initializers
614+
cannot be mixed in the same initializer list.
615+
616+
Example:
617+
618+
\begin{codeblock}
619+
struct A { int x, y; };
620+
struct B { struct A a; };
621+
struct A a = {.y = 1, .x = 2}; // valid C, invalid \Cpp
622+
int arr[3] = {[1] = 5}; // valid C, invalid \Cpp
623+
struct B b = {.a.x = 0}; // valid C, invalid \Cpp
624+
struct A c = {.x = 1, 2}; // valid C, invalid \Cpp
625+
\end{codeblock}
626+
\rationale
627+
In \Cpp, members are destroyed in reverse construction order
628+
and the elements of an initializer list are evaluated in lexical order,
629+
so field initializers must be specified in order.
630+
Array designators conflict with \grammarterm{lambda-expression} syntax.
631+
Nested designators are seldom used.
632+
\effect
633+
Deletion of feature that is incompatible with \Cpp.
634+
\difficulty
635+
Syntactic transformation.
636+
\howwide
637+
Out-of-order initializers are common.
638+
The other features are seldom used.
639+
603640
\ref{dcl.init.string}
604641
\change In \Cpp, when initializing an array of character with a string, the number of
605642
characters in the string (including the terminating \tcode{'\textbackslash 0'}) must not exceed the

source/declarators.tex

Lines changed: 179 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,16 +2416,33 @@
24162416
braced-init-list
24172417
\end{bnf}
24182418

2419+
\begin{bnf}
2420+
\nontermdef{braced-init-list}\br
2421+
\terminal{\{} initializer-list \terminal{,\opt} \terminal{\}}\br
2422+
\terminal{\{} designated-initializer-list \terminal{,\opt} \terminal{\}}\br
2423+
\terminal{\{} \terminal{\}}
2424+
\end{bnf}
2425+
24192426
\begin{bnf}
24202427
\nontermdef{initializer-list}\br
24212428
initializer-clause \terminal{...}\opt\br
24222429
initializer-list \terminal{,} initializer-clause \terminal{...}\opt
24232430
\end{bnf}
24242431

24252432
\begin{bnf}
2426-
\nontermdef{braced-init-list}\br
2427-
\terminal{\{} initializer-list \terminal{,\opt} \terminal{\}}\br
2428-
\terminal{\{} \terminal{\}}
2433+
\nontermdef{designated-initializer-list}\br
2434+
designated-initializer-clause\br
2435+
designated-initializer-list \terminal{,} designated-initializer-clause
2436+
\end{bnf}
2437+
2438+
\begin{bnf}
2439+
\nontermdef{designated-initializer-clause}\br
2440+
designator brace-or-equal-initializer
2441+
\end{bnf}
2442+
2443+
\begin{bnf}
2444+
\nontermdef{designator}\br
2445+
\tcode{.} identifier
24292446
\end{bnf}
24302447

24312448
\begin{bnf}
@@ -2894,6 +2911,11 @@
28942911
the expressions are evaluated in the order
28952912
specified for function calls~(\ref{expr.call}).
28962913

2914+
\pnum
2915+
The same \grammarterm{identifier}
2916+
shall not appear in multiple \grammarterm{designator}{s} of a
2917+
\grammarterm{designated-initializer-list}.
2918+
28972919
\pnum
28982920
An object whose initialization has completed
28992921
is deemed to be constructed,
@@ -2960,15 +2982,53 @@
29602982
When an aggregate is initialized by an initializer list
29612983
as specified in~\ref{dcl.init.list},
29622984
the elements of the initializer list are taken as initializers
2963-
for the elements of the aggregate, in order.
2964-
Each element is copy-initialized
2965-
from the corresponding \grammarterm{initializer-clause}.
2966-
If the \grammarterm{initializer-clause} is an expression and
2985+
for the elements of the aggregate.
2986+
The \defnx{explicitly initialized elements}{explicitly initialized elements!aggregate}
2987+
of the aggregate are determined as follows:
2988+
\begin{itemize}
2989+
\item
2990+
If the initializer list is a \grammarterm{designated-initializer-list},
2991+
the aggregate shall be of class type,
2992+
the \grammarterm{identifier} in each \grammarterm{designator}
2993+
shall name a direct non-static data member of the class, and
2994+
the explicitly initialized elements of the aggregate
2995+
are the elements that are, or contain, those members.
2996+
\item
2997+
If the initializer list is an \grammarterm{initializer-list},
2998+
the explicitly initialized elements of the aggregate
2999+
are the first $n$ elements of the aggregate,
3000+
where $n$ is the number of elements in the initializer list.
3001+
\item
3002+
Otherwise, the initializer list must be \tcode{\{\}},
3003+
and there are no explicitly initialized elements.
3004+
\end{itemize}
3005+
3006+
\pnum
3007+
For each explicitly initialized element:
3008+
\begin{itemize}
3009+
\item
3010+
If the element is an anonymous union object and
3011+
the initializer list is a \grammarterm{designated-initializer-list},
3012+
the anonymous union object is initialized by the
3013+
\grammarterm{designated-initializer-list} \tcode{\{ }\placeholder{D}\tcode{ \}},
3014+
where \placeholder{D} is the \grammarterm{designated-initializer-clause}
3015+
naming a member of the anonymous union object.
3016+
There shall be only one such \grammarterm{designated-initializer-clause}.
3017+
\item
3018+
Otherwise, the element is copy-initialized
3019+
from the corresponding \grammarterm{initializer-clause}
3020+
or the \grammarterm{brace-or-equal-initializer}
3021+
of the corresponding \grammarterm{designated-initializer-clause}.
3022+
If that initializer is of the form
3023+
\grammarterm{assignment-expression} or
3024+
\tcode{= }\grammarterm{assignment-expression}
3025+
and
29673026
a narrowing conversion~(\ref{dcl.init.list}) is required
29683027
to convert the expression, the program is ill-formed.
2969-
\begin{note} If an \grammarterm{initializer-clause} is itself an initializer list,
3028+
\begin{note} If an initializer is itself an initializer list,
29703029
the element is list-initialized, which will result in a recursive application
29713030
of the rules in this section if the element is an aggregate. \end{note}
3031+
\end{itemize}
29723032
\begin{example}
29733033
\begin{codeblock}
29743034
struct A {
@@ -3013,6 +3073,79 @@
30133073
\tcode{d2.d} with 4.
30143074
\end{example}
30153075

3076+
\pnum
3077+
For a non-union aggregate,
3078+
each element that is not an explicitly initialized element
3079+
is initialized as follows:
3080+
\begin{itemize}
3081+
\item
3082+
If the element has a default member initializer~(\ref{class.mem}),
3083+
the element is initialized from that initializer.
3084+
\item
3085+
Otherwise, if the element is not a reference, the element
3086+
is copy-initialized from an empty initializer list~(\ref{dcl.init.list}).
3087+
\item
3088+
Otherwise, the program is ill-formed.
3089+
\end{itemize}
3090+
If the aggregate is a union and the initializer list is empty, then
3091+
\begin{itemize}
3092+
\item
3093+
if any variant member has a default member initializer,
3094+
that member is initialized from its default member initializer;
3095+
\item
3096+
otherwise, the first member of the union (if any)
3097+
is copy-initialized from an empty initializer list.
3098+
\end{itemize}
3099+
\begin{example}
3100+
3101+
\begin{codeblock}
3102+
struct S { int a; const char* b; int c; int d = b[a]; };
3103+
S ss = { 1, "asdf" };
3104+
\end{codeblock}
3105+
3106+
initializes
3107+
\tcode{ss.a}
3108+
with 1,
3109+
\tcode{ss.b}
3110+
with \tcode{"asdf"},
3111+
\tcode{ss.c}
3112+
with the value of an expression of the form
3113+
\tcode{int\{\}}
3114+
(that is, \tcode{0}), and \tcode{ss.d} with the value of \tcode{ss.b[ss.a]}
3115+
(that is, \tcode{'s'}), and in
3116+
3117+
\begin{codeblock}
3118+
struct X { int i, j, k = 42; };
3119+
X a[] = { 1, 2, 3, 4, 5, 6 };
3120+
X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
3121+
\end{codeblock}
3122+
3123+
\tcode{a} and \tcode{b} have the same value
3124+
3125+
\begin{codeblock}
3126+
struct A {
3127+
string a;
3128+
int b = 42;
3129+
int c = -1;
3130+
};
3131+
\end{codeblock}
3132+
3133+
\tcode{A\{.c=21\}} has the following steps:
3134+
\begin{itemize}
3135+
\item Initialize \tcode{a} with \tcode{\{\}}
3136+
\item Initialize \tcode{b} with \tcode{= 42}
3137+
\item Initialize \tcode{c} with \tcode{= 21}
3138+
\end{itemize}
3139+
\end{example}
3140+
3141+
\pnum
3142+
The initializations of the elements of the aggregate
3143+
are evaluated in the element order.
3144+
That is,
3145+
all value computations and side effects associated with a given element
3146+
are sequenced before
3147+
those of any element that follows it in order.
3148+
30163149
\pnum
30173150
An aggregate that is a class can also be initialized with a single
30183151
expression not enclosed in braces, as described in~\ref{dcl.init}.
@@ -3063,8 +3196,11 @@
30633196

30643197
\pnum
30653198
\begin{note}
3066-
Static data members and unnamed bit-fields are not considered
3067-
elements of the aggregate.
3199+
Static data members,
3200+
non-static data members of anonymous union members,
3201+
and
3202+
unnamed bit-fields
3203+
are not considered elements of the aggregate.
30683204
\begin{example}
30693205

30703206
\begin{codeblock}
@@ -3090,7 +3226,7 @@
30903226
\grammarterm{initializer-list}
30913227
is ill-formed if the number of
30923228
\grammarterm{initializer-clause}{s}
3093-
exceeds the number of elements to initialize.
3229+
exceeds the number of elements of the aggregate.
30943230
\begin{example}
30953231

30963232
\begin{codeblock}
@@ -3100,56 +3236,6 @@
31003236
is ill-formed.
31013237
\end{example}
31023238

3103-
\pnum
3104-
If there are fewer \grammarterm{initializer-clause}{s} in the list than there
3105-
are elements in a non-union aggregate, then each element not explicitly initialized
3106-
is initialized as follows:
3107-
\begin{itemize}
3108-
\item
3109-
If the element has a default member initializer~(\ref{class.mem}),
3110-
the element is initialized from that initializer.
3111-
\item
3112-
Otherwise, if the element is not a reference, the element
3113-
is copy-initialized from an empty initializer list~(\ref{dcl.init.list}).
3114-
\item
3115-
Otherwise, the program is ill-formed.
3116-
\end{itemize}
3117-
If the aggregate is a union and the initializer list is empty, then
3118-
\begin{itemize}
3119-
\item
3120-
if any variant member has a default member initializer,
3121-
that member is initialized from its default member initializer;
3122-
\item
3123-
otherwise, the first member of the union (if any)
3124-
is copy-initialized from an empty initializer list.
3125-
\end{itemize}
3126-
\begin{example}
3127-
3128-
\begin{codeblock}
3129-
struct S { int a; const char* b; int c; int d = b[a]; };
3130-
S ss = { 1, "asdf" };
3131-
\end{codeblock}
3132-
3133-
initializes
3134-
\tcode{ss.a}
3135-
with 1,
3136-
\tcode{ss.b}
3137-
with \tcode{"asdf"},
3138-
\tcode{ss.c}
3139-
with the value of an expression of the form
3140-
\tcode{int\{\}}
3141-
(that is, \tcode{0}), and \tcode{ss.d} with the value of \tcode{ss.b[ss.a]}
3142-
(that is, \tcode{'s'}), and in
3143-
3144-
\begin{codeblock}
3145-
struct X { int i, j, k = 42; };
3146-
X a[] = { 1, 2, 3, 4, 5, 6 };
3147-
X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };
3148-
\end{codeblock}
3149-
3150-
\tcode{a} and \tcode{b} have the same value
3151-
\end{example}
3152-
31533239
\pnum
31543240
If a reference member is initialized from its default member initializer
31553241
and a potentially-evaluated subexpression thereof is an aggregate
@@ -3374,19 +3460,19 @@
33743460

33753461
\pnum
33763462
\indextext{initialization!\idxcode{union}}%
3377-
When a union is initialized with a brace-enclosed initializer,
3378-
the braces shall only contain an
3379-
\grammarterm{initializer-clause}
3380-
for the first non-static data member of the union.
3463+
When a union is initialized with an initializer list,
3464+
there shall not be more than one
3465+
explicitly initialized element.
33813466
\begin{example}
3382-
33833467
\begin{codeblock}
33843468
union u { int a; const char* b; };
33853469
u a = { 1 };
33863470
u b = a;
33873471
u c = 1; // error
33883472
u d = { 0, "asdf" }; // error
33893473
u e = { "asdf" }; // error
3474+
u f = { .b = "asdf" };
3475+
u g = { .a = 1, .b = "asdf" }; // error
33903476
\end{codeblock}
33913477
\end{example}
33923478

@@ -3730,9 +3816,15 @@
37303816

37313817
\pnum
37323818
\defnx{List-initialization}{list-initialization} is initialization of an object or reference from a
3733-
\grammarterm{braced-init-list}. Such an initializer is called an \term{initializer
3734-
list}, and the comma-separated \grammarterm{initializer-clause}{s} of the list are
3735-
called the \term{elements} of the initializer list. An initializer list may be empty.
3819+
\grammarterm{braced-init-list}.
3820+
Such an initializer is called an \term{initializer list}, and
3821+
the comma-separated
3822+
\grammarterm{initializer-clause}{s}
3823+
of the \grammarterm{initializer-list}
3824+
or
3825+
\grammarterm{designated-initializer-clause}{s}
3826+
of the \grammarterm{designated-initializer-list}
3827+
are called the \term{elements} of the initializer list. An initializer list may be empty.
37363828
List-initialization can occur in direct-initialization or copy-initialization contexts;
37373829
list-initialization in a direct-initialization context is called
37383830
\defn{direct-list-initialization} and list-initialization in a
@@ -3784,6 +3876,24 @@
37843876
\pnum
37853877
List-initialization of an object or reference of type \tcode{T} is defined as follows:
37863878
\begin{itemize}
3879+
\item
3880+
If the \grammarterm{braced-init-list}
3881+
contains a \grammarterm{designated-initializer-list},
3882+
\tcode{T} shall be an aggregate class.
3883+
The ordered \grammarterm{identifier}{s}
3884+
in the \grammarterm{designator}{s}
3885+
of the \grammarterm{designated-initializer-list}
3886+
shall form a subsequence
3887+
of the ordered \grammarterm{identifier}{s}
3888+
in the direct non-static data members of \tcode{T}.
3889+
Aggregate initialization is performed~(\ref{dcl.init.aggr}).
3890+
\begin{example}
3891+
\begin{codeblock}
3892+
struct A { int x; int y; int z; };
3893+
A a{.y = 2, .x = 1}; // error: designator order does not match declaration order
3894+
A b{.x = 1, .z = 2}; // OK, \tcode{b.y} initialized to \tcode{0}
3895+
\end{codeblock}
3896+
\end{example}
37873897

37883898
\item If \tcode{T} is an aggregate class and the initializer list has a single element
37893899
of type \cvqual{cv} \tcode{U},

0 commit comments

Comments
 (0)