Hasty Briefsbeta

Bilingual

Interconverting std::function with copyable_function

3 days ago
  • C++11's std::function has design flaws: bloated API, const operator() despite mutable callables, and exception handling preventing noexcept.
  • C++26 introduces std::copyable_function to fix these issues while retaining some suboptimal designs like implicit conversions and nullptr assignability.
  • copyable_function and function are interconvertible, but naive implementations can create a 'linked list' of callables, degrading performance over time.
  • libstdc++ currently shows O(n) call overhead as wrappers nest, while Microsoft STL avoids this issue and libc++ hasn't implemented yet.
  • Mixing copyable_function and function in brown-field code can cause logical memory leaks and slower execution, especially with repeated conversions.
  • Recommendation: avoid mixing both types in the same codebase; write custom type-erased callables for new code or consistently use one type.