<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<TITLE>
    CWG Issue 2939</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="2939"></A><H4>2939.
  
Do not allow <TT>reinterpret_cast</TT> from prvalue to rvalue reference
</H4>
<B>Section: </B>7.6.1.10&#160; [<A href="https://wg21.link/expr.reinterpret.cast">expr.reinterpret.cast</A>]
 &#160;&#160;&#160;

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

 <B>Submitter: </B>Brian Bi
 &#160;&#160;&#160;

 <B>Date: </B>2024-10-01<BR>


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

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

<P>In 7.6.1.10 [<A href="https://wg21.link/expr.reinterpret.cast#11">expr.reinterpret.cast</A>] paragraph 11, there is an issue
similar to the one described in <A HREF="2879.html">issue 2879</A>:
The operand for a cast to reference type can be a "glvalue of type
T1", which implies that a prvalue is also acceptable, because it is
materialized per 7.2.1 [<A href="https://wg21.link/basic.lval#7">basic.lval</A>] paragraph 7.  However, no
implementation accepts
<TT>reinterpret_cast&lt;double&amp;&amp;&gt;(0)</TT>.</P>

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

<OL>
<LI>
<P>Change in 7.6.1.10 [<A href="https://wg21.link/expr.reinterpret.cast#1">expr.reinterpret.cast</A>] paragraph 1 as follows:</P>

<BLOCKQUOTE>

The result of the expression reinterpret_cast&lt;T&gt;(v) is the
result of converting the expression v to type T. If T is an lvalue
reference type or an rvalue reference to function type, the result is
an lvalue; if T is an rvalue reference to object type, the result is
an xvalue; otherwise, the result is a prvalue and the lvalue-to-rvalue
(7.3.2 [<A href="https://wg21.link/conv.lval">conv.lval</A>]), array-to-pointer
(7.3.3 [<A href="https://wg21.link/conv.array">conv.array</A>]), and function-to-pointer
(7.3.4 [<A href="https://wg21.link/conv.func">conv.func</A>]) standard conversions are performed on the
expression v. <INS>The temporary materialization conversion
(7.3.5 [<A href="https://wg21.link/conv.rval">conv.rval</A>]) is not performed on v.</INS> Conversions
that can be performed explicitly using reinterpret_cast are listed
below. No other conversion can be performed explicitly using
reinterpret_cast.

</BLOCKQUOTE>
</LI>

<LI>
<P>Remove 7.6.1.10 [<A href="https://wg21.link/expr.reinterpret.cast#3">expr.reinterpret.cast</A>] paragraph 3:</P>

<BLOCKQUOTE class="del">

[<I>Note 1:</I> The mapping performed by reinterpret_cast might, or
might not, produce a representation different from the original
value. &#8212;<I>end note</I>]

</BLOCKQUOTE>
</LI>

<LI>
<P>Remove from 7.6.1.10 [<A href="https://wg21.link/expr.reinterpret.cast#6">expr.reinterpret.cast</A>] paragraph 6 as follows:</P>

<BLOCKQUOTE>

... <DEL>[<I>Note 5:</I> See also 7.3.12 [<A href="https://wg21.link/conv.ptr">conv.ptr</A>] for more
details of pointer conversions. &#8212;<I>end note</I>]</DEL>

</BLOCKQUOTE>
</LI>

<LI>
<P>Change in 7.6.1.10 [<A href="https://wg21.link/expr.reinterpret.cast#11">expr.reinterpret.cast</A>] paragraph 11 as follows:</P>

<BLOCKQUOTE>

A glvalue of type T1, designating an object or function x, can be cast
to the type &#8220;reference to T2&#8221; if an expression of type
&#8220;pointer to T1&#8221; can be explicitly converted to the type
&#8220;pointer to T2&#8221; using a reinterpret_cast. The result is
that of *reinterpret_cast&lt;T2 *&gt;(p) where p is a pointer to x of
type &#8220;pointer to T1&#8221;. <INS>[ Note:</INS> No temporary is
created, no copy is made, and no constructors
(11.4.5 [<A href="https://wg21.link/class.ctor">class.ctor</A>]) or conversion functions
(11.4.8 [<A href="https://wg21.link/class.conv">class.conv</A>]) are called [ Footnote: ... ]. <INS>--
end note ]</INS>

</BLOCKQUOTE>
</LI>
</OL>

<P><B>CWG 2024-10-11</B></P>

<P>The note in 7.6.1.10 [<A href="https://wg21.link/expr.reinterpret.cast#3">expr.reinterpret.cast</A>] paragraph 3 should be kept,
and the list of exceptions in 7.2.1 [<A href="https://wg21.link/basic.lval#7">basic.lval</A>] paragraph 7
established by <A HREF="2879.html">issue 2879</A> should be amended
with a cross-reference to 7.6.1.10 [<A href="https://wg21.link/expr.reinterpret.cast">expr.reinterpret.cast</A>].</P>

<P><B>Proposed resolution (approved by CWG 2024-11-22):</B></P>

<OL>
<LI>
<P>Change in 7.2.1 [<A href="https://wg21.link/basic.lval#7">basic.lval</A>] paragraph 7 as follows:</P>

<BLOCKQUOTE>

Unless otherwise specified (<INS>7.6.1.10 [<A href="https://wg21.link/expr.reinterpret.cast">expr.reinterpret.cast</A>],</INS>
7.6.1.11 [<A href="https://wg21.link/expr.const.cast">expr.const.cast</A>]), whenever a prvalue appears as an
operand of an operator that expects a glvalue for that operand, the
temporary materialization conversion (7.3.5 [<A href="https://wg21.link/conv.rval">conv.rval</A>]) is
applied to convert the expression to an xvalue.

</BLOCKQUOTE>
</LI>

<LI>
<P>Remove from 7.6.1.10 [<A href="https://wg21.link/expr.reinterpret.cast#6">expr.reinterpret.cast</A>] paragraph 6 as follows:</P>

<BLOCKQUOTE>

... <DEL>[<I>Note 5:</I> See also 7.3.12 [<A href="https://wg21.link/conv.ptr">conv.ptr</A>] for more
details of pointer conversions. &#8212;<I>end note</I>]</DEL>

</BLOCKQUOTE>
</LI>

<LI>
<P>Change in 7.6.1.10 [<A href="https://wg21.link/expr.reinterpret.cast#11">expr.reinterpret.cast</A>] paragraph 11 as follows:</P>

<BLOCKQUOTE>

<DEL>A</DEL> <INS>If v is a</INS> glvalue of type T1, designating an
object or function x, <INS>it</INS> can be cast to the type &#8220;reference to
T2&#8221; if an expression of type &#8220;pointer to T1&#8221; can be
explicitly converted to the type &#8220;pointer to T2&#8221; using a
reinterpret_cast. The result is that of *reinterpret_cast&lt;T2
*&gt;(p) where p is a pointer to x of type &#8220;pointer to
T1&#8221;. <INS>[ Note:</INS> No temporary is <INS>materialized
(7.3.5 [<A href="https://wg21.link/conv.rval">conv.rval</A>]) or</INS> created, no copy is made, and no
constructors (11.4.5 [<A href="https://wg21.link/class.ctor">class.ctor</A>]) or conversion functions
(11.4.8 [<A href="https://wg21.link/class.conv">class.conv</A>]) are called [ Footnote: ... ]. <INS>--
end note ]</INS>

</BLOCKQUOTE>
</LI>
</OL>

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