ericniebler / range-v3

Range library for C++14/17/20, basis for C++20's std::ranges
Other
4.12k stars 440 forks source link

view::remove_if not work with move-only predicate #1040

Closed tower120 closed 5 years ago

tower120 commented 5 years ago

view::remove_if does not work with non_copyable (move-only) predicate. Is this by the design?

Following does not compiles:

#include <iostream>
#include <vector>

#include <range/v3/view/remove_if.hpp>

struct Pred{    
    int i = 2;

    Pred(const Pred&) = delete;
    Pred(Pred&&) = default;
    Pred() = default;

    bool operator()(int other) const {
        return other == i;
    }
};

void test_value_capture(){
    using List = std::vector<int>;
    const List list{};
    auto out = list |view::remove_if(Pred());

    std::cout << out.empty();
}

int main()
{
    test_value_capture();
}

https://wandbox.org/permlink/5xQFKzIUyRQBie1s

ericniebler commented 5 years ago

This is by design, especially in the v1.0-beta branch. The views are required to be Semiregular, which subsumes Copyable. There is no way to aggregate a move-only predicate while preserving copyability.

tower120 commented 5 years ago

I see, thanks.