On 12/5/23 12:17, Julien Blanc wrote:
Le 2023-12-01 15:42, Andrey Semashev via Boost a écrit :
Besides, there isn't an efficient way to prevent the user from this issue. Sure, I could specialize scope guards on std::function (at the cost of including <functional>, which would penalize every user, even those who never wanted to use std::function to begin with), but it doesn't prevent users from using boost::function or any other equivalent.
Actually, i was thinking of something far more radical. Something like requiring is_trivially_copyable<F>. From a theoric point of a view, it is clearly not the correct trait, but there's just no way to tell the compiler that we need the initializing expression to be noexcept. In practice, it does the job quite well at preventing bad usage, while still preventing not too much (in our code base, i can't recall of any case where this caused us trouble).
Requiring function objects to be trivially copyable is definitely going too far, as it prohibits binding most objects by value, even if such binding would not throw. For example, you wouldn't be able to bind smart-pointers, std::string or std::optional. It is possible to require the function object initialization to be noexcept, but requiring it would prohibit the use cases where throwing would be fine anyway.