Coroutines — functions that can suspend and resume (C++20).
1#include <coroutine>2#include <generator>34std::generator<int> fibonacci() {5 int a = 0, b = 1;6 while (true) {7 co_yield a; // suspend and yield value8 auto temp = a;9 a = b;10 b = temp + b;11 }12}1314for (int n : fibonacci()) {15 if (n > 100) break;16 std::cout << n << " ";17}
Keywords:
co_await: suspend until readyco_yield: produce value and suspendco_return: finish coroutineUse cases: