C++
'std::remove_if' Is Not a Member of 'std' Fix
std::remove_if lives in one header, <algorithm> - and in the sweep probed here, covering all 106 standard headers in this library's header set, only 5 other headers pull it in on the side, so the textbook <vector> program fails outright. This name also carries the family's most misleading suggestion: most failing files are told to try remove_cv, a type trait that removes nothing. And the call itself removes nothing either - it compacts the kept elements to the front and hands back the new logical end, which is why the erase-remove idiom, not the call alone, is what actually shrinks a container. This guide pins down the failing shapes, the suggestion, and the include that fixes the case by rule.
Written December 3, 2026 · Tested with GCC 13.3.0 · 29 min read
- The error: the include is missing
- The textbook version fails too - and its suggestion names a type trait
- The suggestion in the sweep: 'removecv', 66 times out of 67
- Neighboring headers: 33 fail plainly, 67 fail with a suggestion, and 5 rescue it by accident
Written December 3, 2026
Tested with GCC 13.3.0
What this guide is
std::remove_if is a function template defined in the header <algorithm>. It moves every element of a range for which a predicate returns false to the front of the range, in the elements' original relative order, and returns an iterator to the new logical end: over 1, 2, 3, 4, 5, 6 with a predicate that matches the even values, the kept prefix is 1, 3, 5 and the returned iterator stands at position 3. The call does not shorten the range or the container behind it - the elements past the new end are still there, in a valid but unspecified state - so actually shrinking a container takes a second step, the container's erase, and the pair of calls together is the erase-remove idiom. The range needs forward iterators. The predicate is applied exactly once per element. The classic call dates to C++98, 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 - 'remove_if' 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 5 of the other 105 headers in the library's header set pull the name in on the side, so most files without<algorithm>do reach this error.
For this name the missing include is also the only cause of the error. std::remove_if starts at C++98, so no standard setting produces this wording for the classic call: a file compiled as C++98 with <algorithm> included compiles and runs, as the versions section below shows. The pieces layered on top still have floors of their own - the execution-policy overload needs C++17 and constant-expression use needs C++20 - and those floors belong to the added pieces, not to the classic call.
This name's diagnostic is the family's most misleading one. In the sweep below, 67 of the 100 failing sole-header contexts decorate the error with a did you mean tail - and 66 of them point at remove_cv, a type-traits name that strips const and volatile qualifiers from a type and removes no elements at all. One header, <cstdio>, points at remove instead: that is at least this name's value-based sibling, but in that header it is also the name of a function that deletes files, so following it is wrong twice over. 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 rescue set has only 6 entries, because in this library the name is declared by a header that very little of the library includes.
The error: the include is missing
This program removes the even values from 1, 2, 3, 4, 5, 6, but includes only <limits>:
#include <limits>
bool is_even(int n) { return n % 2 == 0; }
int main() {
int a[6] = {1, 2, 3, 4, 5, 6};
int* e = std::remove_if(a, a + 6, is_even);
return 0;
}
-std=c++20 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:5:19: error: 'remove_if' is not a member of 'std'
5 | int* e = std::remove_if(a, a + 6, is_even);
| ^~~~~~~~~
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++20 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:4:19: error: 'remove_if' is not a member of 'std'
4 | int* e = std::remove_if(a, a + 6, is_even);
| ^~~~~~~~~
The textbook version fails too - and its suggestion names a type trait
The version most often written first - remove the even values from a vector with a lambda - produces an error in this library, without <algorithm>, and this time the error does carry a suggestion:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5, 6};
auto e = std::remove_if(v.begin(), v.end(), [](int n) { return n % 2 == 0; });
std::cout << (e - v.begin()) << "\n";
return 0;
}
-std=c++20 t_textbook.cpp -o t_textbook_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:5:19: error: 'remove_if' is not a member of 'std'; did you mean 'remove_cv'?
5 | auto e = std::remove_if(v.begin(), v.end(), [](int n) { return n % 2 == 0; });
| ^~~~~~~~~
| remove_cv
Nothing rescues the call here: <vector> does not pull the algorithm in - in the sweep below it fails for this name, with this same suggestion. And the suggestion is a trap: std::remove_cv is a type trait - it produces a type with its top-level const and volatile qualifiers removed - and it cannot remove an element from anything. It is offered because its spelling is close, not because it does anything similar. A program that uses std::remove_if 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++20 i_algorithm.cpp -o i_algorithm_prog
$ ./i_algorithm_prog
The suggestion in the sweep: 'remove_cv', 66 times out of 67
The textbook file is typical, not unlucky. In the sweep below, 67 of the 100 failing sole-header contexts carry a did you mean tail: 66 of them suggest remove_cv, and the one remaining context - a file whose only include is <cstdio> - suggests remove. That second suggestion at least names a relative: std::remove is the value-based sibling in <algorithm>, which removes the elements equal to a given value rather than the elements a predicate matches. But <cstdio> declares a different std::remove - the one that takes a file name and deletes the file - so in that context the suggested name is a file operation, not an algorithm at all. Neither suggestion is the fix in any context: both are spelling guesses, and the declaring header is named by neither.
So for this name the diagnostic gives the reader one true piece of information - the name is not a member of std in this file - plus a guess to ignore. The fix has to come from knowing the declaring header, not from reading the error. The move is the same either way: name the declaring header.
Neighboring headers: 33 fail plainly, 67 fail with a suggestion, and 5 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 removes the even values from 1, 2, 3, 4, 5, 6 and requires the kept values 1, 3, 5 and a new logical end at position 3 back, the outcome splits three ways - the plain failure, the failure with a suggestion, or a rescue. Every diagnostic component was classified separately - the error line, any suggestion tail on it, any note lines, and any help lines. These 33 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:
<cassert><cctype><cerrno><cfenv><cfloat><cinttypes><ciso646><climits><clocale><csetjmp><csignal><cstdalign><cstdarg><cstdbool><cstddef><cstdint><cstdlib><cstring><ctime><cuchar><cwchar><cwctype><expected><initializer_list><iosfwd><limits><new><source_location><spanstream><stacktrace><stdfloat><typeinfo><version>
These 66 headers all fail with the same error line carrying the suggestion did you mean 'remove_cv'?:
<any><array><atomic><barrier><bit><bitset><ccomplex><charconv><cmath><codecvt><compare><complex><concepts><condition_variable><coroutine><ctgmath><deque><exception><filesystem><format><forward_list><fstream><future><iomanip><ios><iostream><istream><iterator><latch><list><locale><map><memory><memory_resource><mutex><numbers><numeric><optional><ostream><queue><random><ranges><ratio><scoped_allocator><semaphore><set><shared_mutex><span><sstream><stack><stdexcept><stop_token><streambuf><string><string_view><syncstream><system_error><thread><tuple><type_traits><typeindex><unordered_map><unordered_set><utility><variant><vector>
And one header fails with the suggestion did you mean 'remove'?:
<cstdio>
A file with no include at all fails the plain way. Two entries deserve a pause. <limits> fails plainly: the header of the numeric limits declares nothing close enough to this name to trigger a guess, which is why the first section's program shows the bare line. <ranges> fails with the remove_cv suggestion for the classic name: the C++20 ranges library has its own remove-if name, and it does not bring this one in - that name gets its own section below. And <vector> - the header of the container this call most often rearranges - fails with the remove_cv suggestion, as the textbook section above shows.
No failing header in any shape produced a help line or a different error.
And these headers rescue the program - it compiles and runs with no <algorithm> anywhere in the file:
<algorithm><chrono><execution><functional><regex><valarray>
The declaring header in this library sits underneath very little - a time header, a policy header, a function-object header, the regular-expression header, and the numeric-array header - so the rescue set runs to only 5 transitive entries. That is also why the failing set is nearly the whole library here: with 100 of the other 105 headers failing, the missing include shows itself in almost any file that does not name <algorithm>. <algorithm> - the home header - is on the rescue list by definition, not by transitivity, and the other 5 rescues are this library version's behavior, not a guarantee.
The fix
Add the header - and finish the job with erase, because the call alone only compacts:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5, 6};
auto e = std::remove_if(v.begin(), v.end(), [](int n) { return n % 2 == 0; });
std::cout << "kept";
for (auto it = v.begin(); it != e; ++it) std::cout << " " << *it;
std::cout << " end " << (e - v.begin()) << " size " << v.size() << "\n";
v.erase(e, v.end());
std::cout << "erased";
for (int n : v) std::cout << " " << n;
std::cout << " size " << v.size() << "\n";
return 0;
}
-std=c++20 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
kept 1 3 5 end 3 size 6
erased 1 3 5 size 3
One include, <algorithm>, declares the function - and the fixed program prints two lines that carry this name's whole lesson. The first, kept 1 3 5 end 3 size 6, is the call's own result: the kept values stand at the front in order, the returned iterator marks position 3, and the vector's size is still 6 - nothing has been removed yet. The second, erased 1 3 5 size 3, is what the erase-remove idiom adds: erase on the range from the returned iterator to the end drops the leftover tail, and only then is the size 3. The name is now declared by a header the file names itself, instead of by an internal include of one of the 5 rescuing headers that a different library version is free to rearrange. For a file that does fail - the <limits> shape above, or any other - the same one include is the whole fix for the error, at every standard level: this name has no floor case, as the versions section below shows.
What the call does: it compacts the kept elements to the front and returns the new logical end
The call's whole result is a position, not a shorter container, and its defining choices are what it preserves and what it leaves behind: the elements the predicate does not match keep their original relative order at the front of the range, the elements past the returned iterator are left in a valid but unspecified state, and the range's length - and the container's size - stay exactly as they were until the caller erases the tail. This program walks the shapes in order:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5, 6};
auto e = std::remove_if(v.begin(), v.end(), [](int n) { return n % 2 == 0; });
std::cout << "kept";
for (auto it = v.begin(); it != e; ++it) std::cout << " " << *it;
std::cout << "\n";
std::cout << "end " << (e - v.begin()) << " size " << v.size() << "\n";
v.erase(e, v.end());
std::cout << "erased";
for (int n : v) std::cout << " " << n;
std::cout << " size " << v.size() << "\n";
std::vector<int> w = {5, 2, 7, 4, 9, 6};
auto we = std::remove_if(w.begin(), w.end(), [](int n) { return n > 3; });
std::cout << "gt3";
for (auto it = w.begin(); it != we; ++it) std::cout << " " << *it;
std::cout << " end " << (we - w.begin()) << "\n";
int calls = 0;
std::vector<int> c = {1, 2, 3, 4, 5, 6};
std::remove_if(c.begin(), c.end(), [&calls](int n) { ++calls; return n % 2 == 0; });
std::cout << "calls " << calls << "\n";
std::vector<int> empty;
auto ee = std::remove_if(empty.begin(), empty.end(), [](int n) { return n % 2 == 0; });
std::cout << "empty " << (ee == empty.end() ? "end" : "mid") << "\n";
std::vector<int> all = {2, 4, 6};
auto ae = std::remove_if(all.begin(), all.end(), [](int n) { return n % 2 == 0; });
std::cout << "all " << (ae - all.begin()) << " size " << all.size() << "\n";
std::vector<int> none = {1, 3, 5};
auto ne = std::remove_if(none.begin(), none.end(), [](int n) { return n % 2 == 0; });
std::cout << "none " << (ne - none.begin()) << " " << (ne == none.end() ? "end" : "mid") << "\n";
return 0;
}
-std=c++20 r_semantics.cpp -o r_semantics_prog
$ ./r_semantics_prog
kept 1 3 5
end 3 size 6
erased 1 3 5 size 3
gt3 2 end 1
calls 6
empty end
all 0 size 3
none 3 end
Read in order: the kept line reports 1 3 5 - the elements the predicate did not match, moved to the front in their original order; the call is stable, so 1 still precedes 3 and 3 still precedes 5. The end line reports 3 and size 6 together - the returned iterator is the new logical end, three positions in, while the container still holds six elements; reading the container's size as the count of kept elements is the classic way to misread this call. The erased line reports 1 3 5 size 3 - only after erase takes the range from the returned iterator to the end does the container match the logical range. The gt3 line reports 2 end 1: from 5, 2, 7, 4, 9, 6, removing every value above 3 leaves the single value 2, and the new end stands at position 1. The calls line reports 6: over 6 elements the predicate is applied exactly 6 times - the documented count is exactly the length of the range, no more and no fewer. The empty line reports end: over an empty range the returned iterator is the end itself. The all line reports 0 size 3: when every element matches, the new end is the beginning - position 0 - and the size is still 3 until an erase. The none line reports 3 end: when no element matches, nothing moves and the new end is the end.
The cost rule is the documented count: with a range of length N, the call makes exactly N applications of the predicate - one per element.
Versions: no floor for the classic call - it dates to C++98
This call has no floor inside the probed set. std::remove_if dates to C++98 - the cppreference page carries no since marker on the classic overload, and its defect reports are applied to C++98, which only makes sense for a name that already existed there. The fixed programs in this guide are compiled as C++20, but the name is far older than that: with <algorithm> included, the same program compiled as C++98 compiles and runs, printing 1 3 5 3 - the kept values and the new end's position - and as C++11, C++14, C++17, C++20, and C++23 the same way:
#include <algorithm>
#include <iostream>
bool is_even(int n) { return n % 2 == 0; }
int main() {
int a[6] = {1, 2, 3, 4, 5, 6};
int* e = std::remove_if(a, a + 6, is_even);
std::cout << a[0] << " " << a[1] << " " << a[2] << " " << (e - a) << "\n";
return 0;
}
-std=c++98 c_version98.cpp -o c_version98_prog
$ ./c_version98_prog
1 3 5 3
So this name's error has one cause, not two: a missing <algorithm>. A file that includes <algorithm> and still sees this error does not have a standard problem; the include is not reaching the file that names the call, or the name in the file is not this name. No standard upgrade fixes a missing include, and no include change is needed for an old standard - the two halves of the family's usual advice separate cleanly for this name.
The pieces layered on top of the classic call carry their own floors. 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> v = {1, 2, 3, 4, 5, 6};
auto e = std::remove_if(std::execution::seq, v.begin(), v.end(), [](int n) { return n % 2 == 0; });
return (e - v.begin() == 3) ? 0 : 1;
}
-std=c++20 p_policy_algonly.cpp -o p_policy_algonly_prog
p_policy_algonly.cpp: In function 'int main()':
p_policy_algonly.cpp:5:34: error: 'std::execution' has not been declared
5 | auto e = std::remove_if(std::execution::seq, v.begin(), v.end(), [](int n) { return n % 2 == 0; });
| ^~~~~~~~~
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::remove_if exists - and unlike some of its siblings in this family, the policy form of this name returns the same new-logical-end iterator as the classic call:
-std=c++20 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:6:34: error: 'std::execution' has not been declared
6 | auto e = std::remove_if(std::execution::seq, v.begin(), v.end(), [](int n) { return n % 2 == 0; });
| ^~~~~~~~~
And since C++20 the classic call is also usable in constant expressions - this program's check happens at compile time, and the assertion on the result is part of the build:
#include <algorithm>
#include <array>
constexpr int check() {
std::array<int, 6> a{1, 2, 3, 4, 5, 6};
auto e = std::remove_if(a.begin(), a.end(), [](int n) { return n % 2 == 0; });
return (e - a.begin() == 3 && a[0] == 1 && a[1] == 3 && a[2] == 5) ? 1 : 0;
}
static_assert(check() == 1);
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 int check()':
c_constexpr17.cpp:8:23: error: non-constant condition for static assertion
8 | static_assert(check() == 1);
| ~~~~~~~~^~~
c_constexpr17.cpp:8:20: error: 'constexpr int check()' called in a constant expression
8 | static_assert(check() == 1);
| ~~~~~^~
c_constexpr17.cpp:3:15: note: 'constexpr int check()' declared here
3 | constexpr int check() {
| ^~~~~
So C++17 is the floor for the policy form and C++20 is the floor for constant-expression use - and no standard at any level rescues a missing include, while the classic call needs nothing beyond <algorithm> at any standard from C++98 up.
The neighboring names: seven fail, one compiles outright
The names a reader most often reaches for alongside this one mostly live in <algorithm> too, and in a file whose only include is <vector> this family splits - seven of the eight names fail, and one compiles. std::find, which returns an iterator to the first element equal to a value, fails with a suggestion that points into the ranges library:
-std=c++20 b_find_vector.cpp -o b_find_vector_prog
b_find_vector.cpp: In function 'int main()':
b_find_vector.cpp:5:20: error: 'find' is not a member of 'std'; did you mean 'std::ranges::find'?
5 | auto it = std::find(v.begin(), v.end(), 3);
| ^~~~
std::copy, which copies a range into another range, is the split: in this library a sole <vector> file compiles it outright, with no <algorithm> anywhere - the container header pulls the name in on the side here, as this library version's behavior, not as a rule:
-std=c++20 b_copy_vector.cpp -o b_copy_vector_prog
std::transform, which applies a function to a range and stores the results in a destination range, fails with the plain error:
-std=c++20 b_transform_vector.cpp -o b_transform_vector_prog
b_transform_vector.cpp: In function 'int main()':
b_transform_vector.cpp:5:20: error: 'transform' is not a member of 'std'
5 | auto it = std::transform(v.begin(), v.end(), dest.begin(), [](int n) { return n * 2; });
| ^~~~~~~~~
std::count, which counts the elements equal to a value, fails with a suggestion that points at a bit-counting function, not at any algorithm:
-std=c++20 b_count_vector.cpp -o b_count_vector_prog
b_count_vector.cpp: In function 'int main()':
b_count_vector.cpp:5:19: error: 'count' is not a member of 'std'; did you mean 'popcount'?
5 | auto c = std::count(v.begin(), v.end(), 3);
| ^~~~~
| popcount
std::sort, which orders a range in place, fails with the plain error:
-std=c++20 b_sort_vector.cpp -o b_sort_vector_prog
b_sort_vector.cpp: In function 'int main()':
b_sort_vector.cpp:5:10: error: 'sort' is not a member of 'std'
5 | std::sort(v.begin(), v.end());
| ^~~~
std::remove, this name's own value-based sibling - it removes the elements equal to a value, where std::remove_if removes the elements a predicate matches - fails with a suggestion that points at a different algorithm altogether:
-std=c++20 b_remove_vector.cpp -o b_remove_vector_prog
b_remove_vector.cpp: In function 'int main()':
b_remove_vector.cpp:5:20: error: 'remove' is not a member of 'std'; did you mean 'move'?
5 | auto it = std::remove(v.begin(), v.end(), 3);
| ^~~~~~
| move
std::unique, which compacts away consecutive duplicate elements and returns a new logical end the same way this name does, fails with the plain error:
-std=c++20 b_unique_vector.cpp -o b_unique_vector_prog
b_unique_vector.cpp: In function 'int main()':
b_unique_vector.cpp:5:20: error: 'unique' is not a member of 'std'
5 | auto it = std::unique(v.begin(), v.end());
| ^~~~~~
While std::partition, which rearranges a range so the elements a predicate matches come first and returns the partition point, fails with the plain error too:
-std=c++20 b_partition_vector.cpp -o b_partition_vector_prog
b_partition_vector.cpp: In function 'int main()':
b_partition_vector.cpp:5:20: error: 'partition' is not a member of 'std'
5 | auto it = std::partition(v.begin(), v.end(), [](int n) { return n % 2 == 0; });
| ^~~~~~~~~
No suggestion in this set names the declaring header: one names the ranges form of a neighbor, one names a bit-counting function, and one names a moving algorithm - all three are spelling guesses, and none of them does what the failed name does. And the one name that compiles, std::copy, compiles by accident of this library's internal includes - the rule for it is the same <algorithm> as for the rest. The failing neighbors are shown here only as neighbors, not re-taught.
The neighboring names, fixed
Together the nine names take one header between them - <algorithm> for all nine, with <vector> covering only the containers they work on. The fixed neighbor program reports all nine operations:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5, 6};
auto rf = std::remove_if(v.begin(), v.end(), [](int n) { return n % 2 == 0; });
std::cout << "remove_if";
for (auto it = v.begin(); it != rf; ++it) std::cout << " " << *it;
std::cout << " end " << (rf - v.begin()) << "\n";
std::vector<int> u = {1, 2, 3, 4, 5, 6};
auto f = std::find(u.begin(), u.end(), 3);
std::cout << "find " << (f - u.begin()) << " " << *f << "\n";
auto c = std::count(u.begin(), u.end(), 3);
std::cout << "count " << c << "\n";
std::vector<int> dest(6);
std::copy(u.begin(), u.end(), dest.begin());
std::cout << "copy " << dest.front() << " " << dest.back() << "\n";
std::vector<int> doubled(6);
std::transform(u.begin(), u.end(), doubled.begin(), [](int n) { return n * 2; });
std::cout << "transform";
for (int n : doubled) std::cout << " " << n;
std::cout << "\n";
std::vector<int> w = {6, 3, 5, 1, 4, 2};
std::sort(w.begin(), w.end());
std::cout << "sort";
for (int n : w) std::cout << " " << n;
std::cout << "\n";
std::vector<int> r = {1, 2, 3, 4, 3, 6};
auto re = std::remove(r.begin(), r.end(), 3);
std::cout << "remove";
for (auto it = r.begin(); it != re; ++it) std::cout << " " << *it;
std::cout << " end " << (re - r.begin()) << "\n";
std::vector<int> q = {1, 1, 2, 2, 3, 3};
auto qe = std::unique(q.begin(), q.end());
std::cout << "unique";
for (auto it = q.begin(); it != qe; ++it) std::cout << " " << *it;
std::cout << " end " << (qe - q.begin()) << "\n";
std::vector<int> p = {1, 2, 3, 4, 5, 6};
auto pp = std::partition(p.begin(), p.end(), [](int n) { return n % 2 == 0; });
std::cout << "partition end " << (pp - p.begin()) << " first";
for (auto it = p.begin(); it != pp; ++it) std::cout << " " << *it;
std::cout << "\n";
return 0;
}
-std=c++20 b_neighbors_fixed.cpp -o b_neighbors_fixed_prog
$ ./b_neighbors_fixed_prog
remove_if 1 3 5 end 3
find 2 3
count 1
copy 1 6
transform 2 4 6 8 10 12
sort 1 2 3 4 5 6
remove 1 2 4 6 end 4
unique 1 2 3 end 3
partition end 3 first 6 2 4
Read in order: remove_if reports 1 3 5 end 3 - the kept values and the new logical end. find reports position 2 and the value 3 - the first element equal to 3. count reports 1 - one element equals 3. copy reports 1 6 - the destination's first and last elements after the range was copied into it. transform reports 2 4 6 8 10 12 - each element doubled into the destination. sort reports 1 2 3 4 5 6 - the shuffled range 6, 3, 5, 1, 4, 2 put back in order. remove reports 1 2 4 6 end 4 - both elements equal to 3 compacted out of 1, 2, 3, 4, 3, 6, with the new end at position 4. unique reports 1 2 3 end 3 - the consecutive duplicates in 1, 1, 2, 2, 3, 3 compacted away. And partition reports end 3 with the first segment holding the three even values - partition, unlike this name, makes no promise about the order inside either segment, so the segment is reported as it came out, not as a sorted result.
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::remove_if is a function object in the ranges namespace, declared in <algorithm>, that accepts a whole range - or an iterator pair - and returns a subrange that starts at the new logical end and runs to the range's end: exactly the tail an erase call needs. Compiled as C++20 it answers for the same vector:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5, 6};
auto r = std::ranges::remove_if(v, [](int n) { return n % 2 == 0; });
std::cout << "ranges kept";
for (auto it = v.begin(); it != r.begin(); ++it) std::cout << " " << *it;
std::cout << " tail " << (r.end() == v.end() ? "end" : "mid") << " size " << v.size() << "\n";
v.erase(r.begin(), r.end());
std::cout << "erased";
for (int n : v) std::cout << " " << n;
std::cout << " size " << v.size() << "\n";
return 0;
}
-std=c++20 r_ranges.cpp -o r_ranges_prog
$ ./r_ranges_prog
ranges kept 1 3 5 tail end size 6
erased 1 3 5 size 3
The ranges line reports the kept values 1 3 5, the tail's end at the range's end, and the size still 6 - the ranges form compacts exactly as the classic call does, and its returned subrange begins where the kept prefix stops. The erased line reports 1 3 5 size 3 after the subrange is handed to erase. 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:6:19: error: 'std::ranges' has not been declared
6 | auto r = std::ranges::remove_if(v, [](int n) { return n % 2 == 0; });
| ^~~~~~
And in this library the ranges header does not rescue the ranges form - a file whose only include is <ranges> (with <vector> for the containers) fails for it with a not-a-member error naming the ranges namespace, and a file whose only include is <array> fails for it the same way, the two transcripts identical in shape:
-std=c++20 r_ranges_rangesonly.cpp -o r_ranges_rangesonly_prog
r_ranges_rangesonly.cpp: In function 'int main()':
r_ranges_rangesonly.cpp:5:27: error: 'remove_if' is not a member of 'std::ranges'
5 | auto r = std::ranges::remove_if(v, [](int n) { return n % 2 == 0; });
| ^~~~~~~~~
-std=c++20 r_ranges_arrayonly.cpp -o r_ranges_arrayonly_prog
r_ranges_arrayonly.cpp: In function 'int main()':
r_ranges_arrayonly.cpp:4:27: error: 'remove_if' is not a member of 'std::ranges'
4 | auto r = std::ranges::remove_if(v, [](int n) { return n % 2 == 0; });
| ^~~~~~~~~
That 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. 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::remove_if with the plain name's error and the remove_cv suggestion, 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>
bool is_even(int n) { return n % 2 == 0; }
int main() {
int a[6] = {1, 2, 3, 4, 5, 6};
int* e = remove_if(a, a + 6, is_even);
return 0;
}
-std=c++20 g_unqualified.cpp -o g_unqualified_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:5:14: error: 'remove_if' was not declared in this scope
5 | int* e = remove_if(a, a + 6, is_even);
| ^~~~~~~~~
g_unqualified.cpp:5:14: note: suggested alternatives:
In file included from /usr/include/c++/13/algorithm:73,
from g_unqualified.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:220:1: note: 'std::remove_if'
220 | remove_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred);
| ^~~~~~~~~
In file included from /usr/include/c++/13/algorithm:63:
/usr/include/c++/13/bits/ranges_algo.h:1002:35: note: 'std::ranges::remove_if'
1002 | inline constexpr __remove_if_fn remove_if{};
| ^~~~~~~~~
That wording - was not declared in this scope, with notes pointing at the declarations of both std::remove_if and the ranges form - is a different defect with a different fix: the header is present, the qualifier is not. Qualify the name as std::remove_if; adding includes does not bring the name out of namespace std. Swap the arrays for vectors, and the same unqualified call compiles and runs:
#include <algorithm>
#include <vector>
bool is_even(int n) { return n % 2 == 0; }
int main() {
std::vector<int> v = {1, 2, 3, 4, 5, 6};
auto e = remove_if(v.begin(), v.end(), is_even);
return (e - v.begin() == 3 && v[0] == 1 && v[1] == 3 && v[2] == 5) ? 0 : 1;
}
-std=c++20 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 vectors' iterators are std types, so their associated namespace is std, and lookup finds std::remove_if through them. A list's iterators rescue it the same way - the same unqualified call over lists compiles and runs:
-std=c++20 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>
bool is_even(int n) { return n % 2 == 0; }
int main() {
std::array<int, 6> v{1, 2, 3, 4, 5, 6};
auto e = remove_if(v.begin(), v.end(), is_even);
return 0;
}
-std=c++20 g_unqualified_stdarray.cpp -o g_unqualified_stdarray_prog
g_unqualified_stdarray.cpp: In function 'int main()':
g_unqualified_stdarray.cpp:6:14: error: 'remove_if' was not declared in this scope
6 | auto e = remove_if(v.begin(), v.end(), is_even);
| ^~~~~~~~~
g_unqualified_stdarray.cpp:6:14: note: suggested alternatives:
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:220:1: note: 'std::remove_if'
220 | remove_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _UnaryPredicate __pred);
| ^~~~~~~~~
In file included from /usr/include/c++/13/algorithm:63:
/usr/include/c++/13/bits/ranges_algo.h:1002:35: note: 'std::ranges::remove_if'
1002 | inline constexpr __remove_if_fn remove_if{};
| ^~~~~~~~~
And with using namespace std; in the file, the qualifier question goes away by a different route - the unqualified call over vectors compiles and runs, because the using-directive brings the name into scope directly:
-std=c++20 g_using_namespace.cpp -o g_using_namespace_prog
$ ./g_using_namespace_prog
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 - and a using-directive bypasses the question entirely. Write the qualifier either way.
The exact rule
std::remove_ifis declared by#include <algorithm>, and by no other header by rule. In the sweep probed here, 100 of the other 105 headers in the library's header set - among them<limits>,<ranges>,<vector>,<filesystem>, and<string>- do not declare it, and neither does a file with no include at all; the remaining 5 pull it in transitively, as this library version's behavior, not as a guarantee.'remove_if' 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. It comes in two shapes in the sweep probed here: 33 failing sole headers fail with the plain error line alone, and 67 fail with a suggestion tail - 66 suggestingremove_cvand 1,<cstdio>, suggestingremove. No failing sole header produced a different error. And unlike its younger siblings, this name has no standard-based second cause: the classic call dates to C++98 and compiles with the include in place at every probed standard.- In the sweep probed here, 5 headers besides
<algorithm>rescuestd::remove_iftransitively -<chrono>,<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::remove_ifin. - The result rules are the documented ones, as the fixed programs here show: the fixed program prints
kept 1 3 5 end 3 size 6anderased 1 3 5 size 3for the textbook case; the semantics program printskept 1 3 5,end 3 size 6,erased 1 3 5 size 3,gt3 2 end 1,calls 6,empty end,all 0 size 3, andnone 3 end- the call moves the kept elements to the front in their original order and returns the new logical end, the container's size does not change untileraseis called on the returned range, the predicate is applied exactly once per element, and the boundary cases land where the lines show. - The classic
std::remove_ifdates to C++98: with<algorithm>included it compiles and runs as C++98, C++11, C++14, C++17, C++20, and C++23, each printing1 3 5 3. The layered pieces have floors: the execution-policy overload starts at C++17 - naming the policy with only<algorithm>included fails with'std::execution' has not been declared; with<execution>included alongside it, the policy call compiles and runs, returning the new logical end. 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::findfails in a sole<vector>file with the suggestiondid you mean 'std::ranges::find'?,std::countwithdid you mean 'popcount'?, andstd::removewithdid you mean 'move'?, whilestd::transform,std::sort,std::unique, andstd::partitionfail there with the plain error, andstd::copycompiles outright in that file - all eight neighbors are declared by#include <algorithm>by rule. The fixed neighbor program printsremove_if 1 3 5 end 3,find 2 3,count 1,copy 1 6,transform 2 4 6 8 10 12,sort 1 2 3 4 5 6,remove 1 2 4 6 end 4,unique 1 2 3 end 3, andpartition end 3. - The ranges form of this same name is a function object in namespace
std::ranges, declared in<algorithm>by the standard, needing C++20, returning a subrange that starts at the new logical end - compiled as C++17 the ranges program here fails with'std::ranges' has not been declared, and compiled as C++20 it printsranges kept 1 3 5 tail end size 6anderased 1 3 5 size 3. In the probes here<ranges>alone does not rescue it in this library - it fails with a not-a-member error naming therangesnamespace for the ranges form,<array>alone fails for it the same way, and<ranges>fails with the classic name's error and theremove_cvsuggestion for the classic name in the sweep. 'remove_if' was not declared in this scopefor an unqualifiedremove_ifuse is a different defect: qualify the name asstd::remove_if- 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; and withusing namespace std;in the file it compiles outright, as the last unqualified program shows. Qualify the name either way.
The fixes, ranked
- First, check the include list of the file that names
std::remove_if. If<algorithm>is not there, add it - that is the fix for the only cause of this error. It holds whether or not the file happened to compile before through one of the 5 transitive rescues and stopped compiling when its other includes changed - all 100 of the probed failures are this one cause. - Second, do not follow the suggestion: in 66 of the 67 suggestion-bearing probed contexts the diagnostic offers
remove_cv, a type trait that removes qualifiers from a type and no elements from anything, and in the one remaining context it offersremove, which in that header deletes files. A suggestion on this error is a spelling guess, never this name's fix. - Third, do not chase a standard upgrade for this error: this name dates to C++98, so a build set to any standard from C++98 up keeps the classic call with the include in place, and the error with the include present is never a standard problem.
- Fourth, do not reach for a container's header because the call rearranges a container, or for
<iterator>because the call takes iterators. In the sweep probed here<vector>,<list>,<string>, and<iterator>all fail for this name - none of them is the declaring header by rule. The declaring header is<algorithm>, and a complete program names it alongside the container's own header. - Fifth, do not remove
<algorithm>because a file compiles without it: in the probed set only 5 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. The same warning coversstd::copy, which compiles in a sole<vector>file here by the same kind of accident. - Sixth, do not expect the call to shrink the container: it returns the new logical end and leaves the size unchanged - in the fixed program the size is still 6 after the call. Erase the range from the returned iterator to the end - the erase-remove idiom - to make the removal real, and never read the elements past the returned iterator as kept values: their state is valid but unspecified.
- Seventh, 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. - Eighth, 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 needs C++20 - with its header still
<algorithm>by the standard, not<ranges>. - Ninth, if the error says
was not declared in this scopeinstead, fix the name's qualifier tostd::remove_ifas 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::remove' Is Not a Member of 'std' Fix
- 'std::copy_if' Is Not a Member of 'std' Fix
- 'std::find_if_not' Is Not a Member of 'std' Fix
- 'std::find_if' Is Not a Member of 'std' Fix
Sources
- cppreference, std::remove, std::remove_if - the function templates are defined in
<algorithm>;std::remove_ifremoves the elements for which the predicate returns true by shifting the kept elements forward, returns the new logical end, leaves the sequence unshortened with the tail in a valid but unspecified state, keeps the kept elements' relative order, and applies the predicate exactly once per element; the execution-policy overload is marked since C++17, the classic overload constexpr since C++20, and the page names the erase-remove idiom as the call's usual pairing with a container'serase. - cppreference,
<algorithm>header - the header that declares this name and the other algorithm names used as neighbors here. - cppreference, std::ranges::remove, std::ranges::remove_if - the C++20 ranges forms of these names, function objects in namespace
std::rangesdeclared in<algorithm>, returning a subrange that starts at the new logical end.
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.