The Hidden Performance Trap When Mixing std::function and copyable_function

Interconverting std::function with copyable_function

The Hidden Performance Trap When Mixing std::function and copyable_function

I discovered a critical performance pitfall in C++26 where interconverting std::function and std::copyable_function creates a growing chain of callables. This 'logical memory leak' causes invocation latency to increase linearly with every conversion, even if the underlying logic remains unchanged. While libstdc++ currently exhibits this behavior, I advise developers to avoid mixing these types in brown-field codebases to prevent severe runtime degradation.

Personally I would say that you ought to abandon both, and write your own type-erased callable instead. It takes less than 100 lines!

More from this day

2026-07-29