<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2594: Contradicting definition of empty shared_ptr on shared_ptr(nullptr, d)</title>
<meta property="og:title" content="Issue 2594: Contradicting definition of empty shared_ptr on shared_ptr(nullptr, d)">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2594.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="2594"><a href="lwg-active.html#2594">2594</a>. Contradicting definition of empty <code>shared_ptr</code> on <code>shared_ptr(nullptr, d)</code></h3>
<p><b>Section:</b> 20.3.2.2 <a href="https://wg21.link/util.smartptr.shared">[util.smartptr.shared]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Kazutoshi Satoda <b>Opened:</b> 2016-02-20 <b>Last modified:</b> 2016-06-20</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#util.smartptr.shared">issues</a> in [util.smartptr.shared].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Latest draft (N4567) 20.3.2.2 <a href="https://wg21.link/util.smartptr.shared">[util.smartptr.shared]</a> p1 says:
</p>
<blockquote>
<p>
A <code>shared_ptr</code> object is <em>empty</em> if it does not own a pointer.
</p>
</blockquote>
<p>
Please note that it says "own a pointer". This definition was added as
the resolution for LWG defect <a href="lwg-defects.html#813" title="&quot;empty&quot; undefined for shared_ptr (Status: CD1)">813</a><sup><a href="https://cplusplus.github.io/LWG/issue813" title="Latest snapshot">(i)</a></sup>.
<p/>
20.3.2.2.2 <a href="https://wg21.link/util.smartptr.shared.const">[util.smartptr.shared.const]</a> p8 says about the effect of
<code>shared_ptr(nullptr_t p, D d)</code>:
</p>
<blockquote>
<p>
<i>Effects</i>: Constructs a <code>shared_ptr</code> object that <em>owns</em> the object <code>p</code> and the deleter <code>d</code>.
</p>
</blockquote>
<p>
Please note that it says "<em>owns</em> the object". This was intentionally
changed from "the pointer" as a part of resolution for LWG defect <a href="lwg-defects.html#758" title="shared_ptr and nullptr (Status: C++11)">758</a><sup><a href="https://cplusplus.github.io/LWG/issue758" title="Latest snapshot">(i)</a></sup>,
to cover <code>nullptr_t</code> case.
<p/>
Since <code>shared_ptr(nullptr, d)</code> owns an object of type <code>nullptr_t</code>, but does
not own a pointer, it is said as "empty" by a strict reading of the
above mentioned definition in 20.3.2.2 <a href="https://wg21.link/util.smartptr.shared">[util.smartptr.shared]</a> p1.
<p/>
These cause a contradiction:
<p/>
20.3.2.2.2 <a href="https://wg21.link/util.smartptr.shared.const">[util.smartptr.shared.const]</a> p9 sets a postcondition
<code>use_count() == 1</code> on <code>shared_ptr(nullptr, d)</code>. But 
20.3.2.2.6 <a href="https://wg21.link/util.smartptr.shared.obs">[util.smartptr.shared.obs]</a> p7 says that the return value of <code>use_count()</code>
is "<code>0</code> when <code>*this</code> is <em>empty</em>".
</p>
<p>
Proposed wording changes:
<p/>
Replace the last 2 words in 20.3.2.2 <a href="https://wg21.link/util.smartptr.shared">[util.smartptr.shared]</a> p1 from
</p>
<blockquote><p>
[&hellip;] <em>empty</em> if it does not own a pointer.
</p></blockquote>
<p>
to
</p>
<blockquote><p>
[&hellip;] <em>empty</em> if it does not own an object.
</p></blockquote>
<p>
Note that <code>shared_ptr(nullptr_t)</code> is defined to be empty in synopsis in
20.3.2.2 <a href="https://wg21.link/util.smartptr.shared">[util.smartptr.shared]</a>.
</p>
<blockquote><pre>
constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { }
</pre></blockquote>
<p>
It could be less confusing if <code>shared_ptr(nullptr, d)</code> could be defined to
be empty. But it seems too late to change that (which means changing
whether the deleter is called or not, see 
<a href="https://stackoverflow.com/questions/11164354/">this Stackoverflow article</a>). 
Then I'm proposing just fix the contradiction.
</p>



<p id="res-2594"><b>Proposed resolution:</b></p>
<p>
This wording is relative to N4594.
</p>

<ol>
<li><p>Change 20.3.2.2 <a href="https://wg21.link/util.smartptr.shared">[util.smartptr.shared]</a> p1 as indicated:</p>

<blockquote>
<p>-1- The <code>shared_ptr</code> class template stores a pointer, usually obtained via <code>new</code>. <code>shared_ptr</code> 
implements semantics of shared ownership; the last remaining owner of the pointer is responsible for destroying the 
object, or otherwise releasing the resources associated with the stored pointer. A <code>shared_ptr</code> object is 
<em>empty</em> if it does not own <ins>an object</ins><del>a pointer</del>.
</p>
</blockquote>

</li>
</ol>






</body>
</html>
