Boost logo

Boost Users :

Subject: [Boost-users] [asio] question about binding member function as WaitHandler
From: eburkitt_at_[hidden]
Date: 2008-11-04 22:39:13

The documentation's example 4 uses boost::bind to create a handler for
deadline_timer::async_wait(). The function bound, printer::print(), takes no
parameter and the code compiles and works fine. I tried modifying the
example so that, instead of creating a new, temporary function object in
place for each call to async_wait(), it creates a single boost::function
object as a data member in the ctor, which is then passed in each call.

Using MSVS 2005 and boost 1.37 I found that I had to add a
boost::system::error_code& parameter to print() in order to get it to
compile. Here is my modified printer class, with both the non-working
(version 1) and working (version 2) code to, I hope, make this clearer.

class printer
printer(boost::asio::io_service& io)
: timer_(io, boost::posix_time::seconds(1)),
// timer_fn_(boost::bind(&printer::print, this)), //**version 1
timer_fn_(boost::bind(&printer::print, this, _1)), //**version 2

std::cout << "Final count is " << count_ << "\n";

//void print() //**version 1
void print(const boost::system::error_code& /*e*/) //**version 2
if (count_ < 5)
std::cout << count_ << "\n";

timer_.expires_at(timer_.expires_at() +

//typedef boost::function<void(void)> timer_fn_type; //**version 1
typedef boost::function<void(const boost::system::error_code&)>
timer_fn_type; //**version 2
boost::asio::deadline_timer timer_;
timer_fn_type timer_fn_;
int count_;

The error report for version 1 is "term does not evaluate to a function
taking 1 arguments" at boost/asio/detail/bind_handler.hpp line 39.

I can see why version 1 won't compile. What I don't understand is why the
original example, with its in-place function objects, does.

Thanks for any insight.


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at