isocpp / isocpp/CppCoreGuidelines

Feature Request: gsl::finally.ignore()

Open
#688 44 comments 0 reactions 1 assignee View on GitHub

@gdr-at-ms is already working on this.

Since Aug 22, 2016.

open
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.