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
454 views Asked by Sean At
1
There are 1 answers
Related Questions in C++11
- lvalue and rvalue references
- c++ range-for loop over custom class ::begin() expects 1 argument, 0 provided
- Difference between INT_MIN , INT8_MIN , INT16_MIN. For MAX too
- I am getting segmentation failt while assigning the resourcemanager instance
- Prevent reordering of prefetch instruction in c++
- How to Use libcurl to Check HTTP/S Proxy?
- Why we use `class` when there's `struct` in C++?
- Memory Management in C++: Differences in allocating shared_ptr using new vs make_shared
- Does C++ range-based `for` make copies?
- Is the behaviour is determined when initliasing the inner class's static member variable's value equal to the outer class's static member variable?
- Question about initialization. The output must be zeros with C++11 and afterwards?
- How to replace non-standard "for each" received from Visual C++ users
- G ++ can not pass the parameters in using the C ++ 11 process library under Windows?
- Why the Variadic Constructor with std::is_constructible Fails to Handle Initializer List Initialization?
- Class Object Error 'Undefined Reference For'
Related Questions in UNIQUE-PTR
- How can std::unique_ptr apply EBO on closure?
- how to overload unique_ptr in a class . i am getting compilation error
- unordered_map of class following pImpl idiom using unique_ptr
- Question on unique_ptr behavior w.r.t particular usage of std::move
- Is it safe for a method to take a unique_ptr to this?
- Did the rules for nullptr init of unique_ptr change in C++23?
- Is it possible to initialize elements of a unique_ptr array?
- shared_ptr and unique_ptr: question about a specific case
- Shared pointer with predefined maximum use count
- C++: std::unique_prt + boost -- undefined reference in make_unique
- Trouble constructing a map with immovable value types from initializer list
- In pimpl design using std::unique_ptr, if dtor is put in implementation file BEFORE Impl type definition, why is it compiling ok?
- Would the removal of "const" in dcl.init.list#5 cause havoc in the standard?
- emplace unique_ptr in list
- How to deal with libraries requiring unique_ptr as inputs in pybind11?
Related Questions in SCOPEGUARD
- C++ scope guard with zero overhead
- Performance of golang style defer scope guard in C++
- Is a lambda-expression that only captures by reference guaranteed not to throw?
- Is there any way to extend the lifetime of a temporary object in C++?
- How to avoid warning when using scope guard?
- C++ : other one simple scope guard
- C++: why this simple Scope Guard works?
- Will there be standardization of scope guard/scope exit idioms?
- Difference between ScopeGuard11 and Boost.ScopeExit - just backwards compatibility?
- ScopeGuard usage with multiple resourace allocatons and exit points in a fucntion
- Using std::unique_ptr and lambdas to advance a state of an object
- Life extension of temporary by const reference
- Who copies the return value of a function?
- Why can't Alexandrescu use std::uncaught_exception() to implement SCOPE_FAIL in ScopeGuard11?
- RAII wrapper for function pairs and template specialization
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?
Popular Tags
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)
(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.