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:

118 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 !!!

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

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

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

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

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.แทงบอล

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

스포츠중계 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.

ɮɛȶʍǟռȶօȶօ.օʀɢ 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 토토


메이저안전놀이터 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; 토토사이트

스포츠토토사이트 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.

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. 메이저토토사이트

토토사이트추천 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. 경마사이트

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

Dissertation Help 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

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.

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

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! 바카라사이트

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 ทดลองเล่น

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

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.

software said...

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

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.

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

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.

Essay Writing Help said...

Thank you for sharing information
Are you looking for Essay Writing Help at a cost-effective price? We are an expert academic writing company that provides assignment assistance to students enrolled in colleges and universities. Qnaassignmenthelp.com offered services to students for their Essay Writing & Editing in any topic.

Claris Shere said...

For all of us that apply for your son or daughter, along with the best instructors, you can expect teaching and personal teaching expert services around Jabodetabek ... go to https://kursus-akuntansi.netlify.app/kursus-akuntansi-tanah-abang.html for further

Accounting Assignment Help said...

Thank you for your update
Avail Accounting Assignment Help Online from Subject Matter Experts at Assignmenttask.com.
Assignment Task is one of the best assignment help services providers –We offer assignment writing, essay writing, case study help & dissertation proposal to the students K12 to PhD level in worldwide at best prices. Students can contact us through our 24/7 live chat facility.We have teams of skilled and experienced writers who helping student for assignment writing and their projects in any subjects.
Features of our services are:
24/7 Instant Support
100% Plagiarism Free
On Time Delivery
Subject Area Experts

Consumer Behavior Assignment Help said...

Thank you for sharing this wonderful article

Get Excellent High Quality of Consumer Behavior Assignment Help from Professional Writers. Casestudyhelp.com helps students with their assignment help & writing services in the United Kingdom, United States, Canada, Malaysia, Singapore and other countries. We have a vast team of well-experienced writers who assist you with plagiarism-free content. Students can avail of our solutions with just a visit. Place your order now.

사설토토 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사설토토

Earl Hegel said...

Executive-Education.id has become the tutoring / tutoring services in Jabodetabek as well as its surroundings. Our Bimbel provides learning programs for elementary, middle, senior high school and various exam preparation ... click https://privat-matematika.vercel.app/les-privat-matematika-alam-sutera.html for further

usps liteblue said...

nice article here

Commercial and Residential Cleaning Pittsburgh PA said...

this is great

Reoccurring Pool Maintenance services Charleston SC said...

this is a good blog

Dog and pet grooming Services Charleston SC said...

thanks for providing

Organizer to Declutter home Charleston SC said...

this is awesome

Professional Wedding DJ Services Pittsburgh PA said...

thanks for sharing

Angel17 said...

What a wonderful post. Thanks for sharing! https://www.appliancerepairedm.ca/sherwood-park.html

Bella Feme said...

The blogging skills you have, has been very helpful for the readers. You gave a great start in the blogging world. I have taken notice of your writing skills and will keep this in mind for further reading your upcoming blogs. Skin Care Tips for Women

Guest said...

Excellent weblog, great work. Thanks for this! Concrete Patios

mbuotidem said...

Great post thank you for sharing the information. Check veritas university jupeb admission forms out

whitneyroth345 said...

My favorite food blog is this one. I think the style, design and tone of voice is just top notch. Find out More

lowenthalabrams034 said...

The site is truly basic, and something you could construct yourself on WordPress in merely day, however the substance is the place where this site dominates. home remedies for dog UTI

Tech Admin said...

I appreciate the effort you put into creating this piece and look forward to reading more of your work in the future. Khloe Kardashian Net Worth 2023

mailesnow0234 said...

His webcasts are truly well known and when you have content that gets a ton of ears and eyes to it, at that point so will the promoting pay. dog ear cleaning solution

https://www.sulz.edu.pl said...

Polski Mapa miasta GO FOR MORE BACKLINKS

mailesnow0234 said...

The site is truly basic, and something you could construct yourself on WordPress in merely day, however the substance is the place where this site dominates. how to treat dog ear infection without vet

Anonymous said...

www.tndte.gov.in result 2023

Julia Hadson said...

If you're struggling to understand asynchronous programming in C++, or you're looking for help with a specific assignment related to this topic, consider seeking the assistance of an experienced programming tutor or seeking online https://royalassignmenthelp.co.uk/. With the right guidance and resources, you can gain the skills and confidence you need to tackle even the most complex asynchronous programming challenges in C++.

Julia Hadson said...

If you're struggling to understand asynchronous programming in C++, or you're looking for help with a specific assignment related to this topic, consider seeking the assistance of an experienced programming tutor or seeking online assignment help. With the right guidance and resources, you can gain the skills and confidence you need to tackle even the most complex asynchronous programming challenges in C++.

zeldalittle023 said...

Also it could encourage to installation their personal weblog and to put in writing approximately their stories as a mom and a spouse withinside the twenty first century.
stenciled concrete mornington

zeldalittle023 said...

extraordinary illustration of a site that began as a side interest yet has quick become a business. I like the point they have also. https://www.morningtonconcreting.com.au/coloured-concrete.html

vanriley256 said...

It’s a blog with a very active podcast feed. Their podcasts are insanely popular on itunes, and no doubt they make a fair bit of money from selling ad space on those podcasts. ROOF REPAIR ST. GEORGE

Case Study Writing Help said...

I know many Australian university students need help with case study writing but they are afraid to ask for help. Research Prospect Australia offers 100% confidential case study writing services to help students achieve the highest possible academic grade.

Essays UK said...

Every time, I sit down to start my essay I start getting horrible dreams. I wanted help and Essays UK came to the rescue. I simple can’t thank them enough for supporting me with their education.

Assignment Help USA said...

Assignment Help in USA can be difficult to find because their is a lot of assignment writing services in the market which are really nothing but scam for students. I found one that truly delivers the promises. Thank you Assignment Help Center.

Literature Review Writing Help said...

Just got jacked because I couldn't complete the literature review in time. Really discouraging. But I found literature review writing service from Research Prospect which got me sorted eventually.

jwolfe23 said...

It’s a blog with a very active podcast feed. Their podcasts are insanely popular on itunes, and no doubt they make a fair bit of money from selling ad space on those podcasts.
FLOOR CLEANING

peterglass0934 said...

online community of entrepreneurs who’s goal is to gain financial freedom through creating businesses that can help them achieve that. https://bendigoconcrete.com.au/coloured-concrete/