ryanhaining / cppitertools

Implementation of python itertools and builtin iteration functions for C++17
https://twitter.com/cppitertools
BSD 2-Clause "Simplified" License
1.37k stars 115 forks source link

Combining enumerate with filter #62

Closed kiwwisk closed 5 years ago

kiwwisk commented 5 years ago

Hello, as fist step, thanks for this great library!

Now with the issue:

std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8};

auto f = iter::enumerate(v) | iter::filter([](auto& i) { return std::get<1>(i) > 4; });

for (auto&& [i, e]: f) {
    std::cout << i << ", " << e << '\n';
}

This snippet doesn't compile (g++ 7.4), with error:

error: use of deleted function ‘std::optional<iter::impl::EnumIterYield<long unsigned int, int&> >& std::optional<iter::impl::EnumIterYield<long unsigned int, int&> >::operator=(std::optional<iter::impl::EnumIterYield<long unsigned int, int&> >&&)’

I opened StackOverflow question, since I'm not sure if I didn't do something wrong - https://stackoverflow.com/questions/57648457/cppitertools-how-to-combine-iterenumerate-and-iterfilter

Is it possible to combine filter with enumerate this way? The solutions appears is to change iterbase.hpp:301 from item_p_ = std::move(item); to item_p_.emplace(std::move(item));

ryanhaining commented 5 years ago

Thank you for taking the time to produce an MCVE for this. The suggest fix from Piotr Skotnicki is indeed correct and I just pushed a fix.