C++
'std::mismatch' Is Not a Member of 'std' Fix
std::mismatch lives in one header, <algorithm> - but in the sweep probed here, covering all 106 standard headers in this library's header set, 40 other headers pull it in on the side, the widest rescue set in its family: the textbook <vector> program compiles without the include. The 65 headers that do fail all fail with one plain line, no suggestion at all. This guide pins down the failing shapes, the 40 transitive rescues, where the returned pair lands for equal, prefix, and predicate cases, and the include that fixes it by rule.
Written December 3, 2026 · Tested with GCC 13.3.0 · 23 min read
- The error: the include is missing
- Neighboring headers: 65 fail plainly, none offers a suggestion, and 40 rescue it by accident
- The fix
- What the call does: a pair of iterators to the first difference
Written December 3, 2026
Tested with GCC 13.3.0
What this guide is
std::mismatch is a function template defined in the header <algorithm>. It walks two ranges side by side and returns a pair of iterators to the first position where they differ: elements are compared with operator== unless the caller passes a predicate, and if no difference is found the pair holds the end of the first range and the corresponding position in the second. The classic form takes the first range's two ends and only the second range's start; a bounded form that also takes the second range's end has existed since C++14 and stops at whichever range ends first. The classic call predates C++11, has an execution-policy overload since C++17, and has been usable in constant expressions since C++20.
Because the function lives in <algorithm> - not in a container's header - 'mismatch' is not a member of 'std' is the missing include, and naming that cause is the first step:
- The
#include <algorithm>line is missing, and no other header in the file pulls the name in transitively. For this error, in the contexts probed here, that second clause often saves people by accident: in the sweep below, 40 of the other 105 headers in the library's header set pull the name in on the side, so many files without<algorithm>never reach this error - and a file that reaches it is usually one whose includes are all outside that rescue set, like the<limits>file in the next section.
And for this name the missing include is the only cause of the classic call's error. Some names in this family hide a second cause behind the same wording - a standard floor the name starts at - but std::mismatch predates C++11, so a file that includes <algorithm> compiles this call at every standard level probed here, C++98 included, as the versions section below shows. The pieces layered on top do have floors - the bounded form needs C++14, the execution-policy overload needs C++17, and constant-expression use needs C++20 - but those floors belong to the added pieces, not to the classic call.
This name's diagnostic is plain in every context probed here. In every one of the 65 failing sole-header contexts probed here, the error is one plain line with no suggestion tail, no note lines, and no help lines: the compiler offers no nearest-name guess for this spelling at all. The sweep covers every one of the 106 standard headers in this library's header set, compiled as the only header in the program, and the outcome splits two ways - the plain failure, or a rescue - and the rescue set has 41 entries, the widest in this family so far, because in this library the name is declared by a low-level header that much of the library includes.
The error: the include is missing
This program compares two three-element arrays, 1, 2, 3 and 1, 9, 3, but includes only <limits>:
#include <limits>
int main() {
int a[3] = {1, 2, 3};
int b[3] = {1, 9, 3};
std::mismatch(a, a + 3, b);
return 0;
}
-std=c++17 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:5:10: error: 'mismatch' is not a member of 'std'
5 | std::mismatch(a, a + 3, b);
| ^~~~~~~~
Read it in full: one plain error line - no suggestion tail rides on it, there are no note lines, and there are no help lines. Nothing in the diagnostic names <algorithm> or points anywhere at all. The same program with no include at all fails the same way:
-std=c++17 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:4:10: error: 'mismatch' is not a member of 'std'
4 | std::mismatch(a, a + 3, b);
| ^~~~~~~~
The textbook version does not fail - and that is the trap
<algorithm>:#include <iostream>
#include <vector>
int main() {
std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2 = {1, 9, 3};
auto r = std::mismatch(v1.begin(), v1.end(), v2.begin());
std::cout << (r.first - v1.begin()) << " " << *(r.first) << " " << *(r.second) << "\n";
return 0;
}
-std=c++17 t_textbook.cpp -o t_textbook_prog
$ ./t_textbook_prog
1 2 9
It prints 1 2 9 - index 1, and the two values that differ there. Neither <iostream> nor <vector> is this name's home; the program compiles because <vector> pulls the declaring header in transitively in this library version. That rescue is exactly why this error surprises people when it finally appears: the same call, moved into a file whose includes are <limits>, <chrono>, or <numeric>, fails outright, and nothing about the call changed. A program that uses std::mismatch includes <algorithm>, whether or not it happens to compile without it. A sole <algorithm> file makes the point in miniature - the call compiles and runs with nothing else in sight:
-std=c++17 i_algorithm.cpp -o i_algorithm_prog
$ ./i_algorithm_prog
Neighboring headers: 65 fail plainly, none offers a suggestion, and 40 rescue it by accident
In a sweep where each of the 106 standard headers in this library's header set was compiled as the only header in a program that compares 1, 2, 3 with 1, 9, 3 and checks that the returned pair points at index 1 holding 2 and 9, the outcome splits by compile result two ways - the failure, or a rescue - and the failure comes in exactly one diagnostic shape. Every diagnostic component was classified separately - the error line, any suggestion tail on it, any note lines, and any help lines. These 65 headers all fail with the plain is not a member of 'std' error line - no suggestion tail on that line, no note lines, and no help lines:
<any><atomic><barrier><bit><cassert><cctype><cerrno><cfenv><cfloat><charconv><chrono><cinttypes><ciso646><climits><clocale><compare><concepts><condition_variable><coroutine><csetjmp><csignal><cstdalign><cstdarg><cstdbool><cstddef><cstdint><cstdio><cstdlib><cstring><ctime><cuchar><cwchar><cwctype><exception><expected><format><initializer_list><iosfwd><latch><limits><mutex><new><numbers><numeric><optional><ranges><ratio><scoped_allocator><semaphore><shared_mutex><source_location><span><spanstream><stacktrace><stdfloat><stop_token><syncstream><thread><tuple><type_traits><typeindex><typeinfo><utility><variant><version>
A file with no include at all fails the plain way. Three entries deserve a pause. <limits> fails plainly: the header of the numeric limits declares nothing close enough to this name to trigger a guess. <numeric> fails plainly too: the header of the accumulating names does not declare this one. And <ranges> fails plainly for the classic name: the C++20 ranges library has its own mismatch name, and it does not bring this one in - that name gets its own section below. The split itself has a simple shape: there is no split. This spelling earns no nearest-name suggestion in any probed context, so every failure looks the same - and no shape names <algorithm>.
And these headers rescue the program - it compiles and runs with no <algorithm> anywhere in the file:
<algorithm><array><bitset><ccomplex><cmath><codecvt><complex><ctgmath><deque><execution><filesystem><forward_list><fstream><functional><future><iomanip><ios><iostream><istream><iterator><list><locale><map><memory><memory_resource><ostream><queue><random><regex><set><sstream><stack><stdexcept><streambuf><string><string_view><system_error><unordered_map><unordered_set><valarray><vector>
The shape is the reverse of most of this family: the declaring header in this library sits underneath the container, stream, and iterator headers, so a large part of the library pulls it in on the side, and the rescue set runs to 40 transitive entries. That is also why the failing set is the smaller side here: with 65 of the other 105 headers failing, the missing include shows itself only in files whose includes avoid the whole container and stream family. <algorithm> - the home header - is on the rescue list by definition, not by transitivity, and the other 40 rescues are this library version's behavior, not a guarantee.
The fix
Add the header:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2 = {1, 9, 3};
auto r = std::mismatch(v1.begin(), v1.end(), v2.begin());
std::cout << (r.first - v1.begin()) << " " << *(r.first) << " " << *(r.second) << "\n";
return 0;
}
-std=c++17 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
1 2 9
One include, <algorithm>, declares the function - and the textbook program prints 1 2 9 again, this time by rule: the name is now declared by a header the file names itself, instead of by an internal include of one of the 40 rescuing headers that a different library version is free to rearrange. For a file that does fail - the <limits> shape above - the same one include is the whole fix.
What the call does: a pair of iterators to the first difference
The call returns both positions, not just a verdict, and where the pair lands says which case the ranges are in. A difference is reported with its index and both values; two equal ranges return the end of both; a first range that is a prefix of the second returns its own end with the second iterator on the extra element; a predicate decides what counts as matching; and an empty range differs nowhere:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> a = {1, 2, 3};
std::vector<int> b = {1, 9, 3};
auto r = std::mismatch(a.begin(), a.end(), b.begin());
std::cout << "first-diff " << (r.first - a.begin()) << " " << *(r.first) << " " << *(r.second) << "\n";
std::vector<int> c = {4, 5, 6};
std::vector<int> d = {4, 5, 6};
auto e = std::mismatch(c.begin(), c.end(), d.begin());
std::cout << "equal " << (e.first - c.begin()) << " " << (e.second - d.begin()) << "\n";
std::vector<int> f = {1, 2};
std::vector<int> g = {1, 2, 3, 4};
auto p = std::mismatch(f.begin(), f.end(), g.begin());
std::cout << "prefix " << (p.first - f.begin()) << " " << *(p.second) << "\n";
std::vector<int> h = {2, 4, 6};
std::vector<int> i = {1, 2, 3};
auto q = std::mismatch(h.begin(), h.end(), i.begin(), [](int x, int y) { return x == 2 * y; });
std::cout << "predicate " << (q.first - h.begin()) << "\n";
std::vector<int> mt;
auto z = std::mismatch(mt.begin(), mt.end(), g.begin());
std::cout << "empty " << (z.first - mt.begin()) << "\n";
return 0;
}
-std=c++17 r_semantics.cpp -o r_semantics_prog
$ ./r_semantics_prog
first-diff 1 2 9
equal 3 3
prefix 2 3
predicate 3
empty 0
Read in order: the first-diff line reports index 1 with values 2 and 9 - the first position where the ranges disagree, with both sides in hand. The equal line reports 3 3: no difference exists, so both iterators sit at the end of their range, three steps in. The prefix line reports 2 with the second value 3: the first range ran out at its end while the second still holds 3, which is how a caller tells a prefix from a difference. The predicate line reports 3: under a predicate that treats each first-range value as matching twice the second-range value, every position matches, so the pair ends at the end. The empty line reports 0: an empty first range has no position that could differ.
The bounded form: the second range gets an end too
The classic form trusts that the second range is at least as long as the first. Since C++14 a bounded form takes the second range's end as well, and then the comparison stops at whichever range ends first:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> a = {1, 2, 3};
std::vector<int> b = {1, 2};
auto r = std::mismatch(a.begin(), a.end(), b.begin(), b.end());
std::cout << "shorter-second " << (r.first - a.begin()) << " " << *(r.first) << " second-at-end " << (r.second == b.end() ? 1 : 0) << "\n";
std::vector<int> c = {1, 2};
std::vector<int> d = {1, 2, 9};
auto s = std::mismatch(c.begin(), c.end(), d.begin(), d.end());
std::cout << "shorter-first " << (s.first == c.end() ? 1 : 0) << " " << *(s.second) << "\n";
return 0;
}
-std=c++17 r_bounded.cpp -o r_bounded_prog
$ ./r_bounded_prog
shorter-second 2 3 second-at-end 1
shorter-first 1 9
The shorter-second line reports index 2, the first range's value 3, and second-at-end 1: the ranges agree for as far as the second range goes, and the pair stops there instead of reading past it. The shorter-first line reports the first range exhausted, with the second iterator holding 9 - the mirror image of the prefix case in the classic form, now safe on both sides. The bounded form carries a real floor, and it shows in this library: compiled as C++11, the same four-iterator call fails, because at that standard the only four-argument form is the predicate form, and the second range's end is taken for a predicate and rejected inside it:
-std=c++11 c_bounded_cpp11.cpp -o c_bounded_cpp11_prog
/usr/include/c++/13/bits/predefined_ops.h:158:30: error: no match for call to ‘(__gnu_cxx::__normal_iterator<int*, std::vector<int> >) (int&, int&)’
So the bounded form is the one piece of this name with a floor below the policy overload: C++14, not C++17.
Input iterators: the weakest iterators it accepts
The reading side needs only input iterators - the weakest iterator category there is, the kind that reads each element once, in one pass. This program compares two text streams directly, through stream iterators that cannot be rewound or re-read:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
int main() {
std::istringstream s1("1 2 3");
std::istringstream s2("1 9 3");
auto r = std::mismatch(std::istream_iterator<int>(s1), std::istream_iterator<int>(), std::istream_iterator<int>(s2));
std::cout << *(r.first) << " " << *(r.second) << "\n";
return 0;
}
-std=c++17 r_input.cpp -o r_input_prog
$ ./r_input_prog
2 9
It reports 2 9 - the first pair of values that differ, read straight out of the streams. The policy overloads are the exception in the family: they ask for forward iterators, because a parallel execution may need to revisit the ranges.
Versions: no second cause for the classic call - it predates C++11
This call needs no modern-enough standard, and that is what separates it from some of its neighbors. std::mismatch predates C++11 - the cppreference page carries no "since" marker on the classic overloads. The fixed programs in this guide are compiled as C++17, but there is no floor below that to hit for the classic call: with <algorithm> included, the same comparison compiled as C++98 compiles and runs:
#include <algorithm>
#include <iostream>
int main() {
int a[3] = {1, 2, 3};
int b[3] = {1, 9, 3};
std::pair<int*, int*> r = std::mismatch(a, a + 3, b);
std::cout << (r.first - a) << " " << *(r.first) << " " << *(r.second) << "\n";
return 0;
}
-std=c++98 c_version98.cpp -o c_version98_prog
$ ./c_version98_prog
1 2 9
So this name's error has one cause, not two: add <algorithm>, and no standard flag needs to change with it. A file that includes <algorithm> and still sees this error does not have a version problem with the classic call; the include is not reaching the file that names the call, or the name in the file is not this name.
The pieces layered on top of the classic call do carry floors, and naming one splits in two when a reader tries it. This name does have an execution-policy overload, since C++17 - and naming the policy with only <algorithm> included fails on the policy's name, not the algorithm's:
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2 = {1, 9, 3};
auto r = std::mismatch(std::execution::seq, v1.begin(), v1.end(), v2.begin());
return (r.first - v1.begin()) == 1 ? 0 : 1;
}
-std=c++17 p_policy_algonly.cpp -o p_policy_algonly_prog
p_policy_algonly.cpp: In function 'int main()':
p_policy_algonly.cpp:6:33: error: 'std::execution' has not been declared
6 | auto r = std::mismatch(std::execution::seq, v1.begin(), v1.end(), v2.begin());
| ^~~~~~~~~
That wording - has not been declared, naming std::execution - is the missing-header shape, and here adding the header is the whole fix: add <execution> alongside <algorithm>, and the same call compiles and runs, because the policy overload of std::mismatch exists:
-std=c++17 p_policy_both.cpp -o p_policy_both_prog
$ ./p_policy_both_prog
The policy form belongs to this name, and it carries the C++17 floor. Compiled as C++14, the same two-header program fails on the policy's name again - at that standard the policy names are not there to be declared:
-std=c++14 p_policy_cpp14.cpp -o p_policy_cpp14_prog
p_policy_cpp14.cpp: In function 'int main()':
p_policy_cpp14.cpp:7:33: error: 'std::execution' has not been declared
7 | auto r = std::mismatch(std::execution::seq, v1.begin(), v1.end(), v2.begin());
| ^~~~~~~~~
And since C++20 the classic call is also usable in constant expressions - this program's comparison happens at compile time, and the assertion on the returned pair is part of the build:
#include <algorithm>
#include <array>
constexpr bool check() {
std::array<int, 3> a{1, 2, 3};
std::array<int, 3> b{1, 9, 3};
auto r = std::mismatch(a.begin(), a.end(), b.begin());
return (r.first - a.begin()) == 1 && *(r.first) == 2;
}
static_assert(check());
int main() { return 0; }
-std=c++20 c_constexpr20.cpp -o c_constexpr20_prog
$ ./c_constexpr20_prog
Compiled as C++17, the same program fails - the call is not a constant expression there, and the static assertion cannot hold:
-std=c++17 c_constexpr17.cpp -o c_constexpr17_prog
c_constexpr17.cpp: In function 'constexpr bool check()':
c_constexpr17.cpp:6:27: warning: call to non-'constexpr' function 'std::pair<_T1, _T2> std::mismatch(_InputIterator1, _InputIterator1, _InputIterator2) [with _InputIterator1 = int*; _InputIterator2 = int*]' [-Winvalid-constexpr]
6 | auto r = std::mismatch(a.begin(), a.end(), b.begin());
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:60,
from c_constexpr17.cpp:1:
/usr/include/c++/13/bits/stl_algobase.h:1911:5: note: 'std::pair<_T1, _T2> std::mismatch(_InputIterator1, _InputIterator1, _InputIterator2) [with _InputIterator1 = int*; _InputIterator2 = int*]' declared here
1911 | mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
| ^~~~~~~~
c_constexpr17.cpp: At global scope:
c_constexpr17.cpp:9:20: error: non-constant condition for static assertion
9 | static_assert(check());
| ~~~~~^~
c_constexpr17.cpp:9:20: error: 'constexpr bool check()' called in a constant expression
c_constexpr17.cpp:3:16: note: 'constexpr bool check()' declared here
3 | constexpr bool check() {
| ^~~~~
So C++20 is the floor for that layered piece and C++17 is the floor for the policy form - and no standard at any level rescues a missing include, while no include is needed beyond <algorithm> for the classic call at any standard level.
The neighboring names: two share the side door, one does not
The three names a reader most often reaches for alongside this one all live in <algorithm> too, but in a file whose only include is <vector> they split. std::equal - which reports whether two ranges hold the same elements - compiles in that file, and so does std::lexicographical_compare - which reports whether one range sorts before another: in this library both are declared by the same low-level header <vector> pulls in. std::search - which finds a sub-range inside a range - fails with the plain error in the same file:
-std=c++17 b_search_vector.cpp -o b_search_vector_prog
b_search_vector.cpp: In function 'int main()':
b_search_vector.cpp:5:20: error: 'search' is not a member of 'std'
5 | auto it = std::search(v1.begin(), v1.end(), v2.begin(), v2.end());
| ^~~~~~
Three neighbors, one home by the standard, and two fates in this library: two ride the same transitive rescue as this guide's name, and one is left outside it. They are shown here only as neighbors, not re-taught.
The neighboring names, fixed
Together the four names take one header between them - <algorithm> for all of them, with <vector> covering only the containers they work on. The fixed neighbor program reports all four operations:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> a = {1, 2, 3};
std::vector<int> b = {1, 9, 3};
auto r = std::mismatch(a.begin(), a.end(), b.begin());
std::cout << "mismatch " << (r.first - a.begin()) << " " << *(r.first) << " " << *(r.second) << "\n";
std::cout << "equal " << std::equal(a.begin(), a.end(), b.begin()) << "\n";
std::cout << "lexicographical_compare " << std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end()) << "\n";
std::vector<int> hay = {1, 2, 3, 4};
std::vector<int> needle = {2, 3};
auto it = std::search(hay.begin(), hay.end(), needle.begin(), needle.end());
std::cout << "search " << (it - hay.begin()) << "\n";
return 0;
}
-std=c++17 b_neighbors_fixed.cpp -o b_neighbors_fixed_prog
$ ./b_neighbors_fixed_prog
mismatch 1 2 9
equal 0
lexicographical_compare 1
search 1
Read in order: mismatch reports the first difference at index 1 with both values; equal reports 0, false, because the ranges do differ; lexicographical_compare reports 1, true, because at the first difference the first range's 2 sorts before the second range's 9; and search reports 1, the index where the needle 2, 3 starts inside the haystack.
A ranges form since C++20, in the same header
C++20 added a ranges form of this name, and the standard declares it in the same header as the classic name. std::ranges::mismatch is a function object in the ranges namespace, declared in <algorithm>, that also accepts whole ranges - and it returns a result object holding the two input iterators at the first difference. Compiled as C++20 it compares the same two vectors and reports the difference, read off the result's two input members:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2 = {1, 9, 3};
auto res = std::ranges::mismatch(v1, v2);
std::cout << "ranges " << (res.in1 - v1.begin()) << " " << *(res.in1) << " " << *(res.in2) << "\n";
return 0;
}
-std=c++20 r_ranges.cpp -o r_ranges_prog
$ ./r_ranges_prog
ranges 1 2 9
Two boundaries and one library behavior keep the forms straight. The ranges form needs C++20: compiled as C++17, that same program fails on the namespace itself, with 'std::ranges' has not been declared:
-std=c++17 r_ranges_cpp17.cpp -o r_ranges_cpp17_prog
r_ranges_cpp17.cpp: In function 'int main()':
r_ranges_cpp17.cpp:7:21: error: 'std::ranges' has not been declared
7 | auto res = std::ranges::mismatch(v1, v2);
| ^~~~~~
In this library the ranges header does rescue the ranges form - a file whose only include is <ranges> compiles and runs it - which is this library version's behavior, not the rule: the header the standard names for both forms is <algorithm>, so a complete program names it. A file that includes <array> alone fails for the ranges form, and its diagnostic is the family's one decorated shape: because the classic name is visible in that file through the side door, the compiler suggests it - the wrong name for a ranges call:
-std=c++20 r_ranges_arrayonly.cpp -o r_ranges_arrayonly_prog
r_ranges_arrayonly.cpp: In function 'int main()':
r_ranges_arrayonly.cpp:5:29: error: 'mismatch' is not a member of 'std::ranges'; did you mean 'std::mismatch'?
5 | auto res = std::ranges::mismatch(a, b);
| ^~~~~~~~
In file included from /usr/include/c++/13/array:43,
from r_ranges_arrayonly.cpp:1:
/usr/include/c++/13/bits/stl_algobase.h:2031:5: note: 'std::mismatch' declared here
2031 | mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
| ^~~~~~~~
The classic name, meanwhile, is not pulled in by the ranges header either: in the sweep probed here, a file whose only include is <ranges> fails for std::mismatch with the plain error, as the sweep section above lists.
A different wording - and an argument-dependent rescue that depends on the iterators' types
Including <algorithm> is not the only half of the name. Over plain arrays, the unqualified call fails:
#include <algorithm>
int main() {
int a[3] = {1, 2, 3};
int b[3] = {1, 9, 3};
mismatch(a, a + 3, b);
return 0;
}
-std=c++17 g_unqualified.cpp -o g_unqualified_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:5:5: error: 'mismatch' was not declared in this scope; did you mean 'std::mismatch'?
5 | mismatch(a, a + 3, b);
| ^~~~~~~~
| std::mismatch
In file included from /usr/include/c++/13/algorithm:73,
from g_unqualified.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:327:1: note: 'std::mismatch' declared here
327 | mismatch(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2);
| ^~~~~~~~
That wording - was not declared in this scope, with did you mean 'std::mismatch'? on the error line and a note pointing at a declaration - is a different defect with a different fix: the header is present, the qualifier is not. Qualify the name as std::mismatch; adding includes does not bring the name out of namespace std. Swap the arrays for a vector, and the same unqualified call compiles and runs:
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2 = {1, 9, 3};
auto r = mismatch(v1.begin(), v1.end(), v2.begin());
return (r.first - v1.begin()) == 1 ? 0 : 1;
}
-std=c++17 g_unqualified_vec.cpp -o g_unqualified_vec_prog
$ ./g_unqualified_vec_prog
That compile is argument-dependent lookup at work, not a rule to rely on: the vector's iterators are std types, so their associated namespace is std, and lookup finds std::mismatch through them. A list's iterators rescue it the same way - the same unqualified call over a list compiles and runs:
-std=c++17 g_unqualified_list.cpp -o g_unqualified_list_prog
$ ./g_unqualified_list_prog
But the container being a std type is not enough by itself - over a std::array, the same unqualified call fails with the same wording, because in this library a std::array's iterators are plain pointers, and a pointer has no associated namespace:
#include <algorithm>
#include <array>
int main() {
std::array<int, 3> a{1, 2, 3};
std::array<int, 3> b{1, 9, 3};
auto r = mismatch(a.begin(), a.end(), b.begin());
return (r.first - a.begin()) == 1 ? 0 : 1;
}
-std=c++17 g_unqualified_stdarray.cpp -o g_unqualified_stdarray_prog
g_unqualified_stdarray.cpp: In function 'int main()':
g_unqualified_stdarray.cpp:6:14: error: 'mismatch' was not declared in this scope; did you mean 'std::mismatch'?
6 | auto r = mismatch(a.begin(), a.end(), b.begin());
| ^~~~~~~~
| std::mismatch
In file included from /usr/include/c++/13/algorithm:73,
from g_unqualified_stdarray.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:327:1: note: 'std::mismatch' declared here
327 | mismatch(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2);
| ^~~~~~~~
The pattern across the probed set is exact: the rescue follows the iterator type, not the container's name - class iterators of std containers carry the name in, pointer iterators do not, whatever container stands behind them. Write the qualifier either way.
The exact rule
std::mismatchis declared by#include <algorithm>, and by no other header by rule. In the sweep probed here, 65 of the other 105 headers in the library's header set - among them<limits>,<numeric>,<ranges>,<chrono>, and<utility>- do not declare it, and neither does a file with no include at all; the remaining 40 pull it in transitively, as this library version's behavior, not as a guarantee.'mismatch' is not a member of 'std'has one cause in the contexts probed here: the<algorithm>include is missing and nothing else in the file pulled the name in - and it comes in one shape in the sweep probed here: all 65 failing sole headers fail with one error line and no suggestion tail, no note lines, and no help lines.- In the sweep probed here, 40 headers besides
<algorithm>rescuestd::mismatchtransitively - among them<vector>,<iostream>,<iterator>,<string>, and<map>- so a file whose only include is one of those compiles and runs the call without<algorithm>. That is this library version's behavior, not a guarantee - in the probed set, no header besides<algorithm>is required to pullstd::mismatchin. - The result rules are the documented ones, as the fixed programs here show: the fixed program prints
1 2 9for the textbook case; the semantics program printsfirst-diff 1 2 9,equal 3 3,prefix 2 3,predicate 3, andempty 0- the call returns the first position where the two ranges differ, or the end of the first range when no position differs. - The bounded form follows from the same rule: it stops at the shorter range - the bounded program here prints
shorter-second 2 3 second-at-end 1andshorter-first 1 9- and it starts at C++14: compiled as C++11 the same call fails, because the second range's end is taken for a predicate. - The classic call's reading side needs only input iterators: the stream program here compares two stream iterators and prints
2 9. - The classic
std::mismatchpredates C++11: with<algorithm>included it compiles and runs as C++98, reporting1 2 9. The layered pieces have floors: the bounded form starts at C++14; the execution-policy overload starts at C++17 - with only<algorithm>included, namingstd::execution::seqfails with'std::execution' has not been declared; with<execution>included alongside it, the policy call compiles and runs; and compiled as C++14, the same program fails on the policy's name. Constant-expression use starts at C++20: the static-assert program here compiles as C++20 and fails as C++17. - The neighbors split in this library:
std::equalandstd::lexicographical_compareare declared by#include <algorithm>as well and compile in a sole<vector>file through the same transitive rescue as this name, andstd::searchis declared by it too but fails in that file with the plain error. The fixed neighbor program printsmismatch 1 2 9,equal 0,lexicographical_compare 1, andsearch 1. std::ranges::mismatchis a ranges form of this same name: a function object in namespacestd::ranges, declared in<algorithm>by the standard, needing C++20, returning a result whose two input members point at the first difference - compiled as C++17 the ranges program here fails with'std::ranges' has not been declared, and compiled as C++20 it printsranges 1 2 9for the two three-element vectors. In the probes here<ranges>alone does rescue it in this library,<array>alone fails for it with the suggestiondid you mean 'std::mismatch'?, and<ranges>fails plainly for the classic name in the sweep.'mismatch' was not declared in this scopefor an unqualifiedmismatchuse is a different defect: qualify the name asstd::mismatch- the include alone does not bring the name out of namespacestd. For this name the unqualified call's fate depends on the iterators' types: over plain arrays it fails, as the first unqualified program above shows; over a vector's iterators and over a list's iterators it is rescued through argument-dependent lookup, as the second and third show, because those iterators arestdclass types; over astd::array's iterators it fails again, because in this library those iterators are plain pointers. Qualify the name either way.
The fixes, ranked
- First, check the include list of the file that names
std::mismatch. If<algorithm>is not there, add it - that is the fix for the only cause of this error. It holds whether the file failed plainly - every probed failure did - or happened to compile before through one of the 40 transitive rescues and stopped compiling when its other includes changed. - Second, do not chase a standard flag for the classic call: this name predates C++11, so no flag change fixes this error, and a file with
<algorithm>included that still fails has an include that is not reaching the translation unit, not a version problem. - Third, do not reach for a container's header because the call reads containers, or for
<iterator>because the call takes iterators. In the sweep probed here both happen to rescue this name in this library - and neither is the declaring header by rule. The declaring header is<algorithm>, and a complete program names it alongside the container's own header. - Fourth, do not remove
<algorithm>because a file compiles without it: in the probed set 40 headers rescue the classic name, and those rescues are not guaranteed - a file that drops the include is one internal-include change away from the error in the first section. - Fifth, when the second range may be shorter than the first, use the bounded form and keep the standard at C++14 or later: the classic form reads the second range for as many elements as the first range has, and the bounded form is the one that stops at the second range's end.
- Sixth, if the error names
std::executioninstead -'std::execution' has not been declared- add<execution>alongside<algorithm>and keep the standard at C++17 or later: this name does have a policy overload, and that pair of changes is the whole fix for the policy form. - Seventh, match the standard to the one piece beyond the classic call that has a floor: constant-expression use needs C++20, and the ranges form
std::ranges::mismatchneeds C++20 - with its header still<algorithm>by the standard, not<ranges>. - Eighth, if the error says
was not declared in this scopeinstead, fix the name's qualifier tostd::mismatchas well - and do not rely on argument-dependent lookup to rescue the unqualified call, because a plain array and astd::arrayeach remove that rescue in the probed set.
Related guides on this site
- 'std::unique' Is Not a Member of 'std' Fix
- 'std::search_n' Is Not a Member of 'std' Fix
- 'std::replace' Is Not a Member of 'std' Fix
- 'std::remove' Is Not a Member of 'std' Fix
Sources
- cppreference, std::mismatch - the function template is defined in
<algorithm>; it returns a pair of iterators to the first mismatching elements of the two ranges, comparing withoperator==or a given predicate; the bounded overloads that also take the second range's end are marked since C++14, the execution-policy overloads since C++17, and the classic overloads constexpr since C++20, with no since marker on the classic call itself. - cppreference,
<algorithm>header - the header that declares this name and the other algorithm names used as neighbors here. - cppreference, std::ranges::mismatch - the C++20 ranges form of this name, a function object in namespace
std::rangesdeclared in<algorithm>, returning a result object whose input members point at the first difference. - cppreference, std::equal - the neighboring name that reports whether two ranges hold the same elements, declared in
<algorithm>. - cppreference, std::lexicographical_compare - the neighboring name that reports whether one range sorts before another, declared in
<algorithm>. - cppreference, std::search - the neighboring name that finds a sub-range inside a range, declared in
<algorithm>.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.