std::function — type-erased callable wrapper.
1// Stores any callable with matching signature2std::function<int(int, int)> op;34op = [](int a, int b) { return a + b; };5op = std::bind(std::plus<int>{}, std::placeholders::_1, 2);67int result = op(3, 4); // 7
vs templates:
1// Template: compile-time polymorphism (faster)2template <typename F>3void call(F f) { f(); }45// std::function: runtime polymorphism (flexible)6void call(std::function<void()> f) { f(); }
When to use:
Alternative: Lambda with auto for zero-overhead.