Skip to content

shared generator expressions #2673

@nodatapoints

Description

@nodatapoints

I would like to wrap generator expressions in a function like in the following example.

inline auto build(int n) {
    auto x = xt::make_xshared(xt::arange(n));
    return x*x;
}

xt::xtensor<double, 1> array;
array = build(10);

Currently this gives an error

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

As far as I understand, this is currently only possible for xexpressions of evaluated expressions.
My current workarounds are:

  • Evaluating the generator. This sacrifices laziness.
  • Passing array by reference. This is not idiomatic and only works if the assignment results in a evaluation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions