<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2442: call_once() shouldn't DECAY_COPY()</title>
<meta property="og:title" content="Issue 2442: call_once() shouldn't DECAY_COPY()">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2442.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="2442"><a href="lwg-defects.html#2442">2442</a>. <code>call_once()</code> shouldn't <code>DECAY_COPY()</code></h3>
<p><b>Section:</b> 32.6.7.2 <a href="https://wg21.link/thread.once.callonce">[thread.once.callonce]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Stephan T. Lavavej <b>Opened:</b> 2014-10-01 <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#thread.once.callonce">issues</a> in [thread.once.callonce].</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>
When LWG <a href="lwg-defects.html#891" title="std::thread, std::call_once issue (Status: C++11)">891</a><sup><a href="https://cplusplus.github.io/LWG/issue891" title="Latest snapshot">(i)</a></sup> overhauled <code>call_once()</code>'s specification, it used <code>decay_copy()</code>, following 
LWG <a href="lwg-defects.html#929" title="Thread constructor (Status: C++11)">929</a><sup><a href="https://cplusplus.github.io/LWG/issue929" title="Latest snapshot">(i)</a></sup>'s overhaul of <code>thread</code>'s constructor.
<p/>
In <code>thread</code>'s constructor, this is necessary and critically important. 32.4.3.3 <a href="https://wg21.link/thread.thread.constr">[thread.thread.constr]</a>/5 
"The new thread of execution executes <code><i>INVOKE</i>(<i>DECAY_COPY</i>(std::forward&lt;F&gt;(f)), 
<i>DECAY_COPY</i>(std::forward&lt;Args&gt;(args))...)</code> 
with the calls to <code><i>DECAY_COPY</i></code> being evaluated in the constructing thread." requires the parent thread 
to copy arguments for the child thread to access.
<p/>
In <code>call_once()</code>, this is unnecessary and harmful. It's unnecessary because <code>call_once()</code> doesn't transfer 
arguments between threads. It's harmful because:
</p>
<ul>
<li><p><code>decay_copy()</code> returns a prvalue. Given <code>meow(int&amp;)</code>, <code>meow(i)</code> can be called directly, 
but <code>call_once(flag, meow, i)</code> won't compile.</p></li>
<li><p><code>decay_copy()</code> moves from modifiable rvalues. Given <code>purr(const unique_ptr&lt;int&gt;&amp;)</code>, 
<code>purr(move(up))</code> won't modify <code>up</code>. (This is observable, because moved-from <code>unique_ptr</code>s are 
guaranteed empty.) However, <code>call_once(flag, purr, move(up))</code> will leave <code>up</code> empty after the first active 
execution. Observe the behavioral difference &mdash; if <code>purr()</code> is directly called like this repeatedly until it 
doesn't throw an exception, each call will observe <code>up</code> unchanged. With <code>call_once()</code>, the second active 
execution will observe <code>up</code> to be empty.</p></li>
</ul>
<p>
<code>call_once()</code> should use perfect forwarding without <code>decay_copy()</code>, in order to avoid interfering with the call like this.
</p>

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

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

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

<p>
Looks good to us, but this is really an LWG issue.
</p>

<p><i>[2015-05-07 Lenexa: Move Immediate]</i></p>

<p>LWG 2442 call_once shouldn't decay_copy</p>
<p>STL summarizes the SG1 minutes.</p>
<p>Marshall: Jonathan updated all the issues with SG1 status last night. Except this one.</p>
<p>STL summarizes the issue.</p>
<p>Dietmar: Of course, call_once has become useless.</p>
<p>STL: With magic statics.</p>
<p>Jonathan: Magic statics can't be per object, which I use in future.</p>
<p>Marshall: I see why you are removing the MoveConstructible on the arguments, but what about Callable?</p>
<p>STL: That's a type named Callable, which we will no longer decay_copy. We're still requiring the INVOKE expression to be valid.</p>
<p>Marshall: Okay. Basically, ripping the decay_copy out of here.</p>
<p>STL: I recall searching the Standard for other occurrences and I believe this is the only inappropriate use of decay_copy.</p>
<p>Marshall: We do the decay_copy.</p>
<p>Jonathan: Us too.</p>
<p>Marshall: What do people think?</p>
<p>Jonathan: I think STL's right. In the use I was mentioning inside futures, I actually pass them by reference_wrapper and pointers, to avoid the decay causing problems. Inside the call_once, I then extract the args. So I've had to work around this and didn't realize it was a defect.</p>
<p>Marshall: What do people think is the right resolution?</p>
<p>STL: I would like to see Immediate.</p>
<p>Hwrd: No objections to Immediate.</p>
<p>Marshall: Bill is nodding.</p>
<p>PJP: He said it. Everything STL says applies to our other customers.</p>
<p>Marshall: Any objections to Immediate?</p>
<p>Jonathan: I can't see any funky implementations where a decay_copy would be necessary?</p>
<p>Marshall: 6 votes for Immediate, 0 opposed, 0 abstaining.</p>



<p id="res-2442"><b>Proposed resolution:</b></p>
<p>
This wording is relative to N3936.
</p>

<ol>
<li><p>Change 32.6.7.2 <a href="https://wg21.link/thread.once.callonce">[thread.once.callonce]</a> p1+p2 as depicted:</p>

<blockquote>
<pre>
template&lt;class Callable, class ...Args&gt;
  void call_once(once_flag&amp; flag, Callable&amp;&amp; func, Args&amp;&amp;... args);
</pre>
<blockquote>
<p>
-1- <i>Requires</i>: <del><code>Callable</code> and each <code>Ti</code> in <code>Args</code> shall satisfy the <code>MoveConstructible</code> 
requirements.</del> <code><i>INVOKE</i>(<del><i>DECAY_COPY</i>(</del>std::forward&lt;Callable&gt;(func)<del>)</del>, 
<del><i>DECAY_COPY</i>(</del>std::forward&lt;Args&gt;(args)<del>)</del>...)</code>
(20.9.2) shall be a valid expression.
<p/>
-2- <i>Effects</i>; [&hellip;] An active execution shall call 
<code><i>INVOKE</i>(<del><i>DECAY_COPY</i>(</del>std::forward&lt;Callable&gt;(func)<del>)</del>, 
<del><i>DECAY_COPY</i>(</del>std::forward&lt;Args&gt;(args)<del>)</del>...)</code>. [&hellip;]
</p>
</blockquote>
</blockquote>
</li>
</ol>






</body>
</html>
