Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yield immediate value? #42

Closed
CarterLi opened this issue Oct 12, 2019 · 3 comments · Fixed by #44
Closed

yield immediate value? #42

CarterLi opened this issue Oct 12, 2019 · 3 comments · Fixed by #44
Labels
docs: question I can't get it!

Comments

@CarterLi
Copy link

coro::enumerable<int> test() {
    co_yield 1;
}
test.cpp:5:14: error: non-const lvalue reference to type 'coro::enumerable<int>::value_type' (aka 'int') cannot bind to a temporary of type 'int'
    co_yield 1;
             ^
../interface/coroutine/yield.hpp:83:36: note: passing argument to parameter 'ref' here
        auto yield_value(reference ref) noexcept {
@luncliff
Copy link
Owner

@CarterLi enumerable should be used with reference.
Most of generators are allowing T&&, but it doesn't.

// `co_yield` expression. only for reference
auto yield_value(reference ref) noexcept {
current = std::addressof(ref);
return suspend_always{};
}

The reason for such restriction is to enforce users to reuse existing variable in the coroutine frame.
For example, the code below is reusing the object io_task_t task{};.

auto enumerate_net_tasks(nanoseconds timeout) noexcept(false)
-> coro::enumerable<io_task_t> {
const int half_time = duration_cast<milliseconds>(timeout).count() / 2;
io_task_t task{};
for (auto event : inbound.wait(half_time))
co_yield task = io_task_t::from_address(event.data.ptr);
for (auto event : outbound.wait(half_time))
co_yield task = io_task_t::from_address(event.data.ptr);
}

@luncliff luncliff added the docs: question I can't get it! label Oct 12, 2019
@CarterLi
Copy link
Author

But it's ugly.

coro::enumerable<int> test() {
    int i = 1;
    co_yield i;
}

@luncliff
Copy link
Owner

Well, indeed I agree with the point. Let me change that.
But I couldn't work on this project recently and probably the state will remain unchanged for more upcoming weeks... I'm really sorry about that. I will work as soon as possible and let you know.

If you're in an urgent situation, you can try existing generator implementations in https://github.com/Quuxplusone/coro. I strongly recommend those works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs: question I can't get it!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants