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:

116 comments:

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

Excellent!

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.


#arguments
www.ufgop.org

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!

n8fan.net

www.n8fan.net

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

UI/UX TRAINING INSTITUTE IN GURGAON
RPA TRAINING IN GURGAON
BLUE PRISM TRAINING 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.

online-application.org

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. 온라인카지노사이트


사설토토사이트 said...

Exceptional post however , I was wanting to know if you could write a litte more on this topic? I’d be very thankful if you could elaborate a little bit further. Thanks 사설토토사이트

사설토토사이트 said...

This is the perfect post.사설토토사이트 It helped me a lot. If you have time, I hope you come to my site and share your opinions. Have a nice day.


토토사이트순위 said...

I accidentally searched and visited your site. I still saw several posts during my visit, but the text was neat and readable. I will quote this post and post it on my blog. Would you like to visit my blog later? 토토사이트순위

sòng bạc said...

When I read your article on this topic, the first thought seems profound and difficult. There is also a bulletin board for discussion of articles and photos similar to this topic on my site, but I would like to visit once when I have time to discuss this topic. sòng bạc


betufa168 said...

Nice Blog. Thanks for sharing with us.betufa168

betufa168 said...

Thanks you and I admire you to have the courage the talk about this,This was a very meaningful post for me.เบทยูฟา168

betufa168 said...

Ahhh!!! very well-written post, it was very informative.ufabet

betufa168 said...

Reading your blog is way better than snuggles. It is very informative and I bet you are very knowledgeable in this area..ยูฟาเบท

betufa168 said...

Wow, Great Clicks, I Like These Kind Of Photos, And Clothes I Like Shuts.slot

betufa168 said...

I LOVE THIS WEBSITE, THANKS.สล๊อต

betufa168 said...

I do believe this is an excellent web site. I stumbledupon it I may return yet again since I bookmarked it.baccarat

betufa168 said...

I blog frequently and I truly thank you for your information.บาคาร่า

betufa168 said...

Very nice post. I just stumbled upon your weblog and wanted to say that I have really enjoyed browsing your blog posts.แทงบอล

betufa168 said...

Nice information, valuable and excellent design, as share good stuff with good ideas and concepts.ดูบอลสด

keonha cai said...

I accidentally searched and visited your site. I still saw several posts during my visit, but the text was neat and readable. I will quote this post and post it on my blog. Would you like to visit my blog later? keonha cai


UFABET1688 said...

Excellent read, Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. ufabet

우리카지노 said...

Good morning!! I am also blogging with you. In my blog, articles related to are mainly written, and they are usually called 우리카지노 . If you are curious about , please visit!!

Unknown said...

스포츠중계 Normally I do not learn article on blogs, however I wish to say that this write-up very pressured me to take a look at and do so! Your writing style has been surprised me. Thank you, quite great article.

Unknown said...

Aw, this was a really good post. Spending some time and actual effort to produce a very good article… but what can I say… I put
things off a whole lot and never manage to get anything done.
plenty of fish natalielise 토토


Unknown said...

토토 I’m really impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you modify it yourself? Either way keep up the excellent quality writing, it’s rare to see a nice blog like this one these days.|

Unknown said...

바카라사이트 Youre so cool! I dont suppose Ive read anything like this before. So nice to find somebody with some original thoughts on this subject. realy thank you for starting this up. this website is something that is needed on the web, someone with a little originality. useful job for bringing something new to the internet!

메이저안전놀이터 said...

I am someone who works on the web. Sometimes I like to visit overseas sites 메이저안전놀이터 when I have time. Among them, I like the site related to , but your site seems to be optimized for too!! Very good

casinositewiki said...

Feels lovely'. Is excellence an item to look and quantify, is magnificence an inclination to detect or is excellence a plan to attempt? With each conceivable response to existing inquiries we get more inquiries all things considered. Feel free to visit my website; 토토사이트

betmantotopro said...

wow, its a incredible information. thanks for sharing. i think this article would be nice if you put some image to help describing the topic. Feel free to visit my website; 배트맨토토

메이저토토사이트추천 said...

That's a really impressive new idea! 메이저토토사이트추천 It touched me a lot. I would love to hear your opinion on my site. Please come to the site I run once and leave a comment. Thank you.

스포츠토토사이트 said...

Please keep on posting such quality articles as this is a rare thing to find these days. I am always searching online for posts that can help me. watching forward to another great blog. Good luck to the author! all the best! 스포츠토토사이트

메이저안전놀이터 said...

Your ideas inspired me very much. 메이저안전놀이터 It's amazing. I want to learn your writing skills. In fact, I also have a website. If you are okay, please visit once and leave your opinion. Thank you.

Majortotosite Pro said...

When some one searches for his vital thing,
thus he/she needs to be available that in detail, thus that thing is maintained over here. 토토

Racesite Pro said...

Why users still use to read news papers when in this technological
globe all is available on net?
경마사이트
경마

Oncasinosite Net said...

Wow, amazing blog layout! How lengthy have you been running
a blog for? you make running a blog glance easy. The full glance of your website is great, as neatly as the content material! 바카라사이트

Totopick Pro said...

I like the helpful information you supply to your articles. I'll bookmark your weblog and test again here regularly. I am fairly certain I'll be informed many new stuff proper here!
Best of luck for the following! 토토사이트

jameslpina said...

AtozTopNews is another top website that typically covers news related to gadgets and guides. It's one of the top sites for gadget fans. It focuses on the most recent technology news, including the most comprehensive guides for gadgets and tutorials related to gadgets. If you're an avid gadget user, this website is worth your time, and you should go to this website to get the most up-to-date and current gadget news.

James Smith said...

At MyAssignmenthelp.com, we have a team of the best editors in Australia who provide professional proofreading services to ensure you a flawless paper. With our assignment proofreading services, you can now expect no less than a foolproof copy of your assignment.

메이저토토사이트 said...

I’m very pleased to discover this site. I want to to thank you for ones time for this particularly wonderful read!! I definitely savored every part of it and i also have you saved as a favorite to see new information on your blog. 메이저토토사이트

Eid ul Fitr 2022 said...

Thanks for the information your post brings. I see the novelty of your text, I will share it for everyone to read. I look forward to reading more articles from you. Eid ul Fitr Quotes 2022

토토사이트추천 said...

This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value. Im glad to have found this post as its such an interesting one! I am always on the lookout for quality posts and articles so i suppose im lucky to have found this! I hope you will be adding more in the future. 토토사이트추천

Majortotosite Com said...

You ought to be a part of a contest for one of the finest blogs online. I'm going to highly recommend this web site! 메이저사이트

Racesite Pro said...

I am truly thankful to the holder of this site who has shared this great post at here. 경마사이트

Totopick Pro said...

I don’t even know how I ended up here, but I thought this post was great.
I do not know who you are but certainly you’re going to a famous blogger if you are not already 사설토토

Alex Kim said...

If you are looking for translation assistance in Singapore for a passport then you should choose translation services Singapore from us.Our translators offer singapore translation service.By getting this service you can translate your passport exactly.

affnetz said...

Highly informative article. This site has lots of information and it is useful for us. Thanks for sharing . Pet Surgery in Vizag

Sop Writing said...

Appreciate your thought, if looking for sop for visa which is required at time of taking admission in other counter need not to worry we are here who always provide you best services regarding to sop.

igoal88 said...

Amazing blog your blog is really good and your article has always good thank you for information.
download igoal

homework helper said...

Get online computer science assignment help 24/7 from expert writers at GreatAssignmentHelper.com and score A+ grades. 4/5 rated by College students. We never miss deadlines.

bacarasite18 said...

Very nice work keep it up thanks for sharing the knowledge to us. 바카라사이트인포

badugisite18 said...

It’s really a great and helpful piece of information. 바둑이사이트넷

baccaratsite18 said...


Thanks for giving great kind of information. So useful and practical for me. Thanks for your excellent blog, nice work keep it up thanks for sharing the knowledge. 바카라사이트윈

Great Assignment Helper said...

Ranging from research theme decision, to editing and altering services for assignments on demand, Great Assignment Helper furnishes the best of assignments to students at cheapest rates. Our assignment helper and assignment help desk takes pride in having the best experts aboard.

Great Assignment Helper said...

In today’s world people are very much stucked in their personal works, same is happening with the students that’s why we have strated the services of the IT Assignment Help . Connect with us to avail this premium offer.

Robert Ava said...

It’s appropriate time to make some plans for the future and it is time to be happy. I have read this post and if I could I wish to suggest you few interesting things or advice. Gadget

먹튀검증업체 said...

Hi there, I simply hopped over in your website by way of StumbleUpon. Now not one thing I’d typically learn, but I favored your emotions none the less. Thank you for making something worth reading. 먹튀검증업체

Customer Support said...

My Assignment Help is the most preferred place to get law assignment help Australia services, where we offer 100% plagiarism free and money back guarantee ensuring complete satisfaction of students with every order. Our subject-specific experts are available 24×7 to provide properly formatted and referenced solutions in any subject from any level in Australian universities.

바카라게임사이트 said...

It's amazing. I want to learn your writing skills. In fact, I also have a website. If you are okay, please visit once and leave your opinion. Thank you. 바카라게임사이트

Great Assignment Helper said...

In today’s world people are very much stucked in their personal works, same is happening with the students that’s why we have strated the services of the Law assignment help. Connect with us to avail this premium offer.

Anonymous said...

Thanks for sharing such an interesting post. it services raleigh

Anonymous said...

Thanks for sharing such a nice blog. milwaukee fence installation

Anonymous said...

Glad to found this amazing website. foundation-repair-kansas-city

Anonymous said...

Nice blog. Looking for more info! It services fort lauderdale

casinositeone.JDS said...

I have read this post and if I could I want to suggest you some interesting things or advice. 카지노사이트

casinosite777infoJDS said...

Maybe you can write next articles referring to thisarticle. 카지노사이트

casinositezoneJDS said...


Sweet blog! I found it while browsing on Yahoo News. 카지노사이트

casinositeguidecomJDS said...

Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! 바카라사이트

casinosite said...

I was looking for another article by chance and found your article casinosite I am writing on this topic, so I think it will help a lot. I leave my blog address below. Please visit once.

suck111 said...

Good job! เกมยิงปลาได้เงินจริง
I'm glad I read it. เกมหมุนวงล้อ
Really good article. ae sexy
I am developing my thesis work. betflix joker
Earning money from playing games. joker 777

pg slot game said...

Earning money from playing games. สล็อต ออนไลน์
Wow good trip. รูเล็ต
Thank you very much. ไฮโล
Really good article. brtflix
I am developing my thesis work. สล็อต pg ทดลองเล่น

slotxo8 said...

This weblog is wonderful i love studying your articles. Stay up the good work! You realize, many people are looking around for this information, you can aid them greatly. slotxo168

Case Study Help said...

Facing Problem inCase Study Help ? Well, Instant Assignment is a place where you will get professional support from case study writing experts from skilled and knowledgeable writers at an affordable price.Our team of writers makes the best support to students.

Nikita Soni said...

To get started with norton antivirus, you need to visit Norton.com/setup and enter the norton product key. Norton setup prduct key is a 25 digit activation code.

albert said...

Microsoft Edge is Microsoft’s newest web browser, replacing Internet Explorer in Windows 10. Here’s everything you need to know about Microsoft Edge, whether you’re using Windows 10 or Windows 7 or 8!

albert said...

A CPU (central processing unit) is one of the core components of any computer, laptop, or tablet. It’s the brain of the computer—it’s in charge of running all the calculations necessary to turn on the screen and allow you to use your device.

albert said...

An operating system is software that manages computer hardware resources and provides common services for computer programs. A key function of an operating system is to provide an environment in which applications can run. In general, operating systems are categorized by their purpose as well as their structure.

albert said...

What Is Windows 10? Microsoft’s latest operating system, called windows 10, has finally been released, and it’s been met with great reviews.

software said...

Canon enhances the printer scan functionality, and includes a robust security feature set. For more information, you can visit our website at ij.start.canon and get more information at your convenience.

software said...

To install Office Setup, go to office.com/setup website and enter the Office Setup product key.

software said...

Webroot is a subsidiary of Carbonite, which is, in turn, a subsidiary of OpenText, which provides enterprise information management software, including security software, to companies. For more information visit our website at webroot.com/safe

software said...

If you do not have a Norton account, to create an account from your web browser, go to norton.com/setup and click on Sign In. After that you complete your sign-up process.

software said...

Provide the best MS Office product key for your office. To install the Office Setup Product Key, click on the link office.com/setup given by us.

software said...

Amazon.com mytv is probably the best platform to watch movies and TV shows on the web continuously. Now you will be able to watch your favorite shows on your PC or PC as well as on your Smart TV and Gushing Stick. For more details click on our website amazon.com/mytv

Modafinila said...
This comment has been removed by the author.
001vpn said...

This post is really astounding one! I was delighted to read this, very much useful. Many thanks Feel free to visit my website.
Several useful twitter video download sites
my blog:twitter-videos-download

001vpn said...

This type of article that enlighted me all throughout and thanks for this.This going to be excitement and have fun to read. thank to it. check this out to for further exciting.

my blog: Introducing 21 of the Best Free Material Sites

바카라사이트^ said...

It's really great. Thank you for providing a quality article. There is something you might be interested in. Do you know 바카라사이트 ? If you have more questions, please come to my site and check it out!

nt88 said...

เว็บเล่นสล็อตแตกง่าย ได้เงินจริง ฟรีเครดิตสำหรับผู้เล่นใหม่ และ ได้เป็นที่สุดของเว็บสล็อตแตกบ่อยแตกจริง 2022 ต้องที่นี้เท่านั้น เล่นสล็อตได้เงินจริง NT88

Michael Wade said...

It'd be interesting to see how C++11 lambdas compare to bind. In particular, in what situations is bind preferred. my friend who wrote article on business and management dissertation topics, he is very excited about codding c++

Jane Wilson said...


I'm grateful that you shared this insightful information. Looking for MATLAB Dissertation Help? With a deadline in mind, we offer the greatest assistance with MATLAB dissertation help here. We also offer round-the-clock service.