C++

'std::copy_if' Is Not a Member of 'std' Fix

std::copy_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's diagnostic carries a trap its plainer neighbors lack: 44 of the 100 failing sole-header contexts offer a suggestion, and the common one, 'copy', is a real algorithm that copies every element - following it compiles a program that silently drops the filter. This guide pins down the failing shapes, why the predicate runs over the whole range with no early stop, why the returned iterator is the count of what was copied, why a C++98 build keeps this same error even with the include in place, and the include that fixes the ordinary case by rule.

Written December 3, 2026 · Tested with GCC 13.3.0 · 28 min read

By the end of this guide
  • The error: the include is missing
  • The textbook version fails too - with a suggestion that drops the filter
  • The suggestion in the sweep: 'copy' 39 times, 'copysignf' 5 times
  • Neighboring headers: 56 fail plainly, 44 offer a suggestion, and 5 rescue it by accident

Written December 3, 2026

Tested with GCC 13.3.0

What this guide is

std::copy_if is a function template defined in the header <algorithm>. It walks a source range once, applying a unary predicate to each element, and writes the elements the predicate returns true for to a destination, in their original order - the copy is stable, so passing elements keep the order they had in the source. It returns the output iterator one past the last element written, and it does not modify the source range. Its classic overload needs only input iterators on the source side and an output iterator on the destination side, and the destination is the caller's responsibility: a raw destination must already have room for every element that passes, while an insert iterator such as std::back_inserter grows its container as elements arrive. The classic call dates to 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 - 'copy_if' is not a member of 'std' is usually 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.

But for this name the missing include is not the only cause of the error. std::copy_if starts at C++11, so a file compiled as C++98 sees the very same wording even with <algorithm> included, because the name does not exist at that standard - the versions section below shows that transcript. The pieces layered on top 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 also carries a suggestion more often than not, and the common suggestion is a trap. Of the 100 failing sole-header contexts probed here, 44 decorate the error with a did you mean tail: 39 offer copy - a real algorithm, declared in the same header this call wants, that copies every element with no predicate at all - and 5 offer copysignf, a floating-point sign function from the math headers. The other 56 fail with the plain line alone, and no failing context in the sweep produced a different error 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 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 copies the even elements of a four-element array, 2, 3, 4, 6, into an output array, but includes only <limits>:

C++
#include <limits>
int main() {
    int src[4] = {2, 3, 4, 6};
    int dst[4] = {0, 0, 0, 0};
    int* end = std::copy_if(src, src + 4, dst, [](int x) { return x % 2 == 0; });
    return 0;
}
Output
-std=c++20 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:5:21: error: 'copy_if' is not a member of 'std'
    5 |     int* end = std::copy_if(src, src + 4, dst, [](int x) { return x % 2 == 0; });
      |                     ^~~~~~~

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:

Output
-std=c++20 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:4:21: error: 'copy_if' is not a member of 'std'
    4 |     int* end = std::copy_if(src, src + 4, dst, [](int x) { return x % 2 == 0; });
      |                     ^~~~~~~

The textbook version fails too - with a suggestion that drops the filter

The version most often written first - copy the even elements of a vector into a second vector and print how many arrived - produces an error in this library, without <algorithm>, and the error carries the trap suggestion:

C++
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {2, 3, 4, 6};
    std::vector<int> out;
    std::copy_if(v.begin(), v.end(), std::back_inserter(out), [](int x) { return x % 2 == 0; });
    std::cout << out.size() << "\n";
    return 0;
}
Output
-std=c++20 t_textbook.cpp -o t_textbook_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:6:10: error: 'copy_if' is not a member of 'std'; did you mean 'copy'?
    6 |     std::copy_if(v.begin(), v.end(), std::back_inserter(out), [](int x) { return x % 2 == 0; });
      |          ^~~~~~~
      |          copy

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 not a spelling variant of the missing name. std::copy takes no predicate: a reader who follows the suggestion and drops the predicate argument gets a program that compiles and copies all four elements, 2, 3, 4, 6, odd 3 included - the fixed neighbor program below shows copy doing exactly that over this range. The filter the program was written for is gone, and nothing warns about it. A program that uses std::copy_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:

Terminal
-std=c++20 i_algorithm.cpp -o i_algorithm_prog
$ ./i_algorithm_prog

The suggestion in the sweep: 'copy' 39 times, 'copysignf' 5 times

The textbook file is not unusual. In the sweep below, 39 of the 100 failing sole-header contexts offer copy - every context where the library's base algorithm header, which declares copy, has been pulled in on the side, without the header that declares copy_if. The suggestion follows the partial declaration, not the intent: the compiler can see a name spelled one word shorter, so it offers that name. Five more contexts, all math headers, offer copysignf instead, because in those files the closest declared name to copy_if is the floating-point sign function.

Neither offer is a fix. Following copy changes what the program computes, as the section above shows. Following copysignf changes it further still - that name takes two floating-point values and returns one, and has nothing to do with ranges. The remaining 56 contexts offer nothing at all. So for this name the suggestion tail, when it appears, is decoration on the same missing include - the move it leaves is the right one either way: name the declaring header.

Neighboring headers: 56 fail plainly, 44 offer 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 copies the even elements of 2, 3, 4, 6 into an output array and requires the three values 2, 4, 6 back, the outcome splits three ways - the failure in its two diagnostic shapes, 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 56 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>
  • <bit>
  • <cassert>
  • <cctype>
  • <cerrno>
  • <cfenv>
  • <cfloat>
  • <charconv>
  • <cinttypes>
  • <ciso646>
  • <climits>
  • <clocale>
  • <compare>
  • <concepts>
  • <coroutine>
  • <csetjmp>
  • <csignal>
  • <cstdalign>
  • <cstdarg>
  • <cstdbool>
  • <cstddef>
  • <cstdint>
  • <cstdio>
  • <cstdlib>
  • <cstring>
  • <ctime>
  • <cuchar>
  • <cwchar>
  • <cwctype>
  • <exception>
  • <expected>
  • <initializer_list>
  • <iosfwd>
  • <latch>
  • <limits>
  • <mutex>
  • <new>
  • <numbers>
  • <numeric>
  • <optional>
  • <ratio>
  • <scoped_allocator>
  • <semaphore>
  • <shared_mutex>
  • <source_location>
  • <spanstream>
  • <stacktrace>
  • <stdfloat>
  • <tuple>
  • <type_traits>
  • <typeindex>
  • <typeinfo>
  • <utility>
  • <variant>
  • <version>

These 39 headers fail with the suggestion did you mean 'copy'?:

  • <array>
  • <barrier>
  • <bitset>
  • <codecvt>
  • <condition_variable>
  • <deque>
  • <filesystem>
  • <format>
  • <forward_list>
  • <fstream>
  • <future>
  • <iomanip>
  • <ios>
  • <iostream>
  • <istream>
  • <iterator>
  • <list>
  • <locale>
  • <map>
  • <memory>
  • <memory_resource>
  • <ostream>
  • <queue>
  • <ranges>
  • <set>
  • <span>
  • <sstream>
  • <stack>
  • <stdexcept>
  • <stop_token>
  • <streambuf>
  • <string>
  • <string_view>
  • <syncstream>
  • <system_error>
  • <thread>
  • <unordered_map>
  • <unordered_set>
  • <vector>

And these 5 headers fail with the suggestion did you mean 'copysignf'?:

  • <ccomplex>
  • <cmath>
  • <complex>
  • <ctgmath>
  • <random>

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. <ranges> fails with the copy suggestion for the classic name: the C++20 ranges library has its own copy-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 reads and writes - fails with the copy suggestion, as the textbook section above shows.

No failing header in any shape produced a help line, and no failing header produced 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:

C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main() {
    std::vector<int> v = {2, 3, 4, 6};
    std::vector<int> out;
    auto last = std::copy_if(v.begin(), v.end(), std::back_inserter(out), [](int x) { return x % 2 == 0; });
    std::cout << "copied";
    for (int x : out) std::cout << " " << x;
    std::cout << "\n";
    std::cout << "count " << out.size() << "\n";
    return 0;
}
Terminal
-std=c++20 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
copied 2 4 6
count 3

One include, <algorithm>, declares the function - and the textbook program prints copied 2 4 6 and count 3: of 2, 3, 4, 6, the three even elements arrive, in order, and the 3 stays behind. 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, as long as the standard is C++11 or later; the one case where it is not is the C++98 case in the versions section below.

What the call does: it writes the passing elements, counts them in its return, and scans the whole range

The call's result is split between two places: the destination holds the copied elements, and the returned iterator marks where the writing stopped - one past the last element written, so against the destination's start it is also the count of elements copied. Unlike a search, this call does not stop early: every element must be tested, because a later element may still pass, so the predicate is applied exactly once to each element, in order, however early the failures come:

C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main() {
    std::vector<int> v = {2, 3, 4, 6};
    std::vector<int> out;
    auto last = std::copy_if(v.begin(), v.end(), std::back_inserter(out), [](int x) { return x % 2 == 0; });
    std::cout << "copied";
    for (int x : out) std::cout << " " << x;
    std::cout << "\n";
    std::cout << "count " << out.size() << "\n";
    std::cout << "source";
    for (int x : v) std::cout << " " << x;
    std::cout << "\n";
    int calls = 0;
    std::vector<int> out2;
    std::copy_if(v.begin(), v.end(), std::back_inserter(out2), [&calls](int x) { ++calls; return x % 2 == 0; });
    std::cout << "calls " << calls << "\n";
    int raw[4] = {0, 0, 0, 0};
    int* rend = std::copy_if(v.data(), v.data() + v.size(), raw, [](int x) { return x % 2 == 0; });
    std::cout << "rawcount " << (rend - raw) << " raw " << raw[0] << " " << raw[1] << " " << raw[2] << "\n";
    std::vector<int> mt;
    std::vector<int> out3;
    auto elast = std::copy_if(mt.begin(), mt.end(), std::back_inserter(out3), [](int x) { return x % 2 == 0; });
    std::cout << "empty " << out3.size() << "\n";
    std::vector<int> odd = {3, 5};
    std::vector<int> out4;
    std::copy_if(odd.begin(), odd.end(), std::back_inserter(out4), [](int x) { return x % 2 == 0; });
    std::cout << "none " << out4.size() << "\n";
    return 0;
}
Terminal
-std=c++20 r_semantics.cpp -o r_semantics_prog
$ ./r_semantics_prog
copied 2 4 6
count 3
source 2 3 4 6
calls 4
rawcount 3 raw 2 4 6
empty 0
none 0

Read in order: the copied line reports 2 4 6 - the elements of 2, 3, 4, 6 that pass the even test, in their original order - and the count line reports 3. The source line reports 2 3 4 6: the source range is exactly as it was, 3 included - this call copies out, it does not move, erase, or partition anything in place. The calls line reports 4: the counting predicate was applied to all four elements, once each - there is no early exit to save applications, because the answer is not settled until the last element is tested. The rawcount line reports 3 and raw 2 4 6: writing into a raw array instead of a growing vector, the returned pointer sits 3 past the array's start - one past the last element written - and the array's first three slots hold the copied values. That raw shape is also where the destination rule bites: the array had four slots for at most four passing elements, and a destination that cannot grow must have room for every element that passes, because the call writes as it goes and checks nothing about capacity. The back_inserter shapes above carry no such requirement - the vector grows per element written. The empty line reports 0: an empty range copies nothing, and the returned iterator is the destination's start. The none line reports 0 as well, for 3, 5: a range where no element passes is, at the destination, the same as an empty range.

One return-value subtlety the raw line hides: the returned iterator is of the destination's iterator type. With std::back_inserter that type is a back-insert iterator, which supports no subtraction - the count there is the container's size, as the count line reads it, not a difference of iterators.

Versions: a second cause - the classic call starts at C++11

This call does have a floor. std::copy_if dates to C++11 - the cppreference page's defect reports are applied to C++11 for this name, specifying its return value and its stability at that standard, and there is no earlier form of it. The fixed programs in this guide are compiled as C++20, but the floor is far below that: with <algorithm> included, the same question compiled as C++11 compiles and runs, printing 3, and as C++14 and as C++17 the same way. One standard level lower, the picture flips - compiled as C++98, the same program, include and all, fails with this guide's own error. The C++98 transcript uses a named functor instead of a lambda, so the only C++11 feature left in the file is the call itself:

C++
#include <algorithm>
#include <iostream>
struct Even { bool operator()(int x) const { return x % 2 == 0; } };
int main() {
    int src[4] = {2, 3, 4, 6};
    int dst[4] = {0, 0, 0, 0};
    int* end = std::copy_if(src, src + 4, dst, Even());
    std::cout << (end - dst) << "\n";
    return 0;
}
Output
-std=c++98 c_version98.cpp -o c_version98_prog
c_version98.cpp: In function 'int main()':
c_version98.cpp:7:21: error: 'copy_if' is not a member of 'std'
    7 |     int* end = std::copy_if(src, src + 4, dst, Even());
      |                     ^~~~~~~

Read the transcript in full: the one error is the plain not-a-member line itself - at C++98 the name is not a member of std because it does not exist yet, and with the functor in place there is not even a lambda warning underneath it. Raising the standard to C++11 or later is the whole fix for that shape; no include change reaches it. So this name's error has two causes, not one: a missing <algorithm>, or a standard older than C++11 - and the two are told apart by checking whether the include is already in the file. A file that includes <algorithm>, compiles at C++11 or later, and still sees this error does not have either problem; 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 carry their own 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:

C++
#include <algorithm>
#include <vector>
int main() {
    std::vector<int> v = {2, 3, 4, 6};
    std::vector<int> out(4);
    auto last = std::copy_if(std::execution::seq, v.begin(), v.end(), out.begin(), [](int x) { return x % 2 == 0; });
    return 0;
}
Output
-std=c++20 p_policy_algonly.cpp -o p_policy_algonly_prog
p_policy_algonly.cpp: In function 'int main()':
p_policy_algonly.cpp:6:35: error: 'std::execution' has not been declared
    6 |     auto last = std::copy_if(std::execution::seq, v.begin(), v.end(), out.begin(), [](int x) { return x % 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::copy_if exists:

Terminal
-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:

Output
-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:35: error: 'std::execution' has not been declared
    7 |     auto last = std::copy_if(std::execution::seq, v.begin(), v.end(), out.begin(), [](int x) { return x % 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:

C++
#include <algorithm>
#include <array>
constexpr int check() {
    std::array<int, 4> a{2, 3, 4, 6};
    std::array<int, 4> b{0, 0, 0, 0};
    auto* end = std::copy_if(a.begin(), a.end(), b.begin(), [](int x) { return x % 2 == 0; });
    return int(end - b.begin());
}
static_assert(check() == 3);
int main() { return 0; }
Terminal
-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:

Output
-std=c++17 c_constexpr17.cpp -o c_constexpr17_prog
c_constexpr17.cpp: In function 'constexpr int check()':
c_constexpr17.cpp:6:29: warning: call to non-'constexpr' function '_OIter std::copy_if(_IIter, _IIter, _OIter, _Predicate) [with _IIter = int*; _OIter = int*; _Predicate = check()::<lambda(int)>]' [-Winvalid-constexpr]
    6 |     auto* end = std::copy_if(a.begin(), a.end(), b.begin(), [](int x) { return x % 2 == 0; });
      |                 ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:61,
                 from c_constexpr17.cpp:1:
/usr/include/c++/13/bits/stl_algo.h:694:5: note: '_OIter std::copy_if(_IIter, _IIter, _OIter, _Predicate) [with _IIter = int*; _OIter = int*; _Predicate = check()::<lambda(int)>]' declared here
  694 |     copy_if(_InputIterator __first, _InputIterator __last,
      |     ^~~~~~~
c_constexpr17.cpp: At global scope:
c_constexpr17.cpp:9:23: error: non-constant condition for static assertion
    9 | static_assert(check() == 3);
      |               ~~~~~~~~^~~
c_constexpr17.cpp:9:20: error: 'constexpr int check()' called in a constant expression
c_constexpr17.cpp:3:15: note: 'constexpr int check()' declared here
    3 | constexpr int 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 the classic call needs nothing beyond <algorithm> at any standard from C++11 up.

The neighboring names: one compiles outright, one offers a wrong name, two fail plainly

The names a reader most often reaches for alongside this one mostly live in <algorithm> too, but in a file whose only include is <vector> this family splits four ways - and one member of it does not fail at all. std::copy, which copies every element with no predicate, compiles outright in that file:

Terminal
-std=c++20 b_copy_vector.cpp -o b_copy_vector_prog
$ ./b_copy_vector_prog

That compile is this library's internal-include behavior, not a rule - <vector> pulls in the library's base algorithm header, which declares copy, and that same partial declaration is what produces the did you mean 'copy'? suggestion for this guide's name in the sweep above. std::remove_copy_if - the mirror name, which copies the elements the predicate fails for - fails with a suggestion, and the suggestion is a type-trait name from a different part of the library:

Output
-std=c++20 b_removecopyif_vector.cpp -o b_removecopyif_vector_prog
b_removecopyif_vector.cpp: In function 'int main()':
b_removecopyif_vector.cpp:5:22: error: 'remove_copy_if' is not a member of 'std'; did you mean 'remove_cvref'?
    5 |     auto last = std::remove_copy_if(v.begin(), v.end(), out.begin(), [](int x) { return x % 2 != 0; });
      |                      ^~~~~~~~~~~~~~
      |                      remove_cvref

While std::transform, which writes a computed value per element, and std::partition_copy, which splits a range into two destinations by a predicate, both fail with the plain error:

Output
-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:22: error: 'transform' is not a member of 'std'
    5 |     auto last = std::transform(v.begin(), v.end(), out.begin(), [](int x) { return x * 10; });
      |                      ^~~~~~~~~
Output
-std=c++20 b_partitioncopy_vector.cpp -o b_partitioncopy_vector_prog
b_partitioncopy_vector.cpp: In function 'int main()':
b_partitioncopy_vector.cpp:5:20: error: 'partition_copy' is not a member of 'std'
    5 |     auto pr = std::partition_copy(v.begin(), v.end(), ev.begin(), od.begin(), [](int x) { return x % 2 == 0; });
      |                    ^~~~~~~~~~~~~~

No suggestion in this set is the missing name: remove_cvref removes const, volatile, and reference qualifiers from a type at compile time and copies nothing. And the one outright compile in the set is the exception that proves the header rule - it rests on an internal include a different library version is free to rearrange. The failing neighbors are shown here only as neighbors, not re-taught.

The neighboring names, fixed

Together the five 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 five operations over the range 2, 3, 4, 6:

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {2, 3, 4, 6};
    std::vector<int> c(4);
    auto cl = std::copy(v.begin(), v.end(), c.begin());
    std::cout << "copy " << c[0] << " " << c[1] << " " << c[2] << " " << c[3] << "\n";
    std::vector<int> r(4);
    auto rl = std::remove_copy_if(v.begin(), v.end(), r.begin(), [](int x) { return x % 2 != 0; });
    std::cout << "remove_copy_if " << (rl - r.begin()) << " " << r[0] << " " << r[1] << " " << r[2] << "\n";
    std::vector<int> t(4);
    std::transform(v.begin(), v.end(), t.begin(), [](int x) { return x * 10; });
    std::cout << "transform " << t[0] << " " << t[1] << " " << t[2] << " " << t[3] << "\n";
    std::vector<int> ev(4), od(4);
    auto pr = std::partition_copy(v.begin(), v.end(), ev.begin(), od.begin(), [](int x) { return x % 2 == 0; });
    std::cout << "partition_copy evens " << (pr.first - ev.begin()) << " " << ev[0] << " " << ev[1] << " " << ev[2] << " odds " << (pr.second - od.begin()) << " " << od[0] << "\n";
    std::vector<int> cf(4);
    auto cfl = std::copy_if(v.begin(), v.end(), cf.begin(), [](int x) { return x % 2 == 0; });
    std::cout << "copy_if " << (cfl - cf.begin()) << " " << cf[0] << " " << cf[1] << " " << cf[2] << "\n";
    return 0;
}
Terminal
-std=c++20 b_neighbors_fixed.cpp -o b_neighbors_fixed_prog
$ ./b_neighbors_fixed_prog
copy 2 3 4 6
remove_copy_if 3 2 4 6
transform 20 30 40 60
partition_copy evens 3 2 4 6 odds 1 3
copy_if 3 2 4 6

Read in order: copy reports 2 3 4 6 - every element, the unfiltered baseline the sweep's suggestion would have produced. remove_copy_if, handed the opposite predicate, reports a count of 3 and the same 2 4 6 - it is this call with the test inverted. transform reports 20 30 40 60, each element multiplied by ten. partition_copy reports 3 evens, 2 4 6, and 1 odd, 3 - both halves of the split this call keeps only one of. And copy_if itself reports a count of 3 and 2 4 6, matching the fix section's result through a raw destination.

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::copy_if is a function object in the ranges namespace, declared in <algorithm>, that accepts a whole range and returns a result holding both ends of the operation - the input iterator past the last element read, and the output iterator one past the last element written - rather than the single output iterator the classic form returns. Compiled as C++20 it answers for the same vector:

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {2, 3, 4, 6};
    std::vector<int> out;
    auto res = std::ranges::copy_if(v, std::back_inserter(out), [](int x) { return x % 2 == 0; });
    std::cout << "ranges";
    for (int x : out) std::cout << " " << x;
    std::cout << "\n";
    std::cout << "incount " << (res.in - v.begin()) << " outcount " << out.size() << "\n";
    return 0;
}
Terminal
-std=c++20 r_ranges.cpp -o r_ranges_prog
$ ./r_ranges_prog
ranges 2 4 6
incount 4 outcount 3

The first line reports ranges 2 4 6 - the same three elements the classic form copies. The second line reads the result's two halves: incount 4, because the input side of the result stands past all four elements read, and outcount 3, the size of the destination the output side wrote. 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:

Output
-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:21: error: 'std::ranges' has not been declared
    6 |     auto res = std::ranges::copy_if(v, out.begin(), [](int x) { return x % 2 == 0; });
      |                     ^~~~~~

And in this library the ranges header does not rescue the ranges form - a file whose only include is <ranges> 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:

Output
-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:29: error: 'copy_if' is not a member of 'std::ranges'
    5 |     auto res = std::ranges::copy_if(src, dst, [](int x) { return x % 2 == 0; });
      |                             ^~~~~~~
Output
-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: 'copy_if' is not a member of 'std::ranges'
    5 |     auto res = std::ranges::copy_if(src, dst.begin(), [](int x) { return x % 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::copy_if with the copy 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:

C++
#include <algorithm>
int main() {
    int src[4] = {2, 3, 4, 6};
    int dst[4] = {0, 0, 0, 0};
    int* end = copy_if(src, src + 4, dst, [](int x) { return x % 2 == 0; });
    return 0;
}
Output
-std=c++20 g_unqualified.cpp -o g_unqualified_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:5:16: error: 'copy_if' was not declared in this scope
    5 |     int* end = copy_if(src, src + 4, dst, [](int x) { return x % 2 == 0; });
      |                ^~~~~~~
g_unqualified.cpp:5:16: 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:142:1: note:   'std::copy_if'
  142 | copy_if(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 result,
      | ^~~~~~~
In file included from /usr/include/c++/13/algorithm:63:
/usr/include/c++/13/bits/ranges_algo.h:638:33: note:   'std::ranges::copy_if'
  638 |   inline constexpr __copy_if_fn copy_if{};
      |                                 ^~~~~~~

That wording - was not declared in this scope, with notes pointing at the declarations of both std::copy_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::copy_if; 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:

C++
#include <algorithm>
#include <vector>
int main() {
    std::vector<int> v = {2, 3, 4, 6};
    std::vector<int> out(4);
    auto last = copy_if(v.begin(), v.end(), out.begin(), [](int x) { return x % 2 == 0; });
    return (last - out.begin() == 3) ? 0 : 1;
}
Terminal
-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 vector's iterators are std types, so their associated namespace is std, and lookup finds std::copy_if through them. A list's iterators rescue it the same way - the same unqualified call over a list compiles and runs:

Terminal
-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:

C++
#include <algorithm>
#include <array>
int main() {
    std::array<int, 4> src{2, 3, 4, 6};
    std::array<int, 4> dst{0, 0, 0, 0};
    auto last = copy_if(src.begin(), src.end(), dst.begin(), [](int x) { return x % 2 == 0; });
    return 0;
}
Output
-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:17: error: 'copy_if' was not declared in this scope
    6 |     auto last = copy_if(src.begin(), src.end(), dst.begin(), [](int x) { return x % 2 == 0; });
      |               ^~~~~~~
g_unqualified_stdarray.cpp:6:17: 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:142:1: note:   'std::copy_if'
  142 | copy_if(_ExecutionPolicy&& __exec, _ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 result,
      | ^~~~~~~
In file included from /usr/include/c++/13/algorithm:63:
/usr/include/c++/13/bits/ranges_algo.h:638:33: note:   'std::ranges::copy_if'
  638 |   inline constexpr __copy_if_fn copy_if{};
      |                                 ^~~~~~~

And with using namespace std; in the file, the qualifier question goes away by a different route - the unqualified call over a vector compiles and runs, because the using-directive brings the name into scope directly:

Terminal
-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::copy_if is 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.
  • 'copy_if' is not a member of 'std' has two causes in the contexts probed here: the <algorithm> include is missing and nothing else in the file pulled the name in, or the program is compiled as C++98, where the name does not exist - and it comes in two shapes in the sweep probed here: 56 failing sole headers fail with the plain error line alone, and 44 fail with a suggestion tail - 39 offering copy and 5 offering copysignf. No failing sole header produced a different error, and neither offered name is this function.
  • In the sweep probed here, 5 headers besides <algorithm> rescue std::copy_if transitively - <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 pull std::copy_if in.
  • The result rules are the documented ones, as the fixed programs here show: the fixed program prints copied 2 4 6 and count 3 for the textbook case; the semantics program prints copied 2 4 6, count 3, source 2 3 4 6, calls 4, rawcount 3 raw 2 4 6, empty 0, and none 0 - the call writes the elements the predicate passes, in order, leaves the source unchanged, applies the predicate exactly once per element with no early stop, returns an iterator one past the last element written, and copies nothing for an empty range or a range where nothing passes.
  • The destination rule is the caller's: a raw destination must already have room for every passing element - the raw array here had four slots - while std::back_inserter grows its container as elements arrive; and with back_inserter the returned iterator is a back-insert iterator, so the count is read from the container's size, not from iterator subtraction.
  • The classic std::copy_if dates to C++11: with <algorithm> included it compiles and runs as C++11, C++14, and C++17, each printing 3, and compiled as C++98 it fails with the same not-a-member error, include and all. The layered pieces have floors: the execution-policy overload starts at C++17 - with only <algorithm> included, naming the policy fails 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::copy compiles outright in a sole <vector> file, through this library's internal includes; std::remove_copy_if fails there with the suggestion did you mean 'remove_cvref'?, and std::transform and std::partition_copy fail there with the plain error - all four are declared by #include <algorithm> by rule. The fixed neighbor program prints copy 2 3 4 6, remove_copy_if 3 2 4 6, transform 20 30 40 60, partition_copy evens 3 2 4 6 odds 1 3, and copy_if 3 2 4 6.
  • 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 result holding the input iterator past the end and the output iterator one past the last element written - compiled as C++17 the ranges program here fails with 'std::ranges' has not been declared, and compiled as C++20 it prints ranges 2 4 6 and incount 4 outcount 3. In the probes here <ranges> alone does not rescue it in this library - it fails with a not-a-member error naming the ranges namespace for the ranges form, <array> alone fails for it the same way, and <ranges> fails with the copy suggestion for the classic name in the sweep.
  • 'copy_if' was not declared in this scope for an unqualified copy_if use is a different defect: qualify the name as std::copy_if - the include alone does not bring the name out of namespace std. 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 are std class types; over a std::array's iterators it fails again, because in this library those iterators are plain pointers; and with using 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::copy_if. If <algorithm> is not there, add it - that is the fix for the most common cause of this error. It holds whether the file failed plainly or with a suggestion - all 100 of the probed failures are this one cause - and whether or not it happened to compile before through one of the 5 transitive rescues and stopped compiling when its other includes changed.
  • Second, if <algorithm> is already there, check the standard: this name starts at C++11, so a build set to C++98 keeps the error with the include in place, and raising the standard to C++11 or later is the whole fix for that shape.
  • Third, do not follow the did you mean 'copy'? suggestion: copy takes no predicate and copies every element - over this guide's range it produces 2, 3, 4, 6 where the program wanted 2, 4, 6 - and do not follow copysignf either, which is a floating-point sign function. A suggestion on this error is a spelling guess, not the missing name.
  • Fourth, do not reach for a container's header because the call reads and writes containers, or for <iterator> because the call takes iterators and a back inserter. 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 covers std::copy compiling in a sole <vector> file: that compile rests on an internal include too.
  • Sixth, give the destination room, or give it a way to grow: a raw array or a vector's begin() must already have a slot for every element that passes - the call writes as it scans and checks no capacity - while std::back_inserter appends and needs no sizing at all.
  • Seventh, do not expect the predicate to stop early, and do not read the count off the returned iterator when the destination is a back inserter: the predicate runs once per element over the whole range, and a back-insert iterator supports no subtraction - the count is the destination container's size, or the returned pointer's distance from a raw destination's start.
  • Eighth, if the error names std::execution instead - '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.
  • Ninth, 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>.
  • Tenth, if the error says was not declared in this scope instead, fix the name's qualifier to std::copy_if as well - and do not rely on argument-dependent lookup to rescue the unqualified call, because a plain array and a std::array each remove that rescue in the probed set.

Sources

  • cppreference, std::copy, std::copy_if - the function templates are defined in <algorithm>; std::copy_if copies only the elements for which the predicate returns true, in order, is stable, returns the past-the-end iterator of the destination range, and applies the predicate exactly once per element; the classic overload is marked constexpr since C++20 and the execution-policy overload since C++17, with defect reports applied to C++11 specifying its return value and stability.
  • cppreference, <algorithm> header - the header that declares this name and the other algorithm names used as neighbors here.
  • cppreference, std::ranges::copy, std::ranges::copy_if - the C++20 ranges forms of these names, function objects in namespace std::ranges declared in <algorithm>, returning a result holding the input and output iterators.

Keep Exploring