Save Time, Space & a Little Sanity With std::function_ref
summary
std::function_ref is a non-owning type-erased reference for C++26 callables that stores a callable as a pair of pointers (two pointers), is trivially copyable, avoids allocations and RTTI, and trades ownership for speed and smaller binaries. Benchmarks compile and link generated code up to up to 3,000 callables and show huge code-size and compile-time expansion without type erasure, while function_ref keeps code size nearly flat and often improves runtime versus std::function. Watch for dangling references (string_view-style lifetimes), the non-type template parameter for member pointers, and the thunk + bound-entity implementation details.
AI-written summary Β· about
