mdspan
mdspan copied to clipboard
Structured bindings for `extents`
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;
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.
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 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
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.
The paper will become available as P2906 in the July mailing.
@bernhardmgruber Thanks! : - D
@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 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 thank you very much for the quick fix