scopeguard

const reference for temporary lifetime lengthening

断了今生、忘了曾经 提交于 2020-02-03 10:05:52
问题 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 the second one for unit tests. In both cases there is some temporary hanging on the const reference in the end, but what happens in the meantime is the problem. For release configuration, the const reference points directly to the temp created in

How to avoid warning when using scope guard?

守給你的承諾、 提交于 2019-12-10 15:39:50
问题 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 warning? 回答1: You can disable this warnings by -Wno-unused-variable , though this is a bit dangerous (you loose all realy unused variables). One possible solution is to actually use the variable, but do nothing with it. For example, case it to void:

Why can't Alexandrescu use std::uncaught_exception() to implement SCOPE_FAIL in ScopeGuard11? [duplicate]

跟風遠走 提交于 2019-12-06 17:12:23
问题 This question already has answers here : Scope(failure) in C++11? (2 answers) Closed 6 years ago . Many people are no doubt familiar with Mr. Alexandrescus ScopeGuard template (now part of Loki) and the new version ScopeGuard11 presented here: http://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012-Andrei-Alexandrescu-Systematic-Error-Handling-in-C with source here: https://gist.github.com/KindDragon/4650442 In his talk at c++ and beyond 2012 he mentioned that he couldn't find a way to

Why can't Alexandrescu use std::uncaught_exception() to implement SCOPE_FAIL in ScopeGuard11? [duplicate]

断了今生、忘了曾经 提交于 2019-12-04 22:52:13
This question already has answers here : Scope(failure) in C++11? (2 answers) Closed 6 years ago . Many people are no doubt familiar with Mr. Alexandrescus ScopeGuard template (now part of Loki) and the new version ScopeGuard11 presented here: http://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012-Andrei-Alexandrescu-Systematic-Error-Handling-in-C with source here: https://gist.github.com/KindDragon/4650442 In his talk at c++ and beyond 2012 he mentioned that he couldn't find a way to correctly detect if scope was being exited because of an exception. Therefore he couldn't implement a

C++: why this simple Scope Guard works?

喜夏-厌秋 提交于 2019-11-30 19:36:00
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 <class C> struct finally_t : public C { finally_t(C&& c): C(c) {} ~finally_t() { (*this)(); } }; template <class C> static finally_t<C> finally_create(C&& c) { return std::forward<C>(c); } #define FINCAT_(a, b) a ## b #define FINCAT(a, b) FINCAT_(a, b) #define FINALLY(...) auto FINCAT(FINALY_, __LINE__) = \ finally_create([=](){ __VA_ARGS__ }) int main() { int a = 1; FINALLY( std::cout << "hello" <

C++: why this simple Scope Guard works?

别来无恙 提交于 2019-11-30 16:55:53
问题 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 <class C> struct finally_t : public C { finally_t(C&& c): C(c) {} ~finally_t() { (*this)(); } }; template <class C> static finally_t<C> finally_create(C&& c) { return std::forward<C>(c); } #define FINCAT_(a, b) a ## b #define FINCAT(a, b) FINCAT_(a, b) #define FINALLY(...) auto FINCAT(FINALY_, _

Does ScopeGuard use really lead to better code?

半腔热情 提交于 2019-11-27 17:43:04
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 to better code or if it obfuscates error handling, in that perhaps the guard's callback would be better presented in a catch block? Does anyone have any experience using these in actual production code? Konrad Rudolph It definitely improves your code. Your tentatively formulated claim, that it's obscure and that code would merit from a catch block is

Will there be standardization of scope guard/scope exit idioms?

爱⌒轻易说出口 提交于 2019-11-27 06:12:19
问题 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 leveraging C-style libraries. Does anyone know if this is coming? 回答1: n4189 is a proposal to add make_scope_exit wrappers, and other similar resource handlers, to the language. It is based off of the relatively famous scope_guard talk. The most recent

Does ScopeGuard use really lead to better code?

和自甴很熟 提交于 2019-11-26 19:09:03
问题 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 to better code or if it obfuscates error handling, in that perhaps the guard's callback would be better presented in a catch block? Does anyone have any experience using these in actual production code? 回答1: It definitely improves your code. Your

The simplest and neatest c++11 ScopeGuard

血红的双手。 提交于 2019-11-26 03:55:30
问题 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; public: ScopeGuard( const Lambda& _l) : committed(false) , rollbackLambda(_l) {} template< typename AdquireLambda > ScopeGuard( const AdquireLambda& _al , const Lambda& _l) : committed(false) , rollbackLambda(_l) { _al(); } ~ScopeGuard() { if (!committed) rollbackLambda(); } inline void