<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1110: Is for_each overconstrained?</title>
<meta property="og:title" content="Issue 1110: Is for_each overconstrained?">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1110.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++11">C++11</a> status.</em></p>
<h3 id="1110"><a href="lwg-defects.html#1110">1110</a>. Is <code>for_each</code> overconstrained?</h3>
<p><b>Section:</b> 26.6.5 <a href="https://wg21.link/alg.foreach">[alg.foreach]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Alisdair Meredith <b>Opened:</b> 2009-04-29 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#alg.foreach">active issues</a> in [alg.foreach].</p>
<p><b>View all other</b> <a href="lwg-index.html#alg.foreach">issues</a> in [alg.foreach].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++11">C++11</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Quoting working paper for reference (26.6.5 <a href="https://wg21.link/alg.foreach">[alg.foreach]</a>):
</p>

<blockquote>
<pre>
template&lt;InputIterator Iter, Callable&lt;auto, Iter::reference&gt; Function&gt;
  requires CopyConstructible&lt;Function&gt;
  Function for_each(Iter first, Iter last, Function f);
</pre>
<blockquote>
<p>
1 Effects: Applies f to the result of dereferencing every iterator in the
 range [first,last), starting from first and proceeding to last - 1.
</p>
<p>
2 Returns: f.
</p>
<p>
3 Complexity: Applies f exactly last - first times.
</p>
</blockquote>
</blockquote>

<p>
P2 implies the passed object <code>f</code> should be invoked at each stage, rather than
some copy of <code>f</code>.  This is important if the return value is to usefully
accumulate changes.  So the requirements are an object of type <code>Function</code> can
be passed-by-value, invoked multiple times, and then return by value.  In
this case, <code>MoveConstructible</code> is sufficient. This would open support for
move-only functors, which might become important in concurrent code as you
can assume there are no other references (copies) of a move-only type and so
freely use them concurrently without additional locks.
</p>

<p><i>[
See further discussion starting with c++std-lib-23686.
]</i></p>


<p><i>[
Batavia (2009-05):
]</i></p>

<blockquote>
<p>
Pete suggests we may want to look at this in a broader context
involving other algorithms.
We should also consider the implications of parallelism.
</p>
<p>
Move to Open, and recommend the issue be deferred until after the next
Committee Draft is issued.
</p>
</blockquote>

<p><i>[
2009-10-14 Daniel de-conceptified the proposed resolution.
]</i></p>


<blockquote>
<p>
The note in 26.1 <a href="https://wg21.link/algorithms.general">[algorithms.general]</a>/9 already says the right thing:
</p>
<blockquote><p>
Unless otherwise specified, algorithms that take function objects
as arguments are permitted to copy those function objects freely.
</p></blockquote>
<p>
So we only need to ensure that the wording for <code>for_each</code> is sufficiently
clear, which is the intend of the following rewording.
</p>
</blockquote>

<p><i>[
2009-10-15 Daniel proposes:
]</i></p>


<blockquote>
<ul>
<li>
<p>
Add a new Requires clause just after the prototype declaration (26.6.5 <a href="https://wg21.link/alg.foreach">[alg.foreach]</a>):
</p>
<blockquote>
<p>
<ins><i>Requires:</i> <code>Function</code> shall be <code>MoveConstructible</code>
( [moveconstructible]), <code>CopyConstructible</code> is not required.</ins>
</p>
</blockquote>
</li>
<li>
<p>
Change 26.6.5 <a href="https://wg21.link/alg.foreach">[alg.foreach]</a>/2 as indicated:
</p>

<blockquote><p>
<i>Returns:</i> <ins>std::move(</ins>f<ins>)</ins>.
</p></blockquote>
</li>
</ul>
</blockquote>

<p><i>[
2009-10 post-Santa Cruz:
]</i></p>


<blockquote><p>
Move to Tentatively Ready, using Daniel's wording without the portion
saying "CopyConstructible is not required".
</p></blockquote>

<p><i>[
2009-10-27 Daniel adds:
]</i></p>


<blockquote>
<p>
I see that during the Santa Cruz meeting the originally proposed addition
</p>

<blockquote><p>
, <code>CopyConstructible</code> is not required.
</p></blockquote>

<p>
was removed. I don't think that this removal was a good idea. The combination
of 26.1 <a href="https://wg21.link/algorithms.general">[algorithms.general]</a> p.9
</p>

<blockquote><p>
[<i>Note:</i> Unless otherwise specified, algorithms that take function objects
as arguments are permitted to copy those function objects freely.[..]
</p></blockquote>

<p>
with the fact that <code>CopyConstructible</code> is a refinement <code>MoveConstructible</code>
makes it necessary that such an explicit statement is given. Even the
existence of the usage of <code>std::move</code> in the <i>Returns</i> clause doesn't
help much, because this would still be well-formed for a <code>CopyConstructible</code>
without move constructor. Let me add that the originally proposed
addition reflects current practice in the standard, e.g. 26.7.9 <a href="https://wg21.link/alg.unique">[alg.unique]</a> p.5
usages a similar terminology.
</p>

<p>
For similar wording need in case for auto_ptr see <a href="lwg-closed.html#973" title="auto_ptr characteristics (Status: NAD Editorial)">973</a><sup><a href="https://cplusplus.github.io/LWG/issue973" title="Latest snapshot">(i)</a></sup>.
</p>

<p><i>[
Howard: Moved from Tentatively Ready to Open.
]</i></p>

</blockquote>

<p><i>[
2009-11-20 Howard restores "not <code>CopyConstructible</code>" to the spec.
]</i></p>


<p><i>[
2009-11-22 Moved to Tentatively Ready after 5 positive votes on c++std-lib.
]</i></p>



<p id="res-1110"><b>Proposed resolution:</b></p>
<ul>
<li>
<p>
Add a new Requires clause just after the prototype declaration (26.6.5 <a href="https://wg21.link/alg.foreach">[alg.foreach]</a>):
</p>
<blockquote>
<p>
<ins><i>Requires:</i> <code>Function</code> shall meet the requirements of
<code>MoveConstructible</code> ( [moveconstructible]). 
<code>Function</code> need not meet the requirements of
<code>CopyConstructible</code> ( [copyconstructible]).</ins>
</p>
</blockquote>
</li>
<li>
<p>
Change 26.6.5 <a href="https://wg21.link/alg.foreach">[alg.foreach]</a>/2 as indicated:
</p>

<blockquote><p>
<i>Returns:</i> <ins>std::move(</ins>f<ins>)</ins>.
</p></blockquote>
</li>
</ul>








</body>
</html>
