Menu

1 / 4

Interconverting std::function with copyable_function

Hacker News·4 months ago
#tmgsIBSf
Long article (1205 words) — no AI summary yet.
Reading 0:00
15s threshold

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 .)

P2548 “ copyable_function ” (Hava 2023) contains this guidance for implementors:

It is recommended that implementors do not perform additional allocations when converting from a copyable_function instantiation to a compatible move_only_function instantiation, but this is left as quality-of-implementation.

Note that converting…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.