This question is actually two questions. I will ask them at once because they may be related.
I just had my first look at C++20 coroutines. If have seen various examples, most of them pretty basic. Like the following:
generator<int> ints(int x)
{
for (int i = 0; i < x; ++i)
{
co_yield i;
}
}
All of the examples use a special return type that obviously contains the computation result plus the coroutine context. However no example uses a standard return type. They either sneakily omit that type or define a custom nested class that is hard to understand (for me, currently).
1. Does this mean C++20 standard library does not provide coroutine return types that are ready to use e.g. for a generator?
The best I could find was std::coroutine_handle which is internally used by mentioned custom classes.
In the documents it is said that coroutines are a good tool to implement algorithms that require a piece-by-piece data processing and would normally need to be split in fragments e.g. using a (potentially horribly complicated) state engine. I understand that as well. I even remember some project that would have been a great use case for a coroutine, namely loading a large, complex XML file using a streaming interface.
Still there seems to be a big difference: When implementing a state engine, it is pretty easy to store and load the state to/from disk since all state data is available as standard variables (some error checking and file handling is enough). I think of something like a user interface to cancel/resume a long running computation.
2. Is there an (easy) way to store/load a coroutine context to/from permanent storage as well? Is there anything in the C++20 standard that helps in doing so?