Boost logo

Boost :

From: Andrzej Krzemienski (akrzemi1_at_[hidden])
Date: 2023-09-23 19:51:17


Hi Everyone.
I am very uneasy about the dummy co_retuned value that I see in the
examples of async::generator. Like the one in the echo server example:
https://klemens.dev/async/tutorial.html#echo_server

async::generator<tcp_socket> listen()
{
  tcp_acceptor acceptor({co_await async::this_coro::executor}, {tcp::v4(),
55555});
  for (;;)
  {
    tcp_socket sock = co_await acceptor.async_accept();
    co_yield std::move(sock);
  }
  co_return tcp_socket{acceptor.get_executor()}; // :-(
}

The control will never get to the co_return. The caller never even tries to
observe this value. and yet we are forced to return it.

Interestingly, the coroutine example from ASIO doesn't have this due to a
different design:
https://www.boost.org/doc/libs/1_83_0/doc/html/boost_asio/example/cpp20/coroutines/echo_server.cpp

std::generator doesn't have this.

In other examples the degenerate value is used to indicate the end of
generation.

I do not know the coroutines to be able to tell if this is a design problem
with async::generator or with C++ coroutines in general. But it feels wrong
that the end of generation should be signaled in this way.

Regards,
&rzej;


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk