Boost logo

Boost :

From: Klemens Morgenstern (klemensdavidmorgenstern_at_[hidden])
Date: 2023-08-19 01:37:44


On Sat, Aug 19, 2023 at 4:41 AM Andrzej Krzemienski via Boost
<boost_at_[hidden]> wrote:
>
> Hi Klemens,
> I have another question regarding the generator.
> I think the initial examples in the dcs for `generator` are too simplistic,
> because the caller seems to already know how many times it is allowed to
> co_await on the Awaitable. I tried it with a loop, where I use the explicit
> conversion to bool to check when to break the iteration:
>
> ```
> async::generator<std::string> worker(int limit)
> {
> for (int i = 0; i < limit; ++i) {
> co_yield std::format("--{}--", i);
> }
> }

This is missing a co_return at the end. Because the generators are
async I can't look ahead like std::generator. So this should be

async::generator<std::string> worker(int limit)
{
    for (int i = 0; i < limit; ++i) {
        if (i == (limit - 1))
            co_return std::format("--{}--", i);
        co_yield std::format("--{}--", i);
    }
}

Or you return and empty string and handle it on the consumer side as an EOI.

>
> async::main co_main(int argc, char* argv[])
> {
> try {
> auto w = worker(3);
> while (w) {
> std::cout << co_await w << std::endl;
> }
> }
> catch (std::exception const& e) {
> std::cout << "ERR: " << e.what() << std::endl;
> }
> }
> ```
>
> But I am surprised that this construct ends with throwing an exception.
> Am I doing something wrong?
>
> Regards,
> &rzej;
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


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