<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3216: Rebinding the allocator before calling construct/destroy in allocate_shared</title>
<meta property="og:title" content="Issue 3216: Rebinding the allocator before calling construct/destroy in allocate_shared">
<meta property="og:description" content="C++ library issue. Status: WP">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3216.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#WP">WP</a> status.</em></p>
<h3 id="3216"><a href="lwg-defects.html#3216">3216</a>. Rebinding the allocator before calling <code>construct</code>/<code>destroy</code> in <code>allocate_shared</code></h3>
<p><b>Section:</b> 20.3.2.2.7 <a href="https://wg21.link/util.smartptr.shared.create">[util.smartptr.shared.create]</a> <b>Status:</b> <a href="lwg-active.html#WP">WP</a>
 <b>Submitter:</b> Billy O'Neal III <b>Opened:</b> 2019-06-11 <b>Last modified:</b> 2024-11-28</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#util.smartptr.shared.create">issues</a> in [util.smartptr.shared.create].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#WP">WP</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The new <code>allocate_shared</code> wording says we need to rebind the allocator back to <code>T</code>'s
type before we can call <code>construct</code> or <code>destroy</code>, but this is suboptimal (might make
extra unnecessary allocator copies), and is inconsistent with the containers' behavior, which call
allocator <code>construct</code> on whatever <code>T</code> they want. (For example,
<code>std::list&lt;T, alloc&lt;T&gt;&gt;</code> rebinds to <code>alloc&lt;_ListNode&lt;T&gt;&gt;</code>,
but calls <code>construct(T*)</code> without rebinding back)
<p/>
It seems like we should be consistent with the containers and not require a rebind here. PR would
look something like this, relative to N4810; I'm still not super happy with this wording because
it looks like it might be saying a copy of the allocator must be made we would like to avoid&hellip;
</p>

<p><i>[2019-07 Issue Prioritization]</i></p>

<p>Priority to 3 after discussion on the reflector.</p>
<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">

<p>This wording is relative to <a href="https://wg21.link/n4810">N4810</a>.</p>

<ol>
<li><p>Modify 20.3.2.2.7 <a href="https://wg21.link/util.smartptr.shared.create">[util.smartptr.shared.create]</a> as indicated:</p>

<blockquote class="note">
<p>
[<i>Drafting note:</i> The edits to change <code>pv</code> to <code>pu</code> were suggested by Jonathan
Wakely (thanks!). This wording also has the <code>remove_cv_t</code> fixes specified by LWG <a href="lwg-active.html#3210" title="allocate_shared is inconsistent about removing const from the pointer
passed to allocator construct and destroy (Status: New)">3210</a><sup><a href="https://cplusplus.github.io/LWG/issue3210" title="Latest snapshot">(i)</a></sup>
&mdash; if that change is rejected some of those have to be stripped here.]
</p>
</blockquote>

<blockquote>
<pre>
template&lt;class T, ...&gt;
  shared_ptr&lt;T&gt; make_shared(<i>args</i>);
template&lt;class T, class A, ...&gt;
  shared_ptr&lt;T&gt; allocate_shared(const A&amp; a, <i>args</i>);
template&lt;class T, ...&gt;
  shared_ptr&lt;T&gt; make_shared_default_init(<i>args</i>);
template&lt;class T, class A, ...&gt;
  shared_ptr&lt;T&gt; allocate_shared_default_init(const A&amp; a, <i>args</i>);
</pre>
<blockquote>
<p>
-2- <i>Requires:</i> [&hellip;]
<p/>
[&hellip;]
<p/>
-7- <i>Remarks:</i>
</p>
<ol style="list-style-type: none">
<li><p>(7.1) &mdash; [&hellip;]</p></li>
<li><p>[&hellip;]</p></li>
<li><p>(7.5) &mdash; When a (sub)object of a non-array type <code>U</code> is specified to have an initial
value of <code>v</code>, or <code>U(l...)</code>, where <code>l...</code> is a list of constructor arguments,
<code>allocate_shared</code> shall initialize this (sub)object via the expression
</p>
<ol style="list-style-type: none">
<li><p>(7.5.1) &mdash; <code>allocator_traits&lt;A2&gt;::construct(a2, p<del>v</del><ins>u</ins>, v)</code> or</p></li>
<li><p>(7.5.2) &mdash; <code>allocator_traits&lt;A2&gt;::construct(a2, p<del>v</del><ins>u</ins>, l...)</code></p></li>
</ol>
<p>
respectively, where <code>p<del>v</del><ins>u</ins></code> <ins>is a pointer of type
<code>remove_cv_t&lt;U&gt;*</code></ins> point<del>s</del><ins>ing</ins> to storage suitable to hold
an object of type <code><ins>remove_cv_t&lt;</ins>U<ins>&gt;</ins></code> and <code>a2</code> of type
<code>A2</code> is a <ins>potentially</ins> rebound copy of the allocator <code>a</code>
passed to <code>allocate_shared</code> <del>such that its <code>value_type</code> is <code>remove_cv_t&lt;U&gt;</code></del>.
</p>
</li>
<li><p>(7.6) &mdash; [&hellip;]</p></li>
<li><p>(7.7) &mdash; When a (sub)object of non-array type <code>U</code> is specified to have a default
initial value, <code>allocate_shared</code> <del>shall</del> initialize<ins>s</ins> this (sub)object via
the expression <code>allocator_traits&lt;A2&gt;::construct(a2, p<del>v</del><ins>u</ins>)</code>, where
<code>p<del>v</del><ins>u</ins></code> <ins>is a pointer of type <code>remove_cv_t&lt;U&gt;*</code></ins>
point<del>s</del><ins>ing</ins> to storage suitable to hold an object of type
<code><ins>remove_cv_t&lt;</ins>U<ins>&gt;</ins></code> and <code>a2</code> of type <code>A2</code> is a
<ins>potentially</ins> rebound copy of the allocator <code>a</code> passed to <code>allocate_shared</code>
<del>such that its <code>value_type</code> is <code>remove_cv_t&lt;U&gt;</code></del>.</p></li>
<li><p>[&hellip;]</p></li>
<li><p>(7.12) &mdash; When a (sub)object of non-array type <code>U</code> that was initialized by
<code>allocate_shared</code> is to be destroyed, it is destroyed via the expression
<code>allocator_traits&lt;A2&gt;::destroy(a2, p<del>v</del><ins>u</ins>)</code> where
<code>p<del>v</del><ins>u</ins></code> <ins>is a pointer of type <code>remove_cv_t&lt;U&gt;*</code></ins>
point<del>s</del><ins>ing</ins> to that object of type <code>remove_cv_t&lt;U&gt;</code> and
<code>a2</code> of type <code>A2</code> is a <ins>potentially</ins> rebound copy of the
allocator <code>a</code> passed to <code>allocate_shared</code> <del>such that its <code>value_type</code> is
<code>remove_cv_t&lt;U&gt;</code></del>.</p></li>
</ol>
</blockquote>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2024-08-23; Jonathan provides updated wording]</i></p>

<p>
<code class='backtick'>make_shared_default_init</code> and <code class='backtick'>allocate_shared_default_init</code> were renamed
by <a href="https://wg21.link/P1973R1" title=" Rename _default_init functions (NB Comment DE002)">P1973R1</a> so this needs a rebase.
The edit to (7.11) is just for consistency, so that <code class='backtick'>pv</code> is always <code class='backtick'>void*</code>
and <code class='backtick'>pu</code> is <code>remove_cv_t&lt;U&gt;*</code>.
Accepting this proposed resolution would also resolve issue <a href="lwg-active.html#3210" title="allocate_shared is inconsistent about removing const from the pointer
passed to allocator construct and destroy (Status: New)">3210</a><sup><a href="https://cplusplus.github.io/LWG/issue3210" title="Latest snapshot">(i)</a></sup>.
</p>


<p><i>[2024-10-02; Reflector poll]</i></p>

<p>
Set status to Tentatively Ready after six votes in favour during reflector poll.
</p>

<p><i>[Wrocław 2024-11-23; Status changed: Voting &rarr; WP.]</i></p>



<p id="res-3216"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/N4988" title=" Working Draft, Programming Languages — C++">N4988</a>.</p>

<ol>
<li><p>Modify 20.3.2.2.7 <a href="https://wg21.link/util.smartptr.shared.create">[util.smartptr.shared.create]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class T, ...&gt;
  shared_ptr&lt;T&gt; make_shared(<i>args</i>);
template&lt;class T, class A, ...&gt;
  shared_ptr&lt;T&gt; allocate_shared(const A&amp; a, <i>args</i>);
template&lt;class T, ...&gt;
  shared_ptr&lt;T&gt; make_shared_for_overwrite(<i>args</i>);
template&lt;class T, class A, ...&gt;
  shared_ptr&lt;T&gt; allocate_shared_for_overwrite(const A&amp; a, <i>args</i>);
</pre>
<blockquote>
<p>
-2- <i>Preconditions:</i> [&hellip;]
<p/>
[&hellip;]
<p/>
-7- <i>Remarks:</i>
</p>
<ol style="list-style-type: none">
<li><p>(7.1) &mdash; [&hellip;]</p></li>
<li><p>[&hellip;]</p></li>
<li><p>(7.5) &mdash; When a (sub)object of a non-array type <code>U</code> is specified to have an initial
value of <code>v</code>, or <code>U(l...)</code>, where <code>l...</code> is a list of constructor arguments,
<code>allocate_shared</code> shall initialize this (sub)object via the expression
</p>
<ol style="list-style-type: none">
<li><p>(7.5.1) &mdash; <code>allocator_traits&lt;A2&gt;::construct(a2, p<del>v</del><ins>u</ins>, v)</code> or</p></li>
<li><p>(7.5.2) &mdash; <code>allocator_traits&lt;A2&gt;::construct(a2, p<del>v</del><ins>u</ins>, l...)</code></p></li>
</ol>
<p>
respectively, where <code>p<del>v</del><ins>u</ins></code> <ins>is a pointer of type
<code>remove_cv_t&lt;U&gt;*</code></ins> point<del>s</del><ins>ing</ins> to storage suitable to hold
an object of type <code><ins>remove_cv_t&lt;</ins>U<ins>&gt;</ins></code> and <code>a2</code> of type
<code>A2</code> is a <ins>potentially</ins> rebound copy of the allocator <code>a</code>
passed to <code>allocate_shared</code> <del>such that its <code>value_type</code> is <code>remove_cv_t&lt;U&gt;</code></del>.
</p>
</li>
<li><p>(7.6) &mdash; [&hellip;]</p></li>
<li><p>(7.7) &mdash; When a (sub)object of non-array type <code>U</code> is specified to have a default
initial value, <code>allocate_shared</code> <del>shall</del> initialize<ins>s</ins> this (sub)object via
the expression <code>allocator_traits&lt;A2&gt;::construct(a2, p<del>v</del><ins>u</ins>)</code>, where
<code>p<del>v</del><ins>u</ins></code> <ins>is a pointer of type <code>remove_cv_t&lt;U&gt;*</code></ins>
point<del>s</del><ins>ing</ins> to storage suitable to hold an object of type
<code><ins>remove_cv_t&lt;</ins>U<ins>&gt;</ins></code> and <code>a2</code> of type <code>A2</code> is a
<ins>potentially</ins> rebound copy of the allocator <code>a</code> passed to <code>allocate_shared</code>
<del>such that its <code>value_type</code> is <code>remove_cv_t&lt;U&gt;</code></del>.</p></li>
<li><p>[&hellip;]</p></li>
<li>
<blockquote class="note"><p>[<i>Drafting note</i>:
Issue <a href="lwg-defects.html#4024" title="Underspecified destruction of objects created in std::make_shared_for_overwrite/std::allocate_shared_for_overwrite (Status: WP)">4024</a><sup><a href="https://cplusplus.github.io/LWG/issue4024" title="Latest snapshot">(i)</a></sup> will add <code class='backtick'>make_shared_for_overwrite</code>
and <code class='backtick'>allocate_shared_for_overwrite</code> to (7.11) but that doesn't conflict with this next edit.]
</p></blockquote>
<p>(7.11) &mdash; When a (sub)object of non-array type <code>U</code> that was initialized by
<code>make_shared</code> is to be destroyed, it is destroyed via the expression
<code>p<del>v</del><ins>u</ins>->~U()</code> where <code>p<del>v</del><ins>u</ins></code>
points to that object of type <code>U</code>.</p></li>
<li><p>(7.12) &mdash; When a (sub)object of non-array type <code>U</code> that was initialized by
<code>allocate_shared</code> is to be destroyed, it is destroyed via the expression
<code>allocator_traits&lt;A2&gt;::destroy(a2, p<del>v</del><ins>u</ins>)</code> where
<code>p<del>v</del><ins>u</ins></code> <ins>is a pointer of type <code>remove_cv_t&lt;U&gt;*</code></ins>
point<del>s</del><ins>ing</ins> to that object of type <code>remove_cv_t&lt;U&gt;</code> and
<code>a2</code> of type <code>A2</code> is a <ins>potentially</ins> rebound copy of the
allocator <code>a</code> passed to <code>allocate_shared</code> <del>such that its <code>value_type</code> is
<code>remove_cv_t&lt;U&gt;</code></del>.</p></li>
</ol>
</blockquote>
</blockquote>
</li>
</ol>




</body>
</html>
