Questions tagged [scopeguard]

22 questions
1
vote
2 answers

C++ scope guard with zero overhead

In C++ we can ensure foo is called when we exit a scope by putting foo() in the destructor of a local object. That's what I think of when I head "scope guard." There are plenty of generic implementations. I'm wondering—just for fun—if it's possible…
nebuch
  • 6,475
  • 4
  • 20
  • 39
0
votes
1 answer

Performance of golang style defer scope guard in C++

After reading this question on implementing Go's defer in C++: golang-style "defer" in C++ I had a question on the performance of the go-defer like guard clause in given in one of the answers. It uses a shared_ptr deleter which ignores the passed…
Gonen I
  • 5,576
  • 1
  • 29
  • 60
0
votes
3 answers

Scopeguard and parameters by reference

In this article in the Supporting Parameters by Reference section, they point out the problem with and provide the solution to references. My question is: why don't they just declare the parameters as references in the first place? I.e., instead…
Paul J. Lucas
  • 6,895
  • 6
  • 44
  • 88
0
votes
2 answers

ScopeGuard dismiss

My code needs scope guards, however do I have to manually Dismiss() all the scope guards on exit from a function normally? i.e. void Deleter(MyClass* obj) { delete obj; } MyClass* Func() { MyClass* obj = new MyClass(); ScopeGuard sg1 =…
nakiya
  • 14,063
  • 21
  • 79
  • 118
0
votes
2 answers

C++ : other one simple scope guard

Lets ask you about this simple scope guard: template struct finop_t { T& t; ~finop_t() { t(); } }; #define FINALLY__(l, cl) \ auto FIN ## l ## clo = cl; \ finop_t FIN ## l ## fin { FIN ## l ##…
funny_falcon
  • 427
  • 3
  • 11
0
votes
1 answer

ScopeGuard usage with multiple resourace allocatons and exit points in a fucntion

I have multiple resources allocated in a function, therefore quite a few pointers and out of which I have to return one pointer (let's say ret_ptr) and deallocate others (all othr_ptrs) before leaving the function. I have multiple exit points in…
Dinesh
  • 5
  • 4
0
votes
1 answer

Using std::unique_ptr and lambdas to advance a state of an object

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…
Sean
  • 9,888
  • 4
  • 40
  • 43
1
2