<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4269: unique_copy passes arguments to its predicate backwards</title>
<meta property="og:title" content="Issue 4269: unique_copy passes arguments to its predicate backwards">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4269.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="4269"><a href="lwg-active.html#4269">4269</a>. <code class='backtick'>unique_copy</code> passes arguments to its predicate backwards</h3>
<p><b>Section:</b> 26.7.9 <a href="https://wg21.link/alg.unique">[alg.unique]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2025-05-29 <b>Last modified:</b> 2025-05-29</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#alg.unique">active issues</a> in [alg.unique].</p>
<p><b>View all other</b> <a href="lwg-index.html#alg.unique">issues</a> in [alg.unique].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
For the <code class='backtick'>unique</code> algorithms, 26.7.9 <a href="https://wg21.link/alg.unique">[alg.unique]</a> p1 says:
</p>
<blockquote>
1. Let <code class='backtick'>pred</code> be <code class='backtick'>equal_to{}</code> for the overloads with no parameter <code class='backtick'>pred</code>,
and let <i>E</i> be
<ol style="list-style-type: none">
<li> (1.1)
 &mdash; <code class='backtick'>bool(pred(*(i - 1), *i))</code> for the overloads in namespace <code class='backtick'>std</code>;
</li>
<li>(1.2)
 &mdash; <code class='backtick'>bool(invoke(comp, invoke(proj, *(i - 1)), invoke(proj, *i)))</code>
 for the overloads in namespace <code class='backtick'>ranges</code>.
</li>
</ol>
</blockquote>
<p>
However for the <code class='backtick'>unique_copy</code> algorithms, 26.7.9 <a href="https://wg21.link/alg.unique">[alg.unique]</a> p6 says
that the arguments <code class='backtick'>*i</code> and <code class='backtick'>*(i-1)</code> should be reversed:
</p>
<blockquote>
6. Let <code class='backtick'>pred</code> be <code class='backtick'>equal_to{}</code> for the overloads with no parameter <code class='backtick'>pred</code>,
and let <i>E</i> be
<ol style="list-style-type: none">
<li> (6.1)
 &mdash; <code class='backtick'>bool(pred(*i, *(i - 1)))</code> for the overloads in namespace <code class='backtick'>std</code>;
</li>
<li>(6.2)
 &mdash; <code class='backtick'>bool(invoke(comp, invoke(proj, *i), invoke(proj, *(i - 1))))</code>
 for the overloads in namespace <code class='backtick'>ranges</code>.
</li>
</ol>
</blockquote>
<p>
This reversed order is consistent with the documentation for
<a href="https://stl.boost.org/unique_copy.html">SGI STL <code class='backtick'>unique_copy</code></a>,
although the docs for 
<a href="https://stl.boost.org/unique_copy.html">SGI STL <code class='backtick'>unique</code></a>
show reversed arguments too, and the C++ standard doesn't match that.
</p>
<p>
A survey of known implementations shows that all three of libstdc++, libc++,
and MSVC STL use the <code class='backtick'>pred(*(i - 1), *i)</code> order for all of <code class='backtick'>std::unique</code>,
<code class='backtick'>std::unique_copy</code>, <code class='backtick'>ranges::unique</code>, and <code class='backtick'>ranges::unique_copy</code>. The range-v3
library did the same, and even the SGI STL did too (despite what its docs said).
Only two implementations were found which match the spec and use a different
argument order for <code class='backtick'>unique</code> and <code class='backtick'>unique_copy</code>, Casey Carter's (cmcstl2) and
Fraser Gordon's.
</p>
<p>
In the absence of any known rationale for <code class='backtick'>unique</code> and <code class='backtick'>unique_copy</code> to differ,
it seems sensible to make <code class='backtick'>unique_copy</code> more consistent with <code class='backtick'>unique</code>
(and with the majority of implementations stretching back three decades).
</p>


<p id="res-4269"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N5008" title=" Working Draft, Programming Languages — C++">N5008</a>.
</p>

<ol>
<li><p>Modify 26.7.9 <a href="https://wg21.link/alg.unique">[alg.unique]</a> as indicated:</p>
<blockquote>
6. Let <code class='backtick'>pred</code> be <code class='backtick'>equal_to{}</code> for the overloads with no parameter <code class='backtick'>pred</code>,
and let <i>E</i> be
<ol style="list-style-type: none">
<li> (6.1)
 &mdash; <code>bool(pred(<del>*i,</del> *(i - 1))<ins>, *i</ins>)</code>
 for the overloads in namespace <code class='backtick'>std</code>;
</li>
<li>(6.2)
 &mdash; <code>bool(invoke(comp, <del>invoke(proj, *i),</del>
 invoke(proj, *(i - 1))<ins>, invoke(proj, *i)</ins>))</code>
 for the overloads in namespace <code class='backtick'>ranges</code>.
</li>
</ol>
</blockquote>
</li>
</ol>





</body>
</html>
