C++
'std::merge' Is Not a Member of 'std' Fix
std::merge lives in one header, <algorithm> - and in the sweep probed here, covering all 106 standard headers in this library's header set, only 4 other headers pull it in on the side: <execution>, <functional>, <regex>, and <valarray>. Its error never varies: all 101 failing headers fail with one plain line, no suggestion tail at all. This guide pins down the failing shapes, the 4 transitive rescues, what the merge writes for duplicates, empty ranges, and a reversed comparator, and the include that fixes it by rule.
Written December 3, 2026 · Tested with GCC 13.3.0 · 28 min read
- The error: the include is missing
- The textbook version fails too - with the same plain line
- Neighboring headers: 101 fail plainly, none offers a suggestion, and 4 rescue it by accident
- The fix
Written December 3, 2026
Tested with GCC 13.3.0
What this guide is
std::merge is a function template defined in the header <algorithm>. It merges two sorted ranges into one sorted range beginning at the output iterator the caller passes: elements are compared with operator< unless the caller passes a comparator, every element of both inputs is copied - duplicates included - and the call returns the output iterator just past the last element copied. The two input ranges need only meet the input-iterator requirements, the merge is stable (for equivalent elements, the ones from the first range come first), and the classic call makes at most as many comparisons as the two input lengths added together, minus one. 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 - 'merge' 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 rarely saves anyone: in the sweep below, only 4 of the other 105 headers in the library's header set pull the name in on the side, so most files without<algorithm>reach this error.
And for this name the missing include is the only cause. Some names in this family hide a second cause behind the same wording - a standard floor the name starts at - but std::merge 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 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, and neither produces this error line for the classic call.
This name's diagnostic is the plainest in its family. In every one of the 101 failing sole-header contexts probed here - the textbook file among them - 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 5 entries: <algorithm> itself and 4 transitive rescues.
The error: the include is missing
This program merges two sorted three-element arrays, 1, 3, 5 and 2, 4, 6, into a six-element output array, but includes only <limits>:
#include <limits>
int main() {
int a[3] = {1, 3, 5};
int b[3] = {2, 4, 6};
int o[6] = {0, 0, 0, 0, 0, 0};
std::merge(a, a + 3, b, b + 3, o);
return o[0] == 1 && o[5] == 6 ? 0 : 1;
}
-std=c++17 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:6:10: error: 'merge' is not a member of 'std'
6 | std::merge(a, a + 3, b, b + 3, o);
| ^~~~~
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:5:10: error: 'merge' is not a member of 'std'
5 | std::merge(a, a + 3, b, b + 3, o);
| ^~~~~
The textbook version fails too - with the same plain line
The version most often written first - merge two sorted vectors and print the result - produces this error as well, without <algorithm>:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v1 = {1, 3, 5};
std::vector<int> v2 = {2, 4, 6};
std::vector<int> v3(6);
std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
for (int x : v3) std::cout << x << " ";
std::cout << "\n";
return 0;
}
-std=c++17 t_textbook.cpp -o t_textbook_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:7:10: error: 'merge' is not a member of 'std'
7 | std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
| ^~~~~
It fails with exactly the plain line - no suggestion attached, in a file whose includes declare plenty of other names. Neither <iostream> nor <vector> rescues this name, and there is no wrong name to be led to: the diagnostic simply stops at the missing name. A sole <vector> file fails with the same plain wording:
-std=c++17 v_vector_fail.cpp -o v_vector_fail_prog
v_vector_fail.cpp: In function 'int main()':
v_vector_fail.cpp:6:10: error: 'merge' is not a member of 'std'
6 | std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
| ^~~~~
A program that uses std::merge 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: 101 fail plainly, none offers a suggestion, and 4 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 merges 1, 3, 5 with 2, 4, 6 and checks that the output holds 1, 2, 3, 4, 5, 6, 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 101 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><array><atomic><barrier><bit><bitset><cassert><ccomplex><cctype><cerrno><cfenv><cfloat><charconv><chrono><cinttypes><ciso646><climits><clocale><cmath><codecvt><compare><complex><concepts><condition_variable><coroutine><csetjmp><csignal><cstdalign><cstdarg><cstdbool><cstddef><cstdint><cstdio><cstdlib><cstring><ctgmath><ctime><cuchar><cwchar><cwctype><deque><exception><expected><filesystem><format><forward_list><fstream><future><initializer_list><iomanip><ios><iosfwd><iostream><istream><iterator><latch><limits><list><locale><map><memory><memory_resource><mutex><new><numbers><numeric><optional><ostream><queue><random><ranges><ratio><scoped_allocator><semaphore><set><shared_mutex><source_location><span><spanstream><sstream><stack><stacktrace><stdexcept><stdfloat><stop_token><streambuf><string><string_view><syncstream><system_error><thread><tuple><type_traits><typeindex><typeinfo><unordered_map><unordered_set><utility><variant><vector><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 readers who reach for it because the set operations feel numeric get no hint for their trouble. And <ranges> fails plainly for the classic name: the C++20 ranges library has its own merge 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. Unlike some neighbors in this family, 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><execution><functional><regex><valarray>
The shape is the family shape: <algorithm> sits on top of the container and stream headers, almost nothing pulls it in on the side, and the four headers that do are headers few merge programs include. That is also why the failing set covers nearly the whole library: with 101 of the other 105 headers failing, the missing include is the normal state of a file that was never given <algorithm>, not an edge case. <algorithm> - the home header - is on the rescue list by definition, not by transitivity, and the other 4 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, 3, 5};
std::vector<int> v2 = {2, 4, 6};
std::vector<int> v3(6);
std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
for (int x : v3) std::cout << x << " ";
std::cout << "\n";
return 0;
}
-std=c++17 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
1 2 3 4 5 6
One include, <algorithm>, declares the function - and the textbook program, which failed outright before, now prints 1 2 3 4 5 6: the two sorted inputs interleave into one sorted output. What the include changes is the standing of the program as well as its fate: the name is now declared by a header the file names itself, instead of by an internal include of one of the four rescuing headers that a different library version is free to rearrange.
What the call does: every element from both ranges, in sorted order
The call returns the output iterator just past the last element copied, and what it writes is the full contents of both inputs - its length is always the two input lengths added together. Over two three-element vectors it writes the interleaved sequence and the return sits at the end; duplicates are kept in full; an empty input contributes nothing; a reversed comparator merges descending ranges into descending order; and an insert iterator grows the destination instead of overwriting it:
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> a = {1, 3, 5};
std::vector<int> b = {2, 4, 6};
std::vector<int> o(6);
auto end = std::merge(a.begin(), a.end(), b.begin(), b.end(), o.begin());
std::cout << "merge";
for (int x : o) std::cout << " " << x;
std::cout << " end " << (end - o.begin()) << "\n";
std::vector<int> d1 = {1, 2, 2};
std::vector<int> d2 = {2, 3};
std::vector<int> d(5);
std::merge(d1.begin(), d1.end(), d2.begin(), d2.end(), d.begin());
std::cout << "duplicates";
for (int x : d) std::cout << " " << x;
std::cout << "\n";
std::vector<int> e1;
std::vector<int> e2 = {1, 2};
std::vector<int> e(2);
auto eend = std::merge(e1.begin(), e1.end(), e2.begin(), e2.end(), e.begin());
std::cout << "empty-first";
for (int x : e) std::cout << " " << x;
std::cout << " end " << (eend - e.begin()) << "\n";
std::vector<int> g1 = {5, 3, 1};
std::vector<int> g2 = {6, 4, 2};
std::vector<int> g(6);
std::merge(g1.begin(), g1.end(), g2.begin(), g2.end(), g.begin(), std::greater<int>());
std::cout << "greater";
for (int x : g) std::cout << " " << x;
std::cout << "\n";
std::vector<int> bi;
std::merge(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(bi));
std::cout << "back_inserter";
for (int x : bi) std::cout << " " << x;
std::cout << " size " << bi.size() << "\n";
return 0;
}
-std=c++17 r_semantics.cpp -o r_semantics_prog
$ ./r_semantics_prog
merge 1 2 3 4 5 6 end 6
duplicates 1 2 2 2 3
empty-first 1 2 end 2
greater 6 5 4 3 2 1
back_inserter 1 2 3 4 5 6 size 6
Read in order: the merge line reports 1 2 3 4 5 6 with end 6 - the two sorted inputs interleave, and the returned iterator sits just past the sixth written element. The duplicates line reports 1 2 2 2 3: the value 2 appears twice in the first input and once in the second, and all three copies are written - merging never deduplicates. The empty-first line reports 1 2 with end 2: an empty first range is not an error, the second range is copied whole. The greater line reports 6 5 4 3 2 1: with the greater comparator, the inputs must be sorted descending, and they merge into one descending range. The back_inserter line reports the same six values with size 6: writing through an insert iterator appends to an initially empty vector, which is how a merge grows a destination whose size is not known in advance.
The edge ranges: empty inputs write nothing
No empty input is an error, and the returned iterator tracks exactly what was written. Merging two empty ranges writes nothing at all - the return is the start of the output - and merging one element with an empty range writes that one element:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> e1;
std::vector<int> e2;
std::vector<int> o(1, 9);
auto end = std::merge(e1.begin(), e1.end(), e2.begin(), e2.end(), o.begin());
std::cout << "both-empty end " << (end - o.begin()) << " value " << o[0] << "\n";
std::vector<int> one = {4};
std::vector<int> p(1);
auto pend = std::merge(one.begin(), one.end(), e2.begin(), e2.end(), p.begin());
std::cout << "one end " << (pend - p.begin()) << " value " << p[0] << "\n";
return 0;
}
-std=c++17 k_empty.cpp -o k_empty_prog
$ ./k_empty_prog
both-empty end 0 value 9
one end 1 value 4
The both-empty line reports end 0 and value 9: the return sits where the output began, and the destination element that was already there keeps its old value, because nothing was copied over it. The one line reports end 1 and value 4: the single element of the non-empty input lands in the output, and the return steps past exactly it. A merge with an empty side is a copy of the other side, and a merge of two empty sides is a no-op whose return value says so.
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 merges directly out of two text streams, through stream iterators that cannot be rewound or re-read:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>
int main() {
std::istringstream s1("1 3 5");
std::istringstream s2("2 4 6");
std::vector<int> o;
std::merge(std::istream_iterator<int>(s1), std::istream_iterator<int>(),
std::istream_iterator<int>(s2), std::istream_iterator<int>(),
std::back_inserter(o));
for (int x : o) std::cout << x << " ";
std::cout << "\n";
return 0;
}
-std=c++17 r_input.cpp -o r_input_prog
$ ./r_input_prog
1 2 3 4 5 6
It reports 1 2 3 4 5 6. Each input element is read once and either written or held only until the other side's next element is known - which is why the same call also works unchanged over vectors, lists, and plain arrays. The policy overloads are the exception in the family: they ask for forward iterators, because a parallel execution may need to revisit the ranges.
Stability: equal elements keep their input order
For elements that compare equivalent, the merge is stable: the element from the first range is written before the element from the second range, and each range's own order is preserved. This program merges two one-element ranges whose keys are both 2, tagged by which range they came from:
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
int main() {
std::vector<std::pair<int, char> > a;
a.push_back(std::make_pair(2, 'a'));
std::vector<std::pair<int, char> > b;
b.push_back(std::make_pair(2, 'b'));
std::vector<std::pair<int, char> > o(2);
std::merge(a.begin(), a.end(), b.begin(), b.end(), o.begin());
std::cout << o[0].second << " " << o[1].second << "\n";
return 0;
}
-std=c++17 r_stable.cpp -o r_stable_prog
$ ./r_stable_prog
a b
It reports a b: the first range's 2 lands ahead of the second range's 2, even though neither compares less than the other. Code that merges records by a key and cares which source wins a tie gets the first range's record first - by rule, not by accident of this library.
Versions: no second cause - this name predates C++11
This call needs no modern-enough standard, and that is what separates it from some of its neighbors. std::merge 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: with <algorithm> included, the same merge compiled as C++98 compiles and runs, and compiled as C++11 it does the same:
#include <algorithm>
#include <iostream>
int main() {
int a[3] = {1, 3, 5};
int b[3] = {2, 4, 6};
int o[6] = {0, 0, 0, 0, 0, 0};
std::merge(a, a + 3, b, b + 3, o);
for (int i = 0; i < 6; ++i) std::cout << o[i] << " ";
std::cout << "\n";
return 0;
}
-std=c++98 c_version98.cpp -o c_version98_prog
$ ./c_version98_prog
1 2 3 4 5 6
-std=c++11 c_version11.cpp -o c_version11_prog
$ ./c_version11_prog
1 2 3 4 5 6
Both builds print 1 2 3 4 5 6. 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, 3, 5};
std::vector<int> v2 = {2, 4, 6};
std::vector<int> v3(6);
std::merge(std::execution::seq, v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
return v3[0] == 1 && v3[5] == 6 ? 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:7:21: error: 'std::execution' has not been declared
7 | std::merge(std::execution::seq, v1.begin(), v1.end(), v2.begin(), v2.end(), v3.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::merge 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:8:21: error: 'std::execution' has not been declared
8 | std::merge(std::execution::seq, v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
| ^~~~~~~~~
And since C++20 the classic call is also usable in constant expressions - this program's merge happens at compile time, and the assertion on the merged array is part of the build:
#include <algorithm>
#include <array>
constexpr bool check() {
std::array<int, 3> a{1, 3, 5};
std::array<int, 3> b{2, 4, 6};
std::array<int, 6> o{};
std::merge(a.begin(), a.end(), b.begin(), b.end(), o.begin());
return o[0] == 1 && o[5] == 6;
}
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:7:15: warning: call to non-'constexpr' function '_OIter std::merge(_IIter1, _IIter1, _IIter2, _IIter2, _OIter) [with _IIter1 = int*; _IIter2 = int*; _OIter = int*]' [-Winvalid-constexpr]
7 | std::merge(a.begin(), a.end(), b.begin(), b.end(), o.begin());
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:61,
from c_constexpr17.cpp:1:
/usr/include/c++/13/bits/stl_algo.h:4946:5: note: '_OIter std::merge(_IIter1, _IIter1, _IIter2, _IIter2, _OIter) [with _IIter1 = int*; _IIter2 = int*; _OIter = int*]' declared here
4946 | merge(_InputIterator1 __first1, _InputIterator1 __last1,
| ^~~~~
c_constexpr17.cpp: At global scope:
c_constexpr17.cpp:10:20: error: non-constant condition for static assertion
10 | static_assert(check());
| ~~~~~^~
c_constexpr17.cpp:10: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: all three share the home, and one earns a wrong suggestion
The three names a reader most often reaches for alongside this one all live in <algorithm> too, and all three share the failure in a file whose only include is <vector>. std::inplace_merge - which merges two consecutive sorted halves of one range in place - fails with the plain error:
-std=c++17 b_inplace_merge_vector.cpp -o b_inplace_merge_vector_prog
b_inplace_merge_vector.cpp: In function 'int main()':
b_inplace_merge_vector.cpp:4:10: error: 'inplace_merge' is not a member of 'std'
4 | std::inplace_merge(v.begin(), v.begin() + 3, v.end());
| ^~~~~~~~~~~~~
std::includes - which reports whether one sorted range contains all of another - fails plainly as well:
-std=c++17 b_includes_vector.cpp -o b_includes_vector_prog
b_includes_vector.cpp: In function 'int main()':
b_includes_vector.cpp:5:17: error: 'includes' is not a member of 'std'
5 | return std::includes(v1.begin(), v1.end(), v2.begin(), v2.end()) ? 0 : 1;
| ^~~~~~~~
And std::set_union - which writes the union of two sorted ranges - fails with the family's one wrong suggestion among these neighbors: the tail names is_union, a type trait that tests a class type, not a set operation:
-std=c++17 b_set_union_vector.cpp -o b_set_union_vector_prog
b_set_union_vector.cpp: In function 'int main()':
b_set_union_vector.cpp:6:19: error: 'set_union' is not a member of 'std'; did you mean 'is_union'?
6 | auto e = std::set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), o.begin());
| ^~~~~~~~~
| is_union
Three neighbors, one home, and two diagnostic shapes: two fail plainly like this guide's name, and one carries a suggestion that is wrong in the same way spelling guesses usually are - it names a visible name with a similar spelling, not the operation the program needs. 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, 3, 5};
std::vector<int> b = {2, 4, 6};
std::vector<int> o(6);
std::merge(a.begin(), a.end(), b.begin(), b.end(), o.begin());
std::cout << "merge";
for (int x : o) std::cout << " " << x;
std::cout << "\n";
std::vector<int> v = {1, 3, 5, 2, 4, 6};
std::inplace_merge(v.begin(), v.begin() + 3, v.end());
std::cout << "inplace_merge";
for (int x : v) std::cout << " " << x;
std::cout << "\n";
std::vector<int> d1 = {1, 2, 2};
std::vector<int> d2 = {2, 3};
std::vector<int> u(5, 9);
auto ue = std::set_union(d1.begin(), d1.end(), d2.begin(), d2.end(), u.begin());
std::cout << "set_union";
for (auto it = u.begin(); it != ue; ++it) std::cout << " " << *it;
std::cout << " count " << (ue - u.begin()) << "\n";
std::vector<int> s1 = {1, 2, 3, 4};
std::vector<int> s2 = {2, 4};
std::cout << "includes " << std::includes(s1.begin(), s1.end(), s2.begin(), s2.end()) << "\n";
return 0;
}
-std=c++17 b_neighbors_fixed.cpp -o b_neighbors_fixed_prog
$ ./b_neighbors_fixed_prog
merge 1 2 3 4 5 6
inplace_merge 1 2 3 4 5 6
set_union 1 2 2 3 count 4
includes 1
Read in order: merge writes 1 2 3 4 5 6, the interleaving of the two inputs; the in-place form writes the same sequence by rearranging one vector whose two halves were each sorted; set_union writes 1 2 2 3 with count 4 - over the same duplicated inputs the semantics section merged into five values, the union keeps only the larger count of the value 2, two copies instead of three, which is the documented difference between the two operations; and includes reports 1, true, because the first range holds both values of the second.
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::merge 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' end positions and the output iterator past the merged range. Compiled as C++20 it merges the same two vectors and reports where the writing ended, read off the result's output member:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v1 = {1, 3, 5};
std::vector<int> v2 = {2, 4, 6};
std::vector<int> o(6);
auto res = std::ranges::merge(v1, v2, o.begin());
std::cout << "ranges";
for (int x : o) std::cout << " " << x;
std::cout << " end-at " << (res.out - o.begin()) << "\n";
return 0;
}
-std=c++20 r_ranges.cpp -o r_ranges_prog
$ ./r_ranges_prog
ranges 1 2 3 4 5 6 end-at 6
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:8:21: error: 'std::ranges' has not been declared
8 | auto res = std::ranges::merge(v1, v2, o.begin());
| ^~~~~~
The ranges header is not the ranges form's home in this library: a file whose only include is <ranges> fails for it, with an error that names the namespace the reader used:
-std=c++20 r_ranges_rangesheader.cpp -o r_ranges_rangesheader_prog
r_ranges_rangesheader.cpp: In function 'int main()':
r_ranges_rangesheader.cpp:6:18: error: 'merge' is not a member of 'std::ranges'
6 | std::ranges::merge(a, a + 3, b, b + 3, o);
| ^~~~~
A file that includes <array> alone fails the same way:
-std=c++20 r_ranges_arrayonly.cpp -o r_ranges_arrayonly_prog
r_ranges_arrayonly.cpp: In function 'int main()':
r_ranges_arrayonly.cpp:6:18: error: 'merge' is not a member of 'std::ranges'
6 | std::ranges::merge(a, b, o.begin());
| ^~~~~
That behavior is a library fact, not the rule: the header the standard names for both forms is <algorithm>, so a complete program names it. 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::merge 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, 3, 5};
int b[3] = {2, 4, 6};
int o[6] = {0, 0, 0, 0, 0, 0};
merge(a, a + 3, b, b + 3, o);
return o[0] == 1 && o[5] == 6 ? 0 : 1;
}
-std=c++17 g_unqualified.cpp -o g_unqualified_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:6:5: error: 'merge' was not declared in this scope; did you mean 'std::merge'?
6 | merge(a, a + 3, b, b + 3, o);
| ^~~~~
| std::merge
In file included from /usr/include/c++/13/algorithm:73,
from g_unqualified.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:417:1: note: 'std::merge' declared here
417 | merge(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _ForwardIterator1 __last1, _ForwardIterator2 __first2,
| ^~~~~
That wording - was not declared in this scope, with did you mean 'std::merge'? 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::merge; 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, 3, 5};
std::vector<int> v2 = {2, 4, 6};
std::vector<int> v3(6);
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
return v3[0] == 1 && v3[5] == 6 ? 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::merge 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, 3, 5};
std::array<int, 3> b{2, 4, 6};
std::array<int, 6> o{};
merge(a.begin(), a.end(), b.begin(), b.end(), o.begin());
return o[0] == 1 && o[5] == 6 ? 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:7:5: error: 'merge' was not declared in this scope; did you mean 'std::merge'?
7 | merge(a.begin(), a.end(), b.begin(), b.end(), o.begin());
| ^~~~~
| std::merge
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:417:1: note: 'std::merge' declared here
417 | merge(_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::mergeis declared by#include <algorithm>, and by no other header by rule. In the sweep probed here, 101 of the other 105 headers in the library's header set - among them<vector>,<iostream>,<iterator>,<numeric>,<ranges>, and<limits>- do not declare it, and neither does a file with no include at all; the remaining 4 pull it in transitively, as this library version's behavior, not as a guarantee.'merge' 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 101 failing sole headers fail with one error line and no suggestion tail, no note lines, and no help lines.- In the sweep probed here, 4 headers besides
<algorithm>rescuestd::mergetransitively -<execution>,<functional>,<regex>, and<valarray>- 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::mergein. - The result rules are the documented ones, as the fixed programs here show: the fixed program prints
1 2 3 4 5 6for the textbook case; the semantics program printsmerge 1 2 3 4 5 6 end 6,duplicates 1 2 2 2 3,empty-first 1 2 end 2,greater 6 5 4 3 2 1, and the back-inserter line withsize 6- the call writes every element of both sorted inputs in sorted order, keeps every duplicate, and returns the output iterator just past the last element copied. - The edge ranges follow from the same rule: merging two empty ranges returns the output iterator at index 0 and leaves existing destination values untouched, and merging one element with an empty range writes that element and returns the iterator at index 1 - the edge program here prints
both-empty end 0 value 9andone end 1 value 4. - The classic call's reading side needs only input iterators: the stream program here merges from two stream iterators and prints
1 2 3 4 5 6. The merge is stable: for equivalent elements the first range's element comes first, reported here asa bfor two elements keyed 2. - The classic
std::mergepredates C++11: with<algorithm>included it compiles and runs as C++98 and as C++11, reporting1 2 3 4 5 6in both builds here. The layered pieces have floors: 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 all take the same header:
std::inplace_mergeandstd::includesare declared by#include <algorithm>as well and fail plainly in a sole<vector>file, andstd::set_unionis declared by it too but fails in that file with the suggestion taildid you mean 'is_union'?- a type trait, not the set operation. The fixed neighbor program printsmerge 1 2 3 4 5 6, the in-place line with the same sequence,set_union 1 2 2 3 count 4, andincludes 1- the union keeps the larger count of a duplicated value where merge keeps every copy. std::ranges::mergeis 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 output member sits past the merged range - 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 3 4 5 6 end-at 6for the two three-element vectors. In the probes here<ranges>alone fails for it with'merge' is not a member of 'std::ranges',<array>alone fails the same way, and<ranges>fails plainly for the classic name in the sweep.'merge' was not declared in this scopefor an unqualifiedmergeuse is a different defect: qualify the name asstd::merge- 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::merge. 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 4 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, for
<iterator>because the call takes iterators, or for<numeric>because the call works on sorted sequences. In the sweep probed here all three fail for this name - 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 only 4 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, size the destination for both inputs: the merge writes the two input lengths added together, duplicates included, so a fixed destination that fits only one input - or that deduplicates in the caller's head - is too small. An insert iterator removes the sizing question.
- 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::mergeneeds 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::mergeas 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. - Ninth, if the failing name is a neighboring one, check its diagnostic shape before its home: the neighbors probed here all take the same added
<algorithm>, butset_union's failure in a sole<vector>file carries theis_unionsuggestion - do not rename the call to the trait it suggests.
Which fix for which cause
#include <algorithm>missing,std::mergeused in a file whose only include is one of the 101 headers listed as plain-failing in the sweep section above, or that includes nothing: the message is'merge' is not a member of 'std', with no suggestion tail, no note lines, and no help lines - add#include <algorithm>.std::mergeused in a file whose only include is one of the 4 transitive-rescue headers listed in the sweep section above -<execution>,<functional>,<regex>,<valarray>: no error on this library version - add#include <algorithm>anyway, because those rescues are not guaranteed.std::mergeused in a textbook file including<iostream>and<vector>: the error above, plain - add#include <algorithm>; the textbook includes do not rescue this name.std::mergeused with<algorithm>included, at any standard level from C++98 up: no version error - the classic call predates C++11, and the same merge reports1 2 3 4 5 6compiled as C++98 and as C++11.std::mergecalled with an execution policy, with<algorithm>included but<execution>not: the message is'std::execution' has not been declared- add<execution>; the policy overload exists for this name since C++17, and compiled as C++14 the same program fails on the policy's name for the standard's reason.std::set_unionused in a file whose only include is<vector>: the message is'set_union' is not a member of 'std'; did you mean 'is_union'?- add<algorithm>anyway, and do not rename the call tois_union.mergeused without thestd::qualifier,<algorithm>included, over plain-array arguments or astd::array's iterators: the message is'merge' was not declared in this scope; did you mean 'std::merge'?- qualify the name asstd::merge. Over a vector's iterators or a list's iterators, the same unqualified call compiles through argument-dependent lookup - qualify it anyway, so the call does not depend on the iterators' types.
Checklist
- Check the include list of the file that names the call, not just the project. The include must be in that file, or in a header that file includes directly by design.
- Do not import the container's header, the iterator header, or the numeric header into this error as the fix:
std::mergeand its three neighbors take<algorithm>andstd::vectortakes<vector>, so there are two headers in a complete merge program, one for the operations and one for the container. - Do not assume the textbook includes rescue this name by rule, and do not delete a working
<algorithm>line because a file compiles without it. In the sweep here<iostream>and<vector>fail forstd::merge- plainly, with no suggestion attached - and the 4 headers that rescue it do so only as this library version's behavior. - Read what the call leaves behind as well as what it returns: the output range holds every element of both inputs - the fixed programs here print
1 2 3 4 5 6for the textbook case, andmerge 1 2 3 4 5 6 end 6,duplicates 1 2 2 2 3, and the greater-comparator line6 5 4 3 2 1for the semantics cases - and the returned iterator sits just past the last element written. - After the fix, confirm the program runs, not just that it compiles: the fixed programs here print
1 2 3 4 5 6under C++98 and C++11,1 2 3 4 5 6for the stream-iterator merge,ranges 1 2 3 4 5 6 end-at 6for the ranges form at C++20, and the four neighbor results for the neighbor calls.
Related guides
- 'std::includes' Is Not a Member of 'std' Fix - the membership member of this guide's family:
std::includesreports whether one sorted range contains all of another, from<algorithm>like this guide's name - and in the probes here it fails plainly in the same sole<vector>file thatstd::mergefails in.
Sources
- cppreference,
std::merge— forstd::mergebeing a function template defined in<algorithm>; for it merging two sorted ranges into one sorted range beginning at the output iterator; for the classic overloads carrying no "since" marker - the name predates C++11 - and for the execution-policy overloads existing since C++17 and constant-expression use since C++20; for the input ranges needing only input iterators and the call returning the output iterator just past the last element copied; for the call making at most the sum of the two input lengths minus one comparisons; for the merge being stable; and forstd::mergewriting all copies of a duplicated value wherestd::set_unionwrites only the larger count. - cppreference,
std::ranges::merge— for the ranges form existing since C++20, being defined in<algorithm>, and returning a result holding the output iterator past the merged range. - cppreference, Standard library header
<algorithm>— for<algorithm>being the header whose synopsis declaresstd::merge,std::inplace_merge,std::set_union,std::includes, and the C++20std::ranges::merge, the home-header fact the fix, the neighbor, and the ranges sections of this guide rest on.
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.