std::function — general-purpose function wrapper. Stores any callable.
1#include <functional>23// Different callables4std::function<int(int, int)> f;56// Lambda7f = [](int a, int b) { return a + b; };89// Function pointer10f = [](int a, int b) -> int { return a * b; };1112// Functor13struct Adder {14 int operator()(int a, int b) const { return a + b; }15};16f = Adder{};1718std::cout << f(2, 3); // 51920// Check if empty21if (f) { f(1, 2); }2223// Reset24f = nullptr;
Performance: