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_de开发者_JAVA技巧adline_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_timer
has a constructor that requires a non-const reference to a service. However, when you use make_shared
its parameter is const
. That is, this part of make_shared
is the problem:
template< class T, class A1 > // service is passed by const-reference
boost::shared_ptr< T > make_shared( A1 const & a1 )
{
// ...
::new( pv ) T( a1 ); // but the constructor requires a non-const reference
// ...
}
What you can do is wrap the service up into a reference_wrapper
, using ref
:
#include <boost/ref.hpp>
asio::io_service io1;
shared_ptr<asio::deadline_timer> dt = // pass a "reference"
make_shared<asio::deadline_timer>(boost::ref(io1));
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_wrapper
really stores a pointer to your instance. It can therefore return that pointer dereferenced while still being const
.
精彩评论