jeudi 3 janvier 2019

How to create a cartesian product range from filtered data?

I am trying to create a cartesian-product range out of smaller ranges. I thought ranges::v3::view::cartesian_product would work, but somehow it doesn't.

If I try to create a cartesian product using containers directly, I have no problem. The following compiles:

#include <vector>
#include <range/v3/view/cartesian_product.hpp>

int main() {
    std::vector<int> data1{1,5,2,7,6,3,4,8,9,0};
    std::vector<int> data2{1,5,2,7,6,3,4,8,9,0};
    auto range = ranges::v3::view::cartesian_product(data1, data2);
}

However, as soon as I start using filters:

#include <vector>
#include <range/v3/view/cartesian_product.hpp>
#include <range/v3/view/filter.hpp>

int main() {
    std::vector<int> data1{1,5,2,7,6,3,4,8,9,0};
    std::vector<int> data2{1,5,2,7,6,3,4,8,9,0};
    auto range = ranges::v3::view::cartesian_product(
            data1 | ranges::v3::view::filter([](int v) { return v%2; }),
            data2);
}

I get tons of hard-to-decipher compile errors, starting with:

In file included from contrib/range/v3/view/cartesian_product.hpp:21:0,
                 from cartesian-err.cpp:2:
contrib/range/v3/range_concepts.hpp: In instantiation of ‘class ranges::v3::cartesian_product_view<ranges::v3::remove_if_view<ranges::v3::iterator_range<__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > > >, ranges::v3::logical_negate_<main()::<lambda(int)> > >, ranges::v3::iterator_range<__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > > > >’:
cartesian-err.cpp:10:18:   required from here
contrib/range/v3/range_concepts.hpp:78:50: error: no match for call to ‘(const ranges::v3::_begin_::fn) (const ranges::v3::remove_if_view<ranges::v3::iterator_range<__gnu_cxx::__normal_iterator<int*, std::vector<int> >, __gnu_cxx::__normal_iterator<int*, std::vector<int> > >, ranges::v3::logical_negate_<main()::<lambda(int)> > >&)’
                 using iterator_t = decltype(begin(std::declval<T &>()));
                                             ~~~~~^~~~~~~~~~~~~~~~~~~~~

How do I get around it?

P.S. Is there somewhere some good documentation of the ranges-v3 library? I can't find any and I feel I am walking in the dark...

Aucun commentaire:

Enregistrer un commentaire