C++
'std::count_if' Is Not a Member of 'std' Fix
std::count_if lives in one header, <algorithm> - not in <vector>, where the containers it counts over live, and not in any header a counting program already has by habit. On this library version a handful of headers rescue it by accident: in the sweep probed here, covering all 106 standard headers in the library's header set, <execution>, <functional>, <regex>, and <valarray> pull the name in on the side, and the textbook combination of <iostream> and <vector> does not. This name has no second cause hiding behind the first: it predates C++11, so a file compiled as C++98 with the include present counts correctly - the include is the only thing that can be wrong. This guide pins down the plain failing shape, the four transitive rescues, and the one include that fixes it.
Written December 2, 2026 · Tested with GCC 13.3.0 · 23 min read
- The error: the include is missing, in a lean file
- The textbook version fails too - plainly, with no suggestion to follow
- Neighboring headers: 101 fail plainly, none fail with a suggestion, and four rescue it
- The fix
Written December 2, 2026
Tested with GCC 13.3.0
What this guide is
std::count_if is a function template defined in the header <algorithm>. It returns the number of elements in a range for which the given unary predicate returns true - a count, of the range iterator's difference type, not an iterator and not a position. The range's iterators need only meet the input-iterator requirements - the weakest iterator category any algorithm in this family accepts - the predicate must meet the predicate requirements, and the predicate is applied exactly once per element: a counting call always walks the whole range, because the total is not known until the last element has been tested. The classic overload predates C++11 and has no standard floor of its own.
Because the function lives in one specific header, 'count_if' is not a member of 'std' has one cause, and naming it 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, GCC prints no suggestion tail, no note lines, and no help lines at all - every failing file gets the bare error line, as the sweep below shows.
There is no version form of this failure. std::count_if is an original name, older than C++11, so a file that includes <algorithm> and is compiled as C++98 counts correctly, as the versions section below shows. Its newer neighbors in this family do have that second cause; this name does not, and a reader who raises the standard flag to chase this error is fixing a flag that was never the problem.
A counting program's file most often already holds <vector> for the container and <iostream> for printing, and in the sweep probed here both fail plainly - so the file a reader actually has gets the bare version of the error, with nothing in the diagnostic pointing anywhere. 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 exactly five entries: <algorithm> itself, <execution>, <functional>, <regex>, and <valarray>.
The error: the include is missing, in a lean file
This program counts the even elements of a five-element array, using a free-function predicate, but includes only <limits>:
#include <limits>
bool isEven(int x) { return x % 2 == 0; }
int main() {
int a[5] = {2, 4, 5, 6, 8};
auto n = std::count_if(a, a + 5, isEven);
return n == 4 ? 0 : 1;
}
$ g++ -std=c++17 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:5:19: error: 'count_if' is not a member of 'std'
5 | auto n = std::count_if(a, a + 5, isEven);
| ^~~~~~~~
That is the whole output: one plain error line, and nothing after it. No suggestion tail rides on the error line itself, there are no note lines, and there are no help lines. The same program with no include at all fails the same way, just as bare, at the same call.
The textbook version fails too - plainly, with no suggestion to follow
The version most often written first - count the even elements of a vector with a lambda predicate and print the result - gets the same error, without <algorithm>, and for this name the error line carries no tail:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {2, 4, 6, 8, 10, 11, 12, 14, 16, 18};
auto n = std::count_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; });
std::cout << n << "\n";
return 0;
}
$ g++ -std=c++17 t_textbook.cpp -o t_textbook_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:5:19: error: 'count_if' is not a member of 'std'
5 | auto n = std::count_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; });
| ^~~~~~~~
Nothing follows the caret line: no suggested name, no marker line under the caret, no notes. That plainness is this name's own shape in every include context probed here - no header in the sweep below gives std::count_if any tail at all, on the include failure or anywhere else. The fix here is unchanged by any tail: the predicate count takes <algorithm>, and neither <iostream> nor <vector> rescues it - in the sweep probed here, the only headers that rescue std::count_if besides <algorithm> itself are <execution>, <functional>, <regex>, and <valarray>, and those rescues are properties of this library version, not rules of the language. A program that uses std::count_if includes <algorithm>, whether or not it happens to compile without it.
Neighboring headers: 101 fail plainly, none fail with a suggestion, and four rescue it
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 counts the even elements of a five-element array with a free-function predicate, the outcome splits by compile result and diagnostic shape two ways. 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 at all, 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 plainly the same way. No failing sole header prints a suggestion tail, a note line, or a help line anywhere in this sweep - the suggestion class that other names in this family have is empty for std::count_if.
A sole <vector> file shows the plain shape in full - and <vector> is the header this call is most often expected in, since the container being counted over comes from it. It is not there:
#include <vector>
bool isEven(int x) { return x % 2 == 0; }
int main() {
std::vector<int> v = {2, 4, 5, 6};
auto n = std::count_if(v.begin(), v.end(), isEven);
return n == 3 ? 0 : 1;
}
$ g++ -std=c++17 v_vector.cpp -o v_vector_prog
v_vector.cpp: In function 'int main()':
v_vector.cpp:5:19: error: 'count_if' is not a member of 'std'
5 | auto n = std::count_if(v.begin(), v.end(), isEven);
| ^~~~~~~~
And these headers rescue the program - it compiles and runs with no <algorithm> anywhere in the file:
<algorithm><execution><functional><regex><valarray>
Two contrasts with the neighboring sweeps are worth holding on to. First, the plain class is this name's only failing shape: there is no suggestion class and no link-failure shape in this sweep, so every failing sole header fails at compile time with the same bare error line - the diagnostic gives the reader nothing beyond the name and the fact that std does not have it. Second, the rescue set has no visible container or iterator shape: the rescues are two execution and functional headers, the regular-expression header, and <valarray> - headers that pull the algorithm header's internals in on the side, which is consistent with a name whose home is the general algorithms header, not the container header its ranges usually come from. <algorithm> - the home header - is on the rescue list by definition, not by transitivity, and the other four 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> v = {2, 4, 6, 8, 10, 11, 12, 14, 16, 18};
auto n = std::count_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; });
std::cout << n << "\n";
return 0;
}
$ g++ -std=c++17 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
9
One include, <algorithm>, declares the function - and the count reports 9: of the population 2, 4, 6, 8, 10, 11, 12, 14, 16, 18, every element but the 11 is even. The returned number is the whole result: there is no iterator to dereference and no position to read, just the total the predicate accepted.
The return value is the result: some, none, and all
The three outcomes of the call are three totals. A range where some elements qualify, a range where none do, and a range where all of them do:
#include <algorithm>
#include <iostream>
bool isEven(int x) { return x % 2 == 0; }
int main() {
int a[4] = {2, 4, 5, 6};
std::cout << std::count_if(a, a + 4, isEven) << "\n";
int b[3] = {1, 3, 5};
std::cout << std::count_if(b, b + 3, isEven) << "\n";
int c[3] = {2, 4, 6};
std::cout << std::count_if(c, c + 3, isEven) << "\n";
return 0;
}
$ g++ -std=c++17 r_return.cpp -o r_return_prog
$ ./r_return_prog
3
0
3
Read in order: over 2, 4, 5, 6 the call reports 3 - three of the four elements are even, and the count is only final after the last element has been tested, because counting never stops early. Over 1, 3, 5, where nothing is even, the return is 0 - an ordinary total, not an error and not a not-found marker; there is no end iterator in this function's contract to compare against. And over 2, 4, 6 the return is 3, the full length of the range. Code that uses this function reads that one number, and every value from zero to the range's length is an ordinary outcome.
The edge ranges: empty and one element
No short range is an error. A count over an empty range is zero, a one-element range whose element satisfies the predicate counts one, and a one-element range whose element does not counts zero:
#include <algorithm>
#include <iostream>
bool isEven(int x) { return x % 2 == 0; }
int main() {
int a[1] = {8};
std::cout << "empty " << std::count_if(a, a, isEven) << "\n";
std::cout << "one even " << std::count_if(a, a + 1, isEven) << "\n";
int b[1] = {7};
std::cout << "one odd " << std::count_if(b, b + 1, isEven) << "\n";
return 0;
}
$ g++ -std=c++17 k_empty.cpp -o k_empty_prog
$ ./k_empty_prog
empty 0
one even 1
one odd 0
Input iterators: the count runs on a stream
The iterator requirement is the weakest in this family: input iterators suffice, so the range does not need to be a container at all. This program counts the even values in a stream of integers read one at a time - a range that cannot be rewound and cannot be indexed:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
bool isEven(int x) { return x % 2 == 0; }
int main() {
std::istringstream in("2 4 5 6");
std::istream_iterator<int> first(in), last;
std::cout << std::count_if(first, last, isEven) << "\n";
return 0;
}
$ g++ -std=c++17 i_stream.cpp -o i_stream_prog
$ ./i_stream_prog
3
That is a range shape the forward-iterator algorithms in this family cannot run over at all: a stream iterator can be read once, in order, and std::count_if is specified to need nothing more - exactly one predicate application per element, in order, over the whole range.
Versions: this name has no second cause - it predates C++11
Unlike its newer neighbors, this call does not need a modern standard. The fixed programs in this guide are compiled as C++17, but the classic call has no version floor at all: with <algorithm> included, the same call compiled as C++98 compiles and runs, and compiled as C++11 it reports the same total:
#include <algorithm>
#include <iostream>
bool isEven(int x) { return x % 2 == 0; }
int main() {
int a[5] = {2, 4, 5, 6, 8};
std::cout << std::count_if(a, a + 5, isEven) << "\n";
return 0;
}
$ g++ -std=c++98 c_version98.cpp -o c_version98_prog
$ ./c_version98_prog
4
$ g++ -std=c++11 c_version98.cpp -o c_version98_prog
$ ./c_version98_prog
4
So this name's error has exactly one cause with exactly one fix, and the diagnostic cannot separate two causes because there is only one: a missing include fails plainly under any standard, and no standard flag rescues the call when the include is missing - while no file with the include present fails for the standard's reason. The newer pieces are separate overloads and uses layered on top of the classic call. The execution-policy overload is C++17, and it splits the include question in two: the overloads are declared by <algorithm>, but the policy objects live in <execution>, so a policy call with only <algorithm> included fails on the policy's name, not the algorithm's:
#include <algorithm>
#include <vector>
bool isEven(int x) { return x % 2 == 0; }
int main() {
std::vector<int> v = {2, 4, 5, 6};
auto n = std::count_if(std::execution::seq, v.begin(), v.end(), isEven);
return n == 3 ? 0 : 1;
}
$ g++ -std=c++17 p_policy_algoonly.cpp -o p_policy_algoonly_prog
p_policy_algoonly.cpp: In function 'int main()':
p_policy_algoonly.cpp:6:33: error: 'std::execution' has not been declared
6 | auto n = std::count_if(std::execution::seq, v.begin(), v.end(), isEven);
| ^~~~~~~~~
That wording - has not been declared, naming std::execution - is a different defect with a different missing header: add <execution> alongside <algorithm>, and the same call compiles and runs:
#include <algorithm>
#include <execution>
#include <iostream>
#include <vector>
bool isEven(int x) { return x % 2 == 0; }
int main() {
std::vector<int> v = {2, 4, 5, 6};
std::cout << std::count_if(std::execution::seq, v.begin(), v.end(), isEven) << "\n";
return 0;
}
$ g++ -std=c++17 p_policy_fixed.cpp -o p_policy_fixed_prog
$ ./p_policy_fixed_prog
3
And since C++20 the classic call is also usable in constant expressions - this program's count runs at compile time, and the assertion on its result is part of the build:
#include <algorithm>
#include <array>
constexpr bool isEven(int x) { return x % 2 == 0; }
constexpr long countEvens() {
std::array<int, 5> a = {2, 4, 5, 6, 8};
return std::count_if(a.begin(), a.end(), isEven);
}
static_assert(countEvens() == 4);
int main() { return 0; }
$ g++ -std=c++20 c_constexpr20.cpp -o c_constexpr20_prog
$ ./c_constexpr20_prog
The neighboring name: count is not in either
The sibling this function generalizes does not live in the container's header either. std::count - the value count, which returns the number of elements equal to a given value where std::count_if returns the number its predicate accepts - lives in <algorithm> alongside std::count_if, and it fails in a file whose only include is <vector> with its own plain error and no suggestion tail. It is shown here only as a neighbor, not re-taught:
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 4};
auto n = std::count(v.begin(), v.end(), 4);
return n == 2 ? 0 : 1;
}
$ g++ -std=c++17 b_count_vector.cpp -o b_count_vector_prog
b_count_vector.cpp: In function 'int main()':
b_count_vector.cpp:4:19: error: 'count' is not a member of 'std'
4 | auto n = std::count(v.begin(), v.end(), 4);
| ^~~~~
The neighbors share more than a home: std::count predates C++11 just as std::count_if does, so for neither name is a too-old standard a second cause - a C++98 file that includes <algorithm> and calls either one gets its count, as the versions section above shows for this guide's name.
The neighboring names, fixed
The two names together take the same one added header - <algorithm> covers both, and <vector> covers neither algorithm, only the container they count over. The fixed neighbor program reports 2 for the number of elements equal to 4 in 1, 2, 3, 4, 4:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 4};
std::cout << std::count(v.begin(), v.end(), 4) << "\n";
return 0;
}
$ g++ -std=c++17 b_count_fixed.cpp -o b_count_fixed_prog
$ ./b_count_fixed_prog
2
The split is the whole lesson of this family: the algorithms take <algorithm> and the container takes <vector>, the two headers are both needed by a complete counting program, and a file that has only the container's header gets the not-a-member error for either algorithm name - plainly for both, in every include context probed here.
A different wording - and an argument-dependent rescue that depends on every argument
Including <algorithm> is not the only half of the name, and for this call the unqualified form splits on all of its arguments - the range's iterators and the predicate's type alike. Over plain arrays with a free-function predicate, the unqualified call fails:
#include <algorithm>
bool isEven(int x) { return x % 2 == 0; }
int main() {
int a[5] = {2, 4, 5, 6, 8};
auto n = count_if(a, a + 5, isEven);
return n == 4 ? 0 : 1;
}
$ g++ -std=c++17 g_unqualified.cpp -o g_unqualified_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:5:14: error: 'count_if' was not declared in this scope; did you mean 'std::count_if'?
5 | auto n = count_if(a, a + 5, isEven);
| ^~~~~~~~
| std::count_if
In file included from /usr/include/c++/13/algorithm:73,
from g_unqualified.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:106:1: note: 'std::count_if' declared here
106 | count_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred);
| ^~~~~~~~
That wording - was not declared in this scope, with did you mean 'std::count_if'? 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::count_if; adding includes does not bring the name out of namespace std. (The note happens to point at the execution-policy overload's declaration in a library-internal header - the classic overload is declared in the same home header's internals; the note's target is a detail of this library's layout, not a second home for the name.) Swapping the free-function predicate for a lambda over the same plain array changes nothing - a lambda's type has no associated namespace either, and the call fails with the same wording:
#include <algorithm>
int main() {
int a[5] = {2, 4, 5, 6, 8};
auto n = count_if(a, a + 5, [](int x) { return x % 2 == 0; });
return n == 4 ? 0 : 1;
}
$ g++ -std=c++17 g_unqualified_lambda.cpp -o g_unqualified_lambda_prog
g_unqualified_lambda.cpp: In function 'int main()':
g_unqualified_lambda.cpp:4:14: error: 'count_if' was not declared in this scope; did you mean 'std::count_if'?
4 | auto n = count_if(a, a + 5, [](int x) { return x % 2 == 0; });
| ^~~~~~~~
| std::count_if
In file included from /usr/include/c++/13/algorithm:73,
from g_unqualified_lambda.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:106:1: note: 'std::count_if' declared here
106 | count_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred);
| ^~~~~~~~
Swap the array for a vector, and the same unqualified call compiles and runs:
#include <algorithm>
#include <vector>
bool isEven(int x) { return x % 2 == 0; }
int main() {
std::vector<int> v = {2, 4, 5, 6, 8};
auto n = count_if(v.begin(), v.end(), isEven);
return n == 4 ? 0 : 1;
}
$ g++ -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::count_if through them. But the container being a std type is not enough by itself - over a std::array, the same unqualified call with the same free-function predicate 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 isEven(int x) { return x % 2 == 0; }
int main() {
std::array<int, 5> a = {2, 4, 5, 6, 8};
auto n = count_if(a.begin(), a.end(), isEven);
return n == 4 ? 0 : 1;
}
$ g++ -std=c++17 g_unqualified_stdarray.cpp -o g_unqualified_stdarray_prog
g_unqualified_stdarray.cpp: In function 'int main()':
g_unqualified_stdarray.cpp:6:14: error: 'count_if' was not declared in this scope; did you mean 'std::count_if'?
6 | auto n = count_if(a.begin(), a.end(), isEven);
| ^~~~~~~~
| std::count_if
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:106:1: note: 'std::count_if' declared here
106 | count_if(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Predicate __pred);
| ^~~~~~~~
And the rescue can come from the last argument instead of the first two: keep the plain array, but make the predicate a std type - a std::function wrapping the same test - and the unqualified call compiles and runs, because the predicate argument now has std as an associated namespace:
#include <algorithm>
#include <functional>
int main() {
int a[5] = {2, 4, 5, 6, 8};
std::function<bool(int)> isEven = [](int x) { return x % 2 == 0; };
auto n = count_if(a, a + 5, isEven);
return n == 4 ? 0 : 1;
}
$ g++ -std=c++17 g_unqualified_stdfunc.cpp -o g_unqualified_stdfunc_prog
$ ./g_unqualified_stdfunc_prog
Every argument is a possible carrier and none is a guarantee: plain arrays and pointers carry nothing, a free function and a lambda carry nothing, a vector's iterators carry the name, a std::array's pointer iterators do not, and a std::function predicate carries it on its own. Write the qualifier either way.
The exact rule
std::count_ifis declared by#include <algorithm>, and by no other header by rule. In the sweep probed here, every one of the other 101 headers in the library's header set - among them<vector>,<iostream>,<numeric>,<string>, and<version>- does not declare it, and neither does a file with no include at all.'count_if' is not a member of 'std'has one cause. The missing-include cause: the<algorithm>include is missing and nothing else in the file pulled the name in - in the sweep probed here every failing sole header fails with one plain error line, no suggestion tail, no note lines, and no help lines, and no probed sole header produces a suggestion class, a note class, or a declared-but-not-defined link-failure shape for this name. There is no version cause: with the include present, the call compiles and runs as C++98.- In the sweep probed here, four headers besides
<algorithm>rescuestd::count_iftransitively -<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::count_ifin. - The result rules are the documented ones, as the fixed programs here show: the fixed program prints
9for the number of even elements in the population 2, 4, 6, 8, 10, 11, 12, 14, 16, 18; over 2, 4, 5, 6 the call prints3; over 1, 3, 5 it prints0, an ordinary total rather than a not-found marker; and over 2, 4, 6 it prints3, the range's full length. The returned number is compared against zero and against the range's length by the caller; there is no end iterator in the result. - The edge ranges are ordinary: counting an empty range returns zero, a one-element range whose element satisfies the predicate returns one, and a one-element range whose element does not returns zero - the edge program here prints
empty 0,one even 1, andone odd 0- because the return contract has a total for exactly those cases. - The classic
std::count_ifpredates C++11: with<algorithm>included it compiles and runs as C++98 and as C++11, reporting4both times for the even elements of 2, 4, 5, 6, 8, and it is usable in constant expressions since C++20. The execution-policy overload is C++17 and needs two headers:<algorithm>for the overload and<execution>for the policy objects - with only<algorithm>included, namingstd::execution::seqfails with'std::execution' has not been declared. - The range's iterators must meet the input-iterator requirements and the predicate must meet the predicate requirements; the predicate is applied exactly once per element. The stream program here shows the floor in action: over a stream of integers read one at a time, the count prints
3, the number of even values in 2, 4, 5, 6. std::countis not declared by#include <vector>: in the program here, which includes only<vector>, it gets its ownis not a member of 'std'error, plain, with no suggestion tail - and the fixed neighbor program, which includes both<algorithm>and<vector>, prints2for the value-count call.std::countpredates C++11 just asstd::count_ifdoes, so for that name a standard before C++11 is not a second cause either.'count_if' was not declared in this scopefor an unqualifiedcount_ifuse is a different defect: qualify the name asstd::count_if- the include alone does not bring the name out of namespacestd. For this name the unqualified call's fate depends on every argument: over plain arrays with a free-function predicate or a lambda it fails, as the first two unqualified programs above show; over a vector's iterators it is rescued through argument-dependent lookup, as the third shows, because those iterators arestdtypes; over astd::array's iterators it fails again, because in this library those iterators are plain pointers; and a predicate argument of astdtype rescues the plain-array call through the predicate argument, as the last program shows. Qualify the name either way.
The fixes, ranked
- First, check the include list of the file that names
std::count_if. If<algorithm>is not there, add it - that is the fix, and it holds whether the file failed in a lean context or in a textbook file, or happened to compile before through a transitive rescue. - Second, do not chase the standard flag for this name: with
<algorithm>included, the classic call compiles and runs as C++98, so a too-old standard is not a cause of this error - if the include is present and the error persists, the include is in a different file than the one naming the call, or the name is misspelled. The flag matters only for the pieces layered on top: C++17 for the policy overload, C++20 for constant-expression use. - Third, do not reach for
<vector>because the call counts over a container. In the sweep probed here,<vector>fails forstd::count_ifwith the plain error - the container and the algorithm live in different headers,std::vectorin<vector>andstd::count_ifin<algorithm>, and a complete program needs both. - Fourth, do not be reassured by
<iostream>being present - in the sweep here it fails too, just as plainly - and do not remove<algorithm>because a file compiles without it: in the probed set four headers rescue it, and those rescues are not guaranteed. - Fifth, if the error names
std::executioninstead -'std::execution' has not been declared- the missing header is the other one: add<execution>alongside<algorithm>. That error appears only on the policy overload's path; the classic call never names it. - Sixth, match the standard to the piece you are using beyond the classic call: the policy overload needs C++17, and constant-expression use needs C++20.
- Seventh, if the error says
was not declared in this scopeinstead, fix the name's qualifier tostd::count_ifas well - and do not rely on argument-dependent lookup to rescue the unqualified call, because a plain array, astd::array, a free-function predicate, and a lambda each remove that rescue in one shape or another. - Eighth, if the failing name is the neighboring one, the fix is the same header:
counttakes an added<algorithm>too - one header for both algorithm names, and<vector>for the container alone.
Which fix for which cause
#include <algorithm>missing,std::count_ifused in a file whose only include is one of the 101 headers listed as failing plainly in the sweep section above, or that includes nothing: the message is'count_if' is not a member of 'std', with no suggestion tail, no note lines, and no help lines - add#include <algorithm>.std::count_ifused in a textbook file including<iostream>and<vector>: the message is the same plain error, with no tail - add#include <algorithm>.std::count_ifused with only<algorithm>,<execution>,<functional>,<regex>, or<valarray>included: no error on this library version - add#include <algorithm>anyway when it is not the include already present, because the other four rescues are not guaranteed.std::count_ifused with<algorithm>included, at any standard from C++98 up: no version error - the classic call predates C++11, and the same file reports4compiled as C++98 and as C++11.std::count_ifcalled with an execution policy,<algorithm>included but<execution>not: the message is'std::execution' has not been declared- add#include <execution>alongside<algorithm>, and compile as C++17 or later.count_ifused without thestd::qualifier,<algorithm>included, over array arguments with a free-function predicate or a lambda, or over astd::array's iterators: the message is'count_if' was not declared in this scope; did you mean 'std::count_if'?- qualify the name asstd::count_if. Over a vector's iterators, or over plain arrays with astd::functionpredicate, the same unqualified call compiles through argument-dependent lookup - qualify it anyway, so the call does not depend on any argument's type.
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 check the standard flag for the classic call: this is one of the names where the flag is not a cause - C++98 is old enough for it, and only the layered pieces have floors, C++17 for the policy overload and C++20 for constant-expression use.
- Do not import the container's header into this error:
countandcount_iftake<algorithm>andstd::vectortakes<vector>, so there are two headers in a complete counting program, one for the algorithms and one for the container. - Do not assume the textbook includes rescue this name, and do not delete a working
<algorithm>line because a file compiles without it. In the sweep here<iostream>and<vector>do not rescuestd::count_ifat all, and the four headers that do rescue it do so only transitively. - Read the returned number as a total, not a position: zero is the no-match answer and the range's length is the all-match answer, and both are ordinary. The fixed programs here print
9for the ten-element count and3,0, and3for the return-value cases. - After the fix, confirm the program runs, not just that it compiles: the fixed programs here print
9for the ten-element count,3,0, and3for the return-value cases,4for the C++98 and C++11 calls,3for the policy call,3for the stream count, and2for the neighbor value-count call.
Related guides
- 'std::count' Is Not a Member of 'std' Fix - the same missing-include shape for the value-counting sibling in the same header:
std::countcompares elements against a value wherestd::count_ifapplies a predicate, the two names share the same home header and the same pre-C++11 age, and neither is declared by the container's header. - 'std::find_if_not' Is Not a Member of 'std' Fix - a neighboring search in the same header with the opposite version shape:
std::find_if_notreturns the first element its predicate rejects and is a C++11 name with a real version second cause, wherestd::count_ifreturns a total and predates C++11, so its error has the include as its only cause.
Sources
- cppreference,
std::count,std::count_if— forstd::count_ifbeing a function template defined in<algorithm>; for the classicstd::count_ifcarrying no since-version marker of its own, while the execution-policy overload is since C++17 and the classic call is constexpr since C++20; for the return being the number of elements for which the predicate returnstrue, of the range iterator's difference type; for the iterators needing to meet the input-iterator requirements, with the policy overloads' iterators meeting the forward-iterator requirements; for the predicate expression needing to be convertible tobooland not modifying its argument; and for the complexity being exactly one predicate application per element. - cppreference, Standard library header
<algorithm>— for<algorithm>being the header whose synopsis declaresstd::countandstd::count_if, the home-header fact both the fix and the neighbor 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.