代码之家  ›  专栏  ›  技术社区  ›  navigator

Boost::asio异步等待处理程序签名

  •  6
  • navigator  · 技术社区  · 15 年前

    我将介绍boost::asio示例。我在看 Example 4

    作废打印(本)

    但是async_wait调用需要一个

    处理程序的函数签名必须是:

    常量boost::系统::错误代码&错误//操作的结果。

    Source: Boost documentation

    既然参数类型是函数签名的一部分,为什么在上面的示例中,async_wait接受的处理程序的参数类型不是boost::system::error_code?

    2 回复  |  直到 11 年前
        1
  •  12
  •   Yukiko    15 年前

    正如您正确观察到的,async_wait方法接受一个处理函数,该函数接受一个参数(const boost::system::error_code&)。但在Timer.4示例中,对async_wait的调用通过boost bind传递,如下所示:

    timer_.async_wait(boost::bind(&printer::print, this));
    

    bind返回一个引用方法的函数对象 打印 上课 打印机 . 此函数对象由async_wait方法使用error参数调用(因为这是它期望的签名)。但是错误参数被默默忽略,因为绑定没有引用它。

    boost::bind documentation 提供有关boost::bind的更多详细信息。另见文章 How the Boost Bind Library Can Improve Your C++ Programs

        2
  •  2
  •   Sam F microo8    3 年前

    在对async_wait的调用中,可以使用占位符指定回调函数的参数。检查链接页面上async_wait调用上方的句子:

    您将注意到boost::asio::placeholders::error占位符是 错误对象作为参数。

    在中搜索“占位符” this example 你将看到如何做到这一点。