xtensor icon indicating copy to clipboard operation
xtensor copied to clipboard

`xshared_expression` is hard to use

Open matwey opened this issue 3 months ago • 0 comments

Hello,

I am using xtensor 0.26.0 and the following code snipped adapted from the documentation:

template <class E, xt::enable_xexpression<E, bool> = true>
auto cos_plus_sin(E&& expr) {
        auto shared_expr = xt::make_xshared(std::forward<E>(expr));
        return xt::sin(shared_expr) + xt::cos(shared_expr);
}

None of the following three code snipped can be compiled successfully:

        const xt::xtensor<float, 1> x{1, 2, 4, 8};

        std::cout << x << std::endl;
        std::cout << cos_plus_sin(x) << std::endl;

complains about incompatible cv-qualifiers.

        xt::xtensor<float, 1> x{1, 2, 4, 8};

        std::cout << x << std::endl;
        std::cout << cos_plus_sin(x) << std::endl;

complains that cannot bind rvalue reference to lvalue.

        std::cout << xt::arange<float>(0, 4) << std::endl;
        std::cout << cos_plus_sin(xt::arange<float>(0, 4)) << std::endl;

complains as the following:

/usr/include/xtensor/core/xexpression.hpp:575:9: error: ‘class xt::xgenerator<xt::detail::arange_generator<float, float, float>, float, std::array<long unsigned int, 1> >’ has no member named ‘linear_begin’
  575 |         XTENSOR_FORWARD_METHOD(linear_begin)
      |         ^
/usr/include/xtensor/core/xexpression.hpp:576:9: error: ‘class xt::xgenerator<xt::detail::arange_generator<float, float, float>, float, std::array<long unsigned int, 1> >’ has no member named ‘linear_end’
  576 |         XTENSOR_FORWARD_METHOD(linear_end)
      |         ^

So, how the function from the documentation is supposed to be implemented for all this pretty valid scenarios?

matwey avatar Oct 03 '25 13:10 matwey