Boost logo

Boost Users :

Subject: Re: [Boost-users] [bind][function] Do copy constructors invokedinside boost::bind / boost::function get optimized out?
From: Peter Dimov (pdimov_at_[hidden])
Date: 2010-07-27 06:41:33


Steve Lorimer wrote:
> 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.

This is the optimization that makes the placeholders not occupy any space...
its purpose was to allow function<> to store bind( &X::f, p, _1, _2, _3 )
without going to the heap. Unfortunately, you're right that it results in a
lot more copies. Not sure what can be done here.

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

Rvalue references can help, as the copies can be converted into moves. But
every compiler that has variadic templates and rvalue references also has
std::bind, so there's not much point.


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