<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 932: unique_ptr(pointer p) for pointer deleter types</title>
<meta property="og:title" content="Issue 932: unique_ptr(pointer p) for pointer deleter types">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue932.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#Resolved">Resolved</a> status.</em></p>
<h3 id="932"><a href="lwg-defects.html#932">932</a>. <code>unique_ptr(pointer p)</code> for pointer deleter types</h3>
<p><b>Section:</b> 20.3.1.3.2 <a href="https://wg21.link/unique.ptr.single.ctor">[unique.ptr.single.ctor]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Howard Hinnant <b>Opened:</b> 2008-11-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#unique.ptr.single.ctor">issues</a> in [unique.ptr.single.ctor].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>

<p><b>Addresses US 79</b></p>

<p>
20.3.1.3.2 <a href="https://wg21.link/unique.ptr.single.ctor">[unique.ptr.single.ctor]</a>/5 no longer requires for <code>D</code>
not to be a pointer type.  I believe this restriction was accidently removed
when we relaxed the completeness reuqirements on <code>T</code>. The restriction
needs to be put back in.  Otherwise we have a run time failure that could
have been caught at compile time:
</p>

<blockquote><pre>
{
unique_ptr&lt;int, void(*)(void*)&gt; p1(malloc(sizeof(int)));  <span style="color:#C80000">// should not compile</span>
}  <span style="color:#C80000">// p1.~unique_ptr() dereferences a null function pointer</span>
unique_ptr&lt;int, void(*)(void*)&gt; p2(malloc(sizeof(int)), free);  <span style="color:#C80000">// ok</span>
</pre></blockquote>

<p><i>[
Post Summit:
]</i></p>


<blockquote><p>
Recommend Tentatively Ready.
</p></blockquote>

<p><i>[
2009-07 Frankfurt
]</i></p>


<blockquote><p>
Moved from Tentatively Ready to Open only because the wording needs to be
improved for <code>enable_if</code> type constraining, possibly following Robert's
formula.
</p></blockquote>

<p><i>[
2009-07 Frankfurt:
]</i></p>


<blockquote>
<p>
We need to consider whether some requirements in the Requires paragraphs
of [unique.ptr] should instead be Remarks.
</p>
<p>
Leave Open. Howard to provide wording, and possibly demonstrate how this
can be implemented using enable_if.
</p>
</blockquote>

<p><i>[
2009-07-27 Howard adds:
]</i></p>


<blockquote>
<p>
The two constructors to which this issue applies are not easily constrained
with <code>enable_if</code> as they are not templated:
</p>

<blockquote><pre>
unique_ptr();
explicit unique_ptr(pointer p);
</pre></blockquote>

<p>
To "SFINAE" these constructors away would take heroic effort such as specializing
the entire <code>unique_ptr</code> class template on pointer deleter types.  There
is insufficient motivation for such heroics.  Here is the expected and
reasonable implementation for these constructors:
</p>

<blockquote><pre>
unique_ptr()
    : ptr_(pointer())
    {
        static_assert(!is_pointer&lt;deleter_type&gt;::value,
            "unique_ptr constructed with null function pointer deleter");
    }
explicit unique_ptr(pointer p)
    : ptr_(p)
    {
        static_assert(!is_pointer&lt;deleter_type&gt;::value,
            "unique_ptr constructed with null function pointer deleter");
    }
</pre></blockquote>

<p>
I.e. just use <code>static_assert</code> to verify that the constructor is not
instantiated with a function pointer for a deleter.  The compiler will automatically
take care of issuing a diagnostic if the deleter is a reference type (uninitialized
reference error).
</p>

<p>
In keeping with our discussions in Frankfurt, I'm moving this requirement on
the implementation from the Requires paragraph to a Remarks paragraph.
</p>

</blockquote>

<p><i>[
2009-08-17 Daniel adds:
]</i></p>


<blockquote>
<p>
It is insufficient to require a diagnostic. This doesn't imply an
ill-formed program
as of 3.18 <a href="https://wg21.link/defns.diagnostic">[defns.diagnostic]</a> (a typical alternative would be a compiler
warning), but
exactly that seems to be the intend. I suggest to use the following
remark instead:
</p>

<blockquote><p>
<i>Remarks:</i> The program shall be ill-formed if this constructor is
instantiated when <code>D</code> is a pointer type or reference type.
</p></blockquote>

<p>
Via the general standard rules of 4.1 <a href="https://wg21.link/intro.compliance">[intro.compliance]</a> the "diagnostic
required" is implied.
</p>

</blockquote>

<p><i>[
2009-10 Santa Cruz:
]</i></p>


<blockquote><p>
Moved to Ready.
</p></blockquote>

<p><i>[
2010-03-14 Howard adds:
]</i></p>


<blockquote><p>
We moved
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3073.html">N3073</a>
to the formal motions page in Pittsburgh which should obsolete this issue.  I've
moved this issue to NAD Editorial, solved by N3073.
</p></blockquote>



<p><b>Rationale:</b></p>
<p>
Solved by <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3073.html">N3073</a>.
</p>


<p id="res-932"><b>Proposed resolution:</b></p>
<p>
Change the description of the default constructor in 20.3.1.3.2 <a href="https://wg21.link/unique.ptr.single.ctor">[unique.ptr.single.ctor]</a>:
</p>

<blockquote><pre>
unique_ptr();
</pre>
<blockquote>
<p>
-1- <i>Requires:</i> <code>D</code> shall be default constructible, and that construction
shall not throw an exception. <del><code>D</code> shall 
not be a reference type or pointer type (diagnostic required).</del>
</p>
<p>...</p>
<p><ins>
<i>Remarks:</i> The program shall be ill-formed if this constructor is
instantiated when <code>D</code> is a pointer type or reference type.
</ins></p>
</blockquote>
</blockquote>

<p>
Add  after 20.3.1.3.2 <a href="https://wg21.link/unique.ptr.single.ctor">[unique.ptr.single.ctor]</a>/8:
</p>

<blockquote><pre>
unique_ptr(pointer p);
</pre>
<blockquote>
<p>...</p>
<p><ins>
<i>Remarks:</i> The program shall be ill-formed if this constructor is
instantiated when <code>D</code> is a pointer type or reference type.
</ins></p>
</blockquote>
</blockquote>





</body>
</html>
