logoalt Hacker News

HarHarVeryFunnytoday at 12:02 PM1 replyview on HN

If you need to implement an async state machine, couldn't that just as easily be done with std::future? How do coroutines make this cleaner/better?


Replies

LatencyKillstoday at 12:18 PM

std::future doesn't give you a state machine. You get the building blocks you have to assemble into one manually. Coroutines give you the same building blocks but let the compiler do the assembly, making the suspension points visible in the source while hiding the mechanical boilerplate.

This is why coroutine-based frameworks (e.g., C++20 coroutines with cppcoro) have largely superseded future-chaining for async state machine work — the generated code is often equivalent, but the source code is dramatically cleaner and closer to the synchronous equivalent.

(me: ex-Visual Studio dev who worked extensively on our C++ coroutine implementation)

show 2 replies