<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<TITLE>
    CWG Issue 2985</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="2985"></A><H4>2985.
  
Unclear rules for reference initialization with conversion
</H4>
<B>Section: </B>9.5.4&#160; [<A href="https://wg21.link/dcl.init.ref">dcl.init.ref</A>]
 &#160;&#160;&#160;

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

 <B>Submitter: </B>Anoop Rana
 &#160;&#160;&#160;

 <B>Date: </B>2024-08-13<BR>


<P>[Accepted as a DR at the June, 2025 meeting.]</P>

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

<P>It is unclear whether 9.5.4 [<A href="https://wg21.link/dcl.init.ref#5.4.1">dcl.init.ref</A>] bullet 5.4.1
intends list-initialization or not-list-initializaiton when it talks
about direct-initialization.</P>

<P>Furthermore, the following example was well-formed before the
resolution of <A HREF="1604.html">issue 1604</A> was applied:</P>

<PRE>
  struct X { };
  
  struct Y : X {};

  struct Z {
    operator const Y () const;
  };
  
  Z z;
  X&amp;&amp; r = z; //<SPAN CLASS="cmnt"> #1, ill-formed; was well-formed before CWG1604</SPAN>
</PRE>

<P><U>Possible resolution (January, 2025) [SUPERSEDED]:</U></P>

<P>Change in 9.5.4 [<A href="https://wg21.link/dcl.init.ref#5.4.1">dcl.init.ref</A>] bullet 5.4.1 as follows:</P>

<BLOCKQUOTE>

If T1 or T2 is a class type, user-defined conversions are considered
using the rules for copy-initialization of an object of type
&#8220;cv1 T1&#8221; by user-defined conversion
(9.5 [<A href="https://wg21.link/dcl.init">dcl.init</A>], 12.2.2.5 [<A href="https://wg21.link/over.match.copy">over.match.copy</A>],
12.2.2.6 [<A href="https://wg21.link/over.match.conv">over.match.conv</A>]); the program is ill-formed if the
corresponding non-reference copy-initialization would be
ill-formed. The result <INS>E</INS> of the call to the conversion
function, as described for the non-reference copy-initialization, is
then used to direct-initialize the reference <INS>using the form <TT>(
E )</TT>; if <INS>E</INS> is a prvalue, its cv-qualification is
adjusted to <I>cv1</I></INS>. For this direct-initialization,
user-defined conversions are not considered.

</BLOCKQUOTE>

<P><B>Additional notes (February, 2025)</B></P>

<P>Permitting a binding of <TT>X&amp;&amp;</TT> to a <TT>const Y</TT>
seems ill-advised; the change effected by <A HREF="1604.html">issue 1604</A> in that regard is intended.</P>

<P>In more detail, copy-initializing a T object from a cv T prvalue
succeeds:</P>

<PRE>
  struct X { X() = default; X(X&amp;&amp;) = delete; };
  using CX = const X;
  X x = CX();    //<SPAN CLASS="cmnt"> OK, default-initializes </SPAN>x
</PRE>

<P>However, even with guaranteed copy elision, the pre-CWG1604 model
does not handle derived classes appropriately when reference binding:</P>

<PRE>
  struct X {
    X() = default;
    X(X&amp;&amp;) = delete;
  };
  struct Y : X {};
  struct Z {
    operator Y() { return Y(); }
  };
  X&amp;&amp; x = Z();
</PRE>

<P>In this case, the rvalue reference <TT>x</TT> should bind directly
to the <TT>Y</TT> materialized prvalue; there should never be an
attempt to copy-initialize an <TT>X</TT> from <TT>Z</TT> to satisfy
the reference binding.  However, such direct reference binding would
not be expected to work for a <TT>const Y</TT> prvalue.</P>

<P><B>Proposed resolution (February, 2025) (approved by CWG 2025-06-20):</B></P>

<P>Change in 9.5.4 [<A href="https://wg21.link/dcl.init.ref#5.4.1">dcl.init.ref</A>] bullet 5.4.1 as follows:</P>

<BLOCKQUOTE>

If T1 or T2 is a class type, user-defined conversions are considered
using the rules for copy-initialization of an object of type
&#8220;cv1 T1&#8221; by user-defined conversion
(9.5 [<A href="https://wg21.link/dcl.init">dcl.init</A>], 12.2.2.5 [<A href="https://wg21.link/over.match.copy">over.match.copy</A>],
12.2.2.6 [<A href="https://wg21.link/over.match.conv">over.match.conv</A>]); the program is ill-formed if the
corresponding non-reference copy-initialization would be
ill-formed. The result <INS>E</INS> of the call to the conversion
function, as described for the non-reference copy-initialization, is
then used to direct-initialize the reference <INS>using the form <TT>(
E )</TT></INS>. For this direct-initialization,
user-defined conversions are not considered.

</BLOCKQUOTE>

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