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

C++ 如何将std::array转换为boost::asio::buffer?,c++,c++11,boost,boost-asio,C++,C++11,Boost,Boost Asio,我正在尝试将std::array转换为boost::asio::buffer以用于async\u read\u some,但我总是遇到一些错误: 以下是我的代码示例: array<char, 16> data; tcpSocket.async_read_some(buffer(data), [data](const boost::system::error_code& ec, size_t ammountOfBytes) { if (ec) { cout <&l

我正在尝试将
std::array
转换为
boost::asio::buffer
以用于
async\u read\u some
,但我总是遇到一些错误:

以下是我的代码示例:

array<char, 16> data;
tcpSocket.async_read_some(buffer(data), [data](const boost::system::error_code& ec, size_t ammountOfBytes) {
if (ec) {
    cout << "Read failed with message: " << ec.message() << endl;
}
else {
    cout.write(data.data(), ammountOfBytes);
}
});
数组数据;
tcpSocket.async_read_some(缓冲区(数据),[data](常量boost::system::error_code&ec,size_t ammontofBytes){
国际单项体育联合会(欧共体){
你可以用


这段代码是为我编译的。请提供mcv示例,记住调用闭包时的
data
与调用
async\u read\u some
时传递的数组
data
不同,因为
data
是通过值传递到lambda中的。boost的哪个版本?@rafix07我在Visual Studio中使用boost 1.692017我不太熟悉lambda,你能举例说明为什么lambda内部的“数据”类型可能不同吗?你应该将你的问题更新为MVC代码。你是从自由函数或成员函数调用此代码?异步读取?有些人希望可变缓冲区,asio::缓冲区(数据)如果数据是常量对象,则可以返回常量缓冲区,这不是您的情况,您将
数据
定义为非常量,因此代码应该可以工作。您的lambda按值获取
数据
,因此进行复制,
异步读取一些
从函数范围写入数据,当调用处理程序时,您正在访问
数据
数组的副本,它是n在函数作用域中,对同一个对象使用什么
数据
。您可以通过ref
[&data]
传递它。这是一个深入的主题,请阅读有关lambda捕获的内容
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'
boost::asio::mutable_buffer buff( arr.data(), arr.size() );