代码之家  ›  专栏  ›  技术社区  ›  Adrian

如何将std::array转换为boost::asio::buffer?

  •  1
  • Adrian  · 技术社区  · 7 年前

    我正在尝试转换一个 std::array boost::asio::buffer 用于 async_read_some

    以下是我的代码示例:

    array<char, 16> data;
    tcpSocket.async_read_some(buffer(data), [data](const boost::system::error_code& ec, size_t amountOfBytes) {
    if (ec) {
        cout << "Read failed with message: " << ec.message() << endl;
    }
    else {
        cout.write(data.data(), amountOfBytes);
    }
    });
    

    以下是我得到的错误:

    Error   C2661   'boost::asio::detail::buffer_sequence_adapter_base::init_native_buffer': no overloaded function takes 1 arguments
    Error   C2440   '<function-style-cast>': cannot convert from 'const boost::asio::const_buffers_1' to 'boost::asio::mutable_buffer'
    

    1 回复  |  直到 4 年前
        1
  •  1
  •   Victor Gubin    7 年前

    你可以用 mutable_buffer

    boost::asio::mutable_buffer buff( arr.data(), arr.size() );