Boost logo

Boost :

Subject: Re: [boost] [function] The cost of boost::function
From: Philipp Moeller (philipp.moeller_at_[hidden])
Date: 2012-04-16 18:49:10


Michael Kochetkov <michael.kv_at_[hidden]> writes:

> Hello, as far as I subscribed to boost mail list for a reason I have decided
> to share an interesting (at least for me) observation of boost::function
> facility I have encountered several years ago. The observation is about the
> cost of the solution and you probably may be interested if you use boost in
> your production.

The standard and boost leave the return type of bind (and mem_fn)
undefined as the actual implementation of the returned binder can vary,
which makes it impossible to use reliably on non C++11 compilers.

I think most users are aware of the cost implications that function has
and try to pass the result of bind in context where the type can be
deduced. Maybe a word of warning wouldn't be too bad, though.

>
> Let us consider the following code:
> #include <boost/bind.hpp>
> #include <boost/function.hpp>
>
> inline int g(int i) {
> return ++i;
> }
>
> int main() {
> // AAAAA
> boost::function< int (int) > f1 = boost::bind(&g, _1 );
> f1(1);
>
> // BBBB
> boost::_bi::bind_t<int,int (*)(int),boost::_bi::list1<boost::arg<1>
>> > f2 = boost::bind(&g, _1 );
> f2(1);
>
> // CCCC
> auto f3 = boost::bind(&g, _1 );
> f3(1);
> }
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk