Bind illustrated
来源:互联网 发布:无锡程序员工资 编辑:程序博客网 时间:2024/04/30 00:28
原文地址: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::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:
- Bind illustrated
- XQuery [ILLUSTRATED]
- bind()
- bind()
- Bind
- bind
- bind
- bind
- bind
- bind
- bind
- bind()
- bind()
- TCP/IP Illustrated
- Essential CVS [ILLUSTRATED]
- ASP.NET 2.0 Illustrated
- ASP.NET 2.0 Illustrated
- Switching to VoIP [ILLUSTRATED]
- 秒级处理海量数据,浙江移动大数据平台是怎么做到的
- 绘制优化
- [Codeforces297D]Color the Carpet(构造)
- 实例变量修饰符(@public@protected@private@package )
- 如何在网页中写javascript
- Bind illustrated
- IT面试
- 正则表达式
- ROS探索(3)——SmartCar模型搭建
- 第四届蓝桥杯决赛第四题 高僧斗法 (nim博弈 sg函数)深究
- vertica-卸载
- Board Silly(poj 1558)模拟
- c++第12周实验:运算符的重载
- Simple Game