Boost logo

Boost Users :

From: Rene Rivera (grafik.list_at_[hidden])
Date: 2004-12-27 09:55:52


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<void ()> 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

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