<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3031: Algorithms and predicates with non-const reference arguments</title>
<meta property="og:title" content="Issue 3031: Algorithms and predicates with non-const reference arguments">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3031.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++20">C++20</a> status.</em></p>
<h3 id="3031"><a href="lwg-defects.html#3031">3031</a>. Algorithms and predicates with non-const reference arguments</h3>
<p><b>Section:</b> 26.8 <a href="https://wg21.link/alg.sorting">[alg.sorting]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2017-11-08 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#alg.sorting">issues</a> in [alg.sorting].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p>
This doesn't compile with any major implementation:
</p>
<blockquote><pre>
int i[1] = { };
std::stable_sort(i, i, [](int&amp; x, int&amp; y) { return x &lt; y; });
</pre></blockquote>
<p>
The problem is that the <code>Compare</code> expects non-const references. We say "It is assumed that
<code>comp</code> will not apply any non-constant function through the dereferenced iterator" But that
isn't sufficient to forbid the example.
<p/>
My first thought was to modify [alg.sorting] to make the <code>Compare</code> requirements use
<code>comp(as_const(x), as_const(x))</code> but that would get very verbose to add to every expression
using <code>comp</code>.
</p>

<p><i>[2017-11 Albuquerque Wednesday night issues processing]</i></p>

<p>Priority set to 2; Jonathan to improve the statement of the problem.</p>

<p><i>[2018-02 David Jones provided this <a href="https://bugs.llvm.org/show_bug.cgi?id=34536">truly awful example</a>:]</i></p>

<blockquote><pre>
#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;

struct Base {
    Base(int value) : v(value) {}
    friend bool operator&lt;(const Base&amp; l, const Base&amp; r) { return l.v &lt; r.v; }
    int v;
};

struct Derived : public Base {
    using Base::Base;
    bool operator&lt;(const Derived&amp; o) /* no const here */ { return v &gt; o.v; }
};

int main(void) {
    std::vector&lt;Base&gt; b = {{1}, {5}, {0}, {3}};
    std::vector&lt;Derived&gt; d = {{0}, {1}, {3}, {5}};

    std::cout &lt;&lt; std::lower_bound(d.begin(), d.end(), 4)-&gt;v &lt;&lt; std::endl;

    std::sort(b.begin(), b.end());
    for (const auto &amp;x : b) std::cout &lt;&lt; x.v &lt;&lt; " ";
    std::cout &lt;&lt; std::endl;

    std::sort(d.begin(), d.end());
    for (const auto &amp;x : d) std::cout &lt;&lt; x.v &lt;&lt; " ";
    std::cout &lt;&lt; std::endl;
}

libc++:
=====
$ bin/clang++ -std=c++11 -stdlib=libc++ tmp/ex.cc &amp;&amp; ./a.out
5
0 1 3 5 
0 1 3 5 
=====

libstdc++:
=====
$ bin/clang++ -std=c++11 -stdlib=libstdc++ tmp/ex.cc &amp;&amp; ./a.out
0
0 1 3 5 
5 3 1 0 
=====
</pre></blockquote>

<p><i>[2018-08 Batavia Monday issue discussion]</i></p>

<p>Tim to provide wording; status to 'Open'</p>

<p><i>[ 2018-08-20, Tim adds P/R based on Batavia discussion.]</i></p>

<p>
Similar to the Ranges TS design, the P/R below requires <code>Predicate</code>,
<code>BinaryPredicate</code>, and <code>Compare</code> to accept all mixes of
<code>const</code> and non-<code>const</code> arguments.
</p>

<p><i>[2018-08-23 Batavia Issues processing]</i></p>

<p>Status to Tentatively Ready after minor wording nit (corrected in place)</p>
<p><i>[2018-11, Adopted in San Diego]</i></p>



<p id="res-3031"><b>Proposed resolution:</b></p>
<p> This wording is relative to <a href="https://wg21.link/N4762">N4762</a>.</p>
<ol>
<li>
<p>Edit 26.2 <a href="https://wg21.link/algorithms.requirements">[algorithms.requirements]</a> p6-7 as indicated: </p>
<blockquote>
<p>
-6- The <code>Predicate</code> parameter is used whenever an algorithm expects a function object
(22.10 <a href="https://wg21.link/function.objects">[function.objects]</a>) that, when applied to the result of dereferencing the
corresponding iterator, returns a value testable as <code>true</code>. In other words, if an
algorithm takes <code>Predicate pred</code> as its argument and <code>first</code> as its iterator
argument<ins> with value type <code>T</code></ins>, it should work correctly in the construct
<code>pred(*first)</code> contextually converted to <code>bool</code> (7.3 <a href="https://wg21.link/conv">[conv]</a>). The
function object <code>pred</code> shall not apply any non-constant function through the
dereferenced iterator.<ins> Given a glvalue <code>u</code> of type (possibly <code>const</code>)
<code>T</code> that designates the same object as <code>*first</code>, <code>pred(u)</code> shall be a
valid expression that is equal to <code>pred(*first)</code>.</ins>
<p/>
-7- The <code>BinaryPredicate</code> parameter is used whenever an algorithm expects a function
object that when applied to the result of dereferencing two corresponding iterators or to
dereferencing an iterator and type <code>T</code> when <code>T</code> is part of the signature
returns a value testable as <code>true</code>. In other words, if an algorithm takes
<code>BinaryPredicate binary_pred</code> as its argument and <code>first1</code> and <code>first2</code>
as its iterator arguments<ins> with respective value types <code>T1</code> and <code>T2</code></ins>,
it should work correctly in the construct <code>binary_pred(*first1, *first2)</code>
contextually converted to <code>bool</code> (7.3 <a href="https://wg21.link/conv">[conv]</a>).
<ins>Unless otherwise specified, </ins><code>BinaryPredicate</code> always takes the first iterator's
<code>value_type</code> as its first argument, that is, in those cases when <code>T value</code> is part
of the signature, it should work correctly in the construct <code>binary_pred(*first1, value)</code>
contextually converted to <code>bool</code> (7.3 <a href="https://wg21.link/conv">[conv]</a>). <code>binary_pred</code> shall not
apply any non-constant function through the dereferenced iterators. <ins>Given a glvalue <code>u</code>
of type (possibly <code>const</code>) <code>T1</code> that designates the same object as <code>*first1</code>,
and a glvalue <code>v</code> of type (possibly <code>const</code>) <code>T2</code> that designates the
same object as <code>*first2</code>, <code>binary_pred(u, *first2)</code>, <code>binary_pred(*first1, v)</code>,
and <code>binary_pred(u, v)</code> shall each be a valid expression that is equal to
<code>binary_pred(*first1, *first2)</code>, and <code>binary_pred(u, value)</code> shall
be a valid expression that is equal to <code>binary_pred(*first1, value)</code>.</ins>
</p>
</blockquote>
</li>
<li>
<p>Edit 26.8 <a href="https://wg21.link/alg.sorting">[alg.sorting]</a> p2 as indicated: </p>
<blockquote>
<p>
<code>Compare</code> is a function object type (22.10 <a href="https://wg21.link/function.objects">[function.objects]</a>)<ins> that meets the
requirements for a template parameter named <code>BinaryPredicate</code>
(26.2 <a href="https://wg21.link/algorithms.requirements">[algorithms.requirements]</a>)</ins>. The return value of the function call operation
applied to an object of type <code>Compare</code>, when contextually converted to <code>bool</code>
(7.3 <a href="https://wg21.link/conv">[conv]</a>), yields <code>true</code> if the first argument of the call is less than the second,
and <code>false</code> otherwise. <code>Compare comp</code> is used throughout for algorithms assuming an
ordering relation. <del>It is assumed that <code>comp</code> will not apply any non-constant function
through the dereferenced iterator.</del>
</p>
</blockquote>
</li>
</ol>





</body>
</html>
