mdspan icon indicating copy to clipboard operation
mdspan copied to clipboard

`extents::operator[]`

Open bernhardmgruber opened this issue 4 years ago • 1 comments

Allowing some extents to be compile time constants in the extents class is super useful. I generally regard the extents class as an array of compile and runtime indices. As such, I would also like to treat it more like an array and e.g. be allowed to subscript it. So instead of:

  const auto e = stdex::extents{3, 3};
  for (std::size_t i = 0; i < e.extent(0); ++i)
    for (std::size_t j = 0; j < e.extent(1); ++j)

I could write:

  const auto e = stdex::extents{3, 3};
  for (std::size_t i = 0; i < e[0]; ++i)
    for (std::size_t j = 0; j < e[1]; ++j)

Is there a specific reason for not providing operator[] on extents? It would improve its usability a bit.

bernhardmgruber avatar Nov 30 '21 16:11 bernhardmgruber

Its actually an interesting question: I actually have considered to propose something like partially_static_array which is what the underlying storage thing for extents in our implementation is. Besides that we can consider adding such an operator, generally extents is a bit more than just an array. Stuff like rank or dynamic_rank have a pretty specific meaning, though with slightly different names they might make sense in an "array" context. Also you still need the guaranteed static access to extents.

In either case: it would certainly be possible to propose adding operator[] which essentially does return extent(i). Again this would be C++26 proposals.

crtrott avatar Nov 30 '21 17:11 crtrott

We may rather want to propose something like the maybe_static_array in my new extents implementation. see: #212

crtrott avatar Nov 22 '22 17:11 crtrott