Questions tagged [scopeguard]
22 questions
48
votes
14 answers
The simplest and neatest c++11 ScopeGuard
I'm attempting to write a simple ScopeGuard based on Alexandrescu concepts but with c++11 idioms.
namespace RAII
{
template< typename Lambda >
class ScopeGuard
{
mutable bool committed;
Lambda rollbackLambda;
…

lurscher
- 25,930
- 29
- 122
- 185
33
votes
8 answers
Does ScopeGuard use really lead to better code?
I came across this article written by Andrei Alexandrescu and Petru Marginean many years ago, which presents and discusses a utility class called ScopeGuard for writing exception-safe code. I'd like to know if coding with these objects truly leads…

SCFrench
- 8,244
- 2
- 31
- 61
17
votes
1 answer
Why can't Alexandrescu use std::uncaught_exception() to implement SCOPE_FAIL in ScopeGuard11?
Many people are no doubt familiar with Mr. Alexandrescus ScopeGuard template (now part of Loki) and the new version ScopeGuard11 presented…

odinthenerd
- 5,422
- 1
- 32
- 61
9
votes
1 answer
C++: why this simple Scope Guard works?
Every looked at scope guard so far has a guard boolean variable. For example, see this discussion:
The simplest and neatest c++11 ScopeGuard
But a simple guard works (gcc 4.9, clang 3.6.0):
template
struct finally_t : public C {
…

funny_falcon
- 427
- 3
- 11
9
votes
2 answers
Will there be standardization of scope guard/scope exit idioms?
Running a lambda on scope exit seems like such a basic thing, I would expect it to be standardized. Things like unique_ptr are better, when they apply, but I find there is an endless supply of "one-off" destructors are needed, especially when…

VoidStar
- 5,241
- 1
- 31
- 45
8
votes
2 answers
Who copies the return value of a function?
Is it the caller or the callee copying or moving the return value of a function? For example, if I want to implement the pop() function of a queue, like this
template
class queue
{
std::deque d;
public:
// ... //
T…

Ralph Tandetzky
- 22,780
- 11
- 73
- 120
7
votes
2 answers
RAII wrapper for function pairs and template specialization
I've written a RAII wrapper for C function pairs which initialize and release resources and it serves me well for most cases.
#include
#include
#include
#include
template

legends2k
- 31,634
- 25
- 118
- 222
4
votes
2 answers
How to avoid warning when using scope guard?
I am using folly scope guard, it is working, but it generates a warning saying that the variable is unused:
warning: unused variable ‘g’ [-Wunused-variable]
The code:
folly::ScopeGuard g = folly::makeGuard([&] {close(sock);});
How to avoid such…

Alex
- 3,301
- 4
- 29
- 43
4
votes
1 answer
Life extension of temporary by const reference
C++
I'm trying to see how const references prolong the lifetime of temporaries. I'm running the code from the snippet in one of the answers to What are the differences between pointer variable and reference variable in C++? and got conflicting…

CodeBricks
- 1,771
- 3
- 17
- 37
4
votes
2 answers
Dynamically created scope guards
I've read the article about scope guards (Generic: Change the Way You Write Exception-Safe Code — Forever) in DDJ and I understand their common use.
However, the common use is to instantiate a particular stack guard on the stack for a particular…

Ilya
- 5,533
- 2
- 29
- 57
4
votes
1 answer
Scope Guard Statement in C#
The Resource Acquisition Is Initialization (RAII) idiom and the try-finally statement form the backbone of the traditional approaches to writing exception safe programming.
My question is: Is there something like Scope Guard Statement available on…

Glauco Vinicius
- 2,527
- 3
- 24
- 37
3
votes
3 answers
Is there any way to extend the lifetime of a temporary object in C++?
I wrote a scope guard which resets a value when the scope exits:
template
struct ResetGuard
{
T old_value;
T& obj_to_reset;
ResetGuard(T& obj_to_reset, const T& new_value) :
old_value(obj_to_reset),
…

Antonio Perez
- 463
- 3
- 10
3
votes
1 answer
const reference for temporary lifetime lengthening
I have a question about some C++ standard compliance or lack of it.
In my project I'm using some simple Guard class that uses the const reference trick. I'm using Visual Studio 2005 and there are two configurations - one for normal release build and…

altariste
- 33
- 3
3
votes
0 answers
Difference between ScopeGuard11 and Boost.ScopeExit - just backwards compatibility?
I recently watched Andrei Alexandrescu's talk in 'C++ and Beyond 2012' (Systematic Error Handling with C++), in which he discusses his ScopeGuard11 construct, and specifically SCOPE_EXIT (second part of the talk; or just read the code here). At the…

einpoklum
- 118,144
- 57
- 340
- 684
2
votes
1 answer
Is a lambda-expression that only captures by reference guaranteed not to throw?
With C++20 P0052's scope_guards (and many other flavors of scope guards), consider this typical use case:
auto f = std::fopen(/*...*/);
scope_exit guard_f([&](){ std::fclose(f); });
The code relies on that the lambda expression itself (the…

zwhconst
- 1,352
- 9
- 19