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
453 Views Asked by Sean At
1
There are 1 best solutions below
Related Questions in C++11
- C++ using std::vector across boundaries
- Using QPointer and QObject::connect with C++11
- Using std::vector<> and std::shared_ptr<> should cause error
- invoking function for each variadic template arguments and passing the result as constructor arguments
- Different behavior of async with Visual Studio 2013(Windows8.1) and GCC 4.9(Ubuntu14.10)
- Whether to use T const& or T&&
- C++ IRC Bot Buffer Error
- Downcast from a container of Base* to Derived* without explicit conversion
- Assigning values in a vector in non-sequential order
- Can I use C++11 list-initializer syntax for vectors with variables?
- is it fine to use auto keyword in function parameter?
- Variadic template method and std::function - compilation error
- Clustering on Graph (using Boost Graph Library)
- libc++ difference between vector::insert overloads
- Cannot convert argument1 to const char
Related Questions in UNIQUE-PTR
- asio lambda with unique_ptr capture
- linked list with unique pointers being used as type
- Turning this raw pointer situation into a unique_ptr?
- Dereferencing a temporary unique_ptr
- (Dangling?) Reference returned from function does not "work"
- unique_ptr::get() function with virtual and non-virtual function
- Move a unique_ptr with custom deleter to a shared_ptr
- No op delete for unique_ptr
- How can I pass std::unique_ptr into a function
- C++ static_cast and dynamic_cast of polymorphic classes using unique_ptr
- Vector of buffers in C++
- Why does make_unique of a struct fail when containing an std::promise as member?
- Is it safe to move unique_ptr from a container and erase it?
- Smart pointers - unique_ptr for a stack-allocated variable
- singly linked list push function error
Related Questions in SCOPEGUARD
- How to avoid warning when using scope guard?
- C++: why this simple Scope Guard works?
- C++ : other one simple scope guard
- Will there be standardization of scope guard/scope exit idioms?
- Is a lambda-expression that only captures by reference guaranteed not to throw?
- RAII wrapper for function pairs and template specialization
- Using std::unique_ptr and lambdas to advance a state of an object
- The simplest and neatest c++11 ScopeGuard
- ScopeGuard dismiss
- Dynamically created scope guards
- Is there any way to extend the lifetime of a temporary object in C++?
- Who copies the return value of a function?
- Difference between ScopeGuard11 and Boost.ScopeExit - just backwards compatibility?
- const reference for temporary lifetime lengthening
- Why can't Alexandrescu use std::uncaught_exception() to implement SCOPE_FAIL in ScopeGuard11?
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 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 aScopeExitVisitoror 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::functionor 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_ptrinstances 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.ScopeExityou can forward calls to a helper function or use a conditional similar to what theBoost.ScopeExitdocs 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.