Understanding C++ Coroutines by Example: Generators (Part 1 of 2)
summary
Step‑by‑step construction of a working C++20 generator: compiler transformation into a frame with promise_type, how yield_value and return_void map to suspension points, and why initial_suspend and final suspend matter for exceptions and lifetime. Implements a generator type with a coroutine_handle, call operator, advance/get_value iterator style, shows copy vs pointer storage for yielded values, prints "hello world" and an infinite Fibonacci example, points out inefficiencies and recommends cppcoro/folly or the forthcoming std::generator.
AI-written summary · about
chapters (19)
- 0:00C++ coroutines intro
- 2:18What coroutines are
- 5:08Coroutine suspension
- 9:56Lazy async best practices
- 13:52Initial suspension need
- 16:41Safe coroutine destroy
- 18:47Params to coroutines
- 20:33Generator type design
- 22:20Promise type functions
- 29:54Exceptions in coroutines
- 33:10Coroutines vs state machines
- 42:17Basic generator drawbacks
- 44:30Coroutines vs futures
- 45:54Extra generator calls
- 47:41Yielded value lifetime
- 51:20Has-value checks
- 53:30Avoid value copying
- 57:18Refining generator interface
- 59:44Iterator interface for generators








