std::function
is a versatile class template in the C++ Standard Library that can store and invoke callable objects, such as functions, lambdas, or functors. It provides a flexible way to work with different types of callables in a uniform manner.
Here's a simple example:
#include <functional> // Include for std::function
void myFunction() {
std::cout << "Hello, World!" << std::endl;
}
int main() {
std::function<void()> func = myFunction;
func(); // Output: Hello, World!
return 0;
}
std::function
is particularly useful when you need to pass functions or callbacks as arguments to other functions or store them for later use.