'Is it possible to use co_yield in a callback that is entered multiple times?
I am learning C++20, and there are a question about coroutines. Suppose there is a function that uses a callback to iterate over the data:
void get_number( function<void(int)> callback )
{
for(int i=0; i<10; i++) callback(i);
}
Is it possible to turn it into generator using co_yield in C++20 like below code?
generator<int> co_get_number()
{
auto co_callback = [](int i){
// is it possible use co_yield here?
// co_yield i;
}
get_number(co_callback);
}
int main()
{
for(int i : co_get_number())
{
cout << i << endl;
}
}
Thanks to chris, since the C++20 coroutine is stackless, it cannot be suspended within a nested stackframe. However, I think this requirement is more practical, is there some neat way to make stackless coroutine also do that?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
