Hacker News new | ask | show | jobs
by injidup 1582 days ago
Isn't this kind of thing supported natively with C++ coroutines as of C++20?
4 comments

Kind of.

You still need a runtime library, which should be integrated by C++23, lets see.

What C++ has better than Rust on this regard is that C++20 at least shipped at the necessary vocabulary types and compiler magic, so the plug-and-play story across runtimes (HPX, C++/WinRT, kokos,....) is much more sound today.

Only to a certain extent. The question is not only if something is possible, but also what the most convenient way to express it is. Effect handlers do have some interesting ideas for programmer-level interface, like user-defined typed commands and dynamic pairing of commands with handlers. In this sense, C++20 coroutines or Boost Context are lower-level, and maybe not the most convenient building blocks for implementing complicated concurrency scenarios, yet alone effects that do not look like concurrency at first sight, like exceptions or different forms of mutable state.
This uses stackful continuations. I.e. you can nest arbitrary code between setting the handler and invoking an effect. With C++ language-level coroutines, everything in between would also need to be a coroutine which doesn't compose as well (i.e. this library doesn't suffer from the colored function problem).
You mean threading? That is just one common example for what is possible with arbitrary effect handlers.
There is a CppCon talk where they use co-routines to deal with exceptions, Swift style.