Hasty Briefsbeta

Bilingual

C++26: More Function Wrappers

10 hours ago
  • #callable-wrappers
  • #type-erasure
  • #C++26
  • C++26 introduces std::copyable_function for a copyable and const-correct wrapper, and std::function_ref as a non-owning reference for callables.
  • std::function has issues with binary size and const-correctness, while std::move_only_function (C++23) fixed const-correctness but isn't copyable.
  • std::copyable_function allows qualifiers like const and noexcept in signatures, improving over std::function, and converts implicitly to std::move_only_function.
  • std::function_ref is a lightweight, non-owning wrapper similar to std::string_view, ideal for callback parameters with trivial copyability.
  • Guidelines: use std::function_ref for callbacks, std::move_only_function for non-copyable storage, std::copyable_function for copyable storage, and avoid std::function in new code.