0

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.

Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
  • I don't think it necessary to change the core language in order to support this idiom. And indeed, I'm not aware of any such proposal. – cpplearner Aug 12 '15 at 09:35
  • @cpplearner On the one hand it is not too big (and non-breaking) change in syntax. OTOH it is very *C++*-way feature. – Tomilov Anatoliy Aug 12 '15 at 09:37
  • 1
    "At the moment I can only write a helper class" – and why would you need to do anything else? Perhaps such a class should be in the standard library, but I think this is code smell. If you have a resource, you should encapsulate it into its appropriate managing class, you shouldn't attempt to poke it from outside. – The Paramagnetic Croissant Aug 12 '15 at 09:39
  • Well unary `~` applied to a lambda (which is an expression) already has a meaning, so that would collide. However a close solution can be achieved with just a bit of macro/templates magic, as presented by Andrei Alexandrescu [here](https://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012-Andrei-Alexandrescu-Systematic-Error-Handling-in-C) (near the end), and it looks like `SCOPE_EXIT { /* code */ };`. – Quentin Aug 12 '15 at 09:42
  • @TheParamagneticCroissant Say, one have class with a method, that perform logging during running the method. On exception or simple method finishing the data logged became useless. Maybe I'm not right, but there is need in writing such helper class to clean log structures with semantic of scope guard. Of course, maybe it should be a part of STL. – Tomilov Anatoliy Aug 12 '15 at 09:45
  • @Quentin There still has to be closing part of `SCOPE_EXIT` after closing curly bracket, that's looks ugly. – Tomilov Anatoliy Aug 12 '15 at 09:48
  • @Orient no, it really is just that. Andrei overloaded an infix operator precisely so parentheses aren't needed. – Quentin Aug 12 '15 at 09:49
  • @Quentin Really? Therefore it worth to spend time to watch linked video. – Tomilov Anatoliy Aug 12 '15 at 09:51
  • @Orient you can skip to around 01:12:00 if you don't have much spare time, but the whole presentation is quite interesting :) – Quentin Aug 12 '15 at 09:53
  • @Orient If there's need for logging, then the class that performs the operation should log it itself. If you implement it right, it's only one more method call in the destructor. There's absolutely no need to clobber into the class externally using yet another helper class or function. – The Paramagnetic Croissant Aug 12 '15 at 09:57
  • @TheParamagneticCroissant method is `const`. log structure passed by reference to a plenty of methods of such a classes and totally different (in sense of architecture harmony) structure, rather then the class. An exception generated into methods handled scores of times during life-time of the class. OK, never mind, it is just my local problem :) (maybe the solution is to pass log structure by value (maybe with conversion/fabrication to helper scope guard class) and to rely on its destructor). – Tomilov Anatoliy Aug 12 '15 at 10:13

0 Answers0