Binding a member signal to a function

Posted by the_drow on Stack Overflow See other posts from Stack Overflow or by the_drow
Published on 2010-05-19T02:13:28Z Indexed on 2010/05/19 2:20 UTC
Read the original article Hit count: 474

This line of code compiles correctly without a problem:

boost::bind(boost::ref(connected_),
            boost::dynamic_pointer_cast<session<version> >(shared_from_this()),
            boost::asio::placeholders::error);

However when assigning it to a boost::function or as a callback like this:

socket_->async_connect(connection_->remote_endpoint(),
                      boost::bind(boost::ref(connected_),
                      boost::dynamic_pointer_cast<session<version> >(shared_from_this()),
                      boost::asio::placeholders::error));

I'm getting a whole bunch of incomprehensible errors (linked since it's too long to fit here).

On the other hand I have succeeded binding a free signal to a boost::function like this:

void print(const boost::system::error_code& error)
{
    cout << "session connected";
}

int main()
{
boost::signal<void(const boost::system::error_code &)> connected_;
connected_.connect(boost::bind(&print, boost::asio::placeholders::error));

    client<>::connection_t::socket_ptr socket_(new client<>::connection_t::socket_t(conn->service())); // shared_ptr of a tcp socket

    socket_->async_connect(conn->remote_endpoint(),
                       boost::bind(boost::ref(connected_),
                       boost::asio::placeholders::error));
    conn->service().run(); // io_service.run()
    return 0;
}

This works and prints session connected correctly. What am I doing wrong here?

© Stack Overflow or respective owner

Related posts about boost-bind

Related posts about boost-signals