c++ - boost的read函数参数和bind函数参数看不懂
PHP中文网
PHP中文网 2017-04-17 13:07:24
[C++讨论组]

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

<code>/* 这是一个同步的echo客户端

 * 在sync_echo()函数中的read函数的第三个参数中,bind有两个占位符号,

 * 调用read_complete()函数,可是在read_complete()函数中,需要用到bytes这个参数

 * 我想请问这个bytes是怎么传进去的?

*/

#include <iostream>

#include <boost/thread.hpp>

#include <boost/bind.hpp>

#include <boost/asio.hpp>

#include <boost/shared_ptr.hpp>

#include <boost/enable_shared_from_this.hpp>

 

using namespace boost::asio;

using boost::system::error_code;

 

io_service service;

 

size_t read_complete(char *buf, const error_code &err, size_t bytes)

{

    if (err)

        return 0;

    bool found = std::find(buf, buf+bytes, '\n') < buf + bytes;

    // we read one-by-one until we get to enter, no buffering

    return found? 0: 1;

}

 

ip::tcp::endpoint ep(ip::address::from_string("127.0.0.1"), 8001);

 

void sync_echo(std::string msg)

{

    msg += "\n";

    ip::tcp::socket sock(service);

    sock.connect(ep);

    sock.write_some(buffer(msg));

    char buf[1024];

    int bytes = read(sock, buffer(buf), boost::bind(read_complete, buf, _1, _2));

    std::string copy(buf, bytes - 1);

    msg = msg.substr(0, msg.size() - 1);

    std::cout << "server echoed our " << msg << ": "

        << (copy == msg? "OK": "FAIL") << std::endl;

    sock.close();

}

 

int main(int argc, char *argv[])

{

    // connect several clients

    char *messages[] = {"John says hi",

    "so does James", "Lucy just not home",

    "Boost.Asio is fun!", 0 };

    boost::thread_group threads;

    for (char **message = messages; *message; ++ message)

    {

        threads.create_thread(boost::bind(sync_echo, *message));

        boost::this_thread::sleep(boost::posix_time::millisec(100));

    }

    threads.join_all();

}</code>

PHP中文网
PHP中文网

认证0级讲师

全部回复(2)
天蓬老师

第一次看到,确实有点惊艳到。
搬运,链接:http://zh.highscore.de/cpp/boost/functionobjects.html
关键摘抄:_1 被称为占位符(placeholder),定义于 Boost.Bind。 除了 _1,Boost.Bind 还定义了 _2 和 _3。 通过使用这些占位符,boost::bind() 可以变为一元、二元或三元的函数。
个人理解:
就是说,read函数要求的第三个参数本身是个有2个参数的函数。然后针对你的问题,对于read_complete的三个参数:read函数传入的const error_code &err, size_t bytes,你传入的是buf
如果可以的话,你看read函数源码的执行流程证实一下就知道了。

阿神

问:

我想请问这个bytes是怎么传进去的?

答:在这里:int bytes = read(sock, buffer(buf), boost::bind(read_complete, buf, _1, _2));

read_complete有三个参数,bind的意思就是说,第一个参数是buf,第二个参数是返回的function的第一个参数,第三个参数是返回的function的第二个参数。如果你把_1和_2换过来,那么你在调用返回的function的时候参数也要换过来。

使用现代C++的写法如下:
旧:

1

2

<code>boost::bind(read_complete,b uf, _1, _2)

</code>

新:

1

2

3

4

<code>[&buf](auto a, auto b)

{

    return read_complete(buf, a, b);

}</code>

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号