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.

4644. Strides of empty layout_left and layout_right mappings can be unrepresentable

Section: 23.7.3.4.5.2 [mdspan.layout.left.cons], 23.7.3.4.6.2 [mdspan.layout.right.cons] Status: New Submitter: Ian McCulloch Opened: 2026-11-20 Last modified: 2026-11-27

Priority: Not Prioritized

View all issues with New status.

Discussion:

The constructors of layout_left::mapping and layout_right::mapping require only that "the size of the multidimensional index space e is representable as a value of type index_type". When an extent is zero the size is zero, so this holds whatever the other extents are. stride(r) returns extents().fwd-prod-of-extents(r) for layout_left and extents().rev-prod-of-extents(r) for layout_right, and those products contain the zero extent only for some r. For example:

using E = std::dextents<std::int64_t, 3>;
constexpr std::int64_t n = std::int64_t{1} << 32;
std::layout_right::mapping<E> m(E{0, n, n});   // size 0: precondition holds
auto s = m.stride(0);                          // n * n == 2^64, not representable

No precondition excludes this. The layout mapping requirements (23.7.3.4.2 [mdspan.layout.reqmts]) give m.stride(r) the result type index_type without a representability condition, so stride(0) has no representable result. A conversion to layout_stride reads other.stride(r) and inherits the problem.

LWG 4545(i) identifies the same overflow for the padded layouts' converting constructors, and notes that layout_stride requires "only required_span_size to be representable, allowing strides to overflow". For a nonempty index space every canonical stride is at most the size, so only empty mappings are affected. Such shapes are unusual, but nothing excludes them.

Suggested resolution:

Option A

Add to the preconditions of each layout_left::mapping and layout_right::mapping constructor that establishes extents e (from extents_type, or from other.extents() in the converting constructors):

The product of e.extent(r) over every rank index r with e.extent(r) != 0 is representable as a value of type index_type.

Every layout_left or layout_right stride is either zero or a product of a subset of the nonzero extents, so this makes every canonical stride representable.

The condition is also invariant under permutation of the extents, which a condition on the strides alone is not. With index_type std::int32_t and n = 1 << 16, layout_right with extents {n, 0, n} has representable strides {0, n, 1}, but the same layout with the permuted extents {0, n, n} needs stride n * n. Requiring the product of the nonzero extents to be representable is necessary and sufficient for every canonical stride of both layouts to be representable under every permutation of the extents. It is sufficient by the subset argument above. It is necessary because placing all the nonzero extents before a zero extent in layout_left, or after it in layout_right, makes the stride of the zero-extent axis their full product. Without it, whether an empty canonical mapping is valid depends on the order of dimensions that are otherwise interchangeable, and a generic algorithm that permutes extents and then constructs a canonical mapping, as materializing a permuted view does, can turn a valid empty mapping into an invalid one.

Option B

An alternative resolution would be to require only the strides of a given order to be representable. Add to the layout_left::mapping constructors

e.fwd-prod-of-extents(r) is representable as a value of type index_type for every rank index r of e.

and the same to layout_right::mapping with rev-prof-of-extents.

This removes the overflow in stride(r) but keeps validity dependent on the order of the extents, as above.

Proposed resolution: