xtensor-stack / xtensor

C++ tensors with broadcasting and lazy computing
BSD 3-Clause "New" or "Revised" License
3.32k stars 394 forks source link

No matching conversion xt::keep(...) #2036

Open tdegeus opened 4 years ago

tdegeus commented 4 years ago

The following example:

#include <xtensor/xtensor.hpp>
#include <xtensor/xview.hpp>
#include <xtensor/xio.hpp>

int main()
{
    xt::xtensor<size_t,1> a = {1, 2, 1, 0, 3, 1};
    xt::xtensor<size_t,1> idx = {0, 3};
    xt::xtensor<size_t,1> jdx = {1, 2};
    auto b = xt::view(a, xt::keep(idx + jdx));
    return 0;
}

Fails to compile:

In file included from /Users/tdegeus/Downloads/test/xtensor/main.cpp:1:
In file included from /Users/tdegeus/miniconda3/envs/stable/include/xtensor/xtensor.hpp:20:
In file included from /Users/tdegeus/miniconda3/envs/stable/include/xtensor/xcontainer.hpp:24:
In file included from /Users/tdegeus/miniconda3/envs/stable/include/xtensor/xmath.hpp:28:
In file included from /Users/tdegeus/miniconda3/envs/stable/include/xtensor/xmanipulation.hpp:13:
In file included from /Users/tdegeus/miniconda3/envs/stable/include/xtensor/xbuilder.hpp:34:
In file included from /Users/tdegeus/miniconda3/envs/stable/include/xtensor/xgenerator.hpp:27:
In file included from /Users/tdegeus/miniconda3/envs/stable/include/xtensor/xstrided_view.hpp:27:
In file included from /Users/tdegeus/miniconda3/envs/stable/include/xtensor/xstrided_view_base.hpp:19:
/Users/tdegeus/miniconda3/envs/stable/include/xtensor/xslice.hpp:395:16: error: no matching conversion for functional-style cast from
      'xt::xfunction<xt::detail::plus, const xt::xtensor_container<xt::uvector<unsigned long, std::__1::allocator<unsigned long> >, 1,
      xt::layout_type::row_major, xt::xtensor_expression_tag> &, const xt::xtensor_container<xt::uvector<unsigned long,
      std::__1::allocator<unsigned long> >, 1, xt::layout_type::row_major, xt::xtensor_expression_tag> &>' to 'xkeep_slice<typename
      std::decay_t<xfunction<plus, const xtensor_container<uvector<unsigned long, allocator<unsigned long> >, 1, xt::layout_type::row_major,
      xtensor_expression_tag> &, const xtensor_container<uvector<unsigned long, allocator<unsigned long> >, 1, xt::layout_type::row_major,
      xtensor_expression_tag> &> >::value_type>' (aka 'xkeep_slice<unsigned long>')
        return xkeep_slice<typename std::decay_t<T>::value_type>(std::forward<T>(indices));
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/tdegeus/Downloads/test/xtensor/main.cpp:10:30: note: in instantiation of function template specialization
      'xt::keep<xt::xfunction<xt::detail::plus, const xt::xtensor_container<xt::uvector<unsigned long, std::__1::allocator<unsigned long> >,
      1, xt::layout_type::row_major, xt::xtensor_expression_tag> &, const xt::xtensor_container<xt::uvector<unsigned long,
      std::__1::allocator<unsigned long> >, 1, xt::layout_type::row_major, xt::xtensor_expression_tag> &> >' requested here
    auto b = xt::view(a, xt::keep(idx + jdx));
                             ^
/Users/tdegeus/miniconda3/envs/stable/include/xtensor/xslice.hpp:321:11: note: candidate constructor (the implicit copy constructor) not
      viable: no known conversion from 'xt::xfunction<xt::detail::plus, const xt::xtensor_container<xt::uvector<unsigned long,
      std::__1::allocator<unsigned long> >, 1, xt::layout_type::row_major, xt::xtensor_expression_tag> &, const
      xt::xtensor_container<xt::uvector<unsigned long, std::__1::allocator<unsigned long> >, 1, xt::layout_type::row_major,
      xt::xtensor_expression_tag> &>' to 'const xt::xkeep_slice<unsigned long>' for 1st argument
    class xkeep_slice : public xslice<xkeep_slice<T>>
          ^
/Users/tdegeus/miniconda3/envs/stable/include/xtensor/xslice.hpp:321:11: note: candidate constructor (the implicit move constructor) not
      viable: no known conversion from 'xt::xfunction<xt::detail::plus, const xt::xtensor_container<xt::uvector<unsigned long,
      std::__1::allocator<unsigned long> >, 1, xt::layout_type::row_major, xt::xtensor_expression_tag> &, const
      xt::xtensor_container<xt::uvector<unsigned long, std::__1::allocator<unsigned long> >, 1, xt::layout_type::row_major,
      xt::xtensor_expression_tag> &>' to 'xt::xkeep_slice<unsigned long>' for 1st argument
    class xkeep_slice : public xslice<xkeep_slice<T>>
          ^
/Users/tdegeus/miniconda3/envs/stable/include/xtensor/xslice.hpp:330:18: note: candidate constructor [with C =
      xt::xfunction<xt::detail::plus, const xt::xtensor_container<xt::uvector<unsigned long, std::__1::allocator<unsigned long> >, 1,
      xt::layout_type::row_major, xt::xtensor_expression_tag> &, const xt::xtensor_container<xt::uvector<unsigned long,
      std::__1::allocator<unsigned long> >, 1, xt::layout_type::row_major, xt::xtensor_expression_tag> &>, $1 = void] not viable: expects an
      l-value for 1st argument
        explicit xkeep_slice(C& cont);
                 ^
/Users/tdegeus/miniconda3/envs/stable/include/xtensor/xslice.hpp:331:18: note: candidate constructor not viable: no known conversion from
      'xt::xfunction<xt::detail::plus, const xt::xtensor_container<xt::uvector<unsigned long, std::__1::allocator<unsigned long> >, 1,
      xt::layout_type::row_major, xt::xtensor_expression_tag> &, const xt::xtensor_container<xt::uvector<unsigned long,
      std::__1::allocator<unsigned long> >, 1, xt::layout_type::row_major, xt::xtensor_expression_tag> &>' to 'xt::xkeep_slice<unsigned
      long>::container_type' (aka 'svector<unsigned long>') for 1st argument
        explicit xkeep_slice(container_type&& cont);
                 ^
/Users/tdegeus/miniconda3/envs/stable/include/xtensor/xslice.hpp:334:9: note: candidate template ignored: could not match 'initializer_list'
      against 'xfunction'
        xkeep_slice(std::initializer_list<S> t);
        ^
/Users/tdegeus/miniconda3/envs/stable/include/xtensor/xslice.hpp:359:9: note: candidate constructor not viable: requires 0 arguments, but 1
      was provided
        xkeep_slice() = default;
        ^
1 error generated.
tdegeus commented 4 years ago

Similarly xt::keep(xt::view(...)) does not work

JohanMabille commented 4 years ago

Indeed, xt::keep is not supposed to work on xexpression objects.

tdegeus commented 4 years ago

Is there a reason to allow this?

JohanMabille commented 4 years ago

I think this was mainly to avoid circular dependencies. And also because xt::keep are basic building blocs for views, so it would be weird to have them depend on it. I guess we could add a constructor that accepts a pair of iterators, so it would be possible to sintantiate it from any kind of expression

AntoinePrv commented 3 years ago

Any plan for this? I think it would be useful to be able to pass an xexpression (or atl least a tensor) to xt::keep and xt::drop.