-
Notifications
You must be signed in to change notification settings - Fork 743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow final action to invoke throwing actions #1192
base: main
Are you sure you want to change the base?
Conversation
where an action is provided to final action to execute at end of scope and that action throws, because final action's destructor is marked noexcept, the program will terminate. Instead, allow final action's destructor to be noexcept dependent upon whether the action throws.
Note when discussing this with a few others, there was some pushback regarding adherence to guidelines that destructors must not throw. the spirit of the guideline is to prevent half-baked objects, and throwing during stack unwind. As well, while the destructor being noexcept may achieve stack-unwind termination in a more direct manner, even if final action should invoke a throwing action during stack-unwind, the runtime is still required to terminate: It is arguable whether a termination from an explicit noexcept or stack-unwind throw is better or worse; however I believe a minor nit during already-terminating scenario should not come at the expense/prevention of legitimate code being executed and instead terminating due to an artificial constraint. In other words, with this change, a final action which throws during stack unwind will still terminate as before; but a final action which throws during normal destruction will now be allowed to work, improving the usefulness of a gsl::finally as a general purpose utility to invoke code at scope exit. |
@microsoft-github-policy-service agree company="Microsoft" |
Note I have companion PRs here: these are not additive to this PR but rather alternatives to this PR to determine which the community best likes. In all 3 PRs the same problem we want to address is that gsl finally makes it too easy to write code that would cause surprise crashes at runtime. |
pending discussion of #1193 |
also as a safer existing implementation, see fundamentals TS scope_success: https://cplusplus.github.io/fundamentals-ts/v3.html#scopeguard.exit |
where an action is provided to final action to execute at end of scope and that action throws, because final action's destructor is marked noexcept, the program will terminate. Instead, allow final action's destructor to be noexcept dependent upon whether the action throws.