Interconverting Std:Function with Copyable_function
C++11 introduced std::function as a type-erased
callable-object holder. function was badly designed in a few ways: Its rarely used
“go fish” API bloats
every user; it advertises operator() const even when the controlled object’s operator()
is mutating; it handles what should be a precondition violation by throwing an exception,
which again bloats every user and means its operator() can never be noexcept.
So C++26 fixed these problems by adding
std::copyable_function.
(Which preserves some of function’s
suboptimal design decisions: copyable_function<bool()> remains implicitly convertible
to copyable_function<void()>, contextually convertible to bool, and assignable
from nullptr.)
callable-object holder. function was badly designed in a few ways: Its rarely used
“go fish” API bloats
every user; it advertises operator() const even when the controlled object’s operator()
is mutating; it handles what should be a precondition violation by throwing an exception,
which again bloats every user and means its operator() can never be noexcept.
So C++26 fixed these problems by adding
std::copyable_function.
(Which preserves some of function’s
suboptimal design decisions: copyable_function<bool()> remains implicitly convertible
to copyable_function<void()>, contextually convertible to bool, and assignable
from nullptr.)
Summary
C++26 introduces std::copyable_function to address the shortcomings of std::function, such as its cumbersome API and exception handling. This new type aims to improve usability while ensuring better performance and safety for developers.
Comments (0 of ) ⌛