xtensor icon indicating copy to clipboard operation
xtensor copied to clipboard

Add rvalue overload for `xcontainer::storage`

Open AntoinePrv opened this issue 3 years ago • 2 comments

Motivation

It can make sense for users to move the data/storage out of an xcontainer (for instance statically reshape an xt::xtensor<T, N> to an xt::xtensor<T, 1>).

Right now, we can do

xtensor<T,  1> new_tensor{std::move(old_tensor.storage()),  {old_tensor.size()}, {1}};

But, as suggested by @JohanMabille, we cannot do

xtensor<T, 1> new_tensor(std::move(old_tensor).storage(), ...);

Or

xtensor<T, 2> some_func(....);
xtensor<T, 1> new_tensor(some_func().storage(), ...);

Proposed Implementation

Add one of the following overload to xcontainer: - a/ storage_type&& storage() noexcept&&; - b/ storage_type storage() noexcept&&;

I can send a PR for this.

AntoinePrv avatar Jun 16 '21 18:06 AntoinePrv

Would be great! Actually, it would also be great to have a little section in the docs as well. Maybe somewhere around here ?

tdegeus avatar Jun 17 '21 09:06 tdegeus

@AntoinePrv I thought I had replied and I just realized I didn't, sorry for that.

I think the signature should be the first one since the method will return a data member (which is not a rvalue, thus there a chance to have a copy if you use the second signature).

A PR woule be very welcome!

JohanMabille avatar Jun 23 '21 20:06 JohanMabille