|
Boost Users : |
From: Aaron W. LaFramboise (aaronrabiddog51_at_[hidden])
Date: 2005-01-22 03:04:21
Delfin Rojas wrote:
> I think this should work for all the cases you mention:
>
> http://www.boost.org/libs/smart_ptr/sp_techniques.html#handle
>
> http://www.boost.org/libs/smart_ptr/sp_techniques.html#on_block_exit
Thanks for this pointer! In particular, I did not know about
smart_ptr's termination functor parameter. This has very nice
semantics. smart_ptr plus Boost.Lambda would be quite elegant:
shared_ptr<void> s(0, if_(bind(close, file_desc) == ..., ... ));
Well, sort of, anyway. :) Its too bad that noone seems interested in
adding proper lambda expressions to C++. bind() in particular is a pain.
Jonathan Turkanis makes an important point, though. scope_guard uses
initializer lifetime rules to allow the abstract entity to live beyond
the end of the definition, without having to do any extra memory
allocation. I don't beleive any smart_ptr- or boost::function-based
solutions will have this trait. This could be an important performance
efficiency consideration when adopting an idiom to be used in general.
So, I suppose, real tests are in order, to see which of these approaches
really works on real compilers. Of the four sorts of solutions
mentioned, only scopeguard and creating a throw-away class have any
chance of being optimized out, I think. The smart_ptr syntax is
probably the prettiest, but it could be even prettier if a custom class
that omitted the actual pointer support was created. I'm not sure if
that would be a terribly big win, though.
Aaron W. LaFramboise
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net