<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<TITLE>
    CWG Issue 1773</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="1773"></A><H4>1773.
  
Out-of-lifetime lvalue-to-rvalue conversion
</H4>
<B>Section: </B>7.3.2&#160; [<A href="https://wg21.link/conv.lval">conv.lval</A>]
 &#160;&#160;&#160;

 <B>Status: </B>C++14
 &#160;&#160;&#160;

 <B>Submitter: </B>Canada
 &#160;&#160;&#160;

 <B>Date: </B>2013-09-24<BR><BR>


<A href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3903.html#CA14">N3690 comment
  CA&#160;14<BR></A>

<P>[Moved to DR at the February, 2014 meeting.]</P>

<P>Consider an example like the following:</P>

<PRE>
  struct Base {
    virtual int call() = 0;
  };
  Base *foo() {
    constexpr int x = 0;
    struct Local : Base {
      virtual int call() { return x; }
    };
    static Local local;
    return &amp;local;
  }

  int main() {
    return foo()-&gt;call();
  }
</PRE>

<P>While the likely intention is that the lvalue-to-rvalue conversion of
the block-scope constant is implemented by using the value of the constant
expression in place of reading from storage, it seems that the wording of
7.3.2 [<A href="https://wg21.link/conv.lval#2">conv.lval</A>] paragraph 2 does not prevent this program from
being subject to undefined behaviour caused by lifetime violation.  In
particular, it seems that a name expression that appears in a
potentially-evaluated expression such that the object named is not odr-used
(by that instance of the name) may still be evaluated, in theory, as an
lvalue through which the object named or a subobject thereof is
accessed.</P>

<P><B>Proposed resolution (September, 2013):</B></P>

<P>Change 7.3.2 [<A href="https://wg21.link/conv.lval#2">conv.lval</A>] paragraph 2 as follows:</P>

<BLOCKQUOTE>

<P>When an lvalue-to-rvalue conversion <DEL>occurs in an unevaluated
operand or a subexpression thereof (
Clause 7 [<A href="https://wg21.link/expr">expr</A>])</DEL> <INS>is applied to an expression
<TT>e</TT>, and either</INS>
</P>

<UL>
<LI><P><INS><TT>e</TT> is not potentially evaluated, or</INS></P></LI>

<LI><P><INS>the evaluation of <TT>e</TT> results in the evaluation of a
member <TT>ex</TT> of the set of potential results of <TT>e</TT>, and
<TT>ex</TT> names a variable <TT>x</TT> that is not odr-used by
<TT>ex</TT> (6.3 [<A href="https://wg21.link/basic.def.odr">basic.def.odr</A>]),</INS></P></LI>

</UL>

<P>the value contained in the referenced object is not accessed.
<INS>[<I>Example:</I></INS>
</P>

<PRE>
<INS>  struct S { int n; };
  auto f() {
    S x { 1 };
    constexpr S y { 2 };
    return [&amp;](bool b) { return (b ? y : x).n; };
  }
  auto g = f();
  int m = g(false); //<SPAN CLASS="cmnt"> undefined behavior due to access of </SPAN>x.n<SPAN CLASS="cmnt"> outside its lifetime</SPAN>
  int n = g(true);  //<SPAN CLASS="cmnt"> OK, does not access </SPAN>y.n</INS>
</PRE>

<P>
<INS>&#8212;<I>end example</I>]</INS> In all other cases, the result
of the conversion...</P>

</BLOCKQUOTE>

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