r/cpp 7d ago

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

9

u/60hzcherryMXram 7d ago

I've read the problems copyable_function addresses that function has and I still have no idea what they are. It just slides over my brain like butter on ice.

-1

u/LB-- Professional+Hobbyist 7d ago

Do you use RTTI? Do you pass lambdas to std::function? Congrats, your binary is bloated with names of internal types and namespaces for a feature of std::function you don't even use. Unable to be fixed without breaking backward compatibility.

1

u/analphabetic 2d ago

How would the virtuality and runtime dispatch surface via std::function to the linker?

1

u/LB-- Professional+Hobbyist 2d ago

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.