Skip to content

Commit f29e482

Browse files
authored
Merge 2019-02 CWG Motion 9
P0960r3 Allow initializing aggregates from a parenthesized list of values Fixes #2689.
2 parents 801a67a + ce2f08a commit f29e482

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

source/basic.tex

+6
Original file line numberDiff line numberDiff line change
@@ -3925,6 +3925,12 @@
39253925
\item A temporary object bound to a reference parameter in a function call\iref{expr.call}
39263926
persists until the completion of the full-expression containing the call.
39273927

3928+
\item A temporary object bound to a reference element of
3929+
an aggregate of class type initialized from
3930+
a parenthesized \grammarterm{expression-list}\iref{dcl.init}
3931+
persists until the completion of the full-expression
3932+
containing the \grammarterm{expression-list}.
3933+
39283934
\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.
39293935

39303936
\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

+69-5
Original file line numberDiff line numberDiff line change
@@ -4370,7 +4370,23 @@
43704370
and the initializer is a string literal, see~\ref{dcl.init.string}.
43714371
\item If the initializer is \tcode{()}, the object is value-initialized.
43724372
\item
4373-
Otherwise, if the destination type is an array, the program is ill-formed.
4373+
Otherwise, if the destination type is an array,
4374+
the object is initialized as follows.
4375+
Let $x_1$, $\dotsc$, $x_k$ be
4376+
the elements of the \grammarterm{expression-list}.
4377+
If the destination type is an array of unknown bound,
4378+
it is defined as having $k$ elements.
4379+
Let $n$ denote the array size after this potential adjustment.
4380+
If $k$ is greater than $n$,
4381+
the program is ill-formed.
4382+
Otherwise, the $i^\text{th}$ array element is copy-initialized with
4383+
$x_i$ for each $1 \leq i \leq k$, and
4384+
value-initialized for each $k < i \leq n$.
4385+
For each $1 \leq i < j \leq n$,
4386+
every value computation and side effect associated with
4387+
the initialization of the $i^\text{th}$ element of the array
4388+
is sequenced before those associated with
4389+
the initialization of the $j^\text{th}$ element.
43744390
\item
43754391
If the destination type is a (possibly cv-qualified) class type:
43764392

@@ -4390,12 +4406,60 @@
43904406
constructors are considered.
43914407
The applicable constructors
43924408
are enumerated\iref{over.match.ctor}, and the best one is chosen
4393-
through overload resolution\iref{over.match}.
4394-
The constructor so selected
4409+
through overload resolution\iref{over.match}. Then:
4410+
\begin{itemize}
4411+
\item
4412+
If overload resolution is successful,
4413+
the selected constructor
43954414
is called to initialize the object, with the initializer
43964415
expression or \grammarterm{expression-list} as its argument(s).
4397-
If no constructor applies, or the overload resolution is
4398-
ambiguous, the initialization is ill-formed.
4416+
\item
4417+
Otherwise, if no constructor is viable,
4418+
the destination type is
4419+
a (possibly cv-qualified) aggregate class \tcode{A}, and
4420+
the initializer is a parenthesized \grammarterm{expression-list},
4421+
the object is initialized as follows.
4422+
Let $e_1$, $\dotsc$, $e_n$ be the elements of the aggregate\iref{dcl.init.aggr}.
4423+
Let $x_1$, $\dotsc$, $x_k$ be the elements of the \grammarterm{expression-list}.
4424+
If $k$ is greater than $n$, the program is ill-formed.
4425+
The element $e_i$ is copy-initialized with
4426+
$x_i$ for $1 \leq i \leq k$.
4427+
The remaining elements are initialized with
4428+
their default member initializers, if any, and
4429+
otherwise are value-initialized.
4430+
For each $1 \leq i < j \leq n$,
4431+
every value computation and side effect
4432+
associated with the initialization of $e_i$
4433+
is sequenced before those associated with the initialization of $e_j$.
4434+
\begin{note}
4435+
By contrast with direct-list-initialization,
4436+
narrowing conversions\iref{dcl.init.list} are permitted,
4437+
designators are not permitted,
4438+
a temporary object bound to a reference
4439+
does not have its lifetime extended\iref{class.temporary}, and
4440+
there is no brace elision.
4441+
\begin{example}
4442+
\begin{codeblock}
4443+
struct A {
4444+
int a;
4445+
int&& r;
4446+
};
4447+
4448+
int f();
4449+
int n = 10;
4450+
4451+
A a1{1, f()}; // OK, lifetime is extended
4452+
A a2(1, f()); // well-formed, but dangling reference
4453+
A a3{1.0, 1}; // error: narrowing conversion
4454+
A a4(1.0, 1); // well-formed, but dangling reference
4455+
A a5(1.0, std::move(n)); // OK
4456+
\end{codeblock}
4457+
\end{example}
4458+
\end{note}
4459+
\item
4460+
Otherwise, the initialization is ill-formed.
4461+
\end{itemize}
4462+
43994463
\item
44004464
Otherwise (i.e., for the remaining copy-initialization cases),
44014465
user-defined conversions that can convert from the

source/preprocessor.tex

+1
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)