Skip to content

Commit ee519a2

Browse files
author
Dawn Perchik
committed
P0960r3 Allow initializing aggregates from a parenthesized list of values
1 parent cafdbd8 commit ee519a2

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

source/basic.tex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3916,6 +3916,12 @@
39163916
\item A temporary object bound to a reference parameter in a function call\iref{expr.call}
39173917
persists until the completion of the full-expression containing the call.
39183918

3919+
\item A temporary object bound to a reference element of
3920+
an aggregate of class type initialized from
3921+
a parenthesized \grammarterm{expression-list}\iref{dcl.init}
3922+
persists until the completion of the full-expression
3923+
containing the \grammarterm{expression-list}.
3924+
39193925
\item The lifetime of a temporary bound to the returned value in a function \tcode{return} statement\iref{stmt.return} is not extended; the temporary is destroyed at the end of the full-expression in the \tcode{return} statement.
39203926

39213927
\item A temporary bound to a reference in a \grammarterm{new-initializer}\iref{expr.new} persists until the completion of the full-expression containing the \grammarterm{new-initializer}.

source/declarations.tex

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4361,7 +4361,22 @@
43614361
and the initializer is a string literal, see~\ref{dcl.init.string}.
43624362
\item If the initializer is \tcode{()}, the object is value-initialized.
43634363
\item
4364-
Otherwise, if the destination type is an array, the program is ill-formed.
4364+
Otherwise, if the destination type is an array,
4365+
the object is initialized as follows.
4366+
Let $x_1$, $\dotsc$, $x_k$ be
4367+
the elements of the \grammarterm{expression-list}.
4368+
If the destination type is an array of unknown bound,
4369+
it is defined as having $k$ elements.
4370+
If $k$ is greater than the size of the array,
4371+
the program is ill-formed.
4372+
Otherwise, the $i^\text{th}$ array element is copy-initialized with
4373+
$x_i$ for each $1 \leq i \leq k$, and
4374+
value-initialized for each $k < i \leq n$.
4375+
For each $1 \leq i < j \leq n$,
4376+
every value computation and side effect associated with
4377+
the initialization of the $i^\text{th}$ element of the array
4378+
is sequenced before those associated with
4379+
the initialization of the $j^\text{th}$ element.
43654380
\item
43664381
If the destination type is a (possibly cv-qualified) class type:
43674382

@@ -4385,8 +4400,8 @@
43854400
The constructor so selected
43864401
is called to initialize the object, with the initializer
43874402
expression or \grammarterm{expression-list} as its argument(s).
4388-
If no constructor applies, or the overload resolution is
4389-
ambiguous, the initialization is ill-formed.
4403+
If no constructor applies and the destination type is not an aggregate, or
4404+
the overload resolution is ambiguous, the initialization is ill-formed.
43904405
\item
43914406
Otherwise (i.e., for the remaining copy-initialization cases),
43924407
user-defined conversions that can convert from the
@@ -4402,6 +4417,48 @@
44024417
The call is used
44034418
to direct-initialize, according to the rules above, the object
44044419
that is the destination of the copy-initialization.
4420+
\item
4421+
Otherwise, if the destination type is
4422+
a (possibly cv-qualified) aggregate class \tcode{A} and
4423+
the initializer is a parenthesized \grammarterm{expression-list},
4424+
the object is initialized as follows.
4425+
Let $e_1$, $\dotsc$, $e_n$ be the elements of the aggregate\iref{dcl.init.aggr}.
4426+
Let $x_1$, $\dotsc$, $x_k$ be the elements of the \grammarterm{expression-list}.
4427+
If $k$ is greater than $n$, the program is ill-formed.
4428+
The element $e_i$ is copy-initialized with
4429+
$x_i$ for $1 \leq i \leq k$.
4430+
The remaining elements are initialized with
4431+
their default member initializers, if any, and
4432+
otherwise are value-initialized.
4433+
For each $1 \leq i < j \leq n$,
4434+
every value computation and side effect
4435+
associated with the initialization of $e_i$
4436+
is sequenced before those associated with the initialization of $e_j$.
4437+
\begin{note}
4438+
By contrast with direct-list-initialization,
4439+
narrowing conversions\iref{dcl.init.list} are permitted,
4440+
designators are not permitted,
4441+
a temporary object bound to a reference
4442+
does not have its lifetime extended\iref{class.temporary}, and
4443+
there is no brace elision.
4444+
\begin{example}
4445+
\begin{codeblock}
4446+
struct A {
4447+
int a;
4448+
int&& r;
4449+
};
4450+
4451+
int f();
4452+
int n = 10;
4453+
4454+
A a1{1, f()}; // OK, lifetime is extended
4455+
A a2(1, f()); // well-formed, but dangling reference
4456+
A a3{1.0, 1}; // error: narrowing conversion
4457+
A a4(1.0, 1); // well-formed, but dangling reference
4458+
A a5(1.0, std::move(n)); // OK
4459+
\end{codeblock}
4460+
\end{example}
4461+
\end{note}
44054462
\end{itemize}
44064463

44074464
\item

source/preprocessor.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,7 @@
14751475
\endhead
14761476
\defnxname{cpp_aggregate_bases} & \tcode{201603L} \\ \rowsep
14771477
\defnxname{cpp_aggregate_nsdmi} & \tcode{201304L} \\ \rowsep
1478+
\defnxname{cpp_aggregate_paren_init} & \tcode{201902L} \\ \rowsep
14781479
\defnxname{cpp_alias_templates} & \tcode{200704L} \\ \rowsep
14791480
\defnxname{cpp_aligned_new} & \tcode{201606L} \\ \rowsep
14801481
\defnxname{cpp_attributes} & \tcode{200809L} \\ \rowsep

0 commit comments

Comments
 (0)