c++-coroutine

Making python generator via c++20 coroutines

…衆ロ難τιáo~ 提交于 2021-02-04 13:00:07
问题 Let's say I have this python code: def double_inputs(): while True: x = yield yield x * 2 gen = double_inputs() next(gen) print(gen.send(1)) It prints "2", just as expected. I can make a generator in c++20 like that: #include <coroutine> template <class T> struct generator { struct promise_type; using coro_handle = std::coroutine_handle<promise_type>; struct promise_type { T current_value; auto get_return_object() { return generator{coro_handle::from_promise(*this)}; } auto initial_suspend()

Do temporaries passed to a function that returns awaitable remains valid after suspension point with co_await

帅比萌擦擦* 提交于 2021-01-28 02:24:25
问题 I'm adding support for coroutines ts in an async socket class based on windows io completion ports . Without coroutines the io may be done like this : sock.async_write(io::buffer(somebuff), [](auto&& ... args){ /* in handler */ }); or sock.async_write(std::vector<io::const_buffer>{ ... }, [](auto&& ... args){ /* in handler */ }) where each one will returns void and will notify the result through the handler and doesn't need to cache the parameters because the operation will have been

co_await appears to be suboptimal?

限于喜欢 提交于 2020-08-04 06:14:08
问题 I have an async function void async_foo(A& a, B& b, C&c, function<void(X&, Y&)> callback); I want to use it in a stackless coroutine so I write auto coro_foo(A& a, B& b, C& c, X& x) /* -> Y */ { struct Awaitable { bool await_ready() const noexcept { return false; } bool await_suspend(coroutine_handle<> h) { async_foo(*a_, *b_, *c_, [this, h](X& x, Y& y){ *x_ = std::move(x); y_ = std::move(y); h.resume(); }); } Y await_resume() { return std::move(y); } A* a_; B* b_; C* c_; X* x_; Y y_; };

co_await appears to be suboptimal?

偶尔善良 提交于 2020-08-04 06:12:20
问题 I have an async function void async_foo(A& a, B& b, C&c, function<void(X&, Y&)> callback); I want to use it in a stackless coroutine so I write auto coro_foo(A& a, B& b, C& c, X& x) /* -> Y */ { struct Awaitable { bool await_ready() const noexcept { return false; } bool await_suspend(coroutine_handle<> h) { async_foo(*a_, *b_, *c_, [this, h](X& x, Y& y){ *x_ = std::move(x); y_ = std::move(y); h.resume(); }); } Y await_resume() { return std::move(y); } A* a_; B* b_; C* c_; X* x_; Y y_; };

C++20 in g++10: generator not defined

会有一股神秘感。 提交于 2020-07-09 12:05:37
问题 This MCVE works fine in Visual Studio. #include <experimental/generator> #include <iostream> std::experimental::generator<int> f() { for (int i = 0; i < 10; ++i) co_yield i; } int main () { for (int i : f()) std::cout << i << ' '; return 0; } but in g++10, which is listed as having full support or C++20's coroutines, it does not. (Taking out experimental doesn't help.) I am compiling thus: g++ -g -std=c++2a -fcoroutines -c main.cpp . It complains that there is no include file generator, and

C++20 in g++10: generator not defined

狂风中的少年 提交于 2020-07-09 12:04:22
问题 This MCVE works fine in Visual Studio. #include <experimental/generator> #include <iostream> std::experimental::generator<int> f() { for (int i = 0; i < 10; ++i) co_yield i; } int main () { for (int i : f()) std::cout << i << ' '; return 0; } but in g++10, which is listed as having full support or C++20's coroutines, it does not. (Taking out experimental doesn't help.) I am compiling thus: g++ -g -std=c++2a -fcoroutines -c main.cpp . It complains that there is no include file generator, and

What are the mechanics of coroutines in C++20?

本秂侑毒 提交于 2020-05-24 08:46:25
问题 I was trying to read the documentation (cppreference and the standard documentation on the feature itself) on the sequence of operations that get called when a coroutine function is called, suspended, resumed and terminated. The documentation goes into depth outlining the various extension points that allow library developers to customize the behavior of their coroutine using library components. At a high-level, this language feature seems to be extremely well thought out. Unfortunately, I'm

Are stackless C++20 coroutines a problem?

匆匆过客 提交于 2020-04-07 11:13:45
问题 Based on the following it looks like coroutines in C++20 will be stackless. https://en.cppreference.com/w/cpp/language/coroutines I'm concerned for many reasons: On embedded systems heap allocation is often not acceptable. When in low level code, nesting of co_await would be useful (I don't believe stackless co-routines allow this). With a stackless coroutine, only the top-level routine may be suspended. Any routine called by that top-level routine may not itself suspend. This prohibits

Are stackless C++20 coroutines a problem?

淺唱寂寞╮ 提交于 2020-04-07 11:11:06
问题 Based on the following it looks like coroutines in C++20 will be stackless. https://en.cppreference.com/w/cpp/language/coroutines I'm concerned for many reasons: On embedded systems heap allocation is often not acceptable. When in low level code, nesting of co_await would be useful (I don't believe stackless co-routines allow this). With a stackless coroutine, only the top-level routine may be suspended. Any routine called by that top-level routine may not itself suspend. This prohibits