
On Tue, Sep 4, 2012 at 5:08 AM, Alexander Stoyan <alexander.stoyan@gmail.com> wrote:
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 - http://alexander-stoyan.blogspot.com/2012/02/scope-exit-handlers.html
Looks the same as the one suggested in Boost.ScopeExit docs: http://www.boost.org/doc/libs/1_51_0/libs/scope_exit/doc/html/scope_exit/alt...
-----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Stewart, Robert Sent: Tuesday, September 04, 2012 3:01 PM To: boost@lists.boost.org Subject: Re: [boost] very simple implementation of Andrei Alexandrescu's ScopeGuard idiom
Sergey Radionov wrote:
2012/9/4 Stewart, Robert <Robert.Stewart@sig.com>:
Sergey Radionov wrote:
<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/1844037 58?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... :)
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 http://www.boost.org/community/policy.html#quoting for guidance on how to quote on this list.
--Lorenzo