views:

132

answers:

1

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 &'

+5  A: 

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.

GMan
Excellent, thank you! Had no idea about boost::ref. Was it created in part to get around this type of thing, or are there other uses?
ApplePieIsGood
@Apple: It was designed pretty much for this reason. For example, `boost::thread` will make a copy of its arguments for the new thread, but if you want to actually pass a reference `boost::ref` works there too. It's used whenever you want to pass a reference but the function intends on making a copy.
GMan