4 ms·
Don't let its name fool you. C++ coroutines is nothing about the coroutines you imagine from other languages. C++ coroutines, defined by C++20 standard is noth
by ezoe 7y ago
Don't let its name fool you. C++ coroutines is nothing about the coroutines you imagine from other languages.
C++ coroutines, defined by C++20 standard is nothing but a syntax sugar. It consists of three keywards, co_await, co_yeild and co_return. The functions which use these keywords are said to be a coroutine and it's code will be transformed such that some specific member functions of the user-defined type will be called.
And that's everything. It's just a syntax sugar like range-based for. Actually, it's less than the range-based for because there is no standard library support for it.
If you wrote a class that does, say, lightweight user-mode threading according to the C++ coroutine's syntax sugar expect, it become coroutine in the sense of most other programming languages. It can also support generators and other idioms if YOU or SOMEBODY wrote a library for it. Good luck with that. Because even the C++ SC's experts couldn't design and agree upon a good such standard library until the deadline of C++20.
In my opinion, these features should better be implemented by powerful generic static reflections rather than a dedicated special syntax just for this.
- MaxBarraclough 7y ago> In my opinion, these features should better be implemented by powerful generic static reflections rather than a dedicated special syntax just for this. Boost has had a coroutines library for some years now. What do you make of their approach?
- gpderetta 7y agoC++ coroutines are pretty much the same as stackless generators/async from other languages (python, JS, C#). They have nothing to do with range for. They are not stackful coroutines, like thise from lua for example, but multiple implementations of thise are available as a library. There still a chance they might make it to the standard library.
- nly 7y agoIt's not just syntax sugar, since the compiler still does the hard work of capturing local variables in to a (stackless) activation record
- gpderetta 7y agoAt the end of the day, everything is syntactic sugar over machine code. Doesn't means it is a bad thing though.