Skip to content

Feature Request: gsl::finally.ignore() #688

Open
@rianquinn

Description

@rianquinn

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:
microsoft/GSL#335 (comment)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions