<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 762: std::unique_ptr requires complete type?</title>
<meta property="og:title" content="Issue 762: std::unique_ptr requires complete type?">
<meta property="og:description" content="C++ library issue. Status: CD1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue762.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="762"><a href="lwg-defects.html#762">762</a>. <code>std::unique_ptr</code> requires complete type?</h3>
<p><b>Section:</b> 20.3.1 <a href="https://wg21.link/unique.ptr">[unique.ptr]</a> <b>Status:</b> <a href="lwg-active.html#CD1">CD1</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2007-11-30 <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">issues</a> in [unique.ptr].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#CD1">CD1</a> status.</p>
<p><b>Discussion:</b></p>
<p>
In contrast to the proposed <code>std::shared_ptr</code>, <code>std::unique_ptr</code>
does currently not support incomplete types, because it
gives no explicit grant - thus instantiating <code>unique_ptr</code> with
an incomplete pointee type <code>T</code> automatically belongs to
undefined behaviour according to 16.4.5.8 <a href="https://wg21.link/res.on.functions">[res.on.functions]</a>&#47;2, last
bullet. This is an unnecessary restriction and prevents
many well-established patterns - like the bridge pattern - for <code>std::unique_ptr</code>.
</p>

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


<blockquote><p>
Move to open. The LWG is comfortable with the intent of allowing
incomplete types and making <code>unique_ptr</code> more like <code>shared_ptr</code>, but we are
not comfortable with the wording. The specification for <code>unique_ptr</code>
should be more like that of <code>shared_ptr</code>. We need to know, for individual
member functions, which ones require their types to be complete. The
<code>shared_ptr</code> specification is careful to say that for each function, and
we need the same level of care here. We also aren't comfortable with the
"part of the operational semantic" language; it's not used elsewhere in
the standard, and it's not clear what it means. We need a volunteer to
produce new wording.
</p></blockquote>


<p id="res-762"><b>Proposed resolution:</b></p>
<p>
The proposed changes in the following revision refers to the current state of
N2521 including the assumption that  [unique.ptr.compiletime] will be removed
according to the current state of <a href="lwg-defects.html#740" title="Please remove *_ptr&lt;T[N]&gt; (Status: CD1)">740</a><sup><a href="https://cplusplus.github.io/LWG/issue740" title="Latest snapshot">(i)</a></sup>.
</p>
<p>
The specialization <code>unique_ptr&lt;T[]&gt;</code> has some more restrictive constraints on
type-completeness on <code>T</code> than <code>unique_ptr&lt;T&gt;</code>. The following proposed wordings
try to cope with that. If the committee sees less usefulness on relaxed
constraints on <code>unique_ptr&lt;T[]&gt;</code>, the alternative would be to stop this relaxation
e.g. by adding one further bullet to 20.3.1.4 <a href="https://wg21.link/unique.ptr.runtime">[unique.ptr.runtime]</a>&#47;1:
"<code>T</code> shall be a complete type, if used as template argument of
<code>unique_ptr&lt;T[], D&gt;</code>
</p>
<p>
This issue has some overlap with <a href="lwg-defects.html#673" title="unique_ptr update (Status: CD1)">673</a><sup><a href="https://cplusplus.github.io/LWG/issue673" title="Latest snapshot">(i)</a></sup>, but it seems not to cause any
problems with this one,
because <a href="lwg-defects.html#673" title="unique_ptr update (Status: CD1)">673</a><sup><a href="https://cplusplus.github.io/LWG/issue673" title="Latest snapshot">(i)</a></sup> adds only optional requirements on <code>D</code> that do not conflict
with the here discussed
ones, provided that <code>D::pointer</code>'s operations (including default
construction, copy construction/assignment,
and pointer conversion) are specified <em>not</em> to throw, otherwise this
would have impact on the
current specification of <code>unique_ptr</code>.
</p>

<ol>
<li>
<p>
In 20.3.1 <a href="https://wg21.link/unique.ptr">[unique.ptr]</a>&#47;2 add as the last sentence to the existing para:
</p>

<blockquote><p>
The <code>unique_ptr</code> provides a semantics of strict ownership. A
<code>unique_ptr</code> owns the object it holds a pointer to. A
<code>unique_ptr</code> is not <code>CopyConstructible</code>, nor
<code>CopyAssignable</code>, however it is <code>MoveConstructible</code> and
<code>MoveAssignable</code>. <ins>The template parameter <code>T</code> of
<code>unique_ptr</code> may be an incomplete type.</ins> [ <i>Note:</i> The
uses of <code>unique_ptr</code> include providing exception safety for
dynamically allcoated memory, passing ownership of dynamically allocated
memory to a function, and returning dynamically allocated memory from a
function. -- <i>end note</i> ]
</p></blockquote>
</li>

<li>
<p>
20.3.1.3.2 <a href="https://wg21.link/unique.ptr.single.ctor">[unique.ptr.single.ctor]</a>&#47;1: No changes necessary.
</p>

<blockquote>
<p><i>[
N.B.: We only need the requirement that <code>D</code> is <code>DefaultConstructible</code>.
The current wording says just this.
]</i></p>

</blockquote>
</li>

<li>
<p>
In 20.3.1.3.2 <a href="https://wg21.link/unique.ptr.single.ctor">[unique.ptr.single.ctor]</a>&#47;5 change the requires clause to say:
</p>

<blockquote>
<p>
<i>Requires:</i> <del>The expression <code>D()(p)</code> shall be well formed. The default constructor
of <code>D</code> shall not throw an exception.</del>
<del><code>D</code> must not be a reference type.</del>
<ins>
<code>D</code> shall be default constructible, and that construction
shall not throw an exception.
</ins>
</p>
<p><i>[
N.B.: There is no need that the expression <code>D()(p)</code> is well-formed at
this point. I assume that the current wording is based on the
corresponding <code>shared_ptr</code> wording. In case of <code>shared_ptr</code> this
requirement is necessary, because the corresponding c'tor *can* fail
and must invoke delete <code>p/d(p)</code> in this case. <code>Unique_ptr</code> is simpler in
this regard. The *only* functions that must insist on well-formedness
and well-definedness of the expression <code>get_deleter()(get())</code> are (1)
the destructor and (2) <code>reset</code>. The reasoning for the wording change to
explicitly require <code>DefaultConstructible</code> of <code>D</code> is to guarantee that
invocation of
<code>D</code>'s default c'tor is both well-formed and well-defined. Note also that
we do *not* need the
requirement that <code>T</code> must be complete, also in contrast to <code>shared_ptr</code>.
<code>Shared_ptr</code> needs this, because it's c'tor is a template c'tor which
potentially requires <code>Convertible&lt;Y*, X*&gt;</code>, which
again requires Completeness of <code>Y</code>, if <code>!SameType&lt;X, Y&gt;</code>
]</i></p>

</blockquote>
</li>

<li>
<p>
Merge 20.3.1.3.2 <a href="https://wg21.link/unique.ptr.single.ctor">[unique.ptr.single.ctor]</a>&#47;12+13 thereby removing the sentence
of 12, but transferring the "requires" to 13:
</p>

<blockquote>
<p>
<i>Requires:</i> If <code>D</code> is not an lvalue-reference type then[..]
</p>
<p><i>[
N.B.: For the same reasons as for (3), there is no need that <code>d(p)</code> is
well-formed/well-defined at this point. The current wording guarantees
all what we need, namely that the initialization of both the <code>T*</code>
pointer and the <code>D</code> deleter are well-formed and well-defined.
]</i></p>

</blockquote>
</li>

<li>
20.3.1.3.2 <a href="https://wg21.link/unique.ptr.single.ctor">[unique.ptr.single.ctor]</a>&#47;17: No changes necessary.
</li>
<li>
<p>20.3.1.3.2 <a href="https://wg21.link/unique.ptr.single.ctor">[unique.ptr.single.ctor]</a>&#47;21:</p>

<blockquote><p>
<i>Requires:</i> If <code>D</code> is not a reference type, construction of
the deleter <code>D</code> from an rvalue of type <code>E</code> shall be well
formed and shall not throw an exception. If <code>D</code> is a reference
type, then <code>E</code> shall be the same type as <code>D</code> (diagnostic
required). <code>U*</code> shall be implicitly convertible to <code>T*</code>.
<ins>[<i>Note:</i> These requirements imply that <code>T</code> and <code>U</code>
be complete types. <i>-- end note</i>]</ins>
</p></blockquote>

<p><i>[
N.B.: The current wording of 21 already implicitly guarantees that <code>U</code>
is completely defined, because it requires that <code>Convertible&lt;U*, T*&gt;</code> is
true. If the committee wishes this explicit requirement can be added,
e.g. "<code>U</code> shall be a complete type."
]</i></p>

</li>

<li>
<p>
20.3.1.3.3 <a href="https://wg21.link/unique.ptr.single.dtor">[unique.ptr.single.dtor]</a>: Just before p1 add a new paragraph:
</p>
<blockquote>
<p>
<i>Requires:</i> The expression <code>get_deleter()(get())</code> shall be well-formed,
shall have well-defined behavior, and shall not throw exceptions.
<ins>[<i>Note:</i> The use of <code>default_delete</code> requires <code>T</code> to
be a complete type. <i>-- end note</i>]</ins>
</p>
<p><i>[
N.B.: This requirement ensures that the whole responsibility on
type-completeness of <code>T</code> is delegated to this expression.
]</i></p>

</blockquote>
</li>

<li>
<p>
20.3.1.3.4 <a href="https://wg21.link/unique.ptr.single.asgn">[unique.ptr.single.asgn]</a>&#47;1: No changes necessary, except the
current editorial issue, that "must shall" has to be changed to
"shall", but this change is not a special part of this resolution.
</p>

<p><i>[
N.B. The current wording is sufficient, because we can delegate all
further requirements on the requirements of the effects clause
]</i></p>

</li>

<li>
<p>
20.3.1.3.4 <a href="https://wg21.link/unique.ptr.single.asgn">[unique.ptr.single.asgn]</a>&#47;6:
</p>

<blockquote><p>
<i>Requires:</i> Assignment of the deleter <code>D</code> from an rvalue
<code>D</code> shall not throw an exception. <code>U*</code> shall be implicitly
convertible to <code>T*</code>.
<ins>[<i>Note:</i> These requirements imply that <code>T</code> and <code>U</code>
be complete types. <i>-- end note</i>]</ins>
</p></blockquote>

<p><i>[
N.B.: The current wording of p. 6 already implicitly guarantees that
<code>U</code> is completely defined, because it requires that <code>Convertible&lt;U*, T*&gt;</code>
is true, see (6)+(8).
]</i></p>

</li>

<li>
<p>
20.3.1.3.4 <a href="https://wg21.link/unique.ptr.single.asgn">[unique.ptr.single.asgn]</a>&#47;11: No changes necessary.
</p>
<p><i>[
N.B.: Delegation to requirements of effects clause is sufficient.
]</i></p>

</li>

<li>
<p>
20.3.1.3.5 <a href="https://wg21.link/unique.ptr.single.observers">[unique.ptr.single.observers]</a>&#47;1+4+7+9+11:
</p>

<blockquote>
<pre>T* operator-&gt;() const;</pre>
<blockquote><p>
<ins><i>Note:</i> Use typically requires <code>T</code> shall be complete. &mdash; <i>end note</i>]</ins>
</p></blockquote>
</blockquote>
</li>

<li>
20.3.1.3.6 <a href="https://wg21.link/unique.ptr.single.modifiers">[unique.ptr.single.modifiers]</a>&#47;1: No changes necessary.
</li>

<li>
<p>
20.3.1.3.6 <a href="https://wg21.link/unique.ptr.single.modifiers">[unique.ptr.single.modifiers]</a>&#47;4: Just before p. 4 add a new paragraph:
</p>
<blockquote><p>
<i>Requires:</i> The expression <code>get_deleter()(get())</code> shall be well-formed,
shall have well-defined behavior, and shall not throw exceptions.
</p></blockquote>
</li>

<li>
20.3.1.3.6 <a href="https://wg21.link/unique.ptr.single.modifiers">[unique.ptr.single.modifiers]</a>&#47;7: No changes necessary.
</li>

<li>
<p>
20.3.1.4 <a href="https://wg21.link/unique.ptr.runtime">[unique.ptr.runtime]</a>: Add one additional bullet on paragraph 1:
</p>

<blockquote>
<p>
A specialization for array types is provided with a slightly altered interface.
</p>

<ul>
<li>
...
</li>
<li>
<ins><code>T</code> shall be a complete type.</ins>
</li>
</ul>
</blockquote>
</li>
</ol>

<p><i>[
post Bellevue: Daniel provided revised wording.
]</i></p>







</body>
</html>
