Understanding C++ Coroutines by Example, Part 2: Generators
summary
Covers concrete C++20 generator implementation: shows a naive generator type with coroutine frame, initial_suspend/final_suspend, promise object and handle, and exact implementations of get_return_object, promise::yield_value, return_void and unhandled_exception. Demonstrates avoiding copies (lvalue/rvalue overloads and pointer storage), adds advance/get_value and an iterator-based begin/end API with exception semantics, explains recursive generators (yield whole generator) and an async pattern using co_await on next, and calls out the C++23 proposal issue std::generator limitation: begin only once.
AI-written summary · about
chapters (22)
- 0:00Start
- 1:59Taxonomy of the Coroutines
- 5:39Best Practices
- 6:42Naive Generator
- 14:06Generator Type
- 17:08Get Return Object
- 17:53Initial Suspend and Final Spent
- 21:25Unhandled Exception
- 24:29Move Assignment Operator
- 48:43Can We Return Values of Different Types
- 51:41Parameters to Generators
- 58:02Value Initialized Checks
- 59:58Iterator Type
- 1:01:35Increment Operator
- 1:04:52Iterator
- 1:06:21Reference Operator
- 1:12:40Final Suspend
- 1:15:42Consequences of this Design
- 1:19:50Yielding Values from Nest Generators
- 1:21:23Recursive Generator
- 1:22:10Recursive Generators
- 1:31:45Async Synchronous Generators
related talks
01h 26mC++ Coroutines - Don’t Try This at Home
01h 06mC++ Coroutines from scratch
01h 16mC++ Feature Coroutines, Beginner Friendly
48m“Coroutines: what can't they do?”
58m“Introduction to C++ Coroutines"
57mstd::generator in C++23: When to use, and how to improve it
45mCoroutines explained
01h 23mImplementing a C++ Coroutine Task from Scratch
