boost::asio::co_spawn
requires the completion token with the signature void(std::exception_ptr, R)
for calling an awaitable function boost::asio::awaitable<R>
.
When the return type of awaitable function (R
) is boost::outcome::result<T>
, it fails to compile, because boost::outcome::result<T>
doesn't have default constructor.
auto awaitable_func() -> boost::asio::awaitable<boost::outcome::result<void>> {
co_return std::in_place_type<void>;
}
boost::asio::co_spawn(executor, awaitable_func(), boost::asio::detached);
error: call to deleted constructor of 'boost::outcome_v2::basic_result<void, std::error_code, boost::outcome_v2::policy::error_code_throw_as_system_error<void, std::error_code, void>>'
std::move(handler)(e, T());
^
note: 'basic_result' has been explicitly marked deleted here
basic_result() = delete;
Is there a way to use boost::outcome::result
as the return type of awaitable function?
The issue stems from co_spawns requirement that the return value must be default constructible as you found out yourself. You can workaround that requirement by using
std::optional
.So why does co_spawn require default constructible return values
I think this boils down to this branch. In the above example I show an alternative way of handling exceptions where the return value must be default constructed by co_spawn. Most if not all exception free handlers default construct the return value on error.