本文介绍了的boost :: ASIO async_wait处理程序签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经历了boost :: ASIO的例子。我在看
Example 4

I am going through the boost::asio examples. I am looking at Example 4

什么是混淆的是,该WaitHandler在本实施例具有与签名

What is confusing is that, the WaitHandler in this example has the signature

无效打印(本)

但async_wait调用需要的处理程序,其

But the async_wait call expects a handler whose

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

无效处理程序(
    常量的boost ::系统::错误_ code&放;操作错误//结果。
  );

void handler( const boost::system::error_code& error // Result of operation. );

Source: Boost文档

由于参数类型是一个函数的签名,为什么在上面的例子中,async_wait接受一个处理程序,其参数的一部分的类型的boost ::系统::错误_ code

Since the parameter type is part of a function's signature, why in the example above, async_wait accepts a handler whose parameter is not of type boost::system::error_code?

感谢。

推荐答案

当你正确地观察,该async_wait方法接受一个处理函数,它接受一个参数(常量的boost ::系统::错误_ code&安培)。但在Timer.4例如,async_wait呼叫通过升压绑定通过如下:

As you correctly observe, the async_wait method accepts a handler function which takes one parameter (const boost::system::error_code&). But in the Timer.4 example, the call to async_wait is passed via boost bind as follows:

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

了boost ::绑定返回一个函数对象是指方法的打印的类的打印机的通过的这个的引用的对象。此功能目的是通过用误差参数async_wait方法(因为这是签名,预计)调用。但错误的参数被悄悄忽略,因为它不会被绑定引用。

The boost::bind returns a function object which refers to method print for class printer for the object referenced by this. This function object is called by the async_wait method with the error parameter (since that is the signature it expects). But the error parameter is silently ignored because it is not referenced by the bind.

官方提供了更多的上的boost ::绑定的详细信息。另请参见文章(有可能还有更多文章可用,但我发现这是非常有用的)。

The official boost::bind documentation provides more details on boost::bind. See also the article How the Boost Bind Library Can Improve Your C++ Programs (there are probably many more articles available but I found this one very useful).

这篇关于的boost :: ASIO async_wait处理程序签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 10:12