3

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 same time, I'm beginning to look into the Boost library, and have noticed the BOOST_SCOPE_EXIT macro (the Boost.ScopeExit library).

These two seem to be quite different implementation-wise (BOOST_SCOPE_EXIT uses Boost.TypeOf, nothing like that in ScopeGuard11's SCOPE_EXIT); and the Boost macro is concerned with parameter capture etc. which is less of an issue in C++11; but beyond that, they seem to offer essentially the same functionality.

Am I right or am I missing something? Could one say that ScopeGuard11 is a 'cleaner' mechanism with the same functionality of Boost.ScopeGuard but with less backwards compatibility?

sehe
  • 374,641
  • 47
  • 450
  • 633
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 1
    @TNA: Linked to the code. – einpoklum Feb 20 '15 at 20:07
  • https://gist.github.com/KindDragon/4650442 - moreover see http://stackoverflow.com/questions/10270328/the-simplest-and-neatest-c11-scopeguard which answers the question (I think): BOOST_SCOPE_EXIT has to work for pre-C++11 and hence is not as simple/clean – stijn Feb 20 '15 at 20:07
  • `ScopeGuard11` is movable – TNA Feb 20 '15 at 20:09
  • 3
    @TNA: I hope this is not a stupid question, but - why would I ever move a scope guard? – einpoklum Feb 20 '15 at 20:17
  • @einpoklum I don't know – TNA Feb 20 '15 at 22:27
  • @einpoklum if you want to transfer a scopable resource. E.g. like `unique_lock` which can be "returned-by-move". This is essential idiom for enabling certain thread-aware API's in a way that's both free of race and exception safe – sehe Feb 20 '15 at 23:15
  • @sehe: But why would I move a scope guard in order in order to transfer a scopeable resource? Can you link to an example of this idiom being used? – einpoklum Feb 20 '15 at 23:17
  • 1
    A lock **is** an example of a scopable resource. So, if you do some particular locking that you might want to (conditionally?) transfer onto some other party (such as the caller) you could pass it. (I'm not giving other examples; if I used ScopeExit intensively I'd probably be answering.) – sehe Feb 20 '15 at 23:19
  • 4
    Making it movable also allows `auto foo = /* expression yielding a scope guard */ ;`, since copy-initialization requires a copy/move constructor. – T.C. Feb 21 '15 at 02:07
  • @T.C.: So, if you expand your comment just a bit into an answer I could accept it. – einpoklum Nov 13 '16 at 23:04

0 Answers0