asio C++ library

PrevUpHomeNext

basic_yield_context::operator[]

Return a yield context that sets the specified error_code.

basic_yield_context operator[](
    asio::error_code & ec) const;

By default, when a yield context is used with an asynchronous operation, a non-success error_code is converted to system_error and thrown. This operator may be used to specify an error_code object that should instead be set with the asynchronous operation's result. For example:

template <typename Handler>
void my_coroutine(basic_yield_context<Handler> yield)
{
  ...
  std::size_t n = my_socket.async_read_some(buffer, yield[ec]);
  if (ec)
  {
    // An error occurred.
  }
  ...
}

PrevUpHomeNext