cppcoro
cppcoro copied to clipboard
Add support for std::optional<T> coroutine that allows unwrapping optional values using co_await
See https://github.com/toby-allsopp/coroutine_monad for inspiration.
std::optional<int> parse_int(const std::string_view& s);
std::optional<std::tuple<int, int>> parse_int_pair(const std::string_view& a, const std::string_view& b)
{
co_return std::make_tuple(co_await parse_int(a), co_await parse_int(b));
}
Note that MSVC as of 2017 Update 3 would not currently be able to use such an abstraction as it currently converts from get_return_object()
return-value into coroutine return-type immediately rather than waiting until after the coroutine is suspended. I am told this will be addressed in a future release.
Clang does not have this problem.
Note that MSVC no longer has this problem, neither does gcc. Clang, as you mentioned, traditionally didn't have this problem, but it recently broke (https://github.com/llvm/llvm-project/issues/56532), hopefully they will fix it. Once that happens, all three major compilers should do the lazy implicit conversion which should then support this.