From 1a535d76856abc4dc7918af059328127d35411f7 Mon Sep 17 00:00:00 2001 From: Dawn Perchik Date: Fri, 22 Feb 2019 00:31:17 -1000 Subject: [PATCH 1/3] P0960r3 Allow initializing aggregates from a parenthesized list of values [dcl.init] Add missing definition of 'n' to array initialization bullet. --- source/basic.tex | 6 ++++ source/declarations.tex | 64 +++++++++++++++++++++++++++++++++++++++-- source/preprocessor.tex | 1 + 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/source/basic.tex b/source/basic.tex index bd28e366e0..d302761565 100644 --- a/source/basic.tex +++ b/source/basic.tex @@ -3925,6 +3925,12 @@ \item A temporary object bound to a reference parameter in a function call\iref{expr.call} persists until the completion of the full-expression containing the call. +\item A temporary object bound to a reference element of +an aggregate of class type initialized from +a parenthesized \grammarterm{expression-list}\iref{dcl.init} +persists until the completion of the full-expression +containing the \grammarterm{expression-list}. + \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. \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}. diff --git a/source/declarations.tex b/source/declarations.tex index 7fcee1a752..ce9748b143 100644 --- a/source/declarations.tex +++ b/source/declarations.tex @@ -4370,7 +4370,23 @@ and the initializer is a string literal, see~\ref{dcl.init.string}. \item If the initializer is \tcode{()}, the object is value-initialized. \item -Otherwise, if the destination type is an array, the program is ill-formed. +Otherwise, if the destination type is an array, +the object is initialized as follows. +Let $x_1$, $\dotsc$, $x_k$ be +the elements of the \grammarterm{expression-list}. +If the destination type is an array of unknown bound, +it is defined as having $k$ elements. +Let $n$ denote the array size after this potential adjustment. +If $k$ is greater than $n$, +the program is ill-formed. +Otherwise, the $i^\text{th}$ array element is copy-initialized with +$x_i$ for each $1 \leq i \leq k$, and +value-initialized for each $k < i \leq n$. +For each $1 \leq i < j \leq n$, +every value computation and side effect associated with +the initialization of the $i^\text{th}$ element of the array +is sequenced before those associated with +the initialization of the $j^\text{th}$ element. \item If the destination type is a (possibly cv-qualified) class type: @@ -4394,8 +4410,8 @@ The constructor so selected is called to initialize the object, with the initializer expression or \grammarterm{expression-list} as its argument(s). -If no constructor applies, or the overload resolution is -ambiguous, the initialization is ill-formed. +If no constructor applies and the destination type is not an aggregate, or +the overload resolution is ambiguous, the initialization is ill-formed. \item Otherwise (i.e., for the remaining copy-initialization cases), user-defined conversions that can convert from the @@ -4411,6 +4427,48 @@ The call is used to direct-initialize, according to the rules above, the object that is the destination of the copy-initialization. +\item +Otherwise, if the destination type is +a (possibly cv-qualified) aggregate class \tcode{A} and +the initializer is a parenthesized \grammarterm{expression-list}, +the object is initialized as follows. +Let $e_1$, $\dotsc$, $e_n$ be the elements of the aggregate\iref{dcl.init.aggr}. +Let $x_1$, $\dotsc$, $x_k$ be the elements of the \grammarterm{expression-list}. +If $k$ is greater than $n$, the program is ill-formed. +The element $e_i$ is copy-initialized with +$x_i$ for $1 \leq i \leq k$. +The remaining elements are initialized with +their default member initializers, if any, and +otherwise are value-initialized. +For each $1 \leq i < j \leq n$, +every value computation and side effect +associated with the initialization of $e_i$ +is sequenced before those associated with the initialization of $e_j$. +\begin{note} +By contrast with direct-list-initialization, +narrowing conversions\iref{dcl.init.list} are permitted, +designators are not permitted, +a temporary object bound to a reference +does not have its lifetime extended\iref{class.temporary}, and +there is no brace elision. +\begin{example} +\begin{codeblock} +struct A { + int a; + int&& r; +}; + +int f(); +int n = 10; + +A a1{1, f()}; // OK, lifetime is extended +A a2(1, f()); // well-formed, but dangling reference +A a3{1.0, 1}; // error: narrowing conversion +A a4(1.0, 1); // well-formed, but dangling reference +A a5(1.0, std::move(n)); // OK +\end{codeblock} +\end{example} +\end{note} \end{itemize} \item diff --git a/source/preprocessor.tex b/source/preprocessor.tex index 1bc22aa914..8bc6ecba14 100644 --- a/source/preprocessor.tex +++ b/source/preprocessor.tex @@ -1475,6 +1475,7 @@ \endhead \defnxname{cpp_aggregate_bases} & \tcode{201603L} \\ \rowsep \defnxname{cpp_aggregate_nsdmi} & \tcode{201304L} \\ \rowsep +\defnxname{cpp_aggregate_paren_init} & \tcode{201902L} \\ \rowsep \defnxname{cpp_alias_templates} & \tcode{200704L} \\ \rowsep \defnxname{cpp_aligned_new} & \tcode{201606L} \\ \rowsep \defnxname{cpp_attributes} & \tcode{200809L} \\ \rowsep From 691b7c10530d3265afbf445dff3dd129c7c5692e Mon Sep 17 00:00:00 2001 From: Dawn Perchik Date: Sun, 10 Mar 2019 16:15:53 -0700 Subject: [PATCH 2/3] [dcl.init] Moved the changed bullet in 17.5 to before 17.6.3. --- source/declarations.tex | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/source/declarations.tex b/source/declarations.tex index ce9748b143..4a5407e2d3 100644 --- a/source/declarations.tex +++ b/source/declarations.tex @@ -4413,21 +4413,6 @@ If no constructor applies and the destination type is not an aggregate, or the overload resolution is ambiguous, the initialization is ill-formed. \item -Otherwise (i.e., for the remaining copy-initialization cases), -user-defined conversions that can convert from the -source type to the destination type or (when a conversion function -is used) to a derived class thereof are enumerated as described in~\ref{over.match.copy}, -and the best one is chosen through overload resolution\iref{over.match}. -If the conversion cannot be done or -is ambiguous, the initialization is ill-formed. The function -selected is called with the initializer expression as its -argument; if the function is a constructor, the call is a prvalue -of the cv-unqualified version of the -destination type whose result object is initialized by the constructor. -The call is used -to direct-initialize, according to the rules above, the object -that is the destination of the copy-initialization. -\item Otherwise, if the destination type is a (possibly cv-qualified) aggregate class \tcode{A} and the initializer is a parenthesized \grammarterm{expression-list}, @@ -4469,6 +4454,21 @@ \end{codeblock} \end{example} \end{note} +\item +Otherwise (i.e., for the remaining copy-initialization cases), +user-defined conversions that can convert from the +source type to the destination type or (when a conversion function +is used) to a derived class thereof are enumerated as described in~\ref{over.match.copy}, +and the best one is chosen through overload resolution\iref{over.match}. +If the conversion cannot be done or +is ambiguous, the initialization is ill-formed. The function +selected is called with the initializer expression as its +argument; if the function is a constructor, the call is a prvalue +of the cv-unqualified version of the +destination type whose result object is initialized by the constructor. +The call is used +to direct-initialize, according to the rules above, the object +that is the destination of the copy-initialization. \end{itemize} \item From ce2f08ab94123adb44e58f1176c6ae4f3209eb60 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Mon, 11 Mar 2019 17:20:25 -0700 Subject: [PATCH 3/3] [dcl.init] Merge new direct aggregate init wording into class direct initialization bullet to avoid the wording being unreachable due to an "Otherwise" chain. --- source/declarations.tex | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/source/declarations.tex b/source/declarations.tex index 4a5407e2d3..f23d73225e 100644 --- a/source/declarations.tex +++ b/source/declarations.tex @@ -4406,15 +4406,17 @@ constructors are considered. The applicable constructors are enumerated\iref{over.match.ctor}, and the best one is chosen -through overload resolution\iref{over.match}. -The constructor so selected +through overload resolution\iref{over.match}. Then: +\begin{itemize} +\item +If overload resolution is successful, +the selected constructor is called to initialize the object, with the initializer expression or \grammarterm{expression-list} as its argument(s). -If no constructor applies and the destination type is not an aggregate, or -the overload resolution is ambiguous, the initialization is ill-formed. \item -Otherwise, if the destination type is -a (possibly cv-qualified) aggregate class \tcode{A} and +Otherwise, if no constructor is viable, +the destination type is +a (possibly cv-qualified) aggregate class \tcode{A}, and the initializer is a parenthesized \grammarterm{expression-list}, the object is initialized as follows. Let $e_1$, $\dotsc$, $e_n$ be the elements of the aggregate\iref{dcl.init.aggr}. @@ -4454,6 +4456,10 @@ \end{codeblock} \end{example} \end{note} +\item +Otherwise, the initialization is ill-formed. +\end{itemize} + \item Otherwise (i.e., for the remaining copy-initialization cases), user-defined conversions that can convert from the