I'm on CentOS 6.6 (gcc 4.4.7) and developing with Boost.Asio (1.41). I'd like io_service to call member function run()
in manger object m
when it starts. The code I'm trying to compile looks like:
#include <memory>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
boost::asio::io_service io;
std::unique_ptr<manager> m;
m = std::make_unique<manager>;
io.post(boost::bind(&manager::run, &m));
gcc pitches a fit on the boost::bind
statement, which includes:
/usr/include/boost/bind/mem_fn_template.hpp:40: error: pointer to
member type ‘void (manager::)()’ incompatible with object type
‘std::unique_ptr<manager, std::default_delete<manager> >’
What do I want to be doing here?
The manager object will only know about timers; a separate object that knows about io_service will get added to its constructor later. But the idea is that manager::run()
will create an initial set of timers to bootstrap the system.
Clarification:
My thinking here is that the outer block of code manages the lifetime of m
and that the next statement will be io.run()
. The outer code will destroy m
when io.run()
returns. Hence, passing a raw reference for m
to io
is appropriate. But I'm a modern C++ novice and could be way off base here.
You'd need C++-14 and generalized lambda capture to make this work -- you'd need to move the unique pointer into the lambda. Instead, just use a shared_ptr
, which std::bind
understands natively:
std::shared_ptr<manager> m;
m = std::make_shared<manager>();
io.post(std::bind(&manager::run, std::move(m)));
The std::move
is optional but ensures that m
doesn't keep the manager around when it's not wanted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With