- GCC 14.1 (libstdc++)
- MSVC 19.40
- Clang 18 + libc++
Код: Выделить всё
#include
#include
#include
template
struct Generator {
struct promise_type {
T current;
auto get_return_object() { return Generator{handle_type::from_promise(*this)}; }
static auto get_return_object_on_allocation_failure() noexcept { return Generator{nullptr}; }
auto initial_suspend() noexcept { return std::suspend_always{}; }
auto final_suspend() noexcept { return std::suspend_always{}; }
void return_void() noexcept {}
auto yield_value(T v) noexcept {
current = v;
return std::suspend_always{};
}
void unhandled_exception() { std::terminate(); }
};
using handle_type = std::coroutine_handle
;
handle_type h;
Generator(handle_type h) : h(h) {}
Generator(const Generator&) = delete;
Generator& operator=(const Generator&) = delete;
Generator(Generator&& o) noexcept : h(o.h) { o.h = nullptr; }
~Generator() { if (h) h.destroy(); }
std::optional next() {
if (!h || h.done()) return std::nullopt;
h.resume();
if (h.done()) return std::nullopt;
return h.promise().current;
}
};
Generator makeA() {
for (int i = 0; i < 3; ++i)
co_yield i;
}
Generator makeB() {
while (auto v = makeA().next())
co_yield *v + 10;
}
int main() {
auto g = makeB();
while (auto v = g.next())
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79821696/why-does-a-nested-c20-coroutine-pipeline-deadlock-only-when-compiled-with-clan[/url]