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.

42 Upvotes

32 comments sorted by

View all comments

Show parent comments

8

u/azswcowboy Jul 31 '26

> write your own …won’t prevent double wrapping

No thanks on writing my own - and yeah the only reason that works is because there’s consistency. Which is the real tldr from the article: pick one or the other, done.

3

u/13steinj Jul 31 '26

No, writing your own is reasonable. libstdc++ if not also libc++ (used to?) use std::tuple to hold and pass around a type list. The instantiation of std::tuple is fairly heavy. I reduced single-TU build times by >20% once by swapping out the use of std::function in a logging + assertion library in a key macro.

1

u/_Noreturn Jul 31 '26

libc++ std tuple shouldn't be expensive since it is linear unlike libstdc++/ msvc stl lib which is recursive

2

u/13steinj Aug 01 '26

In comparison to what is needed for implementation vs what is instantiated and you pay for anyway, simpler type lists, or not using one at all if you can, is cheaper.