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.

4603. layout_stride should accept zero strides for empty extents

Section: 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.

  1. 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 converting s[i] to index_type is greater than 0 extents_type::index-cast(s[i]) is non-negative and representable as a value of type index_type (6.9.2 [basic.fundamental]) for all i in the range [0, rank_).

    • (4.2) — REQUIRED-SPAN-SIZE(e, s) is representable as a value of type index_type (6.9.2 [basic.fundamental]).

    • (4.3) — If both rank_ and the size of the multidimensional index space extents() areis greater than 0, then there exists a permutation P of the integers in the range [0, rank_), such that s[p0] > 0 is true and s[pi] >= s[pi-1] * e.extent(pi) is true for all i in the range [1, rank_), where pik is the ikth element of P.

    -5- Effects: […]