Request Short Link
C++ 98
C++ 11
C++ 14
C++ 17
C++ 20
C++ 23
C++ 2c
for-loops as while-loops
array subscription
Show all implicit casts
Show all template parameters of a CallExpr
Use libc++
Transform std::initializer_list
Show noexcept internals
Show padding information
Show coroutine transformation
Show C++ to C transformation
Show object lifetime
Default
15
18
20
22
26
More
GitHub
Patreon
Issues
About
Policies
Examples
C++ Insights @ YouTube
Settings
Version
Workshop: Safe and Efficient C++ for Embedded Environments
×
Made by
Andreas Fertig
Powered by
Flask
and
CodeMirror
Source:
// https://www.scs.stanford.edu/~dm/blog/c++-coroutines.html #include <concepts> #include <coroutine> #include <exception> #include <iostream> struct ReturnObject { struct promise_type { ReturnObject get_return_object() { return {}; } std::suspend_never initial_suspend() { return {}; } std::suspend_never final_suspend() noexcept { return {}; } void unhandled_exception() {} }; }; struct Awaiter { std::coroutine_handle<> *hp_; constexpr bool await_ready() const noexcept { return false; } void await_suspend(std::coroutine_handle<> h) { *hp_ = h; } constexpr void await_resume() const noexcept {} }; ReturnObject counter(std::coroutine_handle<> *continuation_out) { Awaiter a{continuation_out}; for (unsigned i = 0;; ++i) { co_await a; std::cout << "counter: " << i << std::endl; } } void main1() { std::coroutine_handle<> h; counter(&h); for (int i = 0; i < 3; ++i) { std::cout << "In main1 function\n"; h(); } h.destroy(); }
Insight:
Console: