Boost logo

Boost Users :

Subject: Re: [Boost-users] [bind][function] Do copy constructors invoked inside boost::bind / boost::function get optimized out?
From: Emil Dotchevski (emil_at_[hidden])
Date: 2010-07-26 18:13:58


On Mon, Jul 26, 2010 at 3:05 PM, Steve Lorimer <steve.lorimer_at_[hidden]> wrote:
> I'm using boost::bind to convert a function of the form:
> T::fun(boost::intrusive_ptr<U>) into a form which can be stored by
> boost::function.
> Outputting calls to intrusive_ptr_add_ref and intrusive_ptr_release reveals
> there are a massive number of copies of the intrusive_ptr.

By design, boost::bind copies the objects it's passed. The refcounts
can't be optimized out because they are a result of making actual
copies.

If you want boost::bind to only keep a reference to the objects you're
passing to it, use boost::ref or boost::cref. For example, instead of
bind(foo,a) use bind(foo,ref(a)).

HTH

Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode


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