<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3911: unique_ptr's operator* is missing a mandate</title>
<meta property="og:title" content="Issue 3911: unique_ptr's operator* is missing a mandate">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3911.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#New">New</a> status.</em></p>
<h3 id="3911"><a href="lwg-active.html#3911">3911</a>. <code>unique_ptr</code>'s <code>operator*</code> is missing a mandate</h3>
<p><b>Section:</b> 20.3.1.3.5 <a href="https://wg21.link/unique.ptr.single.observers">[unique.ptr.single.observers]</a>, 20.3.1.4.4 <a href="https://wg21.link/unique.ptr.runtime.observers">[unique.ptr.runtime.observers]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Brian Bi <b>Opened:</b> 2023-03-27 <b>Last modified:</b> 2023-06-01</p>
<p><b>Priority: </b>3
</p>
<p><b>View other</b> <a href="lwg-index-open.html#unique.ptr.single.observers">active issues</a> in [unique.ptr.single.observers].</p>
<p><b>View all other</b> <a href="lwg-index.html#unique.ptr.single.observers">issues</a> in [unique.ptr.single.observers].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The return type of <code>std::unique_ptr&lt;T&gt;::operator*</code> is
<code>std::add_lvalue_reference_t&lt;T&gt;</code>,
but there is no mandate stating that <code>*get()</code> is convertible to that type.
There also does not appear to be a mandate that <code>*get()</code> is a valid expression;
dereferenceability is not part of the <i>Cpp17NullablePointer</i> requirements.
A similar issue appears to exist for <code>std::unique_ptr&lt;T[]&gt;::operator[]</code>.
</p>

<p><i>[2023-03-28; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll.
</p>
<p>
"It would be nice to <i>Mandate</i> <code>!reference_converts_from_temporary_v&lt;add_lvalue_reference_t&lt;T&gt;, decltype(*get())&gt;</code>."
"noexcept-specifier isn't quite right, conversion from <code>*get()</code> to <code>T&amp;</code> can throw."
</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">

<p>
This wording is relative to <a href="https://wg21.link/N4944" title=" Working Draft, Standard for Programming Language C++">N4944</a>.
</p>

<ol>
<li>
<p>
Add the following bullet before 20.3.1.3.5 <a href="https://wg21.link/unique.ptr.single.observers">[unique.ptr.single.observers]</a> paragraph 1:
</p>
<blockquote>
<pre><code>
constexpr add_lvalue_reference_t&lt;T&gt; operator*() const noexcept(noexcept(*declval&lt;pointer&gt;()));
</code></pre>
<blockquote>
<p><ins>-?-
<i>Mandates</i>:
<code>*get()</code> is a valid expression
that is convertible to <code>add_lvalue_reference_t&lt;T&gt;</code>.
</ins>
</p>
<p>
-1- <i>Preconditions</i>:
<code>get() != nullptr</code>.
</p>
<p>-2- <i>Returns</i>: <code>*get()</code>.</p>
</blockquote>
</blockquote>
</li>

<li>
<p>
Add the following bullet before 20.3.1.4.4 <a href="https://wg21.link/unique.ptr.runtime.observers">[unique.ptr.runtime.observers]</a> paragraph 1:
</p>
<blockquote>
<pre><code>
constexpr T&amp; operator[](size_t i) const;
</code></pre>
<blockquote>
<p><ins>-?-
<i>Mandates</i>:
<code>get()[i]</code> is a valid expression
that is convertible to <code>T&amp;</code>.
</ins>
</p>
<p>
-1- <i>Preconditions</i>:
<code>i</code> &lt; the number of elements in the array to which the stored pointer points.
</p>
<p>-2- <i>Returns</i>: <code>get()[i]</code>.</p>
</blockquote>
</blockquote>
</li>

</ol>

</blockquote>

<p><i>[2023-04-03; Jonathan provides new wording as requested by LWG]</i></p>




<p id="res-3911"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4944" title=" Working Draft, Standard for Programming Language C++">N4944</a>.
</p>

<ol>
<li>
<p>
Add the following bullet before 20.3.1.3.5 <a href="https://wg21.link/unique.ptr.single.observers">[unique.ptr.single.observers]</a> paragraph 1:
</p>
<blockquote>
<pre><code>
constexpr add_lvalue_reference_t&lt;T&gt; operator*() const noexcept(noexcept(*declval&lt;pointer&gt;()));
</code></pre>
<blockquote>
<p>
-1- <i>Preconditions</i>:
<code>get() != nullptr</code>.
</p>
<p>-2- <i><del>Returns</del> <ins>Effects</ins></i>:
<ins>Equivalent to: <code>return</code></ins>
<code>*get()<ins>;</ins></code><del>.</del>
</p>
</blockquote>
</blockquote>
</li>

<li>
<p>
Add the following bullet before 20.3.1.4.4 <a href="https://wg21.link/unique.ptr.runtime.observers">[unique.ptr.runtime.observers]</a> paragraph 1:
</p>
<blockquote>
<pre><code>
constexpr T&amp; operator[](size_t i) const;
</code></pre>
<blockquote>
<p>
-1- <i>Preconditions</i>:
<ins><code>get() != nullptr</code></ins>
<del>
<code>i</code> &lt; the number of elements in the array to which the stored pointer points</del>.
</p>
<p>-2- <i><del>Returns</del> <ins>Effects</ins></i>:
<ins>Equivalent to: <code>return</code></ins>
<code>get()[i]<ins>;</ins></code><del>.</del>
</p>
</blockquote>
</blockquote>
</li>

</ol>






</body>
</html>
