This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of Ready status.
layout_stride should accept zero strides for empty extentsSection: 23.7.3.4.7.3 [mdspan.layout.stride.cons] Status: Ready Submitter: Tomasz Kamiński Opened: 2026-06-07 Last modified: 2026-07-07
Priority: Not Prioritized
View all issues with Ready status.
Discussion:
layout_stride constructor accepting extents and strides values,
requires that stride values are always greater than zero, even if the extents
represents empty multidimensional index space. As for such extents types
layout_left and layout_right mappings will result in zero strides,
leads to inconsistencies in behavior:
std::dextents<size_t, 3> ext{1, 0, 4};
std::layout_left ll{ext};
std::layout_stride ls{std::layout_left{ext}}; // OK, no precondition on constructor
std::array<size, 3> strides{ll.stride(0), ll.stride(1), ll.stride(2)};
std::layout_stride lse{ext, strides}; // UB, ll.stride(2) is zero
As by definition any mapping over empty multidimensional index space
is unique, we should weaken that precondition and require non-zero
strides only for non-empty extents.
Furthermore, the current wording "result of converting s[i] to
index_type is greater than 0", forces implementation to convert
the inputs to (mostly unsigned) index_type before the check,
preventing detection of negative values. We should use index-cast
instead.
Proposed resolution:
This wording is relative to N5046.
Modify 23.7.3.4.7.3 [mdspan.layout.stride.cons] as indicated:
template<class OtherIndexType> constexpr mapping(const extents_type& e, span<OtherIndexType, rank_> s) noexcept; template<class OtherIndexType> constexpr mapping(const extents_type& e, const array<OtherIndexType, rank_>& s) noexcept;-3- Constraints: […]
-4- Preconditions:
(4.1) —
The result of convertings[i]toindex_typeis greater than0extents_type::index-cast(s[i])is non-negative and representable as a value of typeindex_type(6.9.2 [basic.fundamental]) for alliin the range[0, rank_).(4.2) —
REQUIRED-SPAN-SIZE(e, s)is representable as a value of typeindex_type(6.9.2 [basic.fundamental]).(4.3) — If both
rank_and the size of the multidimensional index spaceextents()areisgreater than0, then there exists a permutationPof the integers in the range[0, rank_), such thats[p0] > 0istrueands[pi] >= s[pi-1] * e.extent(pi)istruefor alliin the range[1, rank_), wherepis theikelement ofikthP.-5- Effects: […]