std::function_ref
| Defined in header <functional>
|
||
template< class... >
class function_ref; // not defined
|
(1) | (since C++26) |
template< class R, class... Args >
class function_ref<R(Args...)>;
template< class R, class... Args >
class function_ref<R(Args...) noexcept>;
template< class R, class... Args >
class function_ref<R(Args...) const>;
template< class R, class... Args >
class function_ref<R(Args...) const noexcept>;
|
(2) | (since C++26) |
Class template std::function_ref is a non-owning function wrapper. A std::function_ref object refers to a Callable target, such as a function, a lambda expression or another function object, and calls it with the signature given as its template argument. It does not copy or own the target and never allocates memory, so the target does not need to be copyable or movable.
A pointer to member function or pointer to data member cannot be the target. To call a member function, or any function known at compile time, pass it as a std::constant_wrapper, as in std::cw<&S::f>, optionally followed by the object to call it on; see constructors.
The template argument can be a function type with a const qualifier, a noexcept specifier, or both. With const, the target is called as a const object. With noexcept, only targets whose calls do not throw are accepted, and operator() is noexcept.
Every specialization of std::function_ref is a TriviallyCopyable type that satisfies copyable.
Member types
| Member | Definition |
BoundEntityType (private)
|
unspecified TriviallyCopyable type that satisfies copyable and is capable of storing a pointer to object value or pointer to function value (exposition-only member type*) |
FunctionPointerType (private)
|
R(*)(BoundEntityType , Args&&...) noexcept(noex )
where noex is |
Data members
| Member | Definition |
BoundEntityType bound-entity
|
a bound entity object (exposition-only member object*) |
FunctionPointerType thunk-ptr
|
a stored pointer to function (exposition-only member object*) |
Member functions
constructs a new function_ref object (public member function) | |
assigns a function_ref (public member function) | |
invokes the stored thunk of a function_ref (public member function) |
Deduction guides
Notes
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_function_ref |
202306L |
(C++26) | std::function_ref
|
202511L |
(C++26) | std::nontype renamed to std::constant_arg
| |
202603L |
(C++26) | Construction from std::constant_wrapper instead of std::constant_arg
| |
202604L |
(C++26) | Construction from another function_ref with a compatible signature copies its target
|
function_ref is intended for function parameters that take a callable, such as a callback or a predicate, which the function calls before it returns. It is cheap to pass by value, and the function that takes it does not need to be a template, so it can be compiled separately. A temporary target, such as a lambda expression in the argument list, lives until the end of the full-expression that contains the call, so it can be used for the whole call.
A function_ref that outlives its target dangles. For example, after std::function_ref<int()> r = [] { return 1; };, r refers to a closure object that has already been destroyed. For this reason, a function_ref can be assigned only from a function pointer, a constant_wrapper, or a function_ref with the same or a compatible signature, not from a lambda expression or another function object. A function pointer is stored by value, so a function_ref constructed from a function or function pointer does not depend on the lifetime of that pointer.
A function_ref always has a target: it has no default constructor and cannot be compared with nullptr.
Constructing a function_ref from another specialization of function_ref whose signature is compatible in the way function pointers are, for example a std::function_ref<void() noexcept> to a std::function_ref<void()>, copies the target of the other function_ref. With any other signature, such as a std::function_ref<int()> to a std::function_ref<void()>, it is unspecified whether the new function_ref refers to that target or to the other function_ref object, which dangles if that object is a temporary.
Example
A function that takes its callback as a function_ref, called with a lambda expression, a function, and a member function bound to an object.
#include <functional>
#include <optional>
#include <print>
#include <string>
#include <type_traits>
#include <utility>
using result = std::optional<std::string>;
// Not a template, so it can be compiled separately, and it accepts any
// callable with a matching signature, even one that cannot be copied
result retry(int times, std::function_ref<result()> action)
{
for (int i = 0; i != times; ++i)
if (result r = action())
return r;
return std::nullopt;
}
result offline() { return std::nullopt; }
struct server
{
int busy_for = 2;
result fetch()
{
if (busy_for-- > 0)
return std::nullopt;
return "data from server";
}
};
int twice(int x) { return 2 * x; }
int square(int x) { return x * x; }
int main()
{
// A lambda: the closure object lives until the end of the full-expression
int calls = 0;
result r = retry(3, [&calls]() -> result
{
if (++calls < 2)
return std::nullopt;
return "data from lambda";
});
std::println("{} after {} calls", r.value_or("nothing"), calls);
// A function
std::println("{}", retry(3, offline).value_or("nothing"));
// A member function bound to an object
server s;
r = retry(5, {std::cw<&server::fetch>, s});
std::println("{}", r.value_or("nothing"));
// A function_ref can be rebound to a function, but not to a lambda,
// which would be destroyed while the function_ref still refers to it
std::function_ref<int(int)> op = twice;
std::println("op(5) = {}", op(5));
op = square;
std::println("op(5) = {}", op(5));
auto lambda = [](int x) { return x + 1; };
static_assert(!std::is_assignable_v<decltype(op)&, decltype(lambda)>);
}
Output:
data from lambda after 2 calls
nothing
data from server
op(5) = 10
op(5) = 25
See also
(C++11) |
copyable wrapper of any copy constructible callable object (class template) |
(C++26) |
copyable wrapper of any copy constructible callable object that supports qualifiers in a given call signature (class template) |
(C++23) |
move-only wrapper of any callable object that supports qualifiers in a given call signature (class template) |
(C++26)(C++26) |
compile-time constant of any structural type, passed as a function argument (class template) |