Boost logo

Boost :

Subject: Re: [boost] very simple implementation of Andrei Alexandrescu's ScopeGuard idiom
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2012-09-03 23:45:59


On Mon, Sep 3, 2012 at 4:51 PM, Sergey Radionov <rsatom_at_[hidden]> wrote:
> 2012/9/4 Vicente J. Botet Escriba <vicente.botet_at_[hidden]>:
>> Le 03/09/12 13:37, Sergey Radionov a écrit :
>>
>>> https://github.com/RSATom/finally_execute/blob/master/finally_execute.h
>>>
>>> based on
>>> http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758?pgno=2
>>>
>>> I know about http://sourceforge.net/projects/loki-lib/ but with
>>> boost::bind this can be much simpler:
>>> https://github.com/RSATom/finally_execute/blob/master/README
>>>
>>> I've used this in many of my projects, and found it very usefull....
>>>
>>> don't understand, why this simple solution does not contributed
>>> already to Boost by someone else... :)
>>>
>>>
>> Hi,
>>
>> have you take a look at Boost.ScopedExit?
> Yes, but I think it's give too "dirty" code. And I don't like macros
> in C++ era :)

Btw, in C++11 era (i.e., lambdas) no macros needed:

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;
}

http://www.boost.org/doc/libs/1_51_0/libs/scope_exit/doc/html/scope_exit/alternatives.html#scope_exit.alternatives.c__11_lambdas

--Lorenzo


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