<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 710: Missing postconditions</title>
<meta property="og:title" content="Issue 710: Missing postconditions">
<meta property="og:description" content="C++ library issue. Status: CD1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue710.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="710"><a href="lwg-defects.html#710">710</a>. Missing postconditions</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> Peter Dimov <b>Opened:</b> 2007-08-24 <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>
A discussion on
<a href="http://groups.google.com/group/comp.std.c++/browse_frm/thread/8e89dceb35cd7971">comp.std.c++</a>
has identified a contradiction in the <code>shared_ptr</code> specification.
The <code>shared_ptr</code> move constructor and the cast functions are
missing postconditions for the <code>get()</code> accessor.
</p>

<p><i>[
Bellevue:
]</i></p>


<blockquote>
<p>
Move to "ready", adopting the first (Peter's) proposed resolution.
</p>
<p>
Note to the project editor: there is an editorial issue here. The
wording for the postconditions of the casts is slightly awkward, and the
editor should consider rewording "If w is the return value...", e. g. as
"For a return value w...".
</p>
</blockquote>


<p id="res-710"><b>Proposed resolution:</b></p>
<p>
Add to 20.3.2.2.2 <a href="https://wg21.link/util.smartptr.shared.const">[util.smartptr.shared.const]</a>:
</p>

<blockquote>
<pre>
shared_ptr(shared_ptr&amp;&amp; r);
template&lt;class Y&gt; shared_ptr(shared_ptr&lt;Y&gt;&amp;&amp; r);
</pre>
<blockquote>
<p>
<i>Postconditions:</i>  <code>*this</code> shall contain the old value of <code>r</code>. <code>r</code>
shall be empty. <ins><code>r.get() == 0</code>.</ins>
</p>
</blockquote>
</blockquote>

<p>
Add to 20.3.2.2.10 <a href="https://wg21.link/util.smartptr.shared.cast">[util.smartptr.shared.cast]</a>:
</p>

<blockquote>
<pre>
template&lt;class T, class U&gt; shared_ptr&lt;T&gt; static_pointer_cast(shared_ptr&lt;U&gt; const&amp; r);
</pre>
<blockquote>
<p>
<ins><i>Postconditions:</i> If <code>w</code> is the return value,
<code>w.get() == static_cast&lt;T*&gt;(r.get()) &amp;&amp; w.use_count() == r.use_count()</code>.</ins>
</p>
</blockquote>
</blockquote>

<blockquote>
<pre>
template&lt;class T, class U&gt; shared_ptr&lt;T&gt; dynamic_pointer_cast(shared_ptr&lt;U&gt; const&amp; r);
</pre>
<blockquote>
<p>
<ins><i>Postconditions:</i> If <code>w</code> is the return value, <code>w.get() == dynamic_cast&lt;T*&gt;(r.get())</code>.</ins>
</p>
</blockquote>
</blockquote>

<blockquote>
<pre>
template&lt;class T, class U&gt; shared_ptr&lt;T&gt; const_pointer_cast(shared_ptr&lt;U&gt; const&amp; r);
</pre>
<blockquote>
<p>
<ins><i>Postconditions:</i> If <code>w</code> is the return value,
<code>w.get() == const_cast&lt;T*&gt;(r.get()) &amp;&amp; w.use_count() == r.use_count()</code>.</ins>
</p>
</blockquote>
</blockquote>

<p>
Alberto Ganesh Barbati has written an
<a href="http://barbati.net/c++/shared_ptr.pdf">alternative proposal</a>
where he suggests (among other things) that the casts be respecified in terms of
the aliasing constructor as follows:
</p>

<p>
Change 20.3.2.2.10 <a href="https://wg21.link/util.smartptr.shared.cast">[util.smartptr.shared.cast]</a>:
</p>

<blockquote>
<p>
-2- <i>Returns:</i> <del>If <code>r</code> is empty, an <code>empty
shared_ptr&lt;T&gt;;</code> otherwise, a <code>shared_ptr&lt;T&gt;</code>
object that stores <code>static_cast&lt;T*&gt;(r.get())</code> and shares ownership with
<code>r</code>.</del> <ins><code>shared_ptr&lt;T&gt;(r, static_cast&lt;T*&gt;(r.get())</code>.</ins>
</p>
</blockquote>

<blockquote>
<p>
-6- <i>Returns:</i>
</p>
<ul>
<li><del>When <code>dynamic_cast&lt;T*&gt;(r.get())</code> returns a nonzero value,
a <code>shared_ptr&lt;T&gt;</code> object that stores a copy 
of it and <i>shares ownership</i> with <code>r</code>;</del></li>
<li><del>Otherwise, an <i>empty</i> <code>shared_ptr&lt;T&gt;</code> object.</del></li>
<li><ins>If <code>p = dynamic_cast&lt;T*&gt;(r.get())</code> is a non-null pointer, <code>shared_ptr&lt;T&gt;(r, p);</code></ins></li>
<li><ins>Otherwise, <code>shared_ptr&lt;T&gt;()</code>.</ins></li>
</ul>
</blockquote>

<blockquote>
<p>
-10- <i>Returns:</i> <del>If <code>r</code> is empty, an <code>empty
shared_ptr&lt;T&gt;;</code> otherwise, a <code>shared_ptr&lt;T&gt;</code>
object that stores <code>const_cast&lt;T*&gt;(r.get())</code> and shares ownership with
<code>r</code>.</del> <ins><code>shared_ptr&lt;T&gt;(r, const_cast&lt;T*&gt;(r.get())</code>.</ins>
</p>
</blockquote>

<p>
This takes care of the missing postconditions for the casts by bringing
in the aliasing constructor postcondition "by reference".
</p>






</body>
</html>
