Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2004-12-27 20:26:02


Roland Schwarz wrote:
> Rene Rivera wrote:
>
>>
>> Not only is it designed to work.. But it's a documented use case:
>>
>> http://www.boost.org/libs/bind/bind.html#with_member_pointers
>>
> Thank you for your reply, I have seen this though.
> What I was unsure about is the fact that in my case I am
> creating the shared_ptr as a temporary object.

Creating a temporary object is (in standard C++) almost equivalent to
creating a named object and destroying it at the end of the full expression.
That is:

    expression_involving( X(args) );

is pretty much the same as

    {
        X x( args );
        expression_involving( x );
    }

You can now see why the example given in the Bind documentation covers your
use case as well.

The "almost" and "pretty much" in the above cover the cases where (1) a
non-conforming compiler doesn't destroy temporaries as prescribed (Sun C++
in its default "legacy compatible" mode) or (2) when the "args" expression
creates an unmanaged resource and there is a danger that the unspecified
evaluation order can cause this resource to be leaked. See the "Best
Practices" section:

http://boost.org/libs/smart_ptr/shared_ptr.htm#BestPractices

Apart from that, yes, your use case is supported and useful; but there is a
reason why all the examples use a named shared_ptr. :-)


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