A function pointer in C++ is a variable that holds the address of a function, so you can call that function indirectly, pass it as an argument or store it in a table. The type includes the return type and parameter types, for example void (*fn)(int).
Function pointers are the oldest callback mechanism in C++. Modern code often prefers std::function, lambdas or templates, but plain function pointers remain important for C APIs, low-level code and zero-overhead callbacks. This article shows the syntax, aliases, callbacks, member function pointers and the modern alternatives.
Wrap the name in (*name) to declare a pointer to a function rather than a function returning a pointer:
A function name converts to a pointer automatically, so & and * are optional. The pointer type must match the function signature. Since C++17, noexcept is part of the function type, so a noexcept function pointer can be assigned to a plain one, but not the reverse.
Use an alias instead of raw declarator syntax:
apply(add, 4, 5) returns 9 and apply(mul, 4, 5) returns 20. Prefer using over typedef, because it reads left to right and also works for templates.
Pass a function pointer to let callers decide behaviour, or build a table to replace a switch:
The table is constexpr, so it needs no runtime initialisation.
A pointer to a member function is a different type and needs an object to call it. The syntax uses ClassName::*, and you call it with .* or ->*:
You cannot assign a member function pointer to an ordinary function pointer. For member functions, std::invoke (C++17) gives a uniform call syntax for function pointers, member pointers and callable objects.
| Tool | Holds | Cost | Use when |
|---|---|---|---|
| Function pointer | Free or static function only | Smallest, no allocation | C interop, simple callbacks |
| Template parameter | Any callable | Inlined at compile time | Algorithms such as std::sort |
std::function | Any copyable callable | Type erasure, may allocate | Storing callbacks with state |
std::move_only_function (C++23) | Any callable, including move-only | Type erasure | Storing move-only callbacks |
std::function_ref (C++26) | Non-owning reference to a callable | Very small | Passing a callback without storing it |
A captureless lambda converts implicitly to a function pointer, which lets you pass one to a C-style API:
A lambda that captures variables does not convert, because the pointer has nowhere to keep the state.
nullptr function pointer, which is undefined behaviour. Check optional callbacks first.void *fn(int), which declares a function.extern "C" when the API requires it.A function pointer holds only a function address. std::function can hold any callable, including lambdas with captures, at the cost of type erasure.
Yes, but the target type selects the overload. int (*p)(int) = f; chooses the f(int) overload.
You can compare them for equality with == and !=. Relational comparisons like less than are not meaningful.
Yes for C APIs and simple stateless callbacks. For stateful callbacks, use templates, lambdas or std::function.
A function pointer stores the address of a function and lets you call it indirectly. Use using aliases for readability, pointers to member functions for class methods and modern wrappers when you need state or ownership. Captureless lambdas convert to function pointers when an API requires one.