root icon indicating copy to clipboard operation
root copied to clipboard

Structured bindings for vectors

Open knoepfel opened this issue 3 years ago • 1 comments

Explain what you would like to see improved

For vector-like constructs (TVector2, TVector3, TLorentzVector, ROOT::Math::DisplacementVector3D, etc.), it would be very helpful to support structured bindings to the Cartesian coordinates (e.g.):

auto const [x, y] = func_that_returns_TVector2();

// which would be equivalent to the pair of statements:
TVector2 const vec = func_that_returns_TVector2();
auto const [a, b] = std::pair{vec[0], vec[1]};

assert(x == a);
assert(y == b);

I understand that this utility would only be helpful for accessing Cartesian coordinates, and not those related to (e.g.) polar representations.

Optional: share how it could be improved

This can be done by introducing suitable template specializations of get<std::size_t>, std::tuple_size, and std::tuple_element. See https://compiler-explorer.com/z/bv4Yef8GE as an example.

If this seems a desirable feature, I am willing to consider helping implement it.

knoepfel avatar Nov 30 '22 16:11 knoepfel

Dear @knoepfel , I am sorry to come back to this only now. It looks like a good proposal to me: @lmoneta what do you think?

dpiparo avatar Apr 25 '24 09:04 dpiparo