<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3003: &lt;future&gt; still has type-erased allocators in promise</title>
<meta property="og:title" content="Issue 3003: &lt;future&gt; still has type-erased allocators in promise">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3003.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="3003"><a href="lwg-defects.html#3003">3003</a>. <code>&lt;future&gt;</code> still has type-erased allocators in <code>promise</code></h3>
<p><b>Section:</b> 32.10.6 <a href="https://wg21.link/futures.promise">[futures.promise]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Billy O'Neal III <b>Opened:</b> 2017-07-16 <b>Last modified:</b> 2025-06-23</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#futures.promise">issues</a> in [futures.promise].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
In Toronto Saturday afternoon LWG discussed LWG <a href="lwg-defects.html#2976" title="Dangling uses_allocator specialization for packaged_task (Status: C++20)">2976</a><sup><a href="https://cplusplus.github.io/LWG/issue2976" title="Latest snapshot">(i)</a></sup> which finishes the job of removing allocator
support from <code>packaged_task</code>. LWG confirmed that, despite the removal of <code>packaged_task</code> allocators
"because it looks like <code>std::function</code>" was incorrect, they wanted to keep the allocator removals anyway,
in large part due to this resolution being a response to an NB comment.
<p/>
If we don't want the type erased allocator situation at all, then we should remove them from the remaining place
they exist in <code>&lt;future&gt;</code>, namely, in <code>promise</code>.
<p/>
This change also resolves potential implementation divergence on whether <code>allocator::construct</code> is intended
to be used on elements constructed in the shared state, and allows the emplace-construction-in-<code>future</code> paper,
<a href="https://wg21.link/P0319" title=" Adding Emplace functions for promise<T>/future<T> (revision 2)">P0319</a>, to be implemented without potential problems there.
</p>

<p><i>[28-Nov-2017 Mailing list discussion - set priority to P2]</i></p>

<p>Lots of people on the ML feel strongly about this; the suggestion was made that a paper would be welcomed laying out the rationale for removing allocator support here (and in other places).</p>

<p><i>[2018-1-26 issues processing telecon]</i></p>

<p>Status to 'Open'; Billy to write a paper.</p>

<p><i>[2019-06-03]</i></p>

<p>Jonathan observes that this resolution conflicts with <a href="lwg-defects.html#2095" title="promise and packaged_task missing constructors needed for uses-allocator construction (Status: Resolved)">2095</a><sup><a href="https://cplusplus.github.io/LWG/issue2095" title="Latest snapshot">(i)</a></sup>.</p>

<p><i>[Varna 2023-06-13; Change status to "LEWG"]</i></p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">

<p>This resolution is relative to <a href="https://wg21.link/n4659">N4659</a>.</p>

<ol>
<li><p>Edit 32.10.6 <a href="https://wg21.link/futures.promise">[futures.promise]</a>, class template <code>promise</code> synopsis, as indicated:</p>

<blockquote>
<pre>
template&lt;class R&gt;
class promise {
public:
  promise();
  <del>template &lt;class Allocator&gt;
    promise(allocator_arg_t, const Allocator&amp; a);</del>
  [&hellip;]
};
template &lt;class R&gt;
  void swap(promise&lt;R&gt;&amp; x, promise&lt;R&gt;&amp; y) noexcept;
<del>template &lt;class R, class Alloc&gt;
  struct uses_allocator&lt;promise&lt;R&gt;, Alloc&gt;;</del>
</pre>
[&hellip;]
<pre>
<del>template &lt;class R, class Alloc&gt;
  struct uses_allocator&lt;promise&lt;R&gt;, Alloc&gt;
    : true_type { };</del>
</pre>
<blockquote>
<p>
<del>-3- <i>Requires:</i> <code>Alloc</code> shall be an Allocator (16.4.4.6 <a href="https://wg21.link/allocator.requirements">[allocator.requirements]</a>).</del>
</p>
</blockquote>
<pre>
promise();
<del>template &lt;class Allocator&gt;
  promise(allocator_arg_t, const Allocator&amp; a);</del>
</pre>
<blockquote>
<p>
-4- <i>Effects:</i> constructs a <code>promise</code> object and a shared state. <del>The second constructor uses the allocator a
to allocate memory for the shared state.</del>
</p>
</blockquote>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2024-09-19; Jonathan provides improved wording]</i></p>

<p>
In July 2023 LEWG  considered this and LWG issue <a href="lwg-defects.html#2095" title="promise and packaged_task missing constructors needed for uses-allocator construction (Status: Resolved)">2095</a><sup><a href="https://cplusplus.github.io/LWG/issue2095" title="Latest snapshot">(i)</a></sup>
and requested a new proposed resolution that kept the existing constructor
(which is useful for controlling how the shared state is allocated)
but removed the <code class='backtick'>uses_allocator</code> specialization that makes <code class='backtick'>promise</code>
incorrectly claim to be allocator-aware.
Some of the rationale in <a href="https://wg21.link/P2787R1" title=" pmr::generator - Promise Types are not Values">P2787R1</a> is applicable here too.
</p>
<p>
Without the <code class='backtick'>uses_allocator</code> specialization, there's no reason to provide
an allocator-extended move constructor, resolving issue <a href="lwg-defects.html#2095" title="promise and packaged_task missing constructors needed for uses-allocator construction (Status: Resolved)">2095</a><sup><a href="https://cplusplus.github.io/LWG/issue2095" title="Latest snapshot">(i)</a></sup>.
</p>
<p>
And if we're going to continue supporting <code class='backtick'>std::promise</code> construction
with an allocator, we could restore that for <code class='backtick'>std::packaged_task</code> too.
That was removed by issue <a href="lwg-defects.html#2921" title="packaged_task and type-erased allocators (Status: C++17)">2921</a><sup><a href="https://cplusplus.github.io/LWG/issue2921" title="Latest snapshot">(i)</a></sup>, but issue <a href="lwg-defects.html#2976" title="Dangling uses_allocator specialization for packaged_task (Status: C++20)">2976</a><sup><a href="https://cplusplus.github.io/LWG/issue2976" title="Latest snapshot">(i)</a></sup>
argues that there was no good reason to do that. Removing <code class='backtick'>uses_allocator</code>
for <code class='backtick'>packaged_task</code> would have made sense (as proposed below for <code class='backtick'>promise</code>)
but 2921 didn't do that (which is why 2976 was needed).
We can restore the <code class='backtick'>packaged_task</code> constructor that takes an allocator,
and just not restore the <code class='backtick'>uses_allocator</code> specialization that implies
it should be fully allocator-aware.
Finally, if we restore that <code class='backtick'>packaged_task</code> constructor then we need to
fix <code class='backtick'>reset()</code> as discussed in issue <a href="lwg-defects.html#2245" title="packaged_task::reset() memory allocation (Status: Resolved)">2245</a><sup><a href="https://cplusplus.github.io/LWG/issue2245" title="Latest snapshot">(i)</a></sup>.
</p>
<p>In summary:
<ul>
<li>Keep support for using an allocator for shared state of a <code class='backtick'>promise</code>.</li>
<li>Remove <code class='backtick'>uses_allocator</code> specialization for <code class='backtick'>promise</code>.</li>
<li>Restore support for using an allocator for the shared state of <code class='backtick'>packaged_task</code>.</li>
<li>Do not restore <code class='backtick'>uses_allocator</code> specialization for <code class='backtick'>packaged_task</code>.</li>
<li>Update <code class='backtick'>packaged_task::reset()</code> to deal with allocators.</li>
</ul>
</p>

<p><i>[Wrocław 2024-11-18; LEWG would prefer a paper for this]</i></p>


<p><i>[2025-06-21 Status changed: LEWG &rarr; Resolved.]</i></p>

Resolved by adoption of <a href="https://wg21.link/P3503R3" title=" Make type-erased allocator use in promise and packaged_task consistent">P3503R3</a> in Sofia.



<p id="res-3003"><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 32.10.6 <a href="https://wg21.link/futures.promise">[futures.promise]</a> as indicated:</p>
<blockquote>
<pre>
<del>template &lt;class R, class Alloc&gt;
  struct uses_allocator&lt;promise&lt;R&gt;, Alloc&gt;;</del>
</pre>
[&hellip;]
<pre>
<del>template &lt;class R, class Alloc&gt;
  struct uses_allocator&lt;promise&lt;R&gt;, Alloc&gt;
    : true_type { };</del>
</pre>
<blockquote>
<p>
<del>-4- <i>Preconditions:</i> <code>Alloc</code> meets the <i>Cpp17Allocator</i> (16.4.4.6.1 <a href="https://wg21.link/allocator.requirements.general">[allocator.requirements.general]</a>).</del>
</p>
</blockquote>
</blockquote>
</li>
<li><p>Modify 32.10.10.1 <a href="https://wg21.link/futures.task.general">[futures.task.general]</a> as indicated:</p>
<blockquote>
<pre>
  template&lt;class R, class... ArgTypes&gt;
  class packaged_task&lt;R(ArgTypes...)&gt; {
  public:
    // construction and destruction
    packaged_task() noexcept;
    template&lt;class F&gt;
      explicit packaged_task(F&amp;&amp; f);
<ins>    template&lt;class F, class Allocator&gt;
      explicit packaged_task(allocator_arg_t, const Allocator&amp; a, F&amp;&amp; f);</ins>
    ~packaged_task();
</pre>
</blockquote>
</li>
<li><p>Modify 32.10.10.2 <a href="https://wg21.link/futures.task.members">[futures.task.members]</a> as indicated:</p>
<blockquote>
<pre>template&lt;class F&gt;
  explicit packaged_task(F&amp;&amp; f);
</pre>
<p><ins>-?- <i>Effects</i>: Equivalent to
<code>packaged_task(allocator_arg, std::allocator&lt;int&gt;(), std::forward&lt;F&gt;(f))</code>.
</ins>
</p>
<blockquote class="note">
[<i>Drafting note</i>:
Uses of <code>std::allocator&lt;int&gt;</code> and
<code>std::allocator&lt;<i>unspecified</i>&gt;</code> are not observable
so this constructor can be implemented without delegating to the
other constructor and without using <code class='backtick'>std::allocator</code>.]
</blockquote>
<pre><ins>template&lt;class F, class Allocator&gt;
  packaged_task(allocator_arg_t, const Allocator&amp; a, F&amp;&amp; f);</ins>
</pre>
<p>-2- <i>Constraints</i>:
<code>remove_cvref_t&lt;F&gt;</code> is not the same type as
<code>packaged_task&lt;R(ArgTypes...)&gt;</code>.
</p>
<p>-3- <i>Mandates</i>:
<code>is_invocable_r_v&lt;R, F&amp;, ArgTypes...&gt;</code> is <code>true</code>.
</p>
<blockquote class="note">
[<i>Drafting note</i>:
Issue <a href="lwg-defects.html#4154" title="The Mandates for std::packaged_task's constructor from a callable entity should consider decaying (Status: WP)">4154</a><sup><a href="https://cplusplus.github.io/LWG/issue4154" title="Latest snapshot">(i)</a></sup> alters these <i>Mandates</i>: and <i>Effects</i>:
but the two edits should combine cleanly.]
</blockquote>
<p>-4- <i>Preconditions</i>:
Invoking a copy of <code class='backtick'>f</code> behaves the same as invoking <code class='backtick'>f</code>.
<ins>
<code class='backtick'>Allocator</code> meets the <i>Cpp17Allocator</i> requirements
(16.4.4.6.1 <a href="https://wg21.link/allocator.requirements.general">[allocator.requirements.general]</a>).
</ins>
</p>
<p>-5- <i>Effects</i>:
<ins>Let <code class='backtick'>A2</code> be
<code>allocator_traits&lt;Allocator&gt;::rebind_alloc&lt;<i>unspecified</i>&gt;</code>
and let <code class='backtick'>a2</code> be an lvalue of type <code class='backtick'>A2</code> initialized with <code class='backtick'>A2(a)</code>.
</ins>
Creates a shared state and initializes
the object's stored task with <code>std::forward&lt;F&gt;(f)</code>.
<ins>
Uses <code class='backtick'>a2</code> to allocate storage for the shared state
and stores a copy of <code class='backtick'>a2</code> in the shared state.
</ins>
</p>
<p>-6- <i>Throws</i>:
<del>
Any exceptions thrown by the copy or move constructor of <code class='backtick'>f</code>,
or bad_alloc if memory for the internal data structures cannot be allocated.
</del>
<ins>
Any exceptions thrown by the initialization of the stored task.
If storage for the shared state cannot be allocated,
any exception thrown by <code class='backtick'>A2::allocate</code>.
</ins>
</p>
<p> &hellip; </p>
<pre>
void reset();
</pre>
<p>-26- <i>Effects</i>:
<del>As if</del>
<ins>Equivalent to:</ins>
<pre>
<ins>if (!valid())
  throw future_error(future_errc::no_state);</ins>
*this = packaged_task(<ins>allocator_arg, a,</ins> std::move(f));
</pre>
where <code class='backtick'>f</code> is the task stored in <code class='backtick'>*this</code>
<ins>and <code class='backtick'>a</code> is the allocator stored in the shared state</ins>.
</p>
<p>
[<i>Note 2</i>: This constructs a new shared state for <code class='backtick'>*this</code>.
The old state is abandoned (32.10.5 <a href="https://wg21.link/futures.state">[futures.state]</a>).
&mdash; <i>end note</i>]
</p>
<p>-27- <i>Throws</i>:
<ol style="list-style-type: none">
<li><del>(27.1) &mdash; bad_alloc if memory for the new shared state cannot be allocated.</del> </li>
<li>(27.2) &mdash; Any exception thrown by the
<ins><code class='backtick'>packaged_task</code> constructor</ins>
<del>move constructor of the task stored in the shared state</del>. </li>
<li>(27.3) &mdash; <code class='backtick'>future_error</code> with an error condition of <code class='backtick'>no_state</code> if <code class='backtick'>*this</code> has no shared state. </li>
</ol>
</p>
</blockquote>
</li>
</ol>





</body>
</html>
