<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3178: std::mismatch is missing an upper bound</title>
<meta property="og:title" content="Issue 3178: std::mismatch is missing an upper bound">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3178.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#Resolved">Resolved</a> status.</em></p>
<h3 id="3178"><a href="lwg-defects.html#3178">3178</a>. <code>std::mismatch</code> is missing an upper bound</h3>
<p><b>Section:</b> 99 [mismatch] <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Geoffrey Romer <b>Opened:</b> 2018-12-20 <b>Last modified:</b> 2020-05-02</p>
<p><b>Priority: </b>0
</p>
<p><b>View all other</b> <a href="lwg-index.html#mismatch">issues</a> in [mismatch].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Consider the following code:
</p>
<blockquote><pre>
std::vector&lt;int&gt; v1 = {1, 2, 3, 4};
std::vector&lt;int&gt; v2 = {1, 2, 3, 5};
auto result = std::mismatch(v1.begin(), v1.begin() + 2, v2.begin(), v2.begin() + 2);
</pre></blockquote>
<p>
The current wording of [mismatch] seems to require <code>result</code> to be <code>{v1.begin() + 3, v2.begin() + 3}</code>, because 3
is the smallest integer <code>n</code> such that <code>*(v1.begin() + n) != *(v2.begin + n)</code>. In other words, if there's a
mismatch that's reachable from <code>first1</code> and <code>first2</code>, then <code>std::mismatch</code> must find and return it,
even if it's beyond the end iterators passed by the user.
<p/>
This is doubly unimplementable: the library has no way of knowing that it's safe to keep going past the end of the user-supplied
range, and even if it could, doing so would violate the complexity requirements. More importantly, it would violate the
fundamental convention that STL algorithms operate on user-supplied ranges, not on the underlying containers.
</p>

<p><i>[2019-01-26 Priority to 0 and Status to Tentatively Ready after discussions on the reflector]</i></p>

<p>
During that reflector discussion several contributers argued in favour for changing the current wording in
99 [mismatch] p3 from "smallest integer" to "smallest <ins>nonnegative</ins> integer". This minor 
wording delta has also been added to the original proposed wording.
</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>This wording is relative to <a href="https://wg21.link/n4791">N4791</a>.</p>

<ol>
<li><p>Change 99 [mismatch] as indicated:</p>
<blockquote>
<pre>
template&lt;class InputIterator1, class InputIterator2&gt;
  constexpr pair&lt;InputIterator1, InputIterator2&gt;
    mismatch(InputIterator1 first1, InputIterator1 last1,
             InputIterator2 first2);
[&hellip;]
namespace ranges {
  template&lt;InputIterator I1, Sentinel&lt;I1&gt; S1, InputIterator I2, Sentinel&lt;I2&gt; S2,
           class Proj1 = identity, class Proj2 = identity,
           IndirectRelation&lt;projected&lt;I1, Proj1&gt;,
           projected&lt;I2, Proj2&gt;&gt; Pred = ranges::equal_to&lt;&gt;&gt;
    constexpr mismatch_result&lt;I1, I2>
      mismatch(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {},
               Proj1 proj1 = {}, Proj2 proj2 = {});
  template&lt;InputRange R1, InputRange R2,
           class Proj1 = identity, class Proj2 = identity,
           IndirectRelation&lt;projected&lt;iterator_t&lt;R1&gt;, Proj1&gt;,
           projected&lt;iterator_t&lt;R2&gt;, Proj2&gt;&gt; Pred = ranges::equal_to&lt;&gt;&gt;
    constexpr mismatch_result&lt;safe_iterator_t&lt;R1&gt;, safe_iterator_t&lt;R2&gt;&gt;
      mismatch(R1&amp;&amp; r1, R2&amp;&amp; r2, Pred pred = {},
               Proj1 proj1 = {}, Proj2 proj2 = {});
}
</pre>
<blockquote>
<p>
-1- Let <code>last2</code> be <code>first2 + (last1 - first1)</code> for the overloads with no parameter <code>last2</code> or <code>r2</code>.
<p/>
-2- Let <code><i>E</i></code> be:
<ol style="list-style-type: none">
<li><p>(2.1) &mdash; <code>!(*(first1 + n) == *(first2 + n))</code> for the overloads with no parameter <code>pred</code>,</p></li>
<li><p>(2.2) &mdash; <code>pred(*(first1 + n), *(first2 + n)) == false</code> for the overloads with a parameter <code>pred</code> and
no parameter <code>proj1</code>,</p></li>
<li><p>(2.3) &mdash; <code>!invoke(pred, invoke(proj1, *(first1 + n)), invoke(proj2, *(first2 + n)))</code> for the overloads with
both parameters <code>pred</code> and <code>proj1</code>.</p></li>
</ol>
<ins>-?- Let <code><i>N</i></code> be <code>min(last1 - first1, last2 - first2)</code>.</ins>
<p/>
-3- <i>Returns:</i> <code>{ first1 + n, first2 + n }</code>, where <code>n</code> is the smallest <ins>nonnegative</ins> integer such 
that <code><i>E</i></code> holds, or <code><del>min(last1 - first1, last2 - first2)</del><ins><i>N</i></ins></code> if no such integer 
<ins>less than <code><i>N</i></code></ins> exists.
<p/>
-4- <i>Complexity:</i> At most <code><del>min(last1 - first1, last2 - first2)</del><ins><i>N</i></ins></code> applications of the
corresponding predicate and any projections.
</p>
</blockquote>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2019-03-15; Daniel comments]</i></p>

<p>
The <a href="https://github.com/cplusplus/draft/issues/2611">editorial issue #2611</a> had been
resolved via this <a href="https://github.com/cplusplus/draft/pull/2613">pull request #2613</a>.
The editorial changes should make the suggested wording changes obsolete and I recommend to close
this issue as <b>Resolved</b>.
</p>

<p><i>[2020-05-02; Reflector discussions]</i></p>

<p>
It seems that the editorial change has fixed the issue already. If the issue author objects,
we can reopen it. Therefore:
<p/>
Resolved by editorial <a href="https://github.com/cplusplus/draft/pull/2613">pull request #2613</a>.
</p>


<p><b>Rationale:</b></p>
Resolved by editorial <a href="https://github.com/cplusplus/draft/pull/2613">pull request #2613</a>.


<p id="res-3178"><b>Proposed resolution:</b></p>





</body>
</html>
