isocpp / isocpp/CppCoreGuidelines
Feature Request: gsl::finally.ignore()
@gdr-at-ms is already working on this.
Since Aug 22, 2016.
- Dominant language
- CSS
- Stars
- 45.3k
- Forks
- 5.6k
- PR merge metrics
- No merged PRs in 30d
Description
C++ exceptions are amazing, but they introduce an issue with how to handle state. The commit / rollback pattern is amazing at handling this:
http://stackoverflow.com/questions/10983791/c-transaction-like-pattern-for-all-or-nothing-work
Basically, it allows you to do the following:
auto cor1 = CommitOrRollback([]{
undo action1
});
action1();
auto cor2 = CommitOrRollback([]{
undo action2
});
action2();
cor1.commit();
cor2.commit();
gsl::finally is exactly the same thing, but is missing the "commit" function. Since the "finally" operation is sort of backwards, I propose calling it "ignore" and can be implemented doing the following (tested and already working in our repo).
void ignore() noexcept { invoke_ = false; }
This added API would not affect how it is currently used, but would add the ability to prevent the action from executing based on a condition, which means it would be able to be used for rolling back state in the event of an exception, something that IMO should be integral to the C++ stack.
Also on the GSL tracker:
https://github.com/Microsoft/GSL/issues/335#issuecomment-241073023
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.