std::function: a deep dive behind the curtain
summary
A concrete walkthrough shows how std::function implements type erasure: partial template specialization extracts return and argument types, a shared_ptr-based type erasure holds a polymorphic callable implementation, and copying semantics can end up sharing rather than cloning the target unless clone logic is added. Library comparisons reveal surprising sizes — MSVC reports roughly a six-pointer footprint while others use a smaller two‑pointer small‑buffer — and C++23’s move_only_function removes RTTI-heavy target access and supports move-only callables and emplace construction.
AI-written summary · about
chapters (40)
- 0:00Introduction
- 2:17Function signatures
- 4:19Function pointer
- 5:47Summary
- 6:20Why use std function
- 7:10Function pointers
- 8:08Instance method
- 9:06Objectoriented approach
- 10:33Objectiveended approach
- 11:56devils advocate
- 13:40task scheduler
- 15:33wrap up
- 17:09naive implementation
- 19:18call operator
- 21:28callable
- 27:33recap
- 28:03question
- 28:13comparison
- 30:00empty state
- 30:56compare pointer
- 32:03member function
- 33:01return type
- 33:48member pointers
- 34:27function signature
- 35:32member functions
- 36:14target type
- 37:39summary and comments
- 40:27standard mandates
- 41:28other implementations
- 42:43benchmarks
- 44:35size
- 44:59pointer size
- 48:41extensions
- 50:15multiple overloads
- 51:12different overloads
- 51:45more ideas
- 55:10moveonly function
- 57:53conclusion
- 58:41our company
- 59:04user group








