function, bind & shared_ptr
I have found that it is bossible to create an in place functor like object
by combining function, bind and shared_ptr.
However altough it seems to work I am not sure whether this is safe
usage. I would be glad if someone more knowledgeable had a look
on it:
class bar {
void foo() {
}
};
boost::function
Roland Schwarz wrote:
I have found that it is bossible to create an in place functor like object by combining function, bind and shared_ptr.
However altough it seems to work I am not sure whether this is safe usage. I would be glad if someone more knowledgeable had a look on it:
class bar { void foo() { } };
boost::function
pfoo; int main(int argc, char* argv[]) { pfoo = boost::bind(&bar::foo, boost::shared_ptr<bar>(new bar)); pfoo(); // ... pfoo = 0; // this operation causes the ~bar dtor to be called return 0; }
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 -- -- Grafik - Don't Assume Anything -- Redshift Software, Inc. - http://redshift-software.com -- rrivera/acm.org - grafik/redshift-software.com - 102708583/icq
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. Roland
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.
Ah I see what your question is now... Bind uses copies of the arguments unless told otherwise with boost::ref. So as long as the function<> exists so will the argument, and hence the shared instance. -- -- Grafik - Don't Assume Anything -- Redshift Software, Inc. - http://redshift-software.com -- rrivera/acm.org - grafik/redshift-software.com - 102708583/icq
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. :-)
participants (3)
-
Peter Dimov
-
Rene Rivera
-
Roland Schwarz