<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4103: ranges::unique_copy's constraints for the case where result is an
    input_iterator are not quite right</title>
<meta property="og:title" content="Issue 4103: ranges::unique_copy's constraints for the case where result is an
    input_iterator are not quite right">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4103.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="4103"><a href="lwg-active.html#4103">4103</a>. <code>ranges::unique_copy</code>'s constraints for the case where <code>result</code> is an
    <code>input_iterator</code> are not quite right</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> Hewill Kang <b>Opened:</b> 2024-05-14 <b>Last modified:</b> 2024-06-24</p>
<p><b>Priority: </b>3
</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>
When <code>r</code> is only an <code>input_range</code> and <code>result</code> is also an
<code>input_iterator</code>, <code>ranges::unique_copy</code> writes the elements of <code>r</code> 
into <code>result</code> and reads the value of <code>result</code> in the next iteration.
<p/>
However, in this case, the function only requires that the <code>value_type</code> of <code>r</code> 
and the <code>value_type</code> of <code>result</code> are the same, which seems too loose because 
the <code>value_type</code> is not particularly useful in the ranges world compared to the
<code>reference</code>, which is also reflected in the fact that the implementation applies the 
compare function on both dereferenced values (<a href="https://godbolt.org/z/jWTnsdGYv">demo</a>):
</p>
<blockquote><pre>
#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;ranges&gt;
#include &lt;vector&gt;

int main() {
  auto r = std::views::istream&lt;bool&gt;(std::cin);
  std::vector&lt;bool&gt; v(10);
  auto proj = [](std::same_as&lt;bool&gt; auto b) { return b; }; // ban vector&lt;bool&gt;::reference
  std::ranges::unique_copy(r, v.begin(), {}, proj);        // <span style="color:red;font-weight:bolder">hard error in libstdc++, libc++ and MSVC-STL</span>
}
</pre></blockquote>

<p><i>[2024-06-24; Reflector poll]</i></p>

<p>
Set priority to 3 after reflector poll.
</p>



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

<ol>

<li><p>Modify 26.4 <a href="https://wg21.link/algorithm.syn">[algorithm.syn]</a>, header <code>&lt;algorithm&gt;</code> synopsis, as indicated:</p>

<blockquote><pre>
#include &lt;initializer_list&gt;     // <i>see <a href="https://wg21.link/initializer.list.syn">[initializer.list.syn]</a></i>

namespace std {
  [&hellip;]
  namespace ranges {
    template&lt;class I, class O&gt;
      using unique_copy_result = in_out_result&lt;I, O&gt;;

    template&lt;input_iterator I, sentinel_for&lt;I&gt; S, weakly_incrementable O, class Proj = identity,
             indirect_equivalence_relation&lt;projected&lt;I, Proj&gt;&gt; C = ranges::equal_to&gt;
      requires indirectly_copyable&lt;I, O&gt; &amp;&amp;
               (forward_iterator&lt;I&gt; ||
                (input_iterator&lt;O&gt; &amp;&amp; <del>same_as&lt;iter_value_t&lt;I&gt;, iter_value_t&lt;O&gt;&gt;</del>
                 <ins>indirect_equivalence_relation&lt;C, projected&lt;I, Proj&gt;, projected&lt;O, Proj&gt;&gt;</ins>) ||
                indirectly_copyable_storable&lt;I, O&gt;)
      constexpr unique_copy_result&lt;I, O&gt;
        unique_copy(I first, S last, O result, C comp = {}, Proj proj = {});
    template&lt;input_range R, weakly_incrementable O, class Proj = identity,
             indirect_equivalence_relation&lt;projected&lt;iterator_t&lt;R&gt;, Proj&gt;&gt; C = ranges::equal_to&gt;
      requires indirectly_copyable&lt;iterator_t&lt;R&gt;, O&gt; &amp;&amp;
               (forward_iterator&lt;iterator_t&lt;R&gt;&gt; ||
                (input_iterator&lt;O&gt; &amp;&amp; <del>same_as&lt;range_value_t&lt;R&gt;, iter_value_t&lt;O&gt;&gt;</del>
                 <ins>indirect_equivalence_relation&lt;C, projected&lt;iterator_t&lt;R&gt;, Proj&gt;,
                                                  projected&lt;O, Proj&gt;&gt;</ins>) ||
                indirectly_copyable_storable&lt;iterator_t&lt;R&gt;, O&gt;)
      constexpr unique_copy_result&lt;borrowed_iterator_t&lt;R&gt;, O&gt;
        unique_copy(R&amp;&amp; r, O result, C comp = {}, Proj proj = {});
  }
  [&hellip;]
}
</pre></blockquote>

</li>

<li><p>Modify 26.7.9 <a href="https://wg21.link/alg.unique">[alg.unique]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;input_iterator I, sentinel_for&lt;I&gt; S, weakly_incrementable O, class Proj = identity,
         indirect_equivalence_relation&lt;projected&lt;I, Proj&gt;&gt; C = ranges::equal_to&gt;
  requires indirectly_copyable&lt;I, O&gt; &amp;&amp;
           (forward_iterator&lt;I&gt; ||
            (input_iterator&lt;O&gt; &amp;&amp; <del>same_as&lt;iter_value_t&lt;I&gt;, iter_value_t&lt;O&gt;&gt;</del>
             <ins>indirect_equivalence_relation&lt;C, projected&lt;I, Proj&gt;, projected&lt;O, Proj&gt;&gt;</ins>) ||
            indirectly_copyable_storable&lt;I, O&gt;)
  constexpr ranges::unique_copy_result&lt;I, O&gt;
    ranges::unique_copy(I first, S last, O result, C comp = {}, Proj proj = {});
template&lt;input_range R, weakly_incrementable O, class Proj = identity,
         indirect_equivalence_relation&lt;projected&lt;iterator_t&lt;R&gt;, Proj&gt;&gt; C = ranges::equal_to&gt;
  requires indirectly_copyable&lt;iterator_t&lt;R&gt;, O&gt; &amp;&amp;
           (forward_iterator&lt;iterator_t&lt;R&gt;&gt; ||
           (input_iterator&lt;O&gt; &amp;&amp; <del>same_as&lt;range_value_t&lt;R&gt;, iter_value_t&lt;O&gt;&gt;</del>
            <ins>indirect_equivalence_relation&lt;C, projected&lt;iterator_t&lt;R&gt;, Proj&gt;,
                                             projected&lt;O, Proj&gt;&gt;</ins>) ||
            indirectly_copyable_storable&lt;iterator_t&lt;R&gt;, O&gt;)
  constexpr ranges::unique_copy_result&lt;borrowed_iterator_t&lt;R&gt;, O&gt;
    ranges::unique_copy(R&amp;&amp; r, O result, C comp = {}, Proj proj = {});
</pre>
<blockquote>
<p>
-6- Let <code>pred</code> be <code>equal_to{}</code> for the overloads in namespace <code>std</code> with no parameter 
<code>pred</code>, [&hellip;]
</p>
</blockquote>
</blockquote>

</li>
</ol>






</body>
</html>
