Boost logo

Boost :

From: Alexander Nasonov (alnsn_at_[hidden])
Date: 2007-08-20 05:23:18


Küppers, Ben <ben.kuppers <at> logicacmg.com> writes:

> I consider the scope guard to be a well-known and well-documented idiom
> and I fail to see what the ScopeExit library adds to it (or fail to be
> convinced by the arguments made in the alternative section of the help).

Scope guard is well-known because it had no competitor. I used to like it until
I realised that scope guard is not good enough in archieving a strong exception
guarantee. Scope guard bodies tend to be complex in many cases. E.g. they often
have conditional or even a loop (for example, you count each successful
push_back and then pop_back all inserted elements inside a rollback block).

ScopeExit depends on non-standard typeof feauture and it couldn't be widely
used before Boost.Typeof has been accepted. With a good promotion this library
has a chance to become well-known too.

> Rolling this functionality into a macro that makes the C++ syntax similar to
> D seems rather pointless and only contributes to a very unnatural C++ syntax.

It looks unfamiliar at present but lambda proposal for C++0x may change it:

// N2329 syntax
ScopeGuard g =
    <>(: &currency_rate_inserted, &commit, &rates, &currency)
    {
        if(currency_rate_inserted && !commit)
            rates.erase(currency);
    }

which is closer to ScopeExit syntax than to ScopeGuard+Boost.Lambda.

// ScopeExit
BOOST_SCOPE_EXIT( (currency_rate_inserted)(commit)(rates)(currency) )
{
    if(currency_rate_inserted && !commit)
        rates.erase(currency);
} BOOST_SCOPE_EXIT_END

// ScopeGuard+Boost.Lambda
ON_BLOCK_EXIT(
    if_(currency_rate_inserted && !_1) [
        bind(
            static_cast<
                std::map<std::string,double>::size_type
(std::map<std::string,double>::*)(std::string const&)
>(&std::map<std::string,double>::erase)
          , &rates
          , currency
          )
    ]
  , boost::cref(commit)
  );

--
Alexander

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