Ravikumar Tulugu
Ravikumar Tulugu

Reputation: 1722

errors using boost::function with boost::bind with boost::asio

i am facing problems combining boost::function with boost::bind and boost::asio. i am facing snippets of code down, the compiler throws tons of errors, i am pasting the root errors.

static void
startAccept(boost::asio::io_service &io,
        boost::asio::ssl::context &ctx,
        boost::asio::ip::tcp::acceptor &acceptor_,
        const boost::system::error_code &ec
        )
{
    //details omitted for brevity reasons.
    return;
}
 boost::function<void(
                boost::asio::io_service &,
                boost::asio::ssl::context &,
                boost::asio::ip::tcp::acceptor &,
                boost::system::error_code &
                )> f;
        f = boost::bind(&startAccept,
                boost::ref(io),
                boost::ref(ctx),
                boost::ref(acceptor_), 
                boost::asio::placeholders::error);

Errors:

In file included from as.cc:5:
In file included from /usr/local/include/boost/bind.hpp:22:
/usr/local/include/boost/bind/bind.hpp:457:93: error: no viable conversion from 'boost::asio::io_service' to
      'const boost::system::error_code'
        unwrapper<F>::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_], a[base_type::a3_], a[base_type::a4_]);
                                                                                            ^~~~~~~~~~~~~~~~~
/usr/local/include/boost/system/error_code.hpp:317:11: note: candidate constructor (the implicit copy constructor) not viable: no known
      conversion from 'boost::asio::io_service' to 'const boost::system::error_code &' for 1st argument
    class error_code
          ^
/usr/local/include/boost/system/error_code.hpp:317:11: note: candidate constructor (the implicit move constructor) not viable: no known
      conversion from 'boost::asio::io_service' to 'boost::system::error_code &&' for 1st argument
    class error_code
          ^

why is the compiler trying to convert boost::asio::io_service to boost::system::error_code ??

Upvotes: 0

Views: 1175

Answers (1)

sehe
sehe

Reputation: 392903

Since the signature already matches, there is no need to use boost::bind when assigning to boost::function:

#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>

static void startAccept(boost::asio::io_service &io, boost::asio::ssl::context &ctx,
                        boost::asio::ip::tcp::acceptor &acceptor_, boost::system::error_code ec) {
    // details omitted for brevity reasons.
    return;
}

int main() {
    boost::function<void(boost::asio::io_service &, boost::asio::ssl::context &, boost::asio::ip::tcp::acceptor &,
            boost::system::error_code)> f;

    f = &startAccept;
}

Note that I changed the last parameter, because it was mismatched in const&/& ness. That was not the issue though.

Once you bind the first parameters, the bound signature becomes a unary function. This might be what you want to pass as a completionhandler (depending on what the completionhandler signature is supposed to be):

object.async_operation(bufs, boost::bind(f,
            boost::ref(io), boost::ref(ctx), boost::ref(acceptor_),
            boost::asio::placeholders::error))

In this case it's a unary completion handler, compatible with boost::function<void(boost::system::error_code)>

Upvotes: 2

Related Questions