chriskohlhoff / asio

Asio C++ Library
http://think-async.com/Asio
4.81k stars 1.2k forks source link

co_spawn on awaitable<T> requires T being default constructible, by design? #1303

Open erinacio opened 1 year ago

erinacio commented 1 year ago

MRE with gcc 13.1.1 / clang 15.0.7 and asio 1.28.0:

#include <asio.hpp>

struct foobar {
  foobar() = delete;
  foobar(const foobar &) = default;
  foobar(foobar &&) = default;
  foobar &operator=(const foobar &) = default;
  foobar &operator=(foobar &&) = default;
};

asio::awaitable<foobar> async_foobar();

void not_ok(const asio::any_io_executor &ex) {
  // I actually use `use_awaitable` in my code, using `detached` here to
  // make it short as this issue is not relevant with completion token type.
  asio::co_spawn(ex, async_foobar(), asio::detached);
}

This happens when I'm using ned14's outcome with asio, where outcome_v2::result<T, E, NVP> is never default constructible. I didn't find anywhere in the documentation indicating that co_spawn can only work on awaitable<T, E> where T is default constructible. I wonder if it's intentional or not, considering that awaitable<T, E> allows T being not default constructible and it works with simple co_await.