Boost logo

Boost Users :

Subject: Re: [Boost-users] [bind][function] Do copy constructors invoked inside boost::bind / boost::function get optimized out?
From: Steve Lorimer (steve.lorimer_at_[hidden])
Date: 2010-07-27 01:34:10


> 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)).

No, I want the object to be reference counted - and am aware that internally
bind has to make copies. However, I expected at least one, perhaps two or
three copies to be made. 10 copies made for a single call to bind seems
really expensive. I'm asking more about whether the optimizer is able to
optimize out several of these copies, or if it's overhead that one has to
accept if one wants to use bind and/or function?

I haven't tested it yet, but I imagine a call to bind with more arguments
will involve a lot more copying, as bind looks something like this for
argument lists internally:

template< class A1 , class A2 , class A3 , class A4 > class list4: private
storage4< A1 , A2 , A3 , A4 >
template< class A1 , class A2 , class A3 , class A4 > struct storage4 :
public storage3< A1 , A2 , A3 >
template< class A1 , class A2 , class A3 > struct storage3 : public
 storage2< A1 , A2 >
template< class A1 , class A2 > struct storage2 : public storage1< A1 >
template< class A1 > struct storage1

Argument A1 is copied for each constructor in the list as it's passed down
the inheritance hierarchy.

Are there plans to implement bind using variadic templates? Not sure if that
would reduce the number of copies?


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at