Boost logo

Boost :

Subject: Re: [boost] [scope_exit] D-style scope(failure) and scope(success) in C++
From: Evgeny Panasyuk (evgeny.panasyuk_at_[hidden])
Date: 2012-10-10 15:47:38


Hello,

> There is Boost.ScopeExit library which emulates D's scope(exit) via
BOOST_SCOPE_EXIT macro, but there are no analogues for D's
scope(failure) and scope(success).

I have just checked Boost.ScopeExit on-line manual
(http://www.boost.org/doc/libs/1_51_0/libs/scope_exit/doc/html/index.html).
Approximately 14 of 16 BOOST_SCOPE_EXIT* samples in manual are in fact
emulations of SCOPE_FAILURE and SCOPE_SUCCESS.
So, looks like in first place Boost.ScopeExit was introduced to simplify
emulation of scope(failure) and scope(success) features.

For instance first example:

void world::add_person(person const& a_person) {
   bool commit = false;

   persons_.push_back(a_person);
   BOOST_SCOPE_EXIT(&commit, &persons_) {
     if(!commit) persons_.pop_back();
   } BOOST_SCOPE_EXIT_END

   // ...

   commit = true;
}

Can be re-implemented with SCOPE_FAILURE
https://github.com/panaseleus/stack_unwinding#d-style-scope-guardsactions
in following way:

void world::add_person(person const& a_person) {
   persons_.push_back(a_person);
   SCOPE_FAILURE(&persons_) {
     persons_.pop_back();
   } SCOPE_FAILURE_END

   // ...
}

Best Regards,
Evgeny


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk