<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2276: Missing requirement on std::promise::set_exception</title>
<meta property="og:title" content="Issue 2276: Missing requirement on std::promise::set_exception">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2276.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#C++17">C++17</a> status.</em></p>
<h3 id="2276"><a href="lwg-defects.html#2276">2276</a>. Missing requirement on <code>std::promise::set_exception</code></h3>
<p><b>Section:</b> 32.10 <a href="https://wg21.link/futures">[futures]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2013-07-30 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#futures">issues</a> in [futures].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++17">C++17</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The standard does not specify the behaviour of this program:
</p>

<blockquote><pre>
#include &lt;future&gt;
#include &lt;cassert&gt;

struct NonTrivial
{
  NonTrivial() : init(true) { }
  ~NonTrivial() { assert(init); }
  bool init;
};

int main()
{
  std::promise&lt;NonTrivial&gt; p;
  auto f = p.get_future();
  p.set_exception(std::exception_ptr());
  f.get();
}
</pre></blockquote>

<p>
The standard doesn't forbid making the state ready with a null
<code>exception_ptr</code>, so what should <code>get()</code> return?  There's no stored
exception to throw, but it can't return a value because none was initialized.
<p/>
A careful reading of the standard shows 32.10.5 <a href="https://wg21.link/futures.state">[futures.state]</a> p8 says
"A shared state is <em>ready</em> only if it holds a value or an exception
ready for retrieval." One can infer from the fact that <code>set_exception()</code>
makes the state ready that it must store a value or exception, so
cannot store "nothing", but that isn't explicit.
<p/>
The <code>promise::set_exception()</code> and <code>promise::set_exception_at_thread_exit()</code> 
members should require <code>p != nullptr</code> or should state the type of exception thrown 
if <code>p</code> is null.
</p>

<p><i>[2015-02 Cologne]</i></p>

<p>Handed over to SG1.</p>

<p><i>[2015-05 Lenexa, SG1 response]</i></p>

<p>SG1 provides P/R and requests move to SG1-OK status: Add Requires clauses for promise (30.6.5 [futures.promise]) set_exception (before p18) and set_exception_at_thread_exit (before p24): Requires: p is not null.</p>

<p><i>[2015-10, Kona issue prioritization]</i></p>

<p>Priority 0, move to Ready</p>


<p id="res-2276"><b>Proposed resolution:</b></p>
<p>
This wording is relative to N4431.
</p>
Add Requires clauses for promise (32.10.6 <a href="https://wg21.link/futures.promise">[futures.promise]</a>) <code>set_exception</code> (before p18) and 
<code>set_exception_at_thread_exit</code> (before p24): <i>Requires</i>: <code>p</code> is not null.

<ol>
<li><p>Change 32.10.6 <a href="https://wg21.link/futures.promise">[futures.promise]</a> as depicted:</p>

<blockquote>
<pre>
void set_exception(exception_ptr p);
</pre>
<blockquote>
<p>
<ins>-??- <i>Requires:</i> <code>p</code> is not null. </ins>
</p>
<p>
-18- <i>Effects:</i> atomically stores the exception pointer <code>p</code> in the shared state and makes that state ready (30.6.4).
</p>
</blockquote>
</blockquote>

<blockquote>
<pre>
void set_exception_at_thread_exit(exception_ptr p);
</pre>
<blockquote>
<p>
<ins>-??- <i>Requires:</i> <code>p</code> is not null. </ins>
</p>
<p>
-24- <i>Effects:</i> Stores the exception pointer <code>p</code> in the shared state without making that state ready immediately. [&hellip;]
</p>
</blockquote>
</blockquote>
</li></ol>






</body>
</html>
