Since the std::generator
is making it into CPP23, I am playing around with MSVC's incomplete version.
However, I notice that it seems lose exactly one yield
when used with std::views::take
. Here is the example:
#include <iostream>
#include <ranges>
#include <experimental/generator>
std::experimental::generator<int> GeneratorFn(void) noexcept
{
co_yield 1;
co_yield 2;
co_yield 3;
co_yield 4;
co_yield 5;
co_yield 6;
co_yield 7;
co_yield 8;
co_yield 9;
co_return;
}
int main(int argc, char** args) noexcept
{
auto Ret = GeneratorFn();
for (auto&& i : Ret | std::views::take(2))
std::cout << i << '\n';
for (auto&& i : Ret | std::views::take(3))
std::cout << i << '\n';
for (auto&& i : Ret | std::views::take(4))
std::cout << i << '\n';
}
The output of this code would be
1
2
4
5
6
8
9
and clearly, the 3
and 7
is missing. It seems like std::views::take
drops the last value the generator
yields.
Is this normal and to be expected in the formal version of C++23?
(Try online: https://godbolt.org/z/v6MModvaz)
std::generator
is a move-onlyview
which modelsinput_range
and has move-only iterators." Theend
in the firsttake
eats an element – Knivesbegin
multiple times on the samegenerator
. – Tagmeme