Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::bind/boost::function and fast delegates
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2013-01-21 16:56:34


Hello Tyler,

please see my answer below.

On Mon, Jan 21, 2013 at 8:22 PM, Littlefield, Tyler <tyler_at_[hidden]> wrote:
> hello all:
> We have a project we're working on that will be distributed, and I didn't
> want to have to set boost as a dependency if we're only using boost::bind
> and boost::function. Given this, I had a couple steps.
> First I thought I could possibly just copy out the code and modify it a bit,
> but it uses a ton of boost::preprocessor, so that'd require copying that out
> too. I'd rather not imbed half of boost in the project.
There is a tool, which you can try: BCP
http://www.boost.org/doc/libs/1_52_0/tools/bcp/doc/html/index.html.
This tool can extract a particular library and all its dependencies
out of boost.

> My other solution (and one I really like) was to write my own delegates.
Don't do that ;) You will end up in a real mess... boost::bind,
boost::function & co have all that preprocessor macros etc. not
because developers were that eager to bloat the code, but because they
needed them to generate overloads for all kinds of variations like
pointer to member, global function etc. and "any possible" number of
params (I think 10 or so): function with one param, with two params
with 3 params and so on. Finally each of these functions can be
non-const, const or volatile or const-volatile. Than you have
arguments (const, volatile, non-const params being passed as pointer,
reference or value). Doing that manually is tedious. Sometimes its
fine to not distinguish the parameter constness, but sometimes you
need to and that depends on the use case.

> I set to work on this and it's coming out good, but I have a couple
> questions.
> I know you can assign function pointers to boost::function objects, though
> I've always done it through bind. I'm curious how this works though:
> I have a normal function pointer set up and working right now, but how does
> boost::function take other pointers (say to static methods)?
> Also I'm curious what boost::bind generates. I'd like to create my own
> version since I love using it to pass in arbitrary values/using
> placeholders, but the boost::function has to be able to take a newly
> constructed function object and bind to that from what I understand. The new
> function object somehow has it's arguments stored on the object so that the
> () operator can just pass those in.
> Any help here would be appreciated.

If you can afford take C++11. It has adopted bind, function (and much
more like lambdas integrated into the language). Than your users are
going to depend on STL only. If you still have a desire to write you
own function delegates, take a look at the bugs which were fixed in
these libs since their inception. You will have to deal with the same
amount of them. And finally, these libs are documented and really well
tested with different compilers and are portable! They also work
around different compiler defects in regards to C++ standard.

And if you still wish to write such a lib(s) take a look at this article:

http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible

> Thanks,
>
> --
> Take care,
> Ty

Regards,
Ovanes


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