<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3454: pointer_traits::pointer_to should be constexpr</title>
<meta property="og:title" content="Issue 3454: pointer_traits::pointer_to should be constexpr">
<meta property="og:description" content="C++ library issue. Status: Open">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3454.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#Open">Open</a> status.</em></p>
<h3 id="3454"><a href="lwg-active.html#3454">3454</a>. <code>pointer_traits::pointer_to</code> should be <code>constexpr</code></h3>
<p><b>Section:</b> 20.2.3 <a href="https://wg21.link/pointer.traits">[pointer.traits]</a> <b>Status:</b> <a href="lwg-active.html#Open">Open</a>
 <b>Submitter:</b> Alisdair Meredith <b>Opened:</b> 2020-06-21 <b>Last modified:</b> 2024-11-19</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#pointer.traits">issues</a> in [pointer.traits].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Open">Open</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Trying to implement a <code>constexpr std::list</code> (inspired by Tim Song's
note on using variant members in the node) as part of evaluating
the <code>constexpr</code> container and adapters proposals, I hit problems
I could not code around in <code>pointer_traits</code>, as only the specialization
for native pointers has a <code>constexpr pointer_to</code> function.
<p/>
This means that containers of my custom allocator, that delegates
all allocation behavior to <code>std::allocator&lt;T&gt;</code> but adds extra
telemetry and uses a fancy pointer, does not work with the approach
I tried for implementing <code>list</code> (common link type, shared between
nodes, and stored as <code>end</code> sentinel directly in the <code>list</code> object).
</p>

<p><i>[2020-07-17; Forwarded to LEWG after review in telecon]</i></p>


<p><i>[2022-07-19; Casey Carter comments]</i></p>

<p>
 This is no longer simply a theoretical problem that impedes implementing 
 <code>constexpr std::list</code>, but an actual defect affecting current 
 implementations of <code>constexpr std::string</code>. More specifically, it 
 makes it impossible to  support so-called "fancy pointers" in a <code>constexpr 
 basic_string</code> that performs the small string optimization (SSO). 
 (<code>pointer_traits::pointer_to</code> is critically necessary to get a 
 pointer that designates the SSO buffer.) As things currently stand, 
 <code>constexpr basic_string</code> can support fancy pointers <em>or</em> SSO, 
 but not both.
</p>
<p><i>[Wrocław 2024-11-18; LEWG approves the direction]</i></p>

<p>
Should there be an Annex C entry noting that program-defined specializations
need to add <code class='backtick'>constexpr</code> to be conforming?
</p>


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

<ol>
<li><p>Modify 20.2.3 <a href="https://wg21.link/pointer.traits">[pointer.traits]</a> as indicated:</p>

<blockquote>
<p>
-1- The class template <code>pointer_traits</code> supplies a uniform interface to certain attributes of pointer-like types.
</p>
<blockquote>
<pre>
namespace std {
  template&lt;class Ptr&gt; struct pointer_traits {
    using pointer         = Ptr;
    using element_type    = <i>see below</i>;
    using difference_type = <i>see below</i>;
    
    template&lt;class U&gt; using rebind = <i>see below</i>;
    
    static <ins>constexpr</ins> pointer pointer_to(<i>see below</i> r);
  };
  [&hellip;]
}
</pre>
</blockquote>
</blockquote>
</li>

<li><p>Modify 20.2.3.3 <a href="https://wg21.link/pointer.traits.functions">[pointer.traits.functions]</a> as indicated:</p>

<blockquote>
<pre>
static <ins>constexpr</ins> pointer pointer_traits::pointer_to(<i>see below</i> r);
static constexpr pointer pointer_traits&lt;T*&gt;::pointer_to(<i>see below</i> r) noexcept;
</pre>
<blockquote>
<p>
-1- <i>Mandates:</i> For the first member function, <code>Ptr::pointer_to(r)</code> is well-formed.
<p/>
-2- <i>Preconditions:</i> For the first member function, <code>Ptr::pointer_to(r)</code> returns a pointer 
to <code>r</code> through which indirection is valid.
<p/>
-3- <i>Returns:</i> The first member function returns <code>Ptr::pointer_to(r)</code>. The second member function
returns <code>addressof(r)</code>.
<p/>
-4- <i>Remarks:</i> If <code>element_type</code> is <i>cv</i> <code>void</code>, the type of <code>r</code> is unspecified; 
otherwise, it is <code>element_type&amp;</code>.
</p>
</blockquote>
</blockquote>
</li>
</ol>






</body>
</html>
