Hacker News new | ask | show | jobs
by convery 1213 days ago
TBH, everytime I read about coroutines and their example usages I get stuck on the "why" question. If the example is a function waiting for data, a std::future seems a lot easier. If the example is a generator, a range/transform/filter or just a class that returns a std::default_sentinel iterator seems more appropriate.

The only reason I can see is lowering the overhead of creating threads, at the cost of harder to read controlflow.

1 comments

One reason is to reduce callback hell where you have to pass a continuation function to an asynchronous operation to run after it has completed. Coroutines can be used to make your code look more like blocking code. ASIO’s coroutine support does this and it makes async operations much easier to read

Also, generators can be used to turn traversal functions into iterators, which is often tedious to do by hand.