mdspan icon indicating copy to clipboard operation
mdspan copied to clipboard

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

Open stephanlachnit opened this issue 2 years ago • 4 comments

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.

stephanlachnit avatar Nov 27 '23 23:11 stephanlachnit

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

stigrs avatar Jan 24 '24 21:01 stigrs

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

stephanlachnit avatar Aug 21 '24 12:08 stephanlachnit

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.

nmm0 avatar Aug 21 '24 13:08 nmm0

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

stephanlachnit avatar Aug 21 '24 14:08 stephanlachnit

if constexpr doesn't exist pre C++17, should I add a macro here? Or just go with if and assume the compiler optimizes for this anyway?

stephanlachnit avatar Nov 18 '24 11:11 stephanlachnit

I don't quite get the build failure on MSVC, would need some help there...

stephanlachnit avatar Nov 18 '24 13:11 stephanlachnit

I don't quite get the build failure on MSVC, would need some help there...

What if you used the requires clause from operator()? I think that operator[] is not used in the MSVC build so we may not have updated it:

  MDSPAN_TEMPLATE_REQUIRES(
    class... SizeTypes,
    /* requires */ (
      extents_type::rank() == sizeof...(SizeTypes) &&
      (detail::are_valid_indices<index_type, SizeTypes...>())
    )
  )

nmm0 avatar Nov 26 '24 17:11 nmm0

@stephanlachnit My apologies, we recently fixed a bunch of naming changes. Would you mind rebasing? If you don't have the time I can take this over just let me know. It would be very nice to have this merged in

nmm0 avatar Feb 14 '25 14:02 nmm0

@stephanlachnit My apologies, we recently fixed a bunch of naming changes. Would you mind rebasing? If you don't have the time I can take this over just let me know. It would be very nice to have this merged in

If you want to take over feel free to do so

stephanlachnit avatar Feb 14 '25 15:02 stephanlachnit

Closing as it is superseded by https://github.com/kokkos/mdspan/pull/400

nmm0 avatar Apr 16 '25 17:04 nmm0