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
Special: C++ Self-study w. Coaching
×
Made by
Andreas Fertig
Powered by
Flask
and
CodeMirror
Source:
#if __has_include(<coroutine>) #include <coroutine> #elif __has_include(<experimental/coroutine>) #include <experimental/coroutine> namespace std { using namespace std::experimental; } #else #error "No coroutine header" #endif #include <cstdio> #include <exception> #include <new> struct generator { struct promise_type { int current_value{}; std::suspend_always yield_value(int value) { current_value = value; return {}; } std::suspend_always initial_suspend() { return {}; } std::suspend_always final_suspend() noexcept { return {}; } generator get_return_object() { return generator{this}; }; void unhandled_exception() { std::terminate(); } void return_value(int v) { current_value = v; } }; generator(generator const&) = delete; generator(generator&& rhs) : handle{std::exchange(rhs.handle, nullptr)} {} ~generator() { if(handle) { handle.destroy(); } } void run() { if(not handle.done()) { handle.resume(); } } auto value() { return handle.promise().current_value; } private: explicit generator(promise_type* p) : handle{std::coroutine_handle<promise_type>::from_promise(*p)} {} std::coroutine_handle<promise_type> handle; }; generator fun() { printf("Hello,"); co_yield 4; printf("C++ Insights.\n"); co_return 2; } int main() { auto s = fun(); s.run(); printf("value: %d\n", s.value()); s.run(); printf("value: %d\n", s.value()); }
Insight:
Console: