增强绑定和占位符 [英] boost bind and place holders

查看:119
本文介绍了增强绑定和占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我正在学习用于网络编程的Boost库,并且阅读了有关Boost Bind的信息,并对以下代码有疑问.

Hi all,
I am learning boost library for the network programming and i read about boost bind and have some doubts in the following code.

boost::asio::async_read_until(socket_, buf, boost::regex("\r\n\r\n"),
          boost::bind(&connection::handle_read, shared_from_this(),
                      ba::placeholders::error,
                      ba::placeholders::bytes_transferred));



绑定里面有三个参数



here inside bind it has three arguments

(shared_from_this(),
                      ba::placeholders::error,
                      ba::placeholders::bytes_transferred)



但是连接"类内部的成员函数"handle_read"只有两个自变量,如下所示.



but the member function "handle_read" inside the "connection" class has only two arguments as given below.

void handle_read(const boost::system::error_code& error,
                    size_t bytes_transferred) {
       ba::async_write(socket_, ba::buffer(message_),
                       boost::bind(&connection::handle_write, shared_from_this(),
                                   ba::placeholders::error,
                                   ba::placeholders::bytes_transferred));
   }



我不明白为什么绑定使用额外的参数shared_from_this(),我也想知道占位符.
任何人都可以对此进行澄清.


问候,
hari.



i dont understand why bind uses an extra argument shared_from_this(),i also want to know about the placeholders.
can any one please clarify on this .


with regards,
hari .

推荐答案

因为这是设计人员编写函数的方式;请注意,在两种情况下,boost::bind()实际上都采用四个参数.您需要检查文档以查看函数中每个参数的重要性.
Because that is the way the designer wrote the function; note that boost::bind() actually takes four parameters in both cases. You need to check the documentation to see the significance of each parameter in a function.


这篇关于增强绑定和占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆