<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3837: std::erase_if overloads for non-associative containers should move (and
not copy) their predicate object</title>
<meta property="og:title" content="Issue 3837: std::erase_if overloads for non-associative containers should move (and
not copy) their predicate object">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3837.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#New">New</a> status.</em></p>
<h3 id="3837"><a href="lwg-active.html#3837">3837</a>. <code>std::erase_if</code> overloads for non-associative containers should move (and
not copy) their predicate object</h3>
<p><b>Section:</b> 27.4.4.5 <a href="https://wg21.link/string.erasure">[string.erasure]</a>, 23.3.5.5 <a href="https://wg21.link/deque.erasure">[deque.erasure]</a>, 23.3.7.7 <a href="https://wg21.link/forward.list.erasure">[forward.list.erasure]</a>, 23.3.11.6 <a href="https://wg21.link/list.erasure">[list.erasure]</a>, 23.3.13.6 <a href="https://wg21.link/vector.erasure">[vector.erasure]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Giuseppe D'Angelo <b>Opened:</b> 2022-12-04 <b>Last modified:</b> 2023-01-06</p>
<p><b>Priority: </b>3
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Consistent uniform erasure added several overloads of <code>std::erase_if</code>. 
All these overloads have fully specified effects, and are either implemented
through the <code>erase</code>/<code>remove_if</code> idiom (for <code>string</code>, <code>vector</code> and <code>deque</code>),
through calls to the container's own <code>remove_if</code> non-static member function 
(for <code>list</code> and <code>forward_list</code>), or through a hand-rolled loop (for
the associative containers &mdash; <code>map</code>, <code>set</code>, 
and their unordered and multi variants).
<p/>
For the overloads that deal with non-associative containers the predicate passed to 
<code>std::erase_if</code> is always <em>copied</em> into the inner call
to <code>std::remove_if</code> or the container's <code>remove_if</code> (see 
27.4.4.5 <a href="https://wg21.link/string.erasure">[string.erasure]</a>, 23.3.13.6 <a href="https://wg21.link/vector.erasure">[vector.erasure]</a>, 
23.3.11.6 <a href="https://wg21.link/list.erasure">[list.erasure]</a>, 23.3.7.7 <a href="https://wg21.link/forward.list.erasure">[forward.list.erasure]</a>, and
23.3.5.5 <a href="https://wg21.link/deque.erasure">[deque.erasure]</a>).
<p/>
Now, algorithms are generally allowed to take as many copies of
predicates as they want &mdash; this is 26.2 <a href="https://wg21.link/algorithms.requirements">[algorithms.requirements]</a>/9, 
but cf. LWG <a href="lwg-active.html#3049" title="Missing wording allowing algorithms to use copies of function objects as substitutes for their parameters (Status: Open)">3049</a><sup><a href="https://cplusplus.github.io/LWG/issue3049" title="Latest snapshot">(i)</a></sup>. 
However it still feels strange/sloppy that a copy here is <em>mandated</em> by the Standard.
An implementation that would otherwise make no copies of a predicate in an algorithm 
<em>must</em> make a copy in the <code>erase_if</code> overloads.
<p/>
The copy of the predicate could be instead replaced by a move without
any change in functionality; after being passed to the underlying
algorithm, the predicate object is never used again by <code>erase_if</code>. 
I am therefore proposing to add a <code>std::move</code> call for the predicate object.
<p/>
One could argue that <code>erase_if</code> should be re-specified so that it is not
necessarily implemented in terms of underlying calls to other
facilities, and could therefore even remove the need of a move on a
high-quality implementation. 
This is a design change, and so I consider it out of scope for a library issue.
<p/>
Of course, moving instead of copying is a detectable change, and
"pathological" predicate types that are copyable but have disabled moves
will get broken, but I don't think those deserve to be supported.
</p>

<p><i>[2023-01-06; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll.
</p>
<p>
"An alternative resolution would be to wrap the predicate in reference_wrapper."
</p>
<p>
"I'd prefer blanket wording that says the actual number of copies and/or moves
is unspecified when the wording depicts a copy of a function object."
</p>
<p>
Related to LWG <a href="lwg-active.html#3049" title="Missing wording allowing algorithms to use copies of function objects as substitutes for their parameters (Status: Open)">3049</a><sup><a href="https://cplusplus.github.io/LWG/issue3049" title="Latest snapshot">(i)</a></sup>.
</p>



<p id="res-3837"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4917" title=" Working Draft, Standard for Programming Language C++">N4917</a>.
</p>

<ol>
<li><p>Modify 27.4.4.5 <a href="https://wg21.link/string.erasure">[string.erasure]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class charT, class traits, class Allocator, class Predicate&gt;
  constexpr typename basic_string&lt;charT, traits, Allocator&gt;::size_type
    erase_if(basic_string&lt;charT, traits, Allocator&gt;&amp; c, Predicate pred);
</pre>
<blockquote>
<p>
-2- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
auto it = remove_if(c.begin(), c.end(), <ins>std::move(</ins>pred<ins>)</ins>);
auto r = distance(it, c.end());
c.erase(it, c.end());
return r;
</pre></blockquote>
</blockquote>
</blockquote>
</li>

<li><p>Modify 23.3.5.5 <a href="https://wg21.link/deque.erasure">[deque.erasure]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class T, class Allocator, class Predicate&gt;
  typename deque&lt;T, Allocator&gt;::size_type
    erase_if(deque&lt;T, Allocator&gt;&amp; c, Predicate pred);
</pre>
<blockquote>
<p>
-2- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
auto it = remove_if(c.begin(), c.end(), <ins>std::move(</ins>pred<ins>)</ins>);
auto r = distance(it, c.end());
c.erase(it, c.end());
return r;
</pre></blockquote>
</blockquote>
</blockquote>
</li>

<li><p>Modify 23.3.7.7 <a href="https://wg21.link/forward.list.erasure">[forward.list.erasure]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class T, class Allocator, class Predicate&gt;
  typename forward_list&lt;T, Allocator&gt;::size_type
    erase_if(forward_list&lt;T, Allocator&gt;&amp; c, Predicate pred);
</pre>
<blockquote>
<p>
-2- <i>Effects</i>: Equivalent to: <code>return c.remove_if(<ins>std::move(</ins>pred<ins>)</ins>);</code>
</p>
</blockquote>
</blockquote>
</li>

<li><p>Modify 23.3.11.6 <a href="https://wg21.link/list.erasure">[list.erasure]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class T, class Allocator, class Predicate&gt;
  typename list&lt;T, Allocator&gt;::size_type
    erase_if(list&lt;T, Allocator&gt;&amp; c, Predicate pred);
</pre>
<blockquote>
<p>
-2- <i>Effects</i>: Equivalent to: <code>return c.remove_if(<ins>std::move(</ins>pred<ins>)</ins>);</code>
</p>
<blockquote><pre>
</pre></blockquote>
</blockquote>
</blockquote>
</li>

<li><p>Modify 23.3.13.6 <a href="https://wg21.link/vector.erasure">[vector.erasure]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class T, class Allocator, class Predicate&gt;
  constexpr typename vector&lt;T, Allocator&gt;::size_type
    erase_if(vector&lt;T, Allocator&gt;&amp; c, Predicate pred);
</pre>
<blockquote>
<p>
-2- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
auto it = remove_if(c.begin(), c.end(), <ins>std::move(</ins>pred<ins>)</ins>);
auto r = distance(it, c.end());
c.erase(it, c.end());
return r;
</pre></blockquote>
</blockquote>
</blockquote>
</li>
</ol>






</body>
</html>
