What are the differences between the new std::function_ref, that was added to C++26, and a "delegate"? Are they the same?
By "delegate" I mean:
sizeof(...) == 16.std::function_ref and this "delegate" are conceptually the same thing, a pointer to a "thunk" and a pointer to an object on which the function is called on, but std::function_ref allows you to constrain the const/volatile/noexcept type of the called function.
std::function_ref<void()> can point to any callable.std::function_ref<void() const noexcept> can only point to a callable with a const noexcept call operator (or member function), or noexcept free functions.constraining a function to be const is useful in multithreaded code to document that it will be called concurrently by multiple threads. (it doesn't replace proper documentation, but it will reduce mistakes)
constraining functions to be noexcept is useful to document that it will be called in a context that cannot propagate or catch exceptions, such as being called from other languages, or across C library boundaries.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With