zlacker

[parent] [thread] 4 comments
1. zarkov+(OP)[view] [source] 2020-04-26 21:09:34
Coroutines in C++.
replies(2): >>ncmncm+R8 >>Reraro+6q1
2. ncmncm+R8[view] [source] 2020-04-26 22:28:26
>>zarkov+(OP)
Coroutines are most easily understood as a way to write a state machine in a way that looks like a function. I.e., it's just a notational trick to make one function do different things according to when it's called.

To see it, imagine you have a struct with a data member for each local variable of your function, and replace your function with a member function that has no local variables, but uses "this" to get at what was local data.

Add one more data member, a number that is set differently right before each place the function returns.

Finally, insert some code at the start of the function that, according to the number, jumps to just after the last return statement executed.

Then, each time you call the function, what happens depends on what happened last time.

There are more details, but that is the gist.

You can write that yourself in C++98, with the body of the function inside a switch statement. Getting it past code review would be the real challenge.

replies(1): >>zarkov+vo
◧◩
3. zarkov+vo[view] [source] [discussion] 2020-04-27 00:48:03
>>ncmncm+R8
I think I get coroutines in theory. It's the C++ specific complexity that is the hardest barrier. Thanks for the explanation though
replies(1): >>ncmncm+gt
◧◩◪
4. ncmncm+gt[view] [source] [discussion] 2020-04-27 01:41:28
>>zarkov+vo
Yes, a lot of extra junk is needed to make them actually work, but the extra junk goes a long way toward obscuring what you actually need to know.

Ultimately, though, you are right that you have to understand it all, once, even if you can't remember it all a month later. The explanations I find online are not good at presenting just the details you need when you need them, and building up to the full picture.

5. Reraro+6q1[view] [source] 2020-04-27 13:47:25
>>zarkov+(OP)
Try to understand them in Simula first, since that's where C++ drew its original inspiration from.
[go to top]