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

Reusing generator leads to skipping value #84

Open
lungsdude opened this issue Apr 14, 2024 · 0 comments
Open

Reusing generator leads to skipping value #84

lungsdude opened this issue Apr 14, 2024 · 0 comments

Comments

@lungsdude
Copy link

So I have this coroutine, which generates a sequence of numbers and store the generator in a variable to reuse it later, but when I do reuse it, it skips over first value:

cppcoro::generator<int> numbers(int start) {
    while (true) {
        co_yield start++;
    }
}

int main() {
    auto generator = numbers(0);
    for (const int& i : generator | std::views::take(5)) {
        std::cout << i << ' '; //Prints "0 1 2 3 4", which is correct
    }

    for (const int& i : generator | std::views::take(5)) {
        std::cout << i << ' '; //Prints "6 7 8 9 10", which is incorrect as it has skipped over 5 for some reason
    }
}

Here's a godbolt link to see it in action

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant