<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<TITLE>
    CWG Issue 2022</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="2022"></A><H4>2022.
  
Copy elision in constant expressions
</H4>
<B>Section: </B>7.7&#160; [<A href="https://wg21.link/expr.const">expr.const</A>]
 &#160;&#160;&#160;

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

 <B>Submitter: </B>Jason Merrill
 &#160;&#160;&#160;

 <B>Date: </B>2014-10-16<BR>


<P>[Adopted at the June, 2016 meeting.]</P>



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

<PRE>
  struct A {
    void *p;
    constexpr A(): p(this) {}
  };

  constexpr A a;		//<SPAN CLASS="cmnt"> well-formed</SPAN>
  constexpr A b = A();          //<SPAN CLASS="cmnt"> ?</SPAN>
</PRE>

<P>The declaration of <TT>a</TT> seems well-formed because the address
of <TT>a</TT> is constant.</P>

<P>The declaration of <TT>b</TT>, however, seems to depend on whether
copy elision is performed.  If it is, the declaration is the equivalent
of <TT>a</TT>; if not, however, this creates a temporary and initializes
<TT>p</TT> to the address of that temporary, making the initialization
non-constant and the declaration ill-formed.</P>

<P>It does not seem desirable for the well-formedness of the program
to depend on whether the implementation performs an optional copy
elision.</P>

<P><B>Notes from the November, 2014 meeting:</B></P>

<P>CWG decided to leave it unspecified whether copy elision is
performed in cases like this and to add a note to
11.4.5.3 [<A href="https://wg21.link/class.copy.ctor">class.copy.ctor</A>] to make clear that that outcome is
intentional.</P>

<P><B>Notes from the May, 2015 meeting:</B></P>

<P>CWG agreed that copy elision should be mandatory in constant
expressions.</P>

<P><B>Proposed resolution (April, 2016):</B></P>

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

<BLOCKQUOTE>

...Expressions that satisfy these requirements<INS>, assuming
that copy elision is performed,</INS> are called
<I>constant expressions</I>. [<I>Note:</I>...

</BLOCKQUOTE>

<LI><P>Change 9.2.6 [<A href="https://wg21.link/dcl.constexpr#7">dcl.constexpr</A>] paragraph 7 as follows,
breaking the existing running text into a bulleted list:</P></LI>

<BLOCKQUOTE>

<P>A call to a <TT>constexpr</TT> function produces the same result as a
call to an equivalent non-<TT>constexpr</TT> function in all respects
except that</P>

<UL>
<LI>

<P>a call to a <TT>constexpr</TT> function can appear in a constant
expression <INS>(7.7 [<A href="https://wg21.link/expr.const">expr.const</A>]) and</INS>
</P>
</LI>

<LI><P>
<INS>copy elision is mandatory in a constant expression
(11.4.5.3 [<A href="https://wg21.link/class.copy.ctor">class.copy.ctor</A>])</INS>.</P></LI>

</UL>

</BLOCKQUOTE>

<LI><P>Change 11.4.5.3 [<A href="https://wg21.link/class.copy.ctor#31">class.copy.ctor</A>] paragraph 31 as follows:</P></LI>

<BLOCKQUOTE>

<P>...This elision of copy/move operations, called copy elision,
is permitted in the following circumstances (which may be
combined to eliminate multiple copies):</P>

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

<P>
<INS>Copy elision is required where an expression is
evaluated in a context requiring a constant expression
(7.7 [<A href="https://wg21.link/expr.const">expr.const</A>]) and in constant initialization
(6.10.3.2 [<A href="https://wg21.link/basic.start.static">basic.start.static</A>]). [<I>Note:</I> Copy elision
might not be performed if the same expression is evaluated
in another context. &#8212;<I>end note</I>]</INS>
[<I>Example:</I>
</P>

<PRE>
  class Thing {
  public:
    Thing();
    ~Thing();
    Thing(const Thing&amp;);
  };

  Thing f() {
    Thing t;
    return t;
  }

  Thing t2 = f();

<INS>  struct A {
    void *p;
    constexpr A(): p(this) {}
  };

  constexpr A a;        //<SPAN CLASS="cmnt"> well-formed, </SPAN>a.p<SPAN CLASS="cmnt"> points to </SPAN>a
  constexpr A b = A();  //<SPAN CLASS="cmnt"> well-formed, </SPAN>b.p<SPAN CLASS="cmnt"> points to </SPAN>b

  void g() {
    A c = A();          //<SPAN CLASS="cmnt"> well-formed, </SPAN>c.p<SPAN CLASS="cmnt"> may point to </SPAN>c<SPAN CLASS="cmnt"> or to an ephemeral temporary</SPAN>
  }</INS>
</PRE>

<P>Here the criteria for elision can be combined...</P>

</BLOCKQUOTE>

</OL>

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