
On Fri, Sep 14, 2012 at 4:19 PM, Vicente J. Botet Escriba <vicente.botet@wanadoo.fr> wrote:
Le 15/09/12 00:28, Lorenzo Caminiti a écrit :
On Fri, Sep 14, 2012 at 1:08 PM, Vicente J. Botet Escriba <vicente.botet@wanadoo.fr> wrote:
So resuming, the boost::scope_guard (without a dismiss state) is a convenient abstraction for c++11 compilers supporting lambdas, and Boost.ScopeExit reflects the same abstraction for C++03.
I will be for an extension of Boost.ScopeExit that provides a non-macro interface, like the one of the preceding scope_guard, for the people that don't need to make their code portable to non c++11 compilers. I will name it however scoped_exit.
Extracted from the Boost.ScopeExit
struct scope_exit { scope_exit(std::function<void (void)> f) : f_(f) {} ~scope_exit(void) { f_(); } private: std::function<void (void)> f_; };
void world::add_person(person const& a_person) { bool commit = false;
persons_.push_back(a_person); scope_exit on_exit1([&commit, this](void) { // Use C++11 lambda. if(!commit) persons_.pop_back(); // `persons_` via captured `this`. });
// ...
commit = true; }
Sure if there's enough interest I can move this scope_exit class from the examples into the Scope Exit lib itself when #ifndef BOOST_NO_LAMBDAS.
My _personal_ opinion is that such a boost::scope_exit class is so trivial that programmers can just program it themselves, but that's just my opinion and I'm happy to add such a feature (or better to move it into the lib from the examples) if Boosters want it. No review should be needed, I can just post the (trivial) changes on this ML and confirm them with Alexander Nasonov.
Lorenzo, thanks for volunteering but the design and implementation is far more complex than the extract from the doc. If we want it to be efficient, it shouldn't use boost.function, boost/multi_index/detail/scope_guard.hpp should be a good staring point.
I see. Then (speaking personally) it might make sense for it to exist in a lib as its impl is not trivial due to the optimization efforts. However, I probably won't have time to add this feature to Scope Exit... Thanks. --Lorenzo