kokkos / mdspan

Reference implementation of mdspan targeting C++23
Other
401 stars 66 forks source link

Add element access via `at()` to `std::mdspan` #302

Open stephanlachnit opened 9 months ago

stephanlachnit commented 9 months ago

Closes #300

Possible implementation for ::at element access to mdspan with boundary checks. The boundary check is implemented with a for loop, as I did not see any other way to achieve this. The error message is inspired by the std::span::at error message from libstdc++ (https://github.com/gcc-mirror/gcc/commit/1fa85dcf656e2f2c7e483c9ed3c2680bf7db6858).

Note that this does not implement ::at element access to mdarray.

stigrs commented 7 months ago

Given that SizeTypes can be signed, it should also check for indices[r] < 0.

stephanlachnit commented 3 weeks ago

Given that SizeTypes can be signed, it should also check for indices[r] < 0.

Thanks for this comment! Is this true for std::span as well? At least in libstdc++, they do not check this:

https://github.com/gcc-mirror/gcc/blob/fd7dabc116b9abc40ee6aa25bcc5d240b8cc516a/libstdc%2B%2B-v3/include/std/span#L290-L300

nmm0 commented 3 weeks ago

In span, I believe it's always just std::size_t, but mdspan has the size type as a template parameter for the extent and can be signed.

stephanlachnit commented 3 weeks ago

In span, I believe it's always just std::size_t, but mdspan has the size type as a template parameter for the extent and can be signed.

Ah right, thanks for pointing this out!

Edit: updated the pull request