如何将boost回调作为参数传递给方法?

时间:2018-07-06 07:51:57

标签: c++ boost parameter-passing boost-asio

我无法将boost回调之一传递给我的方法。

短篇小说: 我正在尝试与boost::asio::ip::tcp::socket合作 我将套接字包装到类SerializationSocket中。

在某些时候我需要使用异步接受,这就是为什么我需要将回调传递到SerializationSocket中。

服务器部分:

void accept_handler(const boost::system::error_code& error)
{}

SerializationSocket socket(1950, accept_handler);

SerializationSocket:

typedef void (SerializationSocket_cb)
     (const boost::system::error_code&, boost::asio::ip::tcp::socket);

...

SerializationSocket::SerializationSocket(int port,
    SerializationSocket_cb cb) {
    ...
    acceptor.async_accept(*mp_Socket, cb);  // this line is SerializationSocket.h:44
}

错误消息告诉我,我的cb类型存在问题:

/home/Common/Libs/boost_1_67_0/include/boost/asio/basic_socket_acceptor.hpp: In instantiation of ‘typename boost::asio::async_result<typename std::decay<WriteHandler>::type, void(boost::system::error_code)>::return_type boost::asio::basic_socket_acceptor<Protocol>::async_accept(boost::asio::basic_socket<Protocol1>&, AcceptHandler&&, typename std::enable_if<std::is_convertible<Protocol, Protocol1>::value>::type*) [with Protocol1 = boost::asio::ip::tcp; AcceptHandler = void (*&)(const boost::system::error_code&, boost::asio::basic_stream_socket<boost::asio::ip::tcp>); Protocol = boost::asio::ip::tcp; typename boost::asio::async_result<typename std::decay<WriteHandler>::type, void(boost::system::error_code)>::return_type = void; typename std::enable_if<std::is_convertible<Protocol, Protocol1>::value>::type = void]’:
../../Common/SerializationSocket.h:44:45:   required from here
/home/Common/Libs/boost_1_67_0/include/boost/asio/detail/handler_type_requirements.hpp:105:6: error: static assertion failed: AcceptHandler type requirements not met
      static_assert(expr, msg);
      ^
/home/Common/Libs/boost_1_67_0/include/boost/asio/detail/handler_type_requirements.hpp:214:3: note: in expansion of macro ‘BOOST_ASIO_HANDLER_TYPE_REQUIREMENTS_ASSERT’
   BOOST_ASIO_HANDLER_TYPE_REQUIREMENTS_ASSERT( \
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/Common/Libs/boost_1_67_0/include/boost/asio/basic_socket_acceptor.hpp:1262:5: note: in expansion of macro ‘BOOST_ASIO_ACCEPT_HANDLER_CHECK’
     BOOST_ASIO_ACCEPT_HANDLER_CHECK(AcceptHandler, handler) type_check;
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/Common/Libs/boost_1_67_0/include/boost/asio/detail/handler_type_requirements.hpp:228:36: error: too few arguments to function
         boost::asio::detail::lvref< \
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
           asio_true_handler_type>()( \
           ~~~~~~~~~~~~~~~~~~~~~~~~~^~~
             boost::asio::detail::lvref<const boost::system::error_code>()), \

这是我use的Boost文档。当我像accept_handler一样直接使用acceptor.async_accept(*mp_Socket, accept_handler);时,它会起作用。

1 个答案:

答案 0 :(得分:1)

好吧,答案可以在boost documentation中找到。很简单

  

处理程序的功能签名必须为:

void handler( const boost::system::error_code& error );

您的处理程序的类型为:

typedef void (SerializationSocket_cb) (const boost::system::error_code&, boost::asio::ip::tcp::socket);
相关问题