flâneur — a map of the web's best reading

Daily bit(e) of C++ | Coroutines: step by step

simontoth.substack.com · 2,487 words · saved by 1 readers

The C++20 standard introduced support for coroutines. For most C++ users, using coroutines is a matter of following a library’s documentation that provides coroutine types. However, if you need to implement a custom coroutine type, this article is for you. In this article, we will go over seven types of coroutines, each introducing more concepts. Any function (except main) can be a coroutine; there are two requirements: the function body must contain at least one of the coroutine keywords: co_return, co_yield, co_await the return type must conform to coroutine requirements (either directly or through std::coroutine_traits) The compiler will then generate the required wrapping code to handle the details. The standard library already provides one coroutine type, the std::generator (C++23), that we can use to implement lazily computed sequences of elements. std::generator supports yielding of values and ranges of values using co_yield value and co_yield std::ranges::elements_of(range). Th

Daily bit(e) of C++ | Coroutines: step by step Daily bit(e) of C++ #441, Coroutines: step by step; a journey through seven coroutine types. Šimon Tóth Mar 17, 2024 6 1 1 Share The C++20 standard introduced support for coroutines. For most C++ users, using coroutines is a matter of following a library’s documentation that provides coroutine types. However, if you need to implement a custom coroutine type, this article is for you. In this article, we will go over seven types of coroutines, each introducing more concepts. Using coroutines Any function (except main) can be a coroutine; there are t

Explore this link on the map →

saved by

related reading