c++ - boost元组导致boost绑定(bind)/boost函数出现问题?

标签 c++ linux boost-asio boost-function

我认为我的问题在这里很常见,但我不太明白我在这里做错了什么。

我正在做一些 boost::asio 的事情并试图编写一个模板化的异步读取函数。

这是一个函数。

template <typename Handler>
void AsyncRead(std::vector<boost::uint8_t>& _inMsg, Handler _handler)
{
    #if debug
      std::cout<< "IConnection::AsyncRead" << std::endl;
    #endif
    using namespace protocols;

    typedef boost::tuple<Handler> tHandler;
    typedef boost::function< void(const boost::system::error_code &, std::vector<boost::uint8_t> &, tHandler ) > HeaderReaderFunc;

    //void (AConnection::*f)(const boost::system::error_code&, std::vector<boost::uint8_t>&, boost::tuple<Handler>) = &AConnection::HandleReadHeader<Handler>;


    tHandler t(boost::make_tuple(_handler));
    HeaderReaderFunc x(boost::bind(&AConnection::HandleReadHeader<Handler>, this, boost::asio::placeholders::error, boost::ref(_inMsg), t));
    inboundHeader.resize(sizeof(SocketIO::MsgData));
    boost::asio::async_read(socket, boost::asio::buffer(inboundHeader), x);

}

在此函数的最后一条语句中,事情开始变糟。

boost::asio::async_read(socket, boost::asio::buffer(inboundHeader), x);

当我尝试将变量“x”作为参数传递给 async_read 函数时。

错误的长度和缺乏可解读的含义都是传奇。

只是错误输出的一个小例子:

boost_1_38_0/boost/asio/detail/bind_handler.hpp: In member function ‘void boost::asio::detail::binder2<Handler, Arg1, Arg2>::operator()() [with Handler =  boost::function<void ()(const boost::system::error_code&, std::vector<unsigned char, std::allocator<unsigned char> >&, boost::tuples::tuple<boost::function<void ()(const boost::system::error_code&)>, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>)>, Arg1 = boost::system::error_code, Arg2 = unsigned int]’:

如果我在这里不使用 boost 函数,而是使用注释掉的行(它是对成员函数的引用),事情似乎可行,但我无法解释为什么会这样。

AConnection::HandleReadHeader 函数模板的函数签名是:

template <typename Handler>
void HandleReadHeader(const boost::system::error_code& _e,
                    std::vector<boost::uint8_t> & _inMsg, 
                    boost::tuple<Handler> _handler)

处理程序的类型:

    boost::function<void (const boost::system::error_code & ) >

函数 AsyncRead 和 HandleReadHeader 是我自己的类的成员 AConnection(可能不重要)。

要么我遗漏了一些关于创建 boost::function 的语法 签名包含 boost::tuple 的对象,或者 boost::asio::async_read 函数的第三个参数类型与我的不匹配 变量“x”。

如有任何帮助,我们将不胜感激。 谢谢。

编辑:

这是有效的代码,但使用成员函数引用而不是 boost::functon。

template <typename Handler>
void AsyncRead(std::vector<boost::uint8_t>& _inMsg, Handler _handler)
{
 #if debug
   std::cout<< "Connection::AsyncRead" << std::endl;
 #endif
 using namespace protocols;
 // Issue a read operation to read exactly the number of bytes in a header.
 void (Connection::*f)(
     const boost::system::error_code&,
     std::vector<boost::uint8_t>&, boost::tuple<Handler>)
   = &Connection::HandleReadHeader<Handler>;

 inboundHeader.resize(sizeof(simple::message_header));
 boost::asio::async_read(socket, boost::asio::buffer(inboundHeader),
     boost::bind(f,
     this, boost::asio::placeholders::error, boost::ref(_inMsg),
     boost::make_tuple(_handler)));
}

最佳答案

您的变量 x 具有此类型:

boost::function< void(const boost::system::error_code &,
    std::vector<boost::uint8_t> &, boost::tuple<Handler>) >

类似于这个签名:

void handler(const boost::system::error_code &,
    std::vector<boost::uint8_t> &, boost::tuple<Handler>)

然而,Boost Asio 文档说处理程序签名应该是这样的:

void handler(const boost::system::error_code&,
    std::size_t bytes_transferred)

因此,您的签名不匹配——甚至不相近。要么您正在使用一些我无法在 Boost 文档中找到的重载,要么您需要更改 HeaderReaderFunc 类型以匹配 async_read 的预期。

关于c++ - boost元组导致boost绑定(bind)/boost函数出现问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8950127/

相关文章:

linux - scp:如何使用过滤器从远程服务器复制文件

c++ - 通过来自任意索引的参数包初始化 std::array

c++ - C COMPS 执行失败的所有作业

c++ - 检查 cin 输入,清除输入缓冲区

c++ - 关闭后 boost asio async_connect成功

C++,boost asio,接收空终止字符串

c++ - 高频接收UDP包 : packet loss?

c++ - 根据 Visual Studio 中的调试/发布配置自动#defines

php - 使用适当的所有权和权限将上传的文件放入主目录

linux - 从 windows 迁移到 ubuntu 12 linux 进行开发