Boost logo

Boost :

From: Alexander Nasonov (alnsn_at_[hidden])
Date: 2006-11-27 13:07:18


Philippe Vaucher wrote:
> FWIW, another interface could be a class like :
>
> // untested
> class scope_guard
> ...

See also <boost/multi_index/detail/scope_guard.hpp>.

> This, used along with boost::bind would make your example become smth like :
>
> glob_t result_glob;
> scope_guard guard(boost::bind(&globfree, &result_glob));
> /* throw or return here */

Try to construct a guard for more complex case:

void foo(std::map<char,int>& codes, char symbol, int code)
{
    bool cancel = codes.insert(std::make_pair(symbol, code)).second;

    { BOOST_SCOPE_EXIT( (cancel)(codes)(symbol) ) {
        if(cancel)
            codes.erase(symbol);
    }} _;

    // ...

    cancel = false; // All is fine at this point, commit.
}

Problem 1: if. You should use boost::lambda.
Problem 2: overloaded erase.

I don't think that many developers will wrap erase. They need
straitforward solution that doesn't require opening lambda docs,
looking for _1->* or bind constructs, trying to figure out why
_1->* &std::map<char,int>::erase doesn't work and so on.

Problem 3: you can set breakpoint at codes.erase(symbol) line but
how do you set it in scope_guard (conditional breakpoint in
~scope_guard with this == &guard stops even if cancel == false)?

-- 
Alexander Nasonov
http://nasonov.blogspot.com
Float like a butterfly, sting like a bee.  Muhammad Ali
This quote is generated by: 
	/usr/pkg/bin/curl -L http://tinyurl.com/veusy         \
	  | sed -e 's/^document\.write(.//' -e 's/.);$//'     \
	        -e 's/<[^>]*>//g' -e 's/^More quotes from //' \
	  | fmt | tee ~/.signature-quote

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