When advancing the state of an object, use of std::swap
works well for simple objects and pointer swaps. For other in place actions, Boost.ScopeExit
works rather well, but it's not terribly elegant if you want to share exit handlers across functions. Is there a C++11 native way to accomplish something similar to Boost.ScopeExit
but allow for better code reuse?
Using std::unique_ptr and lambdas to advance a state of an object
427 Views Asked by Sean At
1
There are 1 best solutions below
Related Questions in C++11
- Angular Show All When No Filter Is Supplied
- Using pagination on a table in AngularJS
- State with different subviews
- Getting and passing MVC Model data to AngularJS controller
- Implementing prerender.io middleware in sails.js
- Token based authorization in nodejs/ExpressJs and Angular(Single Page Application)
- AngularJS, Google App Engine and URLrewrite
- send data from table to another page into forms
- How to write tests for classes with inheritance
- angularJS sending OPTIONS instead of POST
Related Questions in UNIQUE-PTR
- Angular Show All When No Filter Is Supplied
- Using pagination on a table in AngularJS
- State with different subviews
- Getting and passing MVC Model data to AngularJS controller
- Implementing prerender.io middleware in sails.js
- Token based authorization in nodejs/ExpressJs and Angular(Single Page Application)
- AngularJS, Google App Engine and URLrewrite
- send data from table to another page into forms
- How to write tests for classes with inheritance
- angularJS sending OPTIONS instead of POST
Related Questions in SCOPEGUARD
- Angular Show All When No Filter Is Supplied
- Using pagination on a table in AngularJS
- State with different subviews
- Getting and passing MVC Model data to AngularJS controller
- Implementing prerender.io middleware in sails.js
- Token based authorization in nodejs/ExpressJs and Angular(Single Page Application)
- AngularJS, Google App Engine and URLrewrite
- send data from table to another page into forms
- How to write tests for classes with inheritance
- angularJS sending OPTIONS instead of POST
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular # Hahtags
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
(Ab)use
std::unique_ptr
's custom Deleters as aScopeExitVisitor
or Post Condition. Scroll down to ~7th line ofmain()
to see how this is actually used at the call site. The following example allows for eitherstd::function
or lambdas forDeleter
/ScopeExitVisitor
's that don't require any parameters, and a nested class if you do need to pass a parameter to theDeleter
/ScopeExitVisitor
.Which produces:
On the plus side, this trick is nice because:
std::unique_ptr
instances need to have an object assigned to them (e.g. it's perfectly acceptable to leave unneeded Deleters set tonullptr
)reset()
orrelease()
std::unique_ptr
(s) go out of scopeLastly, using
Boost.ScopeExit
you can forward calls to a helper function or use a conditional similar to what theBoost.ScopeExit
docs suggest withbool commit = ...;
. Something similar to:and there's nothing wrong with that, but like was asked in the original question, how do you share code without proxying the call someplace else? Use
std::unique_ptr
's Deleters asScopeExitVisitors
.