chriskohlhoff / asio

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

coro<int,int> return maby error #1528

Open kahotv opened 3 weeks ago

kahotv commented 3 weeks ago

When using coro<int,int>, the return type is not the expected variant<int,int>, but actually int.

see https://think-async.com/Asio/asio-1.30.2/doc/asio/overview/composition/coro.html#asio.overview.composition.coro.result

version 1.30.2

PS D:\workspace\git\vcpkg> .\vcpkg.exe list
...
asio:x86-windows-static                           1.30.2

code

coro<int,int> generator_int(any_io_executor)
{
    int i = 0;
    do
    {
        co_yield i++;
    } while (i < 3);

    co_return 0;
}

awaitable<void> test_print()
{
    auto g = generator_int(co_await this_coro::executor);
    while (g)
    {
        auto ret = co_await g.async_resume(use_awaitable);
        printf("type=%s, value=%d\n", typeid(ret).name(), ret);
    }
    printf("end\n");
}

int main(int argc, char** argv)
{
    io_context ctx;
    co_spawn(ctx, test_print(), detached);
    ctx.run();
}

output:

type=int, value=0
type=int, value=1
type=int, value=2
type=int, value=0
end

This makes it impossible for me to determine whether the return value comes from co_yield or co_return.

(The use of coro<int> is correct, and the return type is optional<int> as expected.)

klemens-morgenstern commented 3 weeks ago

That's by design (just missing proper docs). This is mean for when a return may indicate the error by it's value.