This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of New status.

3995. Issue with custom index conversion in <mdspan>

Section: 24.7.3 [views.multidim] Status: New Submitter: Hewill Kang Opened: 2023-10-11 Last modified: 2023-10-30

Priority: 3

View all issues with New status.

Discussion:

Currently, std::layout_meow::mapping::operator() has the following definition (24.7.3.4.5.3 [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_ (24.7.3.1 [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.

[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."

Proposed resolution: