C++
'std::function' Is Not a Member of 'std' Fix
std::function is a C++11 class template declared in the header
Written November 27, 2026 · Tested with GCC 13.3.0 · 7 min read
- The error: the include is missing
- The same error through neighboring headers: <vector>, <memory>, and <algorithm> are not enough
- The fix
- The same fix for a free function and a std::bind result
Written November 27, 2026
Tested with GCC 13.3.0
What this guide is
std::function is a general-purpose polymorphic function wrapper, available since C++11 and defined in the header <functional>. An instance can store, copy, and invoke any suitable callable target — a function, a lambda expression, a bind expression, or another function object. The same header also declares std::bind, the std::placeholders names, and std::bad_function_call, the exception thrown when an empty std::function is invoked.
Because the class template lives in one specific header, 'function' is not a member of 'std' has one common cause, and GCC's note under the error names it:
- The
#include <functional>line is missing. GCC adds a note naming the header:'std::function' is defined in header '<functional>'; did you forget to '#include <functional>'?
Other headers that sit near callback code — <vector>, <memory>, <algorithm> — do not rescue the program. Each one is tested below, and each fails the same way.
The error: the include is missing
This program stores a lambda in a std::function but includes only <iostream>:
#include <iostream>
int main() {
std::function<int(int)> f = [](int x) { return x * 2; };
std::cout << f(21) << "\n";
return 0;
}
$ g++ -std=c++17 noinclude.cpp -o noinclude_prog
noinclude.cpp: In function 'int main()':
noinclude.cpp:3:10: error: 'function' is not a member of 'std'
3 | std::function<int(int)> f = [](int x) { return x * 2; };
| ^~~~~~~~
noinclude.cpp:2:1: note: 'std::function' is defined in header '<functional>'; did you forget to '#include <functional>'?
1 | #include <iostream>
+++ |+#include <functional>
2 | int main() {
noinclude.cpp:3:19: error: expected primary-expression before 'int'
3 | std::function<int(int)> f = [](int x) { return x * 2; };
| ^~~
noinclude.cpp:4:18: error: 'f' was not declared in this scope
4 | std::cout << f(21) << "\n";
| ^
The default compilation mode prints the identical error, so this shape is not a language-mode problem. The two follow-on errors are consequences, not separate defects: because std::function was never declared, the template arguments could not be read and the variable f was never created, and all three errors disappear together when the include is added.
The same error through neighboring headers: <vector>, <memory>, and <algorithm> are not enough
Callback code often already includes a container, smart-pointer, or algorithm header, and those includes feel close enough. They are not. This program differs from the one above only in the added <vector> line, and it fails the same way:
#include <iostream>
#include <vector>
int main() {
std::function<int(int)> f = [](int x) { return x * 2; };
std::cout << f(21) << "\n";
return 0;
}
$ g++ -std=c++17 vectoronly.cpp -o vectoronly_prog
vectoronly.cpp: In function 'int main()':
vectoronly.cpp:4:10: error: 'function' is not a member of 'std'
4 | std::function<int(int)> f = [](int x) { return x * 2; };
| ^~~~~~~~
vectoronly.cpp:3:1: note: 'std::function' is defined in header '<functional>'; did you forget to '#include <functional>'?
2 | #include <vector>
+++ |+#include <functional>
3 | int main() {
vectoronly.cpp:4:19: error: expected primary-expression before 'int'
4 | std::function<int(int)> f = [](int x) { return x * 2; };
| ^~~
vectoronly.cpp:5:18: error: 'f' was not declared in this scope
5 | std::cout << f(21) << "\n";
| ^
The same result holds for the other two neighboring headers, with the same error and the same note naming <functional>:
- With only
<iostream>and<memory>included, the program fails with'function' is not a member of 'std'. - With only
<algorithm>and<iostream>included, the program fails with'function' is not a member of 'std'.
No standard header is required to declare std::function on behalf of <functional>. If a program compiles on one machine because some header happens to pull <functional> in as a side effect, that is an accident of that library version, not a rule to build on. Include <functional> in every file that names std::function, std::bind, or std::bad_function_call.
The fix
Add the header:
#include <functional>
#include <iostream>
int main() {
std::function<int(int)> f = [](int x) { return x * 2; };
std::cout << f(21) << "\n";
return 0;
}
$ g++ -std=c++17 good.cpp -o good_prog
$ ./good_prog
42
The same program also compiles and runs with -std=c++11, and with no -std flag at all. The signature inside the angle brackets, int(int), is the call signature the wrapper accepts — one int argument, returning int — and it stays the same whatever callable is stored in the wrapper.
The same fix for a free function and a std::bind result
The header fixes more than the lambda case, because <functional> is also where std::bind and the std::placeholders names are declared. This program stores a free function in one std::function and a std::bind result in another, and both call correctly:
#include <functional>
#include <iostream>
int twice(int x) { return x * 2; }
int add(int a, int b) { return a + b; }
int main() {
std::function<int(int)> f = twice;
std::cout << f(21) << "\n";
std::function<int(int)> g = std::bind(add, std::placeholders::_1, 1);
std::cout << g(41) << "\n";
return 0;
}
$ g++ -std=c++17 freefunc.cpp -o freefunc_prog
$ ./freefunc_prog
42
42
std::bind(add, std::placeholders::_1, 1) fixes the second argument of add at 1 and leaves the first as the placeholder _1, so the bound result fits the same int(int) signature as the lambda and the free function. One include supplies the wrapper, the binder, and the placeholder together.
The neighboring error: does not name a template type
Used as a return type in a declaration, the same missing include is worded differently:
#include <iostream>
std::function<int(int)> make();
int main() { return 0; }
$ g++ -std=c++17 variant1.cpp -o variant1_prog
variant1.cpp:2:6: error: 'function' in namespace 'std' does not name a template type
2 | std::function<int(int)> make();
| ^~~~~~~~
variant1.cpp:2:1: note: 'std::function' is defined in header '<functional>'; did you forget to '#include <functional>'?
1 | #include <iostream>
+++ |+#include <functional>
2 | std::function<int(int)> make();
The note underneath is the same, and so is the fix: add #include <functional>. When GCC cannot find the class template at all, it reports the failure in whatever grammatical role the name was playing — a member lookup inside a function body, or a type name in a declaration — so search your error output for the did you forget to '#include <functional>'? note rather than for one exact first line.
A different failure: calling an empty std::function
Once the include is fixed, a default-constructed std::function holds no target, and calling it is a runtime error, not a compile error. Invoking the target of an empty std::function throws std::bad_function_call, and the exception can be caught:
#include <functional>
#include <iostream>
int main() {
std::function<int(int)> f;
try {
std::cout << f(21) << "\n";
} catch (const std::bad_function_call& e) {
std::cout << "caught: " << e.what() << "\n";
}
return 0;
}
$ g++ -std=c++17 emptycall.cpp -o emptycall_prog
$ ./emptycall_prog
caught: bad_function_call
This program compiles cleanly — the declaration is present — so if your program builds and then fails with bad_function_call at run time, the include is not your problem. A target was never assigned, or was cleared, before the call. Assign a callable before invoking the wrapper, or test the wrapper in a condition first: an empty std::function converts to false.
The exact rule
std::function,std::bind,std::placeholders, andstd::bad_function_callare declared by#include <functional>, in C++11 mode or newer, and nowhere else.<iostream>,<vector>,<memory>, and<algorithm>do not declarestd::function.'function' is not a member of 'std'with the notedid you forget to '#include <functional>'?means the include is missing.- The follow-on errors
expected primary-expression before 'int'and'f' was not declared in this scopeare consequences of the first error, not separate defects. Fix the declaration and they disappear. - Calling an empty
std::functionis a separate, runtime failure: it throwsstd::bad_function_callinstead of returning a value.
The fixes, ranked
- First, read GCC's note under the error. It names the missing header directly.
- Second, add
#include <functional>to every file that namesstd::function,std::bind, orstd::bad_function_call— do not rely on another header pulling it in. - Third, if the program now compiles but throws
bad_function_callwhen run, stop editing includes: assign a callable to the wrapper before calling it, or check it in a condition first.
Which fix for which cause
#include <functional>missing,std::functionused inside a function: the message is'function' is not a member of 'std'— add#include <functional>.- Only
<vector>,<memory>, or<algorithm>included: same message, same note — add#include <functional>. std::functionused as a return type without<functional>: the message is'function' in namespace 'std' does not name a template type— add#include <functional>.- Program compiles, then prints or throws
bad_function_callat run time: no include change — assign a target before calling the wrapper.
Checklist
- Read the note under the error before changing anything.
did you forget to '#include <functional>'?settles the cause in one line. - Check the include list of the file that names
std::function, not just the project. The include must be in that file, or in a header that file includes directly by design. - Do not chase the
expected primary-expression before 'int'or'f' was not declared in this scopeerrors separately. They follow the failedstd::functiondeclaration and clear with it. - If the same file also uses
std::bindorstd::placeholders, the one<functional>include covers those names too — no second header is needed for them. - After the fix, confirm the program runs, not just that it compiles: the fixed program here prints
42. - Before calling a
std::function, make sure a target was assigned. A default-constructed wrapper is empty, and calling it throwsstd::bad_function_call.
Related guides
- C++23 std::expected Error-Handling Pipelines — another standard-library feature whose availability turns on the header and the language mode, not on the compiler's age alone.
- C++20 Modules with CMake 3.28 and Ninja — setting up a current GCC toolchain and build, the environment in which the C++11 and later library headers are available.
- C++ Expected Unqualified-Id Fix — a neighboring front-end error where the compiler's wording points at the wrong half of the defect.
Sources
- cppreference,
std::function— forstd::functionbeing a general-purpose polymorphic function wrapper defined in<functional>since C++11, for instances storing, copying, and invoking functions, lambda expressions, bind expressions, and other function objects, and for invoking an emptystd::functionthrowingstd::bad_function_call. - cppreference, Standard library header
<functional>— for<functional>declaringstd::function(C++11),std::bad_function_call(C++11),std::bind(C++11), and thestd::placeholdersnames (C++11).
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.