C++

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

std::function is a C++11 class template declared in the header , and nothing else declares it — not , not , not , and not . Read GCC's note under the error: it names the missing header directly, and the fix is that one include line.

Written November 27, 2026 · Tested with GCC 13.3.0 · 7 min read

By the end of this guide
  • 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>:

C++
#include <iostream>
int main() {
    std::function<int(int)> f = [](int x) { return x * 2; };
    std::cout << f(21) << "\n";
    return 0;
}
Terminal
$ 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:

C++
#include <iostream>
#include <vector>
int main() {
    std::function<int(int)> f = [](int x) { return x * 2; };
    std::cout << f(21) << "\n";
    return 0;
}
Terminal
$ 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:

C++
#include <functional>
#include <iostream>
int main() {
    std::function<int(int)> f = [](int x) { return x * 2; };
    std::cout << f(21) << "\n";
    return 0;
}
Terminal
$ 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:

C++
#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;
}
Terminal
$ 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:

C++
#include <iostream>
std::function<int(int)> make();
int main() { return 0; }
Terminal
$ 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:

C++
#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;
}
Terminal
$ 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, and std::bad_function_call are declared by #include <functional>, in C++11 mode or newer, and nowhere else. <iostream>, <vector>, <memory>, and <algorithm> do not declare std::function.
  • 'function' is not a member of 'std' with the note did 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 scope are consequences of the first error, not separate defects. Fix the declaration and they disappear.
  • Calling an empty std::function is a separate, runtime failure: it throws std::bad_function_call instead 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 names std::function, std::bind, or std::bad_function_call — do not rely on another header pulling it in.
  • Third, if the program now compiles but throws bad_function_call when 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::function used 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::function used 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_call at 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 scope errors separately. They follow the failed std::function declaration and clear with it.
  • If the same file also uses std::bind or std::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 throws std::bad_function_call.

Sources

  • cppreference, std::function — for std::function being 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 empty std::function throwing std::bad_function_call.
  • cppreference, Standard library header <functional> — for <functional> declaring std::function (C++11), std::bad_function_call (C++11), std::bind (C++11), and the std::placeholders names (C++11).

Keep Exploring