Scope guard is very C++-way idiom. It good interacts with exceptions and compliant with RAII. C++ has no native support of scope guards. I mean clean syntax. At the moment I can only write a helper class: c-tor store the lambda as data-member and d-tor call lambda (at scope exit or at exception throwing).
Is there currently any proposal of handy syntax for scope guard in C++?
I mean something like int * i = new int; ~[&i] { if (i) { delete i; i = nullptr; } }; ...
or even (with capture as ref by default) ~{ /* statments */; }
. When lambda-syntax is just syntactic sugar for struct
with operator ()
, there may be lambda-similar-syntax for "inline"-destructor of anonymous struct
.