【问题标题】:boost tuple causing problems with boost bind / boost function?提升元组导致提升绑定/提升功能出现问题?
【发布时间】:2012-01-21 02:16:00
【问题描述】:

我认为我的问题在这里很常见,但我只是看不出我在这里做错了什么。

我正在做一些 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)));
}

【问题讨论】:

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


    【解决方案1】:

    您的变量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 的预期。

    【讨论】:

    • 我想知道为什么当我使用“void (AConnection::*f)(const boost::system::error_code&, std::vector<:uint8_t>&, boost: :tuple) = &AConnection::HandleReadHeader; "参考?我会更加努力地看看它与使用 boost::function 相比有何特别之处。
    • 我不确定...我还没有看到你的代码,你在哪里实际使用了指向成员函数的指针和 async_read 调用。
    • 我更新了问题的代码以显示工作用例。也许这将有助于确定两个代码 sn-ps 之间有什么不同?
    • 我认为你需要继续提炼代码,直到你得到几行简短的工作和非工作案例。仍然有太多内容需要细细琢磨。
    猜你喜欢
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多