<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<TITLE>
    CWG Issue 2400</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="2400"></A><H4>2400.
  
Constexpr virtual functions and temporary objects
</H4>
<B>Section: </B>7.7&#160; [<A href="https://wg21.link/expr.const">expr.const</A>]
 &#160;&#160;&#160;

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

 <B>Submitter: </B>Daveed Vandevoorde
 &#160;&#160;&#160;

 <B>Date: </B>2019-02-08<BR>


<P>[Accepted as a DR at the July, 2019 meeting.]</P>

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

<PRE>
  struct A {
    constexpr virtual int f() const {
      return 1;
    }
  };

  struct B : A {
    constexpr virtual int f() const {
      return 2;
    }
  };

  constexpr B b{};
  constexpr A&amp;&amp; ref = (B)b;

  static_assert(ref.f() == 2, "");
</PRE>

<P>Since the temporary bound to <TT>ref</TT> is non-const,
it can be re-newed to something else, which would make the
invocation <TT>ref.f()</TT> undefined behavior, which the
interpreter is required to catch.</P>

<P>Presumably, <TT>ref.f()</TT> should not be a constant
expression, and 7.7 [<A href="https://wg21.link/expr.const#2">expr.const</A>] paragraph 2 should
have a bullet for invoking a virtual function of a non-const
object unless its lifetime began within the evaluation of
the constant expression.</P>

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

<P>Add the following as a new bullet following 7.7 [<A href="https://wg21.link/expr.const">expr.const</A>]
bullet 4.4:</P>

<BLOCKQUOTE>

<P>An expression <TT>e</TT> is a <I>core constant
expression</I> unless the evaluation of <TT>e</TT>,
following the rules of the abstract machine
(6.10.1 [<A href="https://wg21.link/intro.execution">intro.execution</A>]), would evaluate one of the
following expressions:</P>

<UL>
<LI><P>...</P></LI>

<LI><P>an invocation of an instantiated constexpr function or
constexpr constructor that fails to satisfy the requirements
for a constexpr function or constexpr constructor
(9.2.6 [<A href="https://wg21.link/dcl.constexpr">dcl.constexpr</A>]);</P></LI>

<LI><P><INS>an invocation of a virtual function
(11.7.3 [<A href="https://wg21.link/class.virtual">class.virtual</A>]) for an object unless</INS></P></LI>

<UL>
<LI><P><INS>the object is usable in constant expressions
or</INS></P></LI>

<LI><P><INS>its lifetime began within the evaluation of
<TT>e</TT>;</INS></P></LI>

</UL>

<LI><P>an expression that would exceed the
implementation-defined limits (see
Clause Annex B [<A href="https://wg21.link/implimits">implimits</A>]);</P></LI>

<LI><P>...</P></LI>

</UL>

</BLOCKQUOTE>

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