<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3195: What is the stored pointer value of an empty weak_ptr?</title>
<meta property="og:title" content="Issue 3195: What is the stored pointer value of an empty weak_ptr?">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3195.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="3195"><a href="lwg-defects.html#3195">3195</a>. What is the stored pointer value of an empty <code>weak_ptr</code>?</h3>
<p><b>Section:</b> 20.3.2.3.2 <a href="https://wg21.link/util.smartptr.weak.const">[util.smartptr.weak.const]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Casey Carter <b>Opened:</b> 2019-03-15 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>2
</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>
20.3.2.3.2 <a href="https://wg21.link/util.smartptr.weak.const">[util.smartptr.weak.const]</a> specifies <code>weak_ptr</code>'s default constructor:
</p>
<blockquote>
<pre>
constexpr weak_ptr() noexcept;
</pre>
<blockquote>
<p>
1 <i>Effects:</i> Constructs an empty <code>weak_ptr</code> object.
<p/>
2 <i>Ensures:</i> <code>use_count() == 0</code>.
</p>
</blockquote>
</blockquote>
<p>
and <code>shared_ptr</code> converting constructor template:
</p>
<blockquote>
<pre>
weak_ptr(const weak_ptr&amp; r) noexcept;
template&lt;class Y&gt; weak_ptr(const weak_ptr&lt;Y&gt;&amp; r) noexcept;
template&lt;class Y&gt; weak_ptr(const shared_ptr&lt;Y&gt;&amp; r) noexcept;
</pre>
<blockquote>
<p>
3 <i>Remarks:</i> The second and third constructors shall not participate in overload resolution unless <code>Y*</code> is
compatible with <code>T*</code>.
<p/>
4 <i>Effects:</i> If <code>r</code> is empty, constructs an empty <code>weak_ptr</code> object; otherwise, constructs a
<code>weak_ptr</code> object that shares ownership with <code>r</code> and stores a copy of the pointer stored in <code>r</code>.
<p/>
5 <i>Ensures:</i> <code>use_count() == r.use_count()</code>.
</p>
</blockquote>
</blockquote>
<p>
Note that neither specifies the value of the stored pointer when the resulting <code>weak_ptr</code> is empty.
This didn't matter &mdash; the stored pointer value was unobservable for an empty <code>weak_ptr</code> &mdash;
until we added <code>atomic&lt;weak_ptr&gt;</code>. 32.5.8.7.3 <a href="https://wg21.link/util.smartptr.atomic.weak">[util.smartptr.atomic.weak]</a>/15 says:
</p>
<blockquote>
<p>
<i>Remarks:</i> Two <code>weak_ptr</code> objects are equivalent if they store the same pointer value and either
share ownership, or both are empty. The weak form may fail spuriously. See 32.5.8.2 <a href="https://wg21.link/atomics.types.operations">[atomics.types.operations]</a>.
</p>
</blockquote>
<p>
Two empty <code>weak_ptr</code> objects that store different pointer values are not equivalent. We <em>could</em>
correct this by changing 32.5.8.7.3 <a href="https://wg21.link/util.smartptr.atomic.weak">[util.smartptr.atomic.weak]</a>/15 to "Two <code>weak_ptr</code> objects are
equivalent if they are both empty, or if they share ownership and store the same pointer value." In practice,
an implementation of <code>atomic&lt;weak_ptr&gt;</code> will CAS on both the ownership (control block pointer)
and stored pointer value, so it seems cleaner to pin down the stored pointer value of an empty <code>weak_ptr</code>.
</p>

<p><i>[2019-06-09 Priority set to 2 after reflector discussion]</i></p>


<p><strong>Previous resolution [SUPERSEDED]</strong></p>

<blockquote class="note">
<p>This wording is relative to <a href="https://wg21.link/n4810">N4810</a>.</p>

<ol>
<li><p>Modify 20.3.2.3.2 <a href="https://wg21.link/util.smartptr.weak.const">[util.smartptr.weak.const]</a> as indicated (note the drive-by edit to cleanup the occurrences of "constructs
an object of class foo"):</p>

<blockquote><pre>
constexpr weak_ptr() noexcept;
</pre>
<blockquote>
<p>
-1- <i>Effects:</i> Constructs an empty <del><code>weak_ptr</code></del> object <ins>that stores a null pointer value</ins>.
<p/>
-2- <i>Ensures:</i> <code>use_count() == 0</code>.
</p>
</blockquote>
<pre>
weak_ptr(const weak_ptr&amp; r) noexcept;
template&lt;class Y&gt; weak_ptr(const weak_ptr&lt;Y&gt;&amp; r) noexcept;
template&lt;class Y&gt; weak_ptr(const shared_ptr&lt;Y&gt;&amp; r) noexcept;
</pre>
<blockquote>
<p>
-3- <i>Remarks:</i> The second and third constructors shall not participate in overload resolution unless <code>Y*</code> is
compatible with <code>T*</code>.
<p/>
-4- <i>Effects:</i> If <code>r</code> is empty, constructs an empty <del><code>weak_ptr</code></del> object <ins>that stores a
null pointer value</ins>; otherwise, constructs a <code>weak_ptr</code> object that shares ownership with <code>r</code>
and stores a copy of the pointer stored in <code>r</code>.
<p/>
-5- <i>Ensures:</i> <code>use_count() == r.use_count()</code>.
</p>
</blockquote>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2020-02-14 Casey updates P/R per LWG instruction]</i></p>

While reviewing the P/R in Prague, Tim Song noticed that the stored pointer
value of a moved-from <code>weak_ptr</code> must also be specified.

<p><i>[2020-02-16; Prague]</i></p>

<p>Reviewed revised wording and moved to Ready for Varna.</p>

<p><i>[2020-11-09 Approved In November virtual meeting. Status changed: Ready &rarr; WP.]</i></p>



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

<ol>
<li><p>Modify 20.3.2.3.2 <a href="https://wg21.link/util.smartptr.weak.const">[util.smartptr.weak.const]</a> as indicated:</p>

<blockquote><pre>
constexpr weak_ptr() noexcept;
</pre>
<blockquote>
<p>
-1- <i>Effects:</i> Constructs an empty <code>weak_ptr</code> object <ins>that stores a null pointer value</ins>.
<p/>
-2- <i>Postconditions:</i> <code>use_count() == 0</code>.
</p>
</blockquote>
<pre>
weak_ptr(const weak_ptr&amp; r) noexcept;
template&lt;class Y&gt; weak_ptr(const weak_ptr&lt;Y&gt;&amp; r) noexcept;
template&lt;class Y&gt; weak_ptr(const shared_ptr&lt;Y&gt;&amp; r) noexcept;
</pre>
<blockquote>
<p>
-3- <i>Remarks:</i> The second and third constructors shall not participate in overload resolution unless <code>Y*</code> is
compatible with <code>T*</code>.
<p/>
-4- <i>Effects:</i> If <code>r</code> is empty, constructs an empty <code>weak_ptr</code> object <ins>that stores a
null pointer value</ins>; otherwise, constructs a <code>weak_ptr</code> object that shares ownership with <code>r</code>
and stores a copy of the pointer stored in <code>r</code>.
<p/>
-5- <i>Postconditions:</i> <code>use_count() == r.use_count()</code>.
</p>
</blockquote>
<pre>
weak_ptr(weak_ptr&amp;&amp; r) noexcept;
template&lt;class Y&gt; weak_ptr(weak_ptr&lt;Y&gt;&amp;&amp; r) noexcept;
</pre>
<blockquote>
<p>
-6- <i>Remarks</i>: The second constructor shall not participate in overload resolution unless <code>Y*</code>
is compatible with <code>T*</code>.
<p/>
-7- <i>Effects</i>: Move constructs a <code>weak_ptr</code> instance from <code>r</code>.
<p/>
-8- <i>Postconditions</i>: <code>*this</code> <del>shall contain</del><ins>contains</ins> the old value of <code>r</code>. <code>r</code>
<del>shall be</del><ins>is</ins> empty<del>.</del><ins>, stores a null pointer value, and</ins>
<code>r.use_count() == 0</code>.
</p>
</blockquote>
</blockquote>
</li>
</ol>




</body>
</html>
