2
0
mirror of https://github.com/boostorg/cobalt.git synced 2026-01-26 06:22:31 +00:00
Files
cobalt/doc/reference/async_for.adoc
Klemens Morgenstern f6b33eb69f added composed.
2023-12-12 06:53:27 +08:00

36 lines
836 B
Plaintext

[#async_for]
== cobalt/async_for.hpp
For types like generators a `BOOST_COBALT_FOR` macro is provided, to emulate an `for co_await` loop.
[source,cpp]
----
cobalt::generator<int> gen();
cobalt::main co_main(int argc, char * argv[])
{
BOOST_COBALT_FOR(auto i, gen())
printf("Generated value %d\n", i);
co_return 0;
}
----
The requirement is that the <<awaitable,awaitable>> used in the for loop has an `operator bool` to check if it
can be awaited again. This is the case for <<generator, generator>> and <<promise,promise>>.
That is, `BOOST_COBALT_FOR(__expression__, __declaration__) __statement__` the above statement is roughly equivalent to:
[source,cpp,subs=+quotes]
----
{
auto && __aw__ = co_await __expression__;
while (__aw__)
{
__expression__ = co_await __aw__;
__statement__
}
}
----