例如,在随机访问设备上调用同步读取操作并抛出异常时,我们如何知道读取了多少字节random_access_file?
这是否不受支持,并且要知道读取了多少字节,应该采取过载boost::system::error_code ec?
error_code ec;
size_t s = a.read_some_at(offset, buffers, ec);
offset += s; // need to be done before unwinding
if (ec) throw system_error(ec);
return s;
Run Code Online (Sandbox Code Playgroud)
简短回答:是的。承受ec超载。Asio 中部分成功的操作就是这种情况。
稍长(无关)的答案:当使用例如 c++20 协程时,您可以将错误代码和字节计数作为 tuple返回,您可能会发现更方便:
auto [ec, s] = a.async_read_some_at(offset, buffers, asio::as_tuple(asio::use_awaitable)));
Run Code Online (Sandbox Code Playgroud)
讨厌:as_tuple目前(最后检查的 1.80.0)似乎不能很好地工作asio::use_future:(