#functional
2 posts tagged #functional.
std::function_ref borrows a callable instead of owning it
C++26 adds std::function_ref, the non-owning counterpart to std::function. It is the right parameter type for a callback you will invoke but not store: it copies nothing, allocates nothing, and binds equally to a capturing lambda or a plain function. It is to std::function what string_view is to string.
Converting between std::function and copyable_function nests them
std::function and std::copyable_function are both type-erased callable wrappers and neither recognises the other. Converting between them does not unwrap and rewrap the lambda inside; it wraps the whole previous wrapper. Round-trip in a loop and every call walks a chain of indirections. In this run, 200 round trips made the same calls several hundred times slower.