What is std::function and what overhead does it introduce compared to a lambda or function pointer?

Last updated 21 June 2026

  • std::function is a type-erased wrapper that can hold any callable (lambda, function pointer, functor) matching one signature, e.g. std::function<int(int)>.

  • Overhead: an indirect call that usually blocks inlining, possible heap allocation for large captures (small ones use a small-buffer optimization), and a bigger object to copy.

  • Lambdas and templates keep the concrete type and inline for zero overhead. Function pointers are cheap but stateless. Use std::function only when you must store differing callables behind one interface.

  • https://topictrick.com/blog
  • https://stackoverflow.com/questions/67615330/why-is-stdfunction-too-slow-is-it-that-the-cpu-cant-use-instruction-reorderi
All C++ interview questions