From: Maxim Yegorushkin on

Buster wrote:

[]

> To follow up with the previous posts, the ones
> suggested weren't exactly what I was looking for
> because they require a hard-to-specify local
> variable:
>
> scoped_guard<???> g = make_guard(boost::bind(function, arg1, arg2));
>
> whereas all I wanted to write was:
>
> scoped_guard g = ...
>
> If I've misunderstood, I'd appreciate the clarification.

Please look at the code more closely. Note the line:

typedef const detail::scope_guard_base& scope_guard;

This line makes it possible to write

scoped_guard g = make_guard(boost::bind(function, arg1, arg2));

You don't have to specify template arguments.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Peter Dimov on
Buster wrote:

> To follow up with the previous posts, the ones
> suggested weren't exactly what I was looking for
> because they require a hard-to-specify local
> variable:
>
> scoped_guard<???> g = make_guard(boost::bind(function, arg1, arg2));
>
> whereas all I wanted to write was:
>
> scoped_guard g = ...

class scoped_guard
{
public:

template<class F> explicit scoped_guard( F f ): f_( f )
{
}

~scoped_guard()
{
if( f_ ) f_();
}

void release()
{
f_.clear();
}

private:

boost::function< void() > f_;

scoped_guard( scoped_guard const & );
scoped_guard & operator=( scoped_guard const & );
};


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Buster on
Maxim Yegorushkin wrote:
>
> Please look at the code more closely. Note the line:
>
> typedef const detail::scope_guard_base& scope_guard;
>
> This line makes it possible to write
>
> scoped_guard g = make_guard(boost::bind(function, arg1, arg2));
>
> You don't have to specify template arguments.

Way cool. I'd forgotten about that bit of trickery.
It even calls the function object's destructor.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]