Tuesday, April 06, 2010

Bind illustrated

Asynchronous operations in Asio all expect a function object argument, the completion handler, which they invoke when the asynchronous operation completes. The signature of the handler depends on the type of operation. For example, a handler posted using io_service::post() must have the signature:

void handler();

while an asynchronous wait operation expects:

void handler(error_code ec);

and asynchronous read/write operations want:

void handler(error_code ec, size_t length);

Non-trivial applications will need to pass some context to the completion handler, such as a this pointer. One way to do this is to use a function object adapter like boost::bind, std::tr1::bind or (as of C++0x) std::bind.

Unfortunately, for many C++ programmers, bind represents a little bit of magic. This is not helped by the impenetrable compiler errors that confront you when you use it incorrectly. And, in my experience, the underlying concept (where some function arguments are bound up-front, while others are delayed until the point of call) can present quite a steep learning curve.

I have put together some diagrams to help explain how bind works. For clarity, I have taken a few liberties with C++ syntax (e.g. omitting the parameter types on the function call operator) and (over-)simplified bind's implementation. Finally, the examples are limited to those likely to be useful with Asio. Comments and suggestions welcome.

bind can be used to adapt a user-supplied function expecting one argument into a function object that takes zero arguments. The bound value (123 in this example) is stored in a function object and is automatically passed to the user-supplied function as required:

[ click images for full size ]

Binding an argument can be used to turn a class member function into a zero-argument function object. As you know, non-static member functions have an implicit this parameter. This means that an appropriate pointer needs to be bound into the function object:

Alternatively, the implicit this can be made explicit by adapting a member function into a function object taking one argument:

Function objects will often use both bound arguments and arguments supplied at the point of use. This can be done using member functions:

or non-member functions:

Sometimes the function object's point of use will supply arguments which are not required to call the target function. bind will automatically discard these surplus arguments:

The surplus argument(s) need not be the at the end of the function object signature:

Finally, bind allows you to the reorder arguments to adapt the target function to the necessary function object signature:


Anonymous said...

A picture is worth a thousand words. Well done. Thank you!

Unknown said...

The high volume of posts last few weeks have been great, much appreciated and keep'em coming!

Benjamin van den Hout said...

Excellent explanation, thank you! Looking forward to more postings :)

Anonymous said...

Please, add some words about yours box() function (with diagrams for the nested binds).

Simon said...

Thanks for the pictures, that's what I was looking for as the bind documentation is somewhat unclear.

So? said...


Ralf said...

Wow, excellent article! Those diagrams are fantastic and really clarify a lot of things. Thanks Chris

Sonic said...

may i ask what software did you use to create the diagrams?

Unknown said...

Thanks for the summary. It'd be interesting to see how C++11 lambdas compare to bind. In particular, in what situations is bind preferred, and why?

Anonymous said...

I've a little question :

when we bind some member fn pointer to this pointer . doesn't bind has to figure out the first parameter arg is normal function pointer or member fn pointer so that it binds member fn pointer to this pointer correctly?

can you also explain how does correct interpretation takes place?

Richy said...

Sometimes three year old posts are really helpful!
Thanks a lot!

Anonymous said...

Thanks for being one of those who care for people and mankind.
I'd like to be one of you.

Anonymous said...

Thank you so much! Excellent article!

Anonymous said...

Very nicely explained thank you !!!

Unknown said...

The best argument against democracy is a five-minute conversation with the average voter. See the link below for more info.


Unknown said...

The real necklace of a woman is not her looks but her heart. Visit my site for more interesting offer. Thank you and God bless!



Anonymous said...

Best explanation making it so easy to understand. I invested many hours (without getting any idea) on other links and 3 minutes on your link, must say those 3 minutes did magic. Each picture is worth thousand (or more) words.

Wonderful, thanks.

avvilibber28 said...

Hey! I simply want to give an enormous thumbs up for the nice information you could have here on this post. I will be coming back to your weblog for extra soon. usa online casino

Conner Smith said...

It’s amazing in support of me to truly have a blog site, which will be valuable meant for my knowledge. Thanks admin.
c++ programming tutorial

Conner Smith said...

Your write-ups are far more than wow!
tutorial for c++

acil said...

Best Graphic Designing institute in Delhi & Gurgaon & Graphic Designing Course in Delhi & Gurgaon

Best Web Development Training Institute in Gurgaon & Web Development Course in Gurgaon
Python Training in Gurgaon | Python Institute in Gurgaon


Gryffin said...

Returns a function object based on fn , but with its arguments bound to args . Each argument may either be bound to a value or be a placeholder: - If bound to a value, calling the returned function object will always use that value as argument. click

Horea said...

Thanks a lot for this post.


immigration consultant in Surat said...

Yami immigration is a well-known and experienced immigration consultant in Surat. We provide Immigration Services for many countries such as Italy, Canada, France, Australia, Germany, USA, Malaysia, New Zealand, and Singapore.

Asinfovision said...

Thank you so much for sharing this blog with us, it is really amazing valuable, and informative.
C and C++ Language course in Gurgaon

thomas said...

HDhub4u Tamil produces HD in addition to a high-quality print of newly-launched films to this's customers. This particular site is actually famous for offering Telugu movies on the very next day of the release of films. hdhub4u has been building a name for themselves since the website's begging in year 2017.

thomas said...

WatchCartoonOnline has the server of its situated within European countries. It offers worldwide organizations inconspicuously English-talking countries, in addition to the US, UK, and Australia, to administer some examples. The web based website has been thought of to deal with private time that moves on for an intriguing span.

온라인카지노 사이트 said...

스포츠토토 Every weekend i used to visit this web page, for the reason that i want enjoyment, as this this
web site conations truly pleasant funny information too.

온라인카지노 사이트 said...

Hi there, I discovered your web site by means of
Google while searching for a related topic, your site came up, it appears to be
like great. I have bookmarked it in my google bookmarks. 토토사이트

온라인카지노 사이트 said...

I'm no longer sure where you are getting your info, however good
topic. I must spend some time studying much more or working
out more. Thank you for excellent information I was
searching for this info for my mission. 온라인카지노사이트

온라인카지노 사이트 said...

I'm gone to tell my little brother, that he should also pay a quick visit
this website on regular basis to get updated from hottest reports. 온라인카지노사이트