<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<TITLE>
    CWG Issue 2894</TITLE>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<STYLE TYPE="text/css">
  INS { text-decoration:none; font-weight:bold; background-color:#A0FFA0 }
  .INS { text-decoration:none; background-color:#D0FFD0 }
  DEL { text-decoration:line-through; background-color:#FFA0A0 }
  .DEL { text-decoration:line-through; background-color: #FFD0D0 }
  @media (prefers-color-scheme: dark) {
    HTML { background-color:#202020; color:#f0f0f0; }
    A { color:#5bc0ff; }
    A:visited { color:#c6a8ff; }
    A:hover, a:focus { color:#afd7ff; }
    INS { background-color:#033a16; color:#aff5b4; }
    .INS { background-color: #033a16; }
    DEL { background-color:#67060c; color:#ffdcd7; }
    .DEL { background-color:#67060c; }
  }
  SPAN.cmnt { font-family:Times; font-style:italic }
</STYLE>
</HEAD>
<BODY>
<P><EM>This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21
  Core Issues List revision 118b.
  See http://www.open-std.org/jtc1/sc22/wg21/ for the official
  list.</EM></P>
<P>2025-09-28</P>
<HR>
<A NAME="2894"></A><H4>2894.
  
Functional casts create prvalues of reference type
</H4>
<B>Section: </B>7.6.1.4&#160; [<A href="https://wg21.link/expr.type.conv">expr.type.conv</A>]
 &#160;&#160;&#160;

 <B>Status: </B>CD7
 &#160;&#160;&#160;

 <B>Submitter: </B>Jan Schultke
 &#160;&#160;&#160;

 <B>Date: </B>2024-05-14<BR>


<P>[Accepted as a DR at the November, 2024 meeting.]</P>

<P>(From submission
<A HREF="https://github.com/cplusplus/CWG/issues/536">#536</A>.)</P>

<P>For <TT>T{...}</TT>, the rule in 7.6.1.4 [<A href="https://wg21.link/expr.type.conv#2">expr.type.conv</A>] paragraph 2 yields a prvalue of reference type if <TT>T</TT> is a
reference type:</P>

<BLOCKQUOTE>

... Otherwise, the expression is a prvalue of the specified type whose
result object is direct-initialized (9.4) with the initializer. ...

</BLOCKQUOTE>

<P>Also, it should be clarified that <TT>void(1, 2)</TT>
and <TT>void{1}</TT> are ill-formed.</P>

<P><U>Possible resolution [SUPERSEDED]:</U></P>

<P>Change in 7.6.1.4 [<A href="https://wg21.link/expr.type.conv#1">expr.type.conv</A>] paragraph 1 and 2 as follows:</P>

<BLOCKQUOTE>

<P>A <I>simple-type-specifier</I> (9.2.9.3 [<A href="https://wg21.link/dcl.type.simple">dcl.type.simple</A>])
or <I>typename-specifier</I> (13.8 [<A href="https://wg21.link/temp.res">temp.res</A>]) followed by a
parenthesized optional <I>expression-list</I> or by
a <I>braced-init-list</I> (the initializer) constructs a value of the
specified type given the initializer. If the type is a placeholder for
a deduced class type, it is replaced by the return type of the
function selected by overload resolution for class template deduction
(12.2.2.9 [<A href="https://wg21.link/over.match.class.deduct">over.match.class.deduct</A>]) for the remainder of this
subclause. Otherwise, if the type contains a placeholder type, it is
replaced by the type determined by placeholder type deduction
(9.2.9.7.2 [<A href="https://wg21.link/dcl.type.auto.deduct">dcl.type.auto.deduct</A>]). <INS>Let <TT>T</TT> denote the
resulting type.</INS> [ Example: ... ]
</P>

<P>
If the initializer is a parenthesized single expression, the type
conversion expression is equivalent to the corresponding cast
expression (7.6.3 [<A href="https://wg21.link/expr.cast">expr.cast</A>]). Otherwise, if <DEL>the type</DEL> <INS><TT>T</TT></INS> is <I>cv</I>
<TT>void</TT> <DEL>and</DEL> <INS>,</INS> the
initializer <DEL>is</DEL> <INS>shall be</INS> <TT>()</TT>
or <TT>{}</TT> (after pack expansion, if any), <INS>and</INS> the
expression is a prvalue of type void that performs no
initialization. Otherwise, the expression <DEL>is a prvalue of the
specified type whose result object is direct-initialized
(9.5 [<A href="https://wg21.link/dcl.init">dcl.init</A>]) with the initializer</DEL> <INS> has the
same effect as direct-initializing an invented variable <TT>t</TT> of
type <TT>T</TT> from the initializer and then using <TT>t</TT> as the
result of the expression. The result is an lvalue if <TT>T</TT> is an
lvalue reference type or an rvalue reference to function type, an
xvalue if <TT>T</TT> is an rvalue reference to object type, and a
prvalue otherwise</INS>.  If the initializer is a parenthesized
optional <I>expression-list</I>, <DEL>the specified
type</DEL> <INS><TT>T</TT></INS> shall not be an array type.
</P>

</BLOCKQUOTE>

<P><B>CWG 2024-06-14</B></P>

<P>The resolution above introduces an additional variable even for a
prvalue, which defeats mandatory copy elision.  Use the pattern from
7.6.1.9 [<A href="https://wg21.link/expr.static.cast#4">expr.static.cast</A>] paragraph 4 instead.</P>

<P><B>Proposed resolution (approved by CWG 2024-10-25):</B></P>

<P>Change in 7.6.1.4 [<A href="https://wg21.link/expr.type.conv#1">expr.type.conv</A>] paragraph 1 and 2 as follows, move the example to the bottom, and add bullets:</P>

<BLOCKQUOTE>

<P>A <I>simple-type-specifier</I> (9.2.9.3 [<A href="https://wg21.link/dcl.type.simple">dcl.type.simple</A>])
or <I>typename-specifier</I> (13.8 [<A href="https://wg21.link/temp.res">temp.res</A>]) followed by a
parenthesized optional <I>expression-list</I> or by
a <I>braced-init-list</I> (the initializer) constructs a value of the
specified type given the initializer. If the type is a placeholder for
a deduced class type, it is replaced by the return type of the
function selected by overload resolution for class template deduction
(12.2.2.9 [<A href="https://wg21.link/over.match.class.deduct">over.match.class.deduct</A>]) for the remainder of this
subclause. Otherwise, if the type contains a placeholder type, it is
replaced by the type determined by placeholder type deduction
(9.2.9.7.2 [<A href="https://wg21.link/dcl.type.auto.deduct">dcl.type.auto.deduct</A>]). <INS>Let <TT>T</TT> denote the
resulting type.</INS> <DEL>[ Example: ... ]</DEL>
</P>

<P>
<INS>Then:</INS>
<UL>
<LI>
If the initializer is a parenthesized single expression, the type
conversion expression is equivalent to the corresponding cast
expression (7.6.3 [<A href="https://wg21.link/expr.cast">expr.cast</A>]).
</LI>
<LI>Otherwise, if <DEL>the type</DEL> <INS><TT>T</TT></INS> is <I>cv</I>
<TT>void</TT> <DEL>and</DEL> <INS>,</INS> the
initializer <DEL>is</DEL> <INS>shall be</INS> <TT>()</TT>
or <TT>{}</TT> (after pack expansion, if any), <INS>and</INS> the
expression is a prvalue of type void that performs no
initialization. </LI>
<LI><INS>Otherwise, if <TT>T</TT> is a reference type, the expression has
the same effect as direct-initializing an invented variable <TT>t</TT>
of type <TT>T</TT> from the initializer and then using <TT>t</TT> as
the result of the expression; the result is an lvalue if <TT>T</TT> is
an lvalue reference type or an rvalue reference to function type and
an xvalue otherwise.</INS></LI>
<LI>Otherwise, the expression is a prvalue
of <DEL>the specified</DEL> type <INS><TT>T</TT></INS> whose
result object is direct-initialized (9.5 [<A href="https://wg21.link/dcl.init">dcl.init</A>]) with
the initializer.</LI>
</UL>
If the initializer is a parenthesized
optional <I>expression-list</I>, <DEL>the specified
type</DEL> <INS><TT>T</TT></INS> shall not be an array type.
</P>

<P><INS>[Example: ... ]</INS></P>

</BLOCKQUOTE>

<BR><BR>
</BODY>
</HTML>
