std::function: a deep dive behind the curtain
summary
Explains how std::function stores and invokes callables: a naive implementation uses a virtual call interface and shared_ptr clone semantics, real libraries add small-buffer optimization to avoid heaps, and C++23 introduces std::move_only_function (no RTTI via target_type RTTI). Compares implementations (libstdc++, libc++, MSVC, Folly, function2), reports pointer counts varying from two up to eight-plus, shows member-pointer handling and surprising ABI quirks (MSVC has smallest member-function pointer yet a large function object), and demonstrates when allocations actually occur.
AI-written summary · about
chapters (8)
- 0:00Introduction and context
- 1:25Function fundamentals
- 6:18Why use std::function
- 16:53Building a custom function
- 27:56Standards and features
- 40:25Benchmarking and design
- 54:46Move-only functions and wrap-up
- 1:00:47Q&A and final tricks
related talks
01h 05mIs std::function really the best we can do?
01h 01mCalling Functions: A Tutorial
06mmy favorite C++ feature
48mWhat is a static function but isn't a static function
49mThe Hidden Performance Price of C++ Virtual Functions
01h 03mHidden Overhead of a Function API
01h 09mFunctional Programming in C++
59mTaming Lambdas' uniqueness
