mdspan icon indicating copy to clipboard operation
mdspan copied to clipboard

Structured bindings for `extents`

Open bernhardmgruber opened this issue 3 years ago • 9 comments

The extents class could provide support for structured bindings to improve its usability. This way, we could transform your example from:

  stdex::mdspan m{d.data(), stdex::extents{3, 3}};
  for (std::size_t i = 0; i < m.extent(0); ++i)
    for (std::size_t j = 0; j < m.extent(1); ++j) // was m.extent(0)

to:

  stdex::mdspan m{d.data(), stdex::extents{3, 3}};
  const auto [rows, cols] = m.extents();
  for (std::size_t i = 0; i < rows; ++i)
    for (std::size_t j = 0; j < cols; ++j)

See your modified example: https://godbolt.org/z/enYcvbG8a

It's not a huge improvement, but with increasing rank, it can save some redundant mentioning of m.extents(...). Or prevent accidentially passing the wrong index ;) It also helps when I pass an instance of extents around without its surrounding mdspan. Then instead of:

const auto x = e.extent(0);
const auto y = e.extent(1);
const auto z = e.extent(2);

I can write:

const auto [x, y, z] = e;

bernhardmgruber avatar Nov 30 '21 15:11 bernhardmgruber

This seems like a good extension. We can't change the current proposal to add stuff like this in time for C++23 however since the design approval cutoff is basically here. But we should be able to do this as a separate paper on top of P0009 targeting C++26.

crtrott avatar Nov 30 '21 17:11 crtrott

Not sure how this is possible. Isn't structured bindings always giving back the member thingies? (updated link for your exmaple btw: https://godbolt.org/z/bzTPb7K5Y)

crtrott avatar Nov 22 '22 17:11 crtrott

@crtrott It does, is just that you specialized for int whereas you example uses long unsigned int. This is the fix: https://godbolt.org/z/cW6W3aGMM

SoilRos avatar Mar 10 '23 13:03 SoilRos

Btw, I have a draft proposal for this feature, but my PhD has kept me too busy the last weeks to send it for a first review to the mailing list. Here is what I got so far: https://api.csswg.org/bikeshed/?url=https://gist.githubusercontent.com/bernhardmgruber/3d8c422e2cea2df542411623dd443ca4/raw/afe327ffe7e089912301cbfdf83acb64ca1c6853/std_extents_structured_bindings.bs

I have some time next week, so I could start circulating the draft.

bernhardmgruber avatar Mar 10 '23 14:03 bernhardmgruber

The paper will become available as P2906 in the July mailing.

bernhardmgruber avatar May 31 '23 07:05 bernhardmgruber

@bernhardmgruber Thanks! : - D

mhoemmen avatar May 31 '23 14:05 mhoemmen

@bernhardmgruber thanks for the proposal! I've tried to compile your examples on godbolt, but I've had errors. Do you happen to know why they don't compile?

klimentyev avatar Mar 12 '24 18:03 klimentyev

@klimentyev Thx! It seems mdspan was moved out of the std::experimental namespace and is now available just in std. I will update the paper. Here is a working example (experimental removed): https://godbolt.org/z/aqWEojToq

bernhardmgruber avatar Mar 20 '24 20:03 bernhardmgruber

@bernhardmgruber thank you very much for the quick fix

klimentyev avatar Mar 21 '24 16:03 klimentyev