<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2762: unique_ptr operator*() should be noexcept</title>
<meta property="og:title" content="Issue 2762: unique_ptr operator*() should be noexcept">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2762.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="2762"><a href="lwg-defects.html#2762">2762</a>. <code>unique_ptr operator*()</code> should be <code>noexcept</code></h3>
<p><b>Section:</b> 20.3.1.3.5 <a href="https://wg21.link/unique.ptr.single.observers">[unique.ptr.single.observers]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Ville Voutilainen <b>Opened:</b> 2016-08-04 <b>Last modified:</b> 2023-11-22</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#C++23">C++23</a> status.</p>
<p><b>Discussion:</b></p>
<p>
See LWG <a href="lwg-closed.html#2337" title="shared_ptr operator*() should not be noexcept (Status: NAD)">2337</a><sup><a href="https://cplusplus.github.io/LWG/issue2337" title="Latest snapshot">(i)</a></sup>. Since we aren't removing <code>noexcept</code> from <code>shared_ptr</code>'s
<code>operator*</code>, we should consider adding <code>noexcept</code> to <code>unique_ptr</code>'s <code>operator*</code>.
</p>

<p><i>[2016-08 &mdash; Chicago]</i></p>

<p>Thurs PM: P3, and status to 'LEWG'</p>

<p><i>[2016-08-05 Chicago]</i></p>

<p>
Ville provides an initial proposed wording.
</p>

<p><i>[LEWG Kona 2017]</i></p>

<p>-&gt;Open: Believe these should be <code>noexcept</code> for consistency. We like these. We agree with the proposed resolution.
<code>operator-&gt;()</code> already has <code>noexcept</code>.</p>
<p>Also adds <code>optional::operator*</code></p>
<p>Alisdair points out that fancy pointers might intentionally throw from <code>operator*</code>, and we don't want to prohibit that.</p>
<p>Go forward with conditional <code>noexcept(noexcept(*decltype()))</code>.</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>This wording is relative to N4606.</p>

<blockquote class="note">
<p>
[Drafting note: since this issue is all about consistency, <code>optional</code>'s pointer-like operators
are additionally included.]
</p>
</blockquote>

<ol>
<li><p>In 20.3.1.3 <a href="https://wg21.link/unique.ptr.single">[unique.ptr.single]</a> synopsis, edit as follows:</p>

<blockquote>
<pre>
add_lvalue_reference_t&lt;T&gt; operator*() const <ins>noexcept</ins>;
</pre>
</blockquote>
</li>

<li><p>Before 20.3.1.3.5 <a href="https://wg21.link/unique.ptr.single.observers">[unique.ptr.single.observers]</a>/1, edit as follows:</p>

<blockquote>
<pre>
add_lvalue_reference_t&lt;T&gt; operator*() const <ins>noexcept</ins>;
</pre>
</blockquote>
</li>

<li><p>In 22.5.3 <a href="https://wg21.link/optional.optional">[optional.optional]</a> synopsis, edit as follows:</p>

<blockquote>
<pre>
constexpr T const *operator-&gt;() const <ins>noexcept</ins>;
constexpr T *operator-&gt;() <ins>noexcept</ins>;
constexpr T const &amp;operator*() const &amp; <ins>noexcept</ins>;
constexpr T &amp;operator*() &amp; <ins>noexcept</ins>;
constexpr T &amp;&amp;operator*() &amp;&amp; <ins>noexcept</ins>;
constexpr const T &amp;&amp;operator*() const &amp;&amp; <ins>noexcept</ins>;
</pre>
</blockquote>
</li>

<li><p>Before  [optional.object.observe]/1, edit as follows:</p>

<blockquote>
<pre>
constexpr T const* operator-&gt;() const <ins>noexcept</ins>;
constexpr T* operator-&gt;() <ins>noexcept</ins>;
</pre>
</blockquote>
</li>

<li><p>Before  [optional.object.observe]/5, edit as follows:</p>

<blockquote>
<pre>
constexpr T const&amp; operator*() const &amp; <ins>noexcept</ins>;
constexpr T&amp; operator*() &amp; <ins>noexcept</ins>;
</pre>
</blockquote>
</li>

<li><p>Before  [optional.object.observe]/9, edit as follows:</p>

<blockquote>
<pre>
constexpr T&amp;&amp; operator*() &amp;&amp; <ins>noexcept</ins>;
constexpr const T&amp;&amp; operator*() const &amp;&amp; <ins>noexcept</ins>;
</pre>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2021-06-19 Tim updates wording]</i></p>


<p><i>[2021-06-23; Reflector poll]</i></p>

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

<p><i>[2021-10-14 Approved at October 2021 virtual plenary. Status changed: Voting &rarr; WP.]</i></p>



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

<blockquote class="note">
<p>
[Drafting note: since this issue is all about consistency, <code>optional</code>'s pointer-like operators
are additionally included.]
</p>
</blockquote>

<ol>
<li><p>Edit 20.3.1.3.1 <a href="https://wg21.link/unique.ptr.single.general">[unique.ptr.single.general]</a>, class template <code>unique_ptr</code> synopsis, as indicated:</p>

<blockquote>
<pre>
namespace std {
  template&lt;class T, class D = default_delete&lt;T&gt;&gt; class unique_ptr {
  public:
    [&hellip;]

    // 20.3.1.3.5 <a href="https://wg21.link/unique.ptr.single.observers">[unique.ptr.single.observers]</a>, observers
    add_lvalue_reference_t&lt;T&gt; operator*() const <ins>noexcept(<i>see below</i>)</ins>;
    [&hellip;]
  };
}
</pre>
</blockquote>
</li>

<li><p>Edit 20.3.1.3.5 <a href="https://wg21.link/unique.ptr.single.observers">[unique.ptr.single.observers]</a> as indicated:</p>

<blockquote>
<pre>
add_lvalue_reference_t&lt;T&gt; operator*() const <ins>noexcept(noexcept(*declval&lt;pointer&gt;()))</ins>;
</pre>
<blockquote>
<p>
-1- <i>Preconditions</i>: <code>get() != nullptr</code>.
<p/>
-2- <i>Returns</i>: <code>*get()</code>.
</p>
</blockquote>
</blockquote>
</li>

<li><p>Edit 22.5.3.1 <a href="https://wg21.link/optional.optional.general">[optional.optional.general]</a>, class template <code>optional</code> synopsis, as indicated:</p>

<blockquote>
<pre>
namespace std {
  template&lt;class T&gt;
  class optional {
  public:
    [&hellip;]

    // 22.5.3.7 <a href="https://wg21.link/optional.observe">[optional.observe]</a>, observers
    constexpr const T* operator-&gt;() const <ins>noexcept</ins>;
    constexpr T* operator-&gt;() <ins>noexcept</ins>;
    constexpr const T&amp; operator*() const &amp; <ins>noexcept</ins>;
    constexpr T&amp; operator*() &amp; <ins>noexcept</ins>;
    constexpr T&amp;&amp; operator*() &amp;&amp; <ins>noexcept</ins>;
    constexpr const T&amp;&amp; operator*() const &amp;&amp; <ins>noexcept</ins>;

    [&hellip;]
  };
}
</pre>
</blockquote>
</li>

<li><p>Edit 22.5.3.7 <a href="https://wg21.link/optional.observe">[optional.observe]</a> as indicated:</p>

<blockquote>
<pre>
constexpr const T* operator-&gt;() const <ins>noexcept</ins>;
constexpr T* operator-&gt;() <ins>noexcept</ins>;
</pre>
<blockquote>
<p>
-1- <i>Preconditions</i>: <code>*this</code> contains a value.
<p/>
-2- <i>Returns</i>: <code>val</code>.
<p/>
<del>-3- <i>Throws</i>: Nothing.</del>
<p/>
-4- <i>Remarks:</i> These functions are constexpr functions.
</p>
</blockquote>
<pre>
constexpr const T&amp; operator*() const &amp; <ins>noexcept</ins>;
constexpr T&amp; operator*() &amp; <ins>noexcept</ins>;
</pre>
<blockquote>
<p>
-5- <i>Preconditions</i>: <code>*this</code> contains a value.
<p/>
-6- <i>Returns</i>: <code>*val</code>.
<p/>
<del>-7- <i>Throws</i>: Nothing.</del>
<p/>
-8- <i>Remarks:</i> These functions are constexpr functions.
</p>
</blockquote>
<pre>
constexpr T&amp;&amp; operator*() &amp;&amp; <ins>noexcept</ins>;
constexpr const T&amp;&amp; operator*() const &amp;&amp; <ins>noexcept</ins>;
</pre>
<blockquote>
<p>
-9- <i>Preconditions</i>: <code>*this</code> contains a value.
<p/>
-10- <i>Effects</i>: Equivalent to: <code>return std::move(*val);</code>
</p>
</blockquote>
</blockquote>
</li>

</ol>





</body>
</html>
