【问题标题】:Boost Asio async_wait handlerBoost Asio async_wait 处理程序
【发布时间】:2012-01-30 04:15:38
【问题描述】:

boost asio deadline_timer async_wait 函数采用以下形式的处理程序:

void handler(const boost::system::error_code& error)

我如何定义一个接受const boost::system::error_code& errorint 类型参数的处理程序?

boost::asio::deadline_timer t(io_service);

t.async_wait(handler); //I need the async_wait to take in handler which accepts argument boost::system::error_code& error and an int 

void handler(int, const boost::system::error_code& error )//extra int argument

谢谢。

【问题讨论】:

    标签: c++ boost-asio


    【解决方案1】:

    您可以使用Boost.Bind 为第一个参数提供值:

    t.async_wait(boost::bind(handler, 0, _1));
    

    这里,将使用 0 作为其第一个参数调用处理程序,而 error_code 将简单地作为第二个参数转发。

    【讨论】:

    • 感谢您的回复。只是好奇,符号 _1 在这种情况下是什么意思?在其他地方我可以在哪里使用它?
    • @Steveng:它是一个占位符,仅在boost::bind 的上下文中有意义,它指的是传递给此bind 返回的函子的第一个参数。 bind 返回的函子采用的参数数量是指定占位符的最大序数:这里,bind 的结果是一个函子,它需要一个作为handler 的第二个参数转发的参数。
    • @Steveng:实际上文档说我这样做要好得多“_1 是一个占位符参数,意思是“用第一个输入参数替换。”
    猜你喜欢
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    相关资源
    最近更新 更多