std::function
Brief​
std::function
is a simple yet powerful type. It can basically store any function: this allows you to pass functions as parameters to other functions, or to store a function for later use.
#include <functional>
std::vector<std::function<void()>> callbacks; // A list of functions that we will call when some special event happens.
A std::function
can be a lambda:
callbacks.push_back(
[]() { std::cout << "Lambda\n"; }
);
or a function pointer:
void my_callback() {
std::cout << "Function pointer\n";
}
callbacks.push_back(
&my_callback
);
or a function object:
struct MyCallback {
void operator()() const {
std::cout << "Function object\n";
}
};
callbacks.push_back(
MyCallback{}
);
We would than use our callbacks
like so:
for (const auto& callback : callbacks) {
callback();
}
Signature​
std::function
stores functions with a given signature. For example std::function<int(float)>
is a function that takes a float
and returns an int
.