c++parametersparameter-passingxtensor

What parameter type should I use to accept xexpressions?


Suppose I have the xtensor xexpression waffle.

xt::xtensor_fixed<double, xt::xshape<1, 4>, xt::layout_type::column_major> open = {{1., 3., 2., 5.}};
xt::xtensor_fixed<double, xt::xshape<1, 4>, xt::layout_type::column_major> close = { {5., 1., 6., 2.} };
auto waffle = xt::equal(open, close);

Considering the type of waffle is: xt::detail::xfunction_type_t<xt::detail::not_equal_to, xt::xtensor_fixed<double, xt::xshape<1U, 366U>, xt::layout_type::column_major> &, xt::xtensor_fixed<double, xt::xshape<1U, 366U>, xt::layout_type::column_major> &>

I want to pass waffle into a class constructor:

class WaffleWrapper {
public:
    ??? waffle;
    WaffleWrapper(??? wafflein) {
        ??? waffle = wafflein;
    }
};

What would I use instead of ??? so that I can pass waffle into it?

e.g.

auto waffle = xt::equal(open, close);
WaffleWrapper example(waffle);

Solution

  • Like I commented, as a rule-of-thumb your class should own its data. You should thus create a data-container, which will force the evaluation of the xfunction. Thereafter you run no risk on pointing to data that may have gone out-of-scope.

    If you are worried about loosing the ability to use the fixedness of your array you could consider templating the class:

    #include <xtensor/xarray.hpp>
    #include <xtensor/xfixed.hpp>
    #include <xtensor/xview.hpp>
    #include <xtensor/xio.hpp>
    
    template<class T>
    class WaffleWrapper {
    public:
        T waffle;
    
        WaffleWrapper(const T& wafflein) {
            waffle = wafflein;
        }
    };
    
    int main()
    {
        using fixed_double = xt::xtensor_fixed<double, xt::xshape<1, 4>, xt::layout_type::column_major>;
        using fixed_int = xt::xtensor_fixed<int, xt::xshape<1, 4>, xt::layout_type::column_major>;
    
        fixed_double open = {{1., 3., 2., 5.}};
        fixed_double close = {{5., 1., 6., 2.}};
        auto waffle = xt::equal(open, close);
    
        WaffleWrapper<fixed_int> example(waffle);
    
        return 0;
    }
    

    Note that I abbreviated your typenames for readability.