<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3495: constexpr launder makes pointers to inactive members of unions usable</title>
<meta property="og:title" content="Issue 3495: constexpr launder makes pointers to inactive members of unions usable">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3495.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#C++23">C++23</a> status.</em></p>
<h3 id="3495"><a href="lwg-defects.html#3495">3495</a>. <code>constexpr launder</code> makes pointers to inactive members of unions usable</h3>
<p><b>Section:</b> 17.6.5 <a href="https://wg21.link/ptr.launder">[ptr.launder]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Hubert Tong  <b>Opened:</b> 2020-11-10 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#ptr.launder">issues</a> in [ptr.launder].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++23">C++23</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The wording in 17.6.5 <a href="https://wg21.link/ptr.launder">[ptr.launder]</a> paragraph 4:
</p>
<blockquote style="border-left: 3px solid #ccc;padding-left: 15px;">
<p>
An invocation of this function may be used in a core constant expression whenever the value of 
its argument may be used in a core constant expression.
</p>
</blockquote>
<p>
can be taken to mean that the invocation may be used only when the value of its argument can be 
used in place of the invocation itself.
<p/>
That interpretation is not particularly obvious, but based on comments on the CWG reflector (see 
<a href="https://lists.isocpp.org/core/2020/11/10153.php">here</a>), that is the interpretation 
that matches the design intent.
<p/>
Consider:
</p>
<blockquote><pre>
#include &lt;new&gt;

struct A { int x; int y; };
struct B { float x; int y; };

union U {
  A a;
  B b;
};

constexpr A foo() {
  U u;
  int* byp = &amp;u.b.y;
  static_assert(&amp;u.b.y == static_cast&lt;void*&gt;(&amp;u.a.y));
  u.a.y = 42;
  *std::launder(byp) = 13;
  return u.a;
}

extern constexpr A globA = foo();
</pre></blockquote>
<p>
If the <code>static_assert</code> succeeds, then a possible interpretation is that the source file 
above compiles because the call to <code>std::launder</code> produces a pointer to <code>u.a.y</code>.
That interpretation is apparently not desirable.
</p>

<p><i>[2020-11-21; Reflector prioritization]</i></p>

<p>
Set priority to 3 during reflector discussions.
</p>

<p><i>[2020-12-07; Davis Herring comments]</i></p>

<p>
This issue is related to <a href="https://wg21.link/cwg2464">CWG 2464</a>.
</p>

<p><i>[2021-02-08; Reflector poll]</i></p>

<p>
Set status to Tentatively Ready after five votes in favour during reflector poll.
</p>

<p><i>[2021-02-26 Approved at February 2021 virtual plenary. Status changed: Tentatively Ready &rarr; WP.]</i></p>



<p id="res-3495"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/n4868">N4868</a>. 
</p>

<ol>
<li>
<p>
Modify 17.6.5 <a href="https://wg21.link/ptr.launder">[ptr.launder]</a> as indicated:
</p>

<blockquote>
<pre>
template&lt;class T&gt; [[nodiscard]] constexpr T* launder(T* p) noexcept;
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-4- <i>Remarks:</i> An invocation of this function may be used in a core constant expression <del>whenever 
the</del><ins>if and only if the (converted)</ins> value of its argument may be used in <del>a core 
constant expression</del><ins>place of the function invocation</ins>. A byte of storage <code><i>b</i></code> is 
reachable through a pointer value that points to an object <code><i>Y</i></code> if there is an object <code><i>Z</i></code>, 
pointer-interconvertible with <code><i>Y</i></code>, such that <code><i>b</i></code> is within the storage occupied 
by <code><i>Z</i></code>, or the immediately-enclosing array object if <code><i>Z</i></code> is an array element.
<p/>
[&hellip;]
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
