原文地址:http://blog.think-async.com/2010/04/bind-illustrated.html

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::bindstd::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:

Bind illustrated相关推荐

  1. centos7配置bind重启后错误解决

    最近研究centos7安装bind做DNS服务器,都配置好了后,重启后用systemctl status named 发现好多诸如以下错误:error (network unreachable) re ...

  2. 函数指针amp;绑定: boost::functoin/std::function/bind

    see link: https://isocpp.org/wiki/faq/pointers-to-members function vs template: http://stackoverflow ...

  3. 【硬核技能】舒工自创bind绑定数据方法,类似angular和vue绑定数据原理

    if ($g) {$g.$utils || ($g.$utils = {}); } else {var $g = {};$g.$utils = {}; } /*绑定数据神器*/ $g.$utils.b ...

  4. C语言网络编程:bind函数详解

    文章目录 函数功能 函数头文件 函数使用 函数参数 函数举例 为什么需要bind函数 服务器如何知道客户端的ip和端口号 htons函数 `htons`兄弟函数`htonl`,`ntohs`,`nto ...

  5. 【C++】bind参数绑定 P354(通用的函数适配器)

    1. 什么时候该使用bing ?什么时候该使用lambda? 当只有少数地方调用时候使用lambda,当需要多次调用lambda时,需要定义一个函数,而不是多次编译相同的lambda表达式. 调用bi ...

  6. 用bind架设自己的智能DNS

    中国的南北网络问题,是许多做网站的人的心病 除了使用双通或者多通机房以外,还可以通过多台镜像服务器的方法来提高用户的访问速度 但是,如果使用的双通机房并不是单IP的,或者使用多台镜像的做法,就会面临多 ...

  7. C++11中std::bind的使用

    std::bind函数是用来绑定函数调用的某些参数的.std::bind它可以预先把指定可调用实体的某些参数绑定到已有的变量,产生一个新的可调用实体.它绑定的参数的个数不受限制,绑定的具体哪些参数也不 ...

  8. 【Ubuntu】解决问题:tcp :8080: bind: address already in use

    1.问题描述 在ubuntu中启动一个web程序时报错 tcp :8080: bind: address already in use 2.原因查找 查找占用8080的服务:sudo netstat ...

  9. docker mysql data volume_Docker 持久化存储, Data Volume/Bind Mounting

    docker容器, 再启动之后 我们可以对其进行 修改删除等等. 如果是一个数据库的容器, 里面的数据 不想随着这个容器的消失, 而消失.  就需要持久化数据存储. Data Volume 这是 do ...

最新文章

  1. Linux学习(五)---开机、重启和用户登录注销
  2. vc6.0快捷键大全- -
  3. lodoop打印控件详解
  4. CodeForces - 1486F Pairs of Paths(树上计数+容斥)
  5. [Yii Framework] 数据库查询
  6. hdu 5441 (并查集)
  7. Java谓词的延迟执行
  8. Python四大金刚
  9. python保存rtmp流_ffmpeg 推送、保存rtmp 流命令
  10. 那个 CEO 写下 70 万行代码的公司,马上要上市了
  11. stm32串口_【单片机】STM32串口基本配置
  12. NLP --- 隐马尔可夫HMM(EM算法(期望最大化算法))
  13. python中out函数_cython函数输出与python函数outpu略有不同
  14. 黄绿色——五色配色篇
  15. 骨架屏Skeleton Screen
  16. 力扣(LeetCode)244. 最短单词距离 II(2022.09.01)
  17. windows系统镜像修复计算机,为你详解win7系统还原及映像修复功能
  18. 迁移学习,DomainAdaption 笔记
  19. Thinkcmf QQ邮箱配置
  20. 中台渐入佳境,云徙科技的有所为与有所不为

热门文章

  1. 互联网时代,真的不需要传统杂志了吗?
  2. 关于下载安装latex报错的问题,texlive2021版
  3. Android开发-手机适配之values目录
  4. ORB-SLAM2构建稠密地图 ros-kinetic 深度相机
  5. Codeforces 1215E. Marbles
  6. SOFE之StackOverflowError
  7. 软考高级 2021年信息系统项目管理师视频教材历年真题资料
  8. 批处理打开指定路径下的所有文件
  9. 【UI篇】Android 沉浸式状态栏的那些事
  10. 36管四轮电动车控制器代码, 原理图和Pcb,完整可用。