title: Initializing boost::asio sockets after constructor
body: <p>[Update:] The answer for whoever is interested, is simply wrapping the io_service member var in boost::ref (boost::ref(io_service_))</p>
  
  <p>I am experimenting with the udp server example from boost asio libraries, to see if I can initialize the socket somewhere other than in the constructor. The constructor proposed in the example is the following:</p>
  
  <pre><code>class server
  {
  public:
    server(boost::asio::io_service&amp; io_service, short port)
      : io_service_(io_service),
        socket_(io_service, udp::endpoint(udp::v4(), port))
    {
      socket_.async_receive_from(
          boost::asio::buffer(data_, max_length), sender_endpoint_,
          boost::bind(&amp;server::handle_receive_from, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
    }
  </code></pre>
  
  <p>I made the socket member a pointer, and also a shared pointer so that I don't have to be worried about whether it gets out of scope. "io_service_" is also a member variable and I use it to create the socket. My full code is the following:</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include &lt;boost/bind.hpp&gt;
  #include &lt;boost/asio.hpp&gt;
  #include &lt;boost/shared_ptr.hpp&gt;
  #include &lt;boost/make_shared.hpp&gt;
  
  using boost::asio::ip::udp;
  
  class server
  {
  public:
    server(boost::asio::io_service&amp; io_service, short port)
      : io_service_(io_service)
        //socket_(io_service, udp::endpoint(udp::v4(), port))
    {
        initialize_socket(port);
    }
  
    void initialize_socket(short port)
    {
  
        socket_p = boost::make_shared&lt;udp::socket&gt;(io_service_, udp::endpoint(udp::v4(), port));
        // (...)
        socket_p-&gt;async_receive_from(
                boost::asio::buffer(data_, max_length), sender_endpoint_,
                boost::bind(&amp;server::handle_receive_from, this,
                        boost::asio::placeholders::error,
                        boost::asio::placeholders::bytes_transferred));
    }
  
    void handle_receive_from(const boost::system::error_code&amp; error,
        size_t bytes_recvd)
    {
      if (!error &amp;&amp; bytes_recvd &gt; 0)
      {
        std::cout &lt;&lt; "\nReceived: ";
        std::cout.write(data_, bytes_recvd);
        std::cout &lt;&lt; "\nSending same string back" &lt;&lt; std::endl;
        socket_p-&gt;async_send_to(
            boost::asio::buffer(data_, bytes_recvd), sender_endpoint_,
            boost::bind(&amp;server::handle_send_to, this,
              boost::asio::placeholders::error,
              boost::asio::placeholders::bytes_transferred));
      }
      else
      {
        socket_p-&gt;async_receive_from(
            boost::asio::buffer(data_, max_length), sender_endpoint_,
            boost::bind(&amp;server::handle_receive_from, this,
              boost::asio::placeholders::error,
              boost::asio::placeholders::bytes_transferred));
      }
    }
  
    void handle_send_to(const boost::system::error_code&amp; /*error*/,
        size_t /*bytes_sent*/)
    {
      socket_p-&gt;async_receive_from(
          boost::asio::buffer(data_, max_length), sender_endpoint_,
          boost::bind(&amp;server::handle_receive_from, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
    }
  
  private:
    boost::asio::io_service&amp; io_service_;
    boost::shared_ptr&lt;udp::socket&gt; socket_p;
    udp::endpoint sender_endpoint_;
    enum { max_length = 1024 };
    char data_[max_length];
  };
  
  int main(int argc, char* argv[])
  {
    try
    {
      if (argc != 2)
      {
        std::cerr &lt;&lt; "Usage: async_udp_echo_server &lt;port&gt;\n";
        return 1;
      }
  
      boost::asio::io_service io_service;
  
      using namespace std; // For atoi.
      server s(io_service, atoi(argv[1]));
  
      io_service.run();
    }
    catch (std::exception&amp; e)
    {
      std::cerr &lt;&lt; "Exception: " &lt;&lt; e.what() &lt;&lt; "\n";
    }
  
    return 0;
  }
  </code></pre>
  
  <p>When trying to compile, I get a rather long error message from where I think the important parts are:</p>
  
  <pre><code>required from here
      /usr/local/include/boost/smart_ptr/make_shared_object.hpp:747:5: error: no matching function for call to ‘boost::asio::basic_datagram_socket&lt;boost::asio
      ::ip::udp&gt;::basic_datagram_socket(const boost::asio::io_service&amp;, const boost::asio::ip::basic_endpoint&lt;boost::asio::ip::udp&gt;&amp;)’
           ::new( pv ) T( a1, a2 );
  </code></pre>
  
  <p>(...)</p>
  
  <pre><code>   basic_datagram_socket(boost::asio::io_service&amp; io_service,
     ^
  /usr/local/include/boost/asio/basic_datagram_socket.hpp:108:3: note:   no known conversion for argument 1 from ‘const boost::asio::io_service’ to ‘boost
  ::asio::io_service&amp;’
  </code></pre>
  
  <p>Anybody knows what was wrong with this implementation?</p>