This simple example fails to compile in VS2K8:
io_service io2;
shared_ptr<asio::deadline_timer> dt(make_shared<asio::deadline_timer>(io2, posix_time::seconds(20)));
As does this one:
shared_ptr<asio::deadline_timer> dt = make_shared<asio::deadline_timer>(io2);
The error is:
error C2664: ‘boost::asio::basic_deadline_timer::basic_deadline_timer(boost::asio::io_service &,const boost::posix_time::ptime &)’ : cannot convert parameter 1 from ‘const boost::asio::io_service’ to ‘boost::asio::io_service &’
The problem is that
asio::deadline_timerhas a constructor that requires a non-const reference to a service. However, when you usemake_sharedits parameter isconst. That is, this part ofmake_sharedis the problem:What you can do is wrap the service up into a
reference_wrapper, usingref:This takes your instance, and puts it into an object that can be converted implicitly to a reference to your isntance. You’ve then essentially passed an object representing a non-const reference to your instance.
This works because the
reference_wrapperreally stores a pointer to your instance. It can therefore return that pointer dereferenced while still beingconst.