Boost logo

Boost :

Subject: Re: [boost] very simple implementation of Andrei Alexandrescu's ScopeGuard idiom
From: Alexander Stoyan (alexander.stoyan_at_[hidden])
Date: 2012-09-04 08:08:03

Hi folks,
In contrast to BOOST_SCOPE_EXIT there is a much shorter and less verbose
implementation that is especially good with c++11 lambda usage -

Never lose heart!
      Alexander Stoyan

-----Original Message-----
From: boost-bounces_at_[hidden] [mailto:boost-bounces_at_[hidden]]
On Behalf Of Stewart, Robert
Sent: Tuesday, September 04, 2012 3:01 PM
To: boost_at_[hidden]
Subject: Re: [boost] very simple implementation of Andrei Alexandrescu's
ScopeGuard idiom

Sergey Radionov wrote:
> 2012/9/4 Stewart, Robert <Robert.Stewart_at_[hidden]>:
> > Sergey Radionov wrote:
> >>
> >>
> >>
> >> based on
> >>
> >> I know about <> but
> >> with boost::bind this can be much simpler:
> >>
> >> <>
> >>
> >> 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... :)
> >
> > I created a version, too, but the usage is much simpler.
> >
> > Given the following:
> >
> > FILE * const file(fopen("test", "r"));
> > if (file)
> > {
> > // A
> > }
> >
> > Your usage, at A, is:
> >
> > finally_execute _(make_fin_exec(
> > boost::bind(fclose, file)));
> >
> > Mine is:
> >
> > scope_guard _(fclose, file);
> >
> > As you can see, a little more complication in the component
> > makes usage much simpler.
> In this case, I think, much simple usage gives less clear
> code, since people reading code will see boost::bind and
> understand what it mean.
> With your version, he maybe should have to a look to help or
> header...

Objectively, your version requires knowing how to use
boost::bind, which arguably is a good thing, and requires calling
a function template, both of which make the statement more
verbose. My version uses the same calling convention as, say,
boost::thread: pass the function object and the arguments it
requires. It even accepts a std::tr1::function<void()> so you
can bind to your heart's content.

As for the concern that one must read help or a header to
understand how to use scope_guard, surely you jest. One cannot
understand finally_execute or make_fin_exec() without doing
likewise. You simply construct an instance of scope_guard with
the function object to call, and any required arguments. What
could be simpler?

> > There's also Boost.Scope Exit, though it's even more
> > verbose:
[snip unreferenced content]
> > Rob Stewart
[snip my signature block]
> > IMPORTANT: The information contained in this email and/or
[snip my employer's notice]
> > Unsubscribe & other changes:
[snip dev list's notice]

Please read for guidance
on how to quote on this list.

Rob Stewart robert.stewart_at_[hidden]
Software Engineer using std::disclaimer;
Dev Tools & Components
Susquehanna International Group, LLP


IMPORTANT: The information contained in this email and/or its attachments is
confidential. If you are not the intended recipient, please notify the
sender immediately by reply and immediately delete this message and all its
attachments. Any review, use, reproduction, disclosure or dissemination of
this message or any attachment by an unintended recipient is strictly
prohibited. Neither this message nor any attachment is intended as or should
be construed as an offer, solicitation or recommendation to buy or sell any
security or other financial instrument. Neither the sender, his or her
employer nor any of their respective affiliates makes any warranties as to
the completeness or accuracy of any of the information contained herein or
that this message or any of its attachments is free of viruses.

Unsubscribe & other changes:

Boost list run by bdawes at, gregod at, cpdaniel at, john at