<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<TITLE>
    CWG Issue 597</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="597"></A><H4>597.
  
Conversions applied to out-of-lifetime non-POD lvalues
</H4>
<B>Section: </B>6.8.4&#160; [<A href="https://wg21.link/basic.life">basic.life</A>]
 &#160;&#160;&#160;

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

 <B>Submitter: </B>Mike Miller
 &#160;&#160;&#160;

 <B>Date: </B>27 September 2006<BR>


<P>[Voted into the WP at the February, 2012 meeting;
moved to DR at the October, 2012 meeting.]</P>

<P>An lvalue referring to an out-of-lifetime non-POD class objects can
be used in limited ways, subject to the restrictions in
6.8.4 [<A href="https://wg21.link/basic.life#6">basic.life</A>] paragraph 6:</P>

<BLOCKQUOTE>

if the original object will be or was of a non-POD class type, the
program has undefined behavior if:

<UL>
<LI><P>the lvalue is used to access a non-static data member or call a
non-static member function of the object, or</P></LI>

<LI><P>the lvalue is implicitly converted (7.3.12 [<A href="https://wg21.link/conv.ptr">conv.ptr</A>])
to a reference to a base class type, or</P></LI>

<LI><P>the lvalue is used as the operand of a <TT>static_cast</TT>
(7.6.1.9 [<A href="https://wg21.link/expr.static.cast">expr.static.cast</A>]) except when the conversion is
ultimately to <I>cv</I> <TT>char&amp;</TT> or <I>cv</I> <TT>unsigned
char&amp;</TT> ), or</P></LI>

<LI><P>the lvalue is used as the operand of a <TT>dynamic_cast</TT>
(7.6.1.7 [<A href="https://wg21.link/expr.dynamic.cast">expr.dynamic.cast</A>]) or as the operand
of <TT>typeid</TT>.</P></LI>
</UL>

</BLOCKQUOTE>

<P>There are at least a couple of questionable things in this list.
First, there is no &#8220;implicit conversion to a reference to a
base class,&#8221; as assumed by the second bullet.  Presumably
this is intended to say that the lvalue is bound to a reference to
a base class, and the cross-reference should be to
9.5.4 [<A href="https://wg21.link/dcl.init.ref">dcl.init.ref</A>], not to 7.3.12 [<A href="https://wg21.link/conv.ptr">conv.ptr</A>]
(which deals with pointer conversions).  However, even given that
adjustment, it is not clear why it is forbidden to bind a reference
to a non-virtual base class of an out-of-lifetime object, as that is
just an address offset calculation.  (Binding to a virtual base, of
course, would require access to the value of the object and thus
cannot be done outside the object's lifetime.)</P>

<P>The third bullet also appears questionable.  It's not clear why
<TT>static_cast</TT> is discussed at all here, as the only
permissible <TT>static_cast</TT> conversions involving reference types
and non-POD classes are to references to base or derived classes and
to the same type, modulo cv-qualification; if implicit
&#8220;conversion&#8221; to a base class reference is forbidden in the
second bullet, why would an explicit conversion be permitted in the
third?  Was this intended to refer to
<TT>reinterpret_cast</TT>?  Also, is there a reason to allow char
types but disallow array-of-char types (which are more likely to be
useful than a single char)?</P>

<P><B>Proposed resolution (March, 2008):</B></P>

<OL>
<LI><P>Change 6.8.4 [<A href="https://wg21.link/basic.life#5">basic.life</A>] paragraph 5 as follows:</P></LI>

<BLOCKQUOTE>

<P>...If the object will be or was of a non-trivial class type,
the program has undefined behavior if:</P>

<UL>
<LI><P>the pointer is used to access a non-static data member or
call a non-static member function of the object, or</P></LI>

<LI><P>the pointer is implicitly converted (7.3.12 [<A href="https://wg21.link/conv.ptr">conv.ptr</A>]) to a pointer to a <INS>virtual</INS> base class
<DEL>type</DEL>, or</P></LI>

<LI><P>the pointer is used as the operand of a <TT>static_cast</TT>
(7.6.1.9 [<A href="https://wg21.link/expr.static.cast">expr.static.cast</A>]) <DEL>(</DEL>except when the conversion is
to <DEL><TT>void*</TT>, or to <TT>void*</TT> and subsequently to
<TT>char*</TT>, or <TT>unsigned char*</TT>).</DEL> <INS>pointer to
<I>cv</I> <TT>void</TT>, or to pointer to <I>cv</I> <TT>void</TT> and
subsequently to pointer to <I>cv</I> <TT>char</TT> or pointer to
<I>cv</I> <TT>unsigned char</TT>, or</INS>
</P></LI>

<LI><P>the pointer is used as the operand of a <TT>dynamic_cast</TT>
(7.6.1.7 [<A href="https://wg21.link/expr.dynamic.cast">expr.dynamic.cast</A>])...</P></LI>

</UL>

</BLOCKQUOTE>

<LI><P>Change 6.8.4 [<A href="https://wg21.link/basic.life#6">basic.life</A>] paragraph 6 as follows:
</P></LI>

<BLOCKQUOTE>

<P>...if the original object will be or was of a non-trivial
class type, the program has undefined behavior if:</P>

<UL>
<LI><P>the lvalue is used to access a non-static data member or
call a non-static member function of the object, or</P></LI>

<LI><P>the lvalue is <DEL>implicitly converted (7.3.12 [<A href="https://wg21.link/conv.ptr">conv.ptr</A>])</DEL> <INS>bound</INS> to a reference to a <INS>virtual</INS>
base class <DEL>type</DEL> <INS>(9.5.4 [<A href="https://wg21.link/dcl.init.ref">dcl.init.ref</A>])</INS>,
or</P></LI>

<LI><P><DEL>the lvalue is used as the operand of a
<TT>static_cast</TT> (7.6.1.9 [<A href="https://wg21.link/expr.static.cast">expr.static.cast</A>]) except when the
conversion is ultimately to <I>cv</I> <TT>char&amp;</TT> or
<I>cv</I> <TT>unsigned char&amp;</TT>, or</DEL></P></LI>

<LI><P>the lvalue is used as the operand of a
<TT>dynamic_cast</TT> (7.6.1.7 [<A href="https://wg21.link/expr.dynamic.cast">expr.dynamic.cast</A>]) or as the
operand of <TT>typeid</TT>.</P></LI>

</UL>

</BLOCKQUOTE>

</OL>

<P><I>[Drafting notes: Paragraph 5 was changed to track the
changes to paragraph 6.  See also the resolution for <A HREF="658.html">issue 658</A>.]</I></P>

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