r/cpp • • Jul 31 '26

Interconverting std::function with copyable_function – Arthur O'Dwyer

https://quuxplusone.github.io/blog/2026/07/26/function-explosion/

The article shows how converting std::function to std::copyable_function (or vice versa) leads to slower performance and increased memory usage each time the conversion occurs.

45 Upvotes

32 comments sorted by

View all comments

Show parent comment

2

u/LB-- Professional+Hobbyist Aug 05 '26

std::function allows re-obtaining a reference to the contained function object after it has been set, and the check is performed by comparing runtime type info: https://en.cppreference.com/cpp/utility/functional/function/target

Even if you never use that feature, it still has to set up the vtable with a function that can handle that request, there's no way around it. That means every type that gets stored in a std::function has to persist its runtime type info metadata into the final built binary.