4 ms·
This incredibly powerful facility is also available in C++20 with coroutines. 'yield return' is written as 'co_yield'. Also since "iterator" means something els
by StellarScience 4y ago
This incredibly powerful facility is also available in C++20 with coroutines. 'yield return' is written as 'co_yield'. Also since "iterator" means something else in C++, this special usage of coroutines is called a "generator".
'co_yield' makes iterators/generators super easy to write as shown in this article. But also it enables great abstraction/encapsulation for where you want to provide an interface allowing callers to iterate over elements, without exposing the underlying container type.
(Note that 'std::generator' is not yet officially standardized, but with a little work we're using Microsoft's 'std::experimental::generator' across platforms, compiling with clang and gcc. Alternatively, I hear the cppcoro library is pretty great.)
- mgraczyk 4y agoFacebook already uses coroutines pretty heavily in certain C++ services. Was kind of surprising to see, I haven't seen coroutines used much in C++ because of the lack of standard library support (no composeable promises yet)