<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 813: "empty" undefined for shared_ptr</title>
<meta property="og:title" content="Issue 813: &quot;empty&quot; undefined for shared_ptr">
<meta property="og:description" content="C++ library issue. Status: CD1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue813.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#CD1">CD1</a> status.</em></p>
<h3 id="813"><a href="lwg-defects.html#813">813</a>. "empty" undefined for <code>shared_ptr</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#CD1">CD1</a>
 <b>Submitter:</b> Matt Austern <b>Opened:</b> 2008-02-26 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</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#CD1">CD1</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Several places in 20.3.2.2 <a href="https://wg21.link/util.smartptr.shared">[util.smartptr.shared]</a> refer to an "empty" <code>shared_ptr</code>.
However, that term is nowhere defined. The closest thing we have to a
definition is that the default constructor creates an empty <code>shared_ptr</code>
and that a copy of a default-constructed <code>shared_ptr</code> is empty. Are any
other <code>shared_ptr</code>s empty? For example, is <code>shared_ptr((T*) 0)</code> empty? What
are the properties of an empty <code>shared_ptr</code>? We should either clarify this
term or stop using it.
</p>
<p>
One reason it's not good enough to leave this term up to the reader's
intuition is that, in light of
<a href="http://www.open-std.org/JTC1/sc22/WG21/docs/papers/2007/n2351.htm">N2351</a>
and issue <a href="lwg-defects.html#711" title="Contradiction in empty shared_ptr (Status: C++11)">711</a><sup><a href="https://cplusplus.github.io/LWG/issue711" title="Latest snapshot">(i)</a></sup>, most readers'
intuitive understanding is likely to be wrong. Intuitively one might
expect that an empty <code>shared_ptr</code> is one that doesn't store a pointer,
but, whatever the definition is, that isn't it.
</p>

<p><i>[
Peter adds:
]</i></p>


<blockquote>
<p>
Or, what is an "empty" <code>shared_ptr</code>?
</p>

<ul>
<li>
<p>
Are any other <code>shared_ptrs</code> empty?
</p>
<p>
Yes. Whether a given <code>shared_ptr</code> instance is empty or not is (*)
completely specified by the last mutating operation on that instance.
Give me an example and I'll tell you whether the <code>shared_ptr</code> is empty or
not.
</p>
<blockquote><p>
(*) If it isn't, this is a legitimate defect.
</p></blockquote>
</li>

<li>
<p>
For example, is <code>shared_ptr((T*) 0)</code> empty?
</p>
<p>
No. If it were empty, it would have a <code>use_count()</code> of 0, whereas it is
specified to have an <code>use_count()</code> of 1.
</p>
</li>

<li>
<p>
What are the properties of an empty <code>shared_ptr</code>?
</p>
<p>
The properties of an empty <code>shared_ptr</code> can be derived from the
specification. One example is that its destructor is a no-op. Another is
that its <code>use_count()</code> returns 0. I can enumerate the full list if you
really like.
</p>
</li>

<li>
<p>
We should either clarify this term or stop using it.
</p>
<p>
I don't agree with the imperative tone
</p>
<p>
A clarification would be either a no-op - if it doesn't contradict the
existing wording - or a big mistake if it does.
</p>
<p>
I agree that a clarification that is formally a no-op may add value.
</p>
</li>

<li>
<p>
However, that term is nowhere defined.
</p>
<p>
Terms can be useful without a definition. Consider the following
simplistic example. We have a type <code>X</code> with the following operations
defined:
</p>
<blockquote><pre>
X x;
X x2(x);
X f(X x);
X g(X x1, X x2);
</pre></blockquote>
<p>
A default-constructed value is green.<br/>
A copy has the same color as the original.<br/>
<code>f(x)</code> returns a red value if the argument is green, a green value otherwise.<br/>
<code>g(x1,x2)</code> returns a green value if the arguments are of the same color, a red value otherwise.
</p>

<p>
Given these definitions, you can determine the color of every instance
of type <code>X</code>, even if you have absolutely no idea what green and red mean.
</p>

<p>
Green and red are "nowhere defined" and completely defined at the same time.
</p>
</li>
</ul>

<p>
Alisdair's wording is fine.
</p>
</blockquote>


<p id="res-813"><b>Proposed resolution:</b></p>
<p>
Append the following sentance to 20.3.2.2 <a href="https://wg21.link/util.smartptr.shared">[util.smartptr.shared]</a>
</p>
<blockquote><p>
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. <ins>A <code>shared_ptr</code> object that does not own
a pointer is said to be <i>empty</i>.</ins>
</p></blockquote>





</body>
</html>
