<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4179: Wrong range in [alg.search]</title>
<meta property="og:title" content="Issue 4179: Wrong range in [alg.search]">
<meta property="og:description" content="C++ library issue. Status: WP">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4179.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#WP">WP</a> status.</em></p>
<h3 id="4179"><a href="lwg-defects.html#4179">4179</a>. Wrong range in [alg.search]</h3>
<p><b>Section:</b> 26.6.15 <a href="https://wg21.link/alg.search">[alg.search]</a> <b>Status:</b> <a href="lwg-active.html#WP">WP</a>
 <b>Submitter:</b> Oscar Slotosch <b>Opened:</b> 2024-12-05 <b>Last modified:</b> 2025-02-16</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#alg.search">issues</a> in [alg.search].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#WP">WP</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Originally reported as editorial request <a href="https://github.com/cplusplus/draft/issues/7474">#7474</a>:
<p/>
During the qualification of the C++ STL Validas has pointed me to the following issue:
<p/>
The specification of 26.6.15 <a href="https://wg21.link/alg.search">[alg.search]</a> has a wrong range. Currently the range is 
"<code>[first1, last1 - (last2 - first2))</code>" (exclusive) but should be 
"<code>[first1, last1 - (last2 - first2)]</code>" (inclusive). So please correct the closing ")" to "]". 
Otherwise the last occurrence will not be found. We observed the issue in C++14 and C++17 
and cppreference.com.
<p/>
The implementations do the right thing and work correctly and find even the last occurrence.
<p/>
For example in the list <code class='backtick'>{1, 2, 3, 4, 5}</code> the pattern <code class='backtick'>{4, 5}</code> should be found (obviously).
In the case the last element is not included it will not be found.
<p/>
<a href="https://godbolt.org/z/daMa5nTY9">Demonstration on godbolt</a> shows that the implementation 
is working and finds the pattern.
</p>

<p><i>[2024-12-07; Daniel comments and provides wording]</i></p>

<p>
The mentioned wording issue is present in all previous standard versions, including the 
<a href="https://sgistl.github.io/search.html">SGI STL</a>. It needs to be fixed in all search 
variants specified in 26.6.15 <a href="https://wg21.link/alg.search">[alg.search]</a> (except for the form using a <code class='backtick'>Searcher</code>).
</p>

<p><i>[2025-02-07; Reflector poll]</i></p>

<p>
Set status to Tentatively Ready after ten votes in favour during reflector poll.
</p>

<p><i>[Hagenberg 2025-02-16; Status changed: Voting &rarr; WP.]</i></p>



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

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

<blockquote>
<pre>
template&lt;class ForwardIterator1, class ForwardIterator2&gt;
  constexpr ForwardIterator1
    search(ForwardIterator1 first1, ForwardIterator1 last1,
           ForwardIterator2 first2, ForwardIterator2 last2);
[&hellip;]
template&lt;class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
         class BinaryPredicate&gt;
ForwardIterator1
  search(ExecutionPolicy&amp;&amp; exec,
         ForwardIterator1 first1, ForwardIterator1 last1,
         ForwardIterator2 first2, ForwardIterator2 last2,
         BinaryPredicate pred);
</pre>
<blockquote>
<p>
-1- <i>Returns</i>: The first iterator <code class='backtick'>i</code> in the range <code>[first1, last1 - (last2 - first2)<ins>]</ins><del>)</del></code> 
such that for every nonnegative integer <code class='backtick'>n</code> less than <code class='backtick'>last2 - first2</code> the following 
corresponding conditions hold: <code class='backtick'>*(i + n) == *(first2 + n), pred(*(i + n)</code>, <code class='backtick'>*(first2 + n)) != false</code>. 
Returns <code class='backtick'>first1</code> if <code class='backtick'>[first2, last2)</code> is empty, otherwise returns <code class='backtick'>last1</code> if no such iterator is found.
<p/>
-2- [&hellip;]
</p>
</blockquote>
<pre>
template&lt;forward_iterator I1, sentinel_for&lt;I1&gt; S1, forward_iterator I2,
         sentinel_for&lt;I2&gt; S2, class Pred = ranges::equal_to,
         class Proj1 = identity, class Proj2 = identity&gt;
  requires indirectly_comparable&lt;I1, I2, Pred, Proj1, Proj2&gt;
  constexpr subrange&lt;I1&gt;
    ranges::search(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {},
                   Proj1 proj1 = {}, Proj2 proj2 = {});
template&lt;forward_range R1, forward_range R2, class Pred = ranges::equal_to,
         class Proj1 = identity, class Proj2 = identity&gt;
  requires indirectly_comparable&lt;iterator_t&lt;R1&gt;, iterator_t&lt;R2&gt;, Pred, Proj1, Proj2&gt;
  constexpr borrowed_subrange_t&lt;R1&gt;
    ranges::search(R1&amp;&amp; r1, R2&amp;&amp; r2, Pred pred = {},
                   Proj1 proj1 = {}, Proj2 proj2 = {});
</pre>
<blockquote>
<p>
-3- <i>Returns</i>:
</p>
<ol style="list-style-type: none">
<li><p>(3.1) &mdash; <code class='backtick'>{i, i + (last2 - first2)}</code>, where <code class='backtick'>i</code> is the first iterator in the range 
<code>[first1, last1 - (last2 - first2)<ins>]</ins><del>)</del></code> such that for every non-negative integer <code class='backtick'>n</code> 
less than <code class='backtick'>last2 - first2</code> the condition</p>
<blockquote><pre>
bool(invoke(pred, invoke(proj1, *(i + n)), invoke(proj2, *(first2 + n))))
</pre></blockquote>
<p>is <code class='backtick'>true</code>.</p>
</li>
<li><p>(3.2) &mdash; Returns <code class='backtick'>{last1, last1}</code> if no such iterator exists.</p></li>
</ol>
<p>
-4- [&hellip;]
</p>
</blockquote>
<pre>
template&lt;class ForwardIterator, class Size, class T = iterator_traits&lt;ForwardIterator&gt;::value_type&gt;
  constexpr ForwardIterator
    search_n(ForwardIterator first, ForwardIterator last,
             Size count, const T&amp; value);
[&hellip;]
template&lt;class ExecutionPolicy, class ForwardIterator, class Size,
         class T = iterator_traits&lt;ForwardIterator&gt;::value_type,
         class BinaryPredicate&gt;
  ForwardIterator
    search_n(ExecutionPolicy&amp;&amp; exec,
             ForwardIterator first, ForwardIterator last,
             Size count, const T&amp; value,
             BinaryPredicate pred);
</pre>
<blockquote>
<p>
-5- [&hellip;]
<p/>
-6- [&hellip;]
<p/>
-7- <i>Returns</i>: The first iterator <code class='backtick'>i</code> in the range <code>[first, last-count<ins>]</ins><del>)</del></code> 
such that for every non-negative integer <code class='backtick'>n</code> less than <code class='backtick'>count</code> the condition <code><i>E</i></code> is <code class='backtick'>true</code>. 
Returns <code class='backtick'>last</code> if no such iterator is found.
<p/>
-8- [&hellip;]
</p>
</blockquote>
<pre>
template&lt;forward_iterator I, sentinel_for&lt;I&gt; S,
         class Pred = ranges::equal_to, class Proj = identity,
         class T = projected_value_t&lt;I, Proj&gt;&gt;
  requires indirectly_comparable&lt;I, const T*, Pred, Proj&gt;
  constexpr subrange&lt;I&gt;
    ranges::search_n(I first, S last, iter_difference_t&lt;I&gt; count,
                     const T&amp; value, Pred pred = {}, Proj proj = {});
template&lt;forward_range R, class Pred = ranges::equal_to,
         class Proj = identity, class T = projected_value_t&lt;iterator_t&lt;R&gt;, Proj&gt;&gt;
  requires indirectly_comparable&lt;iterator_t&lt;R&gt;, const T*, Pred, Proj&gt;
  constexpr borrowed_subrange_t&lt;R&gt;
    ranges::search_n(R&amp;&amp; r, range_difference_t&lt;R&gt; count,
                     const T&amp; value, Pred pred = {}, Proj proj = {});
</pre>
<blockquote>
<p>
-9- <i>Returns</i>: <code class='backtick'>{i, i + count}</code> where <code class='backtick'>i</code> is the first iterator in the range 
<code>[first, last - count<ins>]</ins><del>)</del></code> such that for every non-negative integer 
<code class='backtick'>n</code> less than <code class='backtick'>count</code>, the following condition holds: <code class='backtick'>invoke(pred, invoke(proj, *(i + n)), value)</code>. 
Returns <code class='backtick'>{last, last}</code> if no such iterator is found.
<p/>
-10- [&hellip;]
</p>
</blockquote>
</blockquote>

</li>

</ol>





</body>
</html>
