2
0
mirror of https://github.com/boostorg/cobalt.git synced 2026-01-19 04:02:16 +00:00
Files
cobalt/doc/reference/result.adoc
Klemens Morgenstern 45901641ac renamed to cobalt.
2023-10-16 21:42:07 +08:00

41 lines
1.2 KiB
Plaintext

[#result]
== cobalt/result.hpp
Awaitables can be modified to return `system::result` or
`std::tuple` instead of using exceptions.
[source,cpp]
----
// value only
T res = co_await foo();
// as result
system::result<T, std::exception_ptr> res = co_await cobalt::as_result(foo());
// as tuple
std::tuple<std::exception_ptr, T> res = co_await cobalt::as_tuple(foo());
----
Awaitables can also provide custom ways to handle results and tuples,
by providing `await_resume` overloads using `cobalt::as_result_tag` and `cobalt::as_tuple_tag`.:
[source,cpp, subs="+quotes"]
----
__your_result_type__ await_resume(cobalt::as_result_tag);
__your_tuple_type__ await_resume(cobalt::as_tuple_tag);
----
This allows an awaitable to provide other error types than `std::exception_ptr`,
for example `system::error_code`. This is done by <<op,op>> and <<channel,channel>>.
[source,cpp]
----
// example of an op with result system::error_code, std::size_t
system::result<std::size_t> await_resume(cobalt::as_result_tag);
std::tuple<system::error_code, std::size_t> await_resume(cobalt::as_tuple_tag);
----
NOTE: Awaitables are still allowed to throw exceptions, e.g. for critical exceptions such as OOM.