Issue with custom index conversion in <mdspan>
Hewill Kang

Created on 2023-10-11.00:00:00 last changed 1 month ago


Date: 2023-10-15.00:00:00

[ 2023-10-30; Reflector poll ]

Set priority to 3 after reflector poll. "P4 - doesn't affect 'normal' uses of custom index types. Only affects expert users that interface with the mapping directly, because mdspan does the conversions."

Date: 2023-10-11.00:00:00

Currently, std::layout_meow::mapping::operator() has the following definition ([mdspan.layout.left.obs]):

template<class... Indices>
  constexpr index_type operator()(Indices... i) const noexcept;

-2- Constraints:

  1. (2.1) — sizeof...(Indices) == extents_type::rank() is true,

  2. (2.2) — (is_convertible_v<Indices, index_type> && ...) is true, and

  3. (2.3) — (is_nothrow_constructible_v<index_type, Indices> && ...) is true.

Preconditions: extents_type::index-cast(i) is a multidimensional index in extents_ ([mdspan.overview]).

Effects: Let P be a parameter pack such that

is_same_v<index_sequence_for<Indices...>, index_sequence<P...>>

is true. Equivalent to:

return ((static_cast<index_type>(i) * stride(P)) + ... + 0);

Above, is_convertible_v<Indices, index_type> implies that index_type can be constructed through rvalue-qualified conversion operators. However, we cast the lvalue i in the return statement, which makes the expression possibly ill-formed. The same goes for extents_type::index-cast(i).

However, if we use std::move before casting, this will result in the rvalue-qualified conversion operator being called in Preconditions via extents_type::index-cast(i) before the mapping index is actually calculated, so that the expression may no longer be valid. And such an issue already exists in mdspan::operator[].

In addition, the variadic version of mdspan::operator[] constraints is_convertible_v<OtherIndexTypes, index_type>, but its array/span version constraints is_convertible_v<const OtherIndexType&, index_type>.

This seems to bring inconsistency as mdspan[arr] may not necessarily guarantee mdspan[arr[i]...].

I think we should unanimously require that custom indexes can be converted to index_type via const lvalue references, which eliminates the worry of conversion expiration.

Date User Action Args
2023-10-30 17:19:14adminsetmessages: + msg13790
2023-10-11 00:00:00admincreate