Boost :: asio async_wait handler signature

I am looking at examples of boost :: asio. I look Example 4

What is confusing is that WaitHandler in this example has a signature

void print (this)

But the async_wait call expects a handler whose

handler function signature should be:

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

Source: documentation

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

THANKS.

+6
boost boost-asio
source share
2 answers

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

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

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

The official boost :: bind publication contains more information about boost :: bind. See also the article How the Bost Bind library can improve your C ++ programs (there are probably many more articles, but I found this very useful).

+11
source share

The async_wait call indicates what parameters your callback function uses using placeholders. Check the sentence just above the async_wait call on the page you linked to:

You will notice that the name boost :: asio :: placeholders :: error placeholder is not specified here, since the print member function does not accept the error object as a parameter.

Find the "placeholder" in this example and you will see how to do it.

+2
source share

All Articles