24 Containers library [containers]

24.7 Views [views]

24.7.1 General [views.general]

The header <span> defines the view span.
The header <mdspan> defines the class template mdspan and other facilities for interacting with these multidimensional views.

24.7.2 Header <span> synopsis [span.syn]

namespace std { // constants inline constexpr size_t dynamic_extent = numeric_limits<size_t>::max(); // [views.span], class template span template<class ElementType, size_t Extent = dynamic_extent> class span; template<class ElementType, size_t Extent> inline constexpr bool ranges::enable_view<span<ElementType, Extent>> = true; template<class ElementType, size_t Extent> inline constexpr bool ranges::enable_borrowed_range<span<ElementType, Extent>> = true; // [span.objectrep], views of object representation template<class ElementType, size_t Extent> span<const byte, Extent == dynamic_extent ? dynamic_extent : sizeof(ElementType) * Extent> as_bytes(span<ElementType, Extent> s) noexcept; template<class ElementType, size_t Extent> span<byte, Extent == dynamic_extent ? dynamic_extent : sizeof(ElementType) * Extent> as_writable_bytes(span<ElementType, Extent> s) noexcept; }

24.7.3 Class template span [views.span]

24.7.3.1 Overview [span.overview]

A span is a view over a contiguous sequence of objects, the storage of which is owned by some other object.
All member functions of span have constant time complexity.
namespace std { template<class ElementType, size_t Extent = dynamic_extent> class span { public: // constants and types using element_type = ElementType; using value_type = remove_cv_t<ElementType>; using size_type = size_t; using difference_type = ptrdiff_t; using pointer = element_type*; using const_pointer = const element_type*; using reference = element_type&; using const_reference = const element_type&; using iterator = implementation-defined; // see [span.iterators] using const_iterator = std::const_iterator<iterator>; using reverse_iterator = std::reverse_iterator<iterator>; using const_reverse_iterator = std::const_iterator<reverse_iterator>; static constexpr size_type extent = Extent; // [span.cons], constructors, copy, and assignment constexpr span() noexcept; template<class It> constexpr explicit(extent != dynamic_extent) span(It first, size_type count); template<class It, class End> constexpr explicit(extent != dynamic_extent) span(It first, End last); template<size_t N> constexpr span(type_identity_t<element_type> (&arr)[N]) noexcept; template<class T, size_t N> constexpr span(array<T, N>& arr) noexcept; template<class T, size_t N> constexpr span(const array<T, N>& arr) noexcept; template<class R> constexpr explicit(extent != dynamic_extent) span(R&& r); constexpr span(const span& other) noexcept = default; template<class OtherElementType, size_t OtherExtent> constexpr explicit(see below) span(const span<OtherElementType, OtherExtent>& s) noexcept; ~span() noexcept = default; constexpr span& operator=(const span& other) noexcept = default; // [span.sub], subviews template<size_t Count> constexpr span<element_type, Count> first() const; template<size_t Count> constexpr span<element_type, Count> last() const; template<size_t Offset, size_t Count = dynamic_extent> constexpr span<element_type, see below> subspan() const; constexpr span<element_type, dynamic_extent> first(size_type count) const; constexpr span<element_type, dynamic_extent> last(size_type count) const; constexpr span<element_type, dynamic_extent> subspan( size_type offset, size_type count = dynamic_extent) const; // [span.obs], observers constexpr size_type size() const noexcept; constexpr size_type size_bytes() const noexcept; [[nodiscard]] constexpr bool empty() const noexcept; // [span.elem], element access constexpr reference operator[](size_type idx) const; constexpr reference front() const; constexpr reference back() const; constexpr pointer data() const noexcept; // [span.iterators], iterator support constexpr iterator begin() const noexcept; constexpr iterator end() const noexcept; constexpr const_iterator cbegin() const noexcept { return begin(); } constexpr const_iterator cend() const noexcept { return end(); } constexpr reverse_iterator rbegin() const noexcept; constexpr reverse_iterator rend() const noexcept; constexpr const_reverse_iterator crbegin() const noexcept { return rbegin(); } constexpr const_reverse_iterator crend() const noexcept { return rend(); } private: pointer data_; // exposition only size_type size_; // exposition only }; template<class It, class EndOrSize> span(It, EndOrSize) -> span<remove_reference_t<iter_reference_t<It>>>; template<class T, size_t N> span(T (&)[N]) -> span<T, N>; template<class T, size_t N> span(array<T, N>&) -> span<T, N>; template<class T, size_t N> span(const array<T, N>&) -> span<const T, N>; template<class R> span(R&&) -> span<remove_reference_t<ranges::range_reference_t<R>>>; }
span<ElementType, Extent> is a trivially copyable type ([basic.types.general]).
ElementType is required to be a complete object type that is not an abstract class type.

24.7.3.2 Constructors, copy, and assignment [span.cons]

constexpr span() noexcept;
Constraints: Extent == dynamic_­extent || Extent == 0 is true.
Postconditions: size() == 0 && data() == nullptr.
template<class It> constexpr explicit(extent != dynamic_extent) span(It first, size_type count);
Constraints: Let U be remove_­reference_­t<iter_­reference_­t<It>>.
  • is_­convertible_­v<U(*)[], element_­type(*)[]> is true.
    [Note 1:
    The intent is to allow only qualification conversions of the iterator reference type to element_­type.
    โ€” end note]
Preconditions:
Effects: Initializes data_­ with to_­address(first) and size_­ with count.
Throws: Nothing.
template<class It, class End> constexpr explicit(extent != dynamic_extent) span(It first, End last);
Constraints: Let U be remove_­reference_­t<iter_­reference_­t<It>>.
Preconditions:
Effects: Initializes data_­ with to_­address(first) and size_­ with last - first.
Throws: When and what last - first throws.
template<size_t N> constexpr span(type_identity_t<element_type> (&arr)[N]) noexcept; template<class T, size_t N> constexpr span(array<T, N>& arr) noexcept; template<class T, size_t N> constexpr span(const array<T, N>& arr) noexcept;
Constraints: Let U be remove_­pointer_­t<decltype(data(arr))>.
  • extent == dynamic_­extent || N == extent is true, and
  • is_­convertible_­v<U(*)[], element_­type(*)[]> is true.
    [Note 3:
    The intent is to allow only qualification conversions of the array element type to element_­type.
    โ€” end note]
Effects: Constructs a span that is a view over the supplied array.
[Note 4:
type_­identity_­t affects class template argument deduction.
โ€” end note]
Postconditions: size() == N && data() == data(arr) is true.
template<class R> constexpr explicit(extent != dynamic_extent) span(R&& r);
Constraints: Let U be remove_­reference_­t<ranges​::​range_­reference_­t<R>>.
  • R satisfies ranges​::​contiguous_­range and ranges​::​sized_­range.
  • Either R satisfies ranges​::​borrowed_­range or is_­const_­v<element_­type> is true.
  • remove_­cvref_­t<R> is not a specialization of span.
  • remove_­cvref_­t<R> is not a specialization of array.
  • is_­array_­v<remove_­cvref_­t<R>> is false.
  • is_­convertible_­v<U(*)[], element_­type(*)[]> is true.
    [Note 5:
    The intent is to allow only qualification conversions of the range reference type to element_­type.
    โ€” end note]
Preconditions:
Effects: Initializes data_­ with ranges​::​data(r) and size_­ with ranges​::​size(r).
Throws: What and when ranges​::​data(r) and ranges​::​size(r) throw.
constexpr span(const span& other) noexcept = default;
Postconditions: other.size() == size() && other.data() == data().
template<class OtherElementType, size_t OtherExtent> constexpr explicit(see below) span(const span<OtherElementType, OtherExtent>& s) noexcept;
Constraints:
  • extent == dynamic_­extent || OtherExtent == dynamic_­extent || extent == OtherExtent is true, and
  • is_­convertible_­v<OtherElementType(*)[], element_­type(*)[]> is true.
    [Note 6:
    The intent is to allow only qualification conversions of the OtherElementType to element_­type.
    โ€” end note]
Preconditions: If extent is not equal to dynamic_­extent, then s.size() is equal to extent.
Effects: Constructs a span that is a view over the range [s.data(), s.data() + s.size()).
Postconditions: size() == s.size() && data() == s.data().
Remarks: The expression inside explicit is equivalent to: extent != dynamic_extent && OtherExtent == dynamic_extent
constexpr span& operator=(const span& other) noexcept = default;
Postconditions: size() == other.size() && data() == other.data().

24.7.3.3 Deduction guides [span.deduct]

template<class It, class EndOrSize> span(It, EndOrSize) -> span<remove_reference_t<iter_reference_t<It>>>;
Constraints: It satisfies contiguous_­iterator.
template<class R> span(R&&) -> span<remove_reference_t<ranges::range_reference_t<R>>>;
Constraints: R satisfies ranges​::​contiguous_­range.

24.7.3.4 Subviews [span.sub]

template<size_t Count> constexpr span<element_type, Count> first() const;
Mandates: Count <= Extent is true.
Preconditions: Count <= size() is true.
Effects: Equivalent to: return R{data(), Count}; where R is the return type.
template<size_t Count> constexpr span<element_type, Count> last() const;
Mandates: Count <= Extent is true.
Preconditions: Count <= size() is true.
Effects: Equivalent to: return R{data() + (size() - Count), Count}; where R is the return type.
template<size_t Offset, size_t Count = dynamic_extent> constexpr span<element_type, see below> subspan() const;
Mandates: Offset <= Extent && (Count == dynamic_extent || Count <= Extent - Offset) is true.
Preconditions: Offset <= size() && (Count == dynamic_extent || Count <= size() - Offset) is true.
Effects: Equivalent to: return span<ElementType, see below>( data() + Offset, Count != dynamic_extent ? Count : size() - Offset);
Remarks: The second template argument of the returned span type is: Count != dynamic_extent ? Count : (Extent != dynamic_extent ? Extent - Offset : dynamic_extent)
constexpr span<element_type, dynamic_extent> first(size_type count) const;
Preconditions: count <= size() is true.
Effects: Equivalent to: return {data(), count};
constexpr span<element_type, dynamic_extent> last(size_type count) const;
Preconditions: count <= size() is true.
Effects: Equivalent to: return {data() + (size() - count), count};
constexpr span<element_type, dynamic_extent> subspan( size_type offset, size_type count = dynamic_extent) const;
Preconditions: offset <= size() && (count == dynamic_extent || count <= size() - offset) is true.
Effects: Equivalent to: return {data() + offset, count == dynamic_extent ? size() - offset : count};

24.7.3.5 Observers [span.obs]

constexpr size_type size() const noexcept;
Effects: Equivalent to: return size_­;
constexpr size_type size_bytes() const noexcept;
Effects: Equivalent to: return size() * sizeof(element_­type);
[[nodiscard]] constexpr bool empty() const noexcept;
Effects: Equivalent to: return size() == 0;

24.7.3.6 Element access [span.elem]

constexpr reference operator[](size_type idx) const;
Preconditions: idx < size() is true.
Effects: Equivalent to: return *(data() + idx);
constexpr reference front() const;
Preconditions: empty() is false.
Effects: Equivalent to: return *data();
constexpr reference back() const;
Preconditions: empty() is false.
Effects: Equivalent to: return *(data() + (size() - 1));
constexpr pointer data() const noexcept;
Effects: Equivalent to: return data_­;

24.7.3.7 Iterator support [span.iterators]

using iterator = implementation-defined;
The type models contiguous_­iterator ([iterator.concept.contiguous]), meets the Cpp17RandomAccessIterator requirements ([random.access.iterators]), and meets the requirements for constexpr iterators ([iterator.requirements.general]), whose value type is value_­type and whose reference type is reference.
All requirements on container iterators ([container.requirements]) apply to span​::​iterator as well.
constexpr iterator begin() const noexcept;
Returns: An iterator referring to the first element in the span.
If empty() is true, then it returns the same value as end().
constexpr iterator end() const noexcept;
Returns: An iterator which is the past-the-end value.
constexpr reverse_iterator rbegin() const noexcept;
Effects: Equivalent to: return reverse_­iterator(end());
constexpr reverse_iterator rend() const noexcept;
Effects: Equivalent to: return reverse_­iterator(begin());

24.7.3.8 Views of object representation [span.objectrep]

template<class ElementType, size_t Extent> span<const byte, Extent == dynamic_extent ? dynamic_extent : sizeof(ElementType) * Extent> as_bytes(span<ElementType, Extent> s) noexcept;
Effects: Equivalent to: return R{reinterpret_­cast<const byte*>(s.data()), s.size_­bytes()}; where R is the return type.
template<class ElementType, size_t Extent> span<byte, Extent == dynamic_extent ? dynamic_extent : sizeof(ElementType) * Extent> as_writable_bytes(span<ElementType, Extent> s) noexcept;
Constraints: is_­const_­v<ElementType> is false.
Effects: Equivalent to: return R{reinterpret_­cast<byte*>(s.data()), s.size_­bytes()}; where R is the return type.

24.7.4 Header <mdspan> synopsis [mdspan.syn]

namespace std { // [mdspan.extents], class template extents template<class IndexType, size_t... Extents> class extents; // [mdspan.extents.dextents], alias template dextents template<class IndexType, size_t Rank> using dextents = see below; // [mdspan.layout], layout mapping struct layout_left; struct layout_right; struct layout_stride; // [mdspan.accessor.default], class template default_­accessor template<class ElementType> class default_accessor; // [mdspan.mdspan], class template mdspan template<class ElementType, class Extents, class LayoutPolicy = layout_right, class AccessorPolicy = default_accessor<ElementType>> class mdspan; }

24.7.5 Overview [mdspan.overview]

A multidimensional index space is a Cartesian product of integer intervals.
Each interval can be represented by a half-open range , where and are the lower and upper bounds of the dimension.
The rank of a multidimensional index space is the number of intervals it represents.
The size of a multidimensional index space is the product of for each dimension i if its rank is greater than 0, and 1 otherwise.
An integer r is a rank index of an index space S if r is in the range .
A pack of integers idx is a multidimensional index in a multidimensional index space S (or representation thereof) if both of the following are true:
  • sizeof...(idx) is equal to the rank of S, and
  • for every rank index i of S, the value of idx is an integer in the interval of S.

24.7.6 Class template extents [mdspan.extents]

24.7.6.1 Overview [mdspan.extents.overview]

The class template extents represents a multidimensional index space of rank equal to sizeof...(Extents).
In subclause ([views]), extents is used synonymously with multidimensional index space.
namespace std { template<class IndexType, size_t... Extents> class extents { public: using index_type = IndexType; using size_type = make_unsigned_t<index_type>; using rank_type = size_t; // [mdspan.extents.obs], observers of the multidimensional index space static constexpr rank_type rank() noexcept { return sizeof...(Extents); } static constexpr rank_type rank_dynamic() noexcept { return dynamic-index(rank()); } static constexpr size_t static_extent(rank_type) noexcept; constexpr index_type extent(rank_type) const noexcept; // [mdspan.extents.cons], constructors constexpr extents() noexcept = default; template<class OtherIndexType, size_t... OtherExtents> constexpr explicit(see below) extents(const extents<OtherIndexType, OtherExtents...>&) noexcept; template<class... OtherIndexTypes> constexpr explicit extents(OtherIndexTypes...) noexcept; template<class OtherIndexType, size_t N> constexpr explicit(N != rank_dynamic()) extents(span<OtherIndexType, N>) noexcept; template<class OtherIndexType, size_t N> constexpr explicit(N != rank_dynamic()) extents(const array<OtherIndexType, N>&) noexcept; // [mdspan.extents.cmp], comparison operators template<class OtherIndexType, size_t... OtherExtents> friend constexpr bool operator==(const extents&, const extents<OtherIndexType, OtherExtents...>&) noexcept; // [mdspan.extents.expo], exposition-only helpers constexpr size_t fwd-prod-of-extents(rank_type) const noexcept; // exposition only constexpr size_t rev-prod-of-extents(rank_type) const noexcept; // exposition only template<class OtherIndexType> static constexpr auto index-cast(OtherIndexType&&) noexcept; // exposition only private: static constexpr rank_type dynamic-index(rank_type) noexcept; // exposition only static constexpr rank_type dynamic-index-inv(rank_type) noexcept; // exposition only array<index_type, rank_dynamic()> dynamic-extents{}; // exposition only }; template<class... Integrals> explicit extents(Integrals...) -> see below; }
Mandates:
  • IndexType is a signed or unsigned integer type, and
  • each element of Extents is either equal to dynamic_­extent, or is representable as a value of type IndexType.
Each specialization of extents models regular and is trivially copyable.
Let be the element of Extents.
is a dynamic extent if it is equal to dynamic_­extent, otherwise is a static extent.
Let be the value of dynamic-extents[dynamic-index(r)] if is a dynamic extent, otherwise .
The interval of the multidimensional index space represented by an extents object is .

24.7.6.2 Exposition-only helpers [mdspan.extents.expo]

static constexpr rank_type dynamic-index(rank_type i) noexcept; // exposition only
Preconditions: i <= rank() is true.
Returns: The number of with for which is a dynamic extent.
static constexpr rank_type dynamic-index-inv(rank_type i) noexcept; // exposition only
Preconditions: i < rank_­dynamic() is true.
Returns: The minimum value of r such that dynamic-index(r + 1) == i + 1 is true.
constexpr size_t fwd-prod-of-extents(rank_type i) const noexcept; // exposition only
Preconditions: i <= rank() is true.
Returns: If i > 0 is true, the product of extent(k) for all k in the range [0, i), otherwise 1.
constexpr size_t rev-prod-of-extents(rank_type i) const noexcept; // exposition only
Preconditions: i < rank() is true.
Returns: If i + 1 < rank() is true, the product of extent(k) for all k in the range [i + 1, rank()), otherwise 1.
template<class OtherIndexType> static constexpr auto index-cast(OtherIndexType&& i) noexcept; // exposition only
Effects:
  • If OtherIndexType is an integral type other than bool, then equivalent to return i;,
  • otherwise, equivalent to return static_­cast<index_­type>(i);.
[Note 1:
This function will always return an integral type other than bool.
Since this function's call sites are constrained on convertibility of OtherIndexType to index_­type, integer-class types can use the static_­cast branch without loss of precision.
โ€” end note]

24.7.6.3 Constructors [mdspan.extents.cons]

template<class OtherIndexType, size_t... OtherExtents> constexpr explicit(see below) extents(const extents<OtherIndexType, OtherExtents...>& other) noexcept;
Constraints:
  • sizeof...(OtherExtents) == rank() is true.
  • ((OtherExtents == dynamic_­extent || Extents == dynamic_­extent || OtherExtents ==
    Extents) && ...)
    is true.
Preconditions:
  • other.extent(r) equals for each r for which is a static extent, and
  • either
    • sizeof...(OtherExtents) is zero, or
    • other.extent(r) is representable as a value of type index_­type for every rank index r of other.
Postconditions: *this == other is true.
Remarks: The expression inside explicit is equivalent to: (((Extents != dynamic_extent) && (OtherExtents == dynamic_extent)) || ... ) || (numeric_limits<index_type>::max() < numeric_limits<OtherIndexType>::max())
template<class... OtherIndexTypes> explicit constexpr extents(OtherIndexTypes... exts) noexcept;
Let N be sizeof...(OtherIndexTypes), and let exts_­arr be array<index_­type, N>{static_­cast<
index_­type>(std​::​move(exts))...}
.
Constraints:
  • (is_­convertible_­v<OtherIndexTypes, index_­type> && ...) is true,
  • (is_­nothrow_­constructible_­v<index_­type, OtherIndexTypes> && ...) is true, and
  • N == rank_­dynamic() || N == rank() is true.
    [Note 1:
    One can construct extents from just dynamic extents, which are all the values getting stored, or from all the extents with a precondition.
    โ€” end note]
Preconditions:
  • If N != rank_­dynamic() is true, exts_­arr[r] equals for each r for which is a static extent, and
  • either
    • sizeof...(exts) == 0 is true, or
    • each element of exts is nonnegative and is representable as a value of type index_­type.
Postconditions: *this == extents(exts_­arr) is true.
template<class OtherIndexType, size_t N> constexpr explicit(N != rank_dynamic()) extents(span<OtherIndexType, N> exts) noexcept; template<class OtherIndexType, size_t N> constexpr explicit(N != rank_dynamic()) extents(const array<OtherIndexType, N>& exts) noexcept;
Constraints:
  • is_­convertible_­v<const OtherIndexType&, index_­type> is true,
  • is_­nothrow_­constructible_­v<index_­type, const OtherIndexType&> is true, and
  • N == rank_­dynamic() || N == rank() is true.
Preconditions:
  • If N != rank_­dynamic() is true, exts[r] equals for each r for which is a static extent, and
  • either
    • N is zero, or
    • exts[r] is nonnegative and is representable as a value of type index_­type for every rank index r.
Effects:
  • If N equals dynamic_­rank(), for all d in the range [0, rank_­dynamic()), direct-non-list-initializes dynamic-extent[d] with as_­const(exts[d]).
  • Otherwise, for all d in the range [0, rank_­dynamic()), direct-non-list-initializes dynamic-extent[d] with as_­const(exts[dynamic-index-inv(d)]).
template<class... Integrals> explicit extents(Integrals...) -> see below;
Constraints: (is_­convertible_­v<Integrals, size_­t> && ...) is true.
Remarks: The deduced type is dextents<size_­t, sizeof...(Integrals)>.

24.7.6.4 Observers of the multidimensional index space [mdspan.extents.obs]

static constexpr size_t static_extent(rank_type i) noexcept;
Preconditions: i < rank() is true.
Returns: .
constexpr index_type extent(rank_type i) const noexcept;
Preconditions: i < rank() is true.
Returns: .

24.7.6.5 Comparison operators [mdspan.extents.cmp]

template<class OtherIndexType, size_t... OtherExtents> friend constexpr bool operator==(const extents& lhs, const extents<OtherIndexType, OtherExtents...>& rhs) noexcept;
Returns: true if lhs.rank() equals rhs.rank() and if lhs.extent(r) equals rhs.extent(r) for every rank index r of rhs, otherwise false.

24.7.6.6 Alias template dextents [mdspan.extents.dextents]

template<class IndexType, size_t Rank> using dextents = see below;
Result: A type E that is a specialization of extents such that E​::​rank() == Rank && E​::​rank() == E​::​rank_­dynamic() is true, and E​::​index_­type denotes IndexType.

24.7.7 Layout mapping [mdspan.layout]

24.7.7.1 General [mdspan.layout.general]

  • M denotes a layout mapping class.
  • m denotes a (possibly const) value of type M.
  • i and j are packs of (possibly const) integers that are multidimensional indices in m.extents() ([mdspan.overview]).
    [Note 1:
    The type of each element of the packs can be a different integer type.
    โ€” end note]
  • r is a (possibly const) rank index of typename M​::​extents_­type.
  • is a pack of (possibly const) integers for which sizeof...() == M​::​extents_­type​::​rank() is true, the element is equal to 1, and all other elements are equal to 0.
In subclauses [mdspan.layout.reqmts] through [mdspan.layout.stride], let is-mapping-of be the exposition-only variable template defined as follows: template<class Layout, class Mapping> constexpr bool is-mapping-of = // exposition only is_same_v<typename Layout::template mapping<typename Mapping::extents_type>, Mapping>;

24.7.7.2 Requirements [mdspan.layout.reqmts]

A type M meets the layout mapping requirements if
  • M models copyable and equality_­comparable,
  • is_­nothrow_­move_­constructible_­v<M> is true,
  • is_­nothrow_­move_­assignable_­v<M> is true,
  • is_­nothrow_­swappable_­v<M> is true, and
  • the following types and expressions are well-formed and have the specified semantics.
typename M::extents_type
Result: A type that is a specialization of extents.
typename M::index_type
Result: typename M​::​extents_­type​::​index_­type.
typename M::rank_type
Result: typename M​::​extents_­type​::​rank_­type.
typename M::layout_type
Result: A type MP that meets the layout mapping policy requirements ([mdspan.layout.policy.reqmts]) and for which is-mapping-of<MP, M> is true.
m.extents()
Result: const typename M​::​extents_­type&
m(i...)
Result: typename M​::​index_­type
Returns: A nonnegative integer less than numeric_­limits<typename M​::​index_­type>​::​max() and less than or equal to numeric_­limits<size_­t>​::​max().
m(i...) == m(static_cast<typename M::index_type>(i)...)
Result: bool
Returns: true
m.required_span_size()
Result: typename M​::​index_­type
Returns: If the size of the multidimensional index space m.extents() is 0, then 0, else 1 plus the maximum value of m(i...) for all i.
m.is_unique()
Result: bool
Returns: true only if for every i and j where (i != j || ...) is true, m(i...) != m(j...) is true.
[Note 1:
A mapping can return false even if the condition is met.
For certain layouts, it is possibly not feasible to determine efficiently whether the layout is unique.
โ€” end note]
m.is_exhaustive()
Result: bool
Returns: true only if for all k in the range [0, m.required_­span_­size()) there exists an i such that m(i...) equals k.
[Note 2:
A mapping can return false even if the condition is met.
For certain layouts, it is possibly not feasible to determine efficiently whether the layout is exhaustive.
โ€” end note]
m.is_strided()
Result: bool
Returns: true only if for every rank index r of m.extents() there exists an integer such that, for all i where is a multidimensional index in m.extents() ([mdspan.overview]), m((i + )...) - m(i...) equals .
[Note 3:
This implies that for a strided layout .
โ€” end note]
[Note 4:
A mapping can return false even if the condition is met.
For certain layouts, it is possibly not feasible to determine efficiently whether the layout is strided.
โ€” end note]
m.stride(r)
Preconditions: m.is_­strided() is true.
Result: typename M​::​index_­type
Returns: as defined in m.is_­strided() above.
M::is_always_unique()
Result: A constant expression ([expr.const]) of type bool.
Returns: true only if m.is_­unique() is true for all possible objects m of type M.
[Note 5:
A mapping can return false even if the above condition is met.
For certain layout mappings, it is possibly not feasible to determine whether every instance is unique.
โ€” end note]
M::is_always_exhaustive()
Result: A constant expression ([expr.const]) of type bool.
Returns: true only if m.is_­exhaustive() is true for all possible objects m of type M.
[Note 6:
A mapping can return false even if the above condition is met.
For certain layout mappings, it is possibly not feasible to determine whether every instance is exhaustive.
โ€” end note]
M::is_always_strided()
Result: A constant expression ([expr.const]) of type bool.
Returns: true only if m.is_­strided() is true for all possible objects m of type M.
[Note 7:
A mapping can return false even if the above condition is met.
For certain layout mappings, it is possibly not feasible to determine whether every instance is strided.
โ€” end note]

24.7.7.3 Layout mapping policy requirements [mdspan.layout.policy.reqmts]

A type MP meets the layout mapping policy requirements if for a type E that is a specialization of extents, MP​::​mapping<E> is valid and denotes a type X that meets the layout mapping requirements ([mdspan.layout.reqmts]), and for which the qualified-id X​::​layout_­type is valid and denotes the type MP and the qualified-id X​::​extents_­type denotes E.

24.7.7.4 Layout mapping policies [mdspan.layout.policy.overview]

namespace std { struct layout_left { template<class Extents> class mapping; }; struct layout_right { template<class Extents> class mapping; }; struct layout_stride { template<class Extents> class mapping; }; }
Each of layout_­left, layout_­right, and layout_­stride meets the layout mapping policy requirements and is a trivial type.

24.7.7.5 Class template layout_­left​::​mapping [mdspan.layout.left]

24.7.7.5.1 Overview [mdspan.layout.left.overview]

layout_­left provides a layout mapping where the leftmost extent has stride 1, and strides increase left-to-right as the product of extents.
namespace std { template<class Extents> class layout_left::mapping { public: using extents_type = Extents; using index_type = typename extents_type::index_type; using size_type = typename extents_type::size_type; using rank_type = typename extents_type::rank_type; using layout_type = layout_left; // [mdspan.layout.left.cons], constructors constexpr mapping() noexcept = default; constexpr mapping(const mapping&) noexcept = default; constexpr mapping(const extents_type&) noexcept; template<class OtherExtents> constexpr explicit(!is_convertible_v<OtherExtents, extents_type>) mapping(const mapping<OtherExtents>&) noexcept; template<class OtherExtents> constexpr explicit(!is_convertible_v<OtherExtents, extents_type>) mapping(const layout_right::mapping<OtherExtents>&) noexcept; template<class OtherExtents> constexpr explicit(extents_type::rank() > 0) mapping(const layout_stride::mapping<OtherExtents>&); constexpr mapping& operator=(const mapping&) noexcept = default; // [mdspan.layout.left.obs], observers constexpr const extents_type& extents() const noexcept { return extents_; } constexpr index_type required_span_size() const noexcept; template<class... Indices> constexpr index_type operator()(Indices...) const noexcept; static constexpr bool is_always_unique() noexcept { return true; } static constexpr bool is_always_exhaustive() noexcept { return true; } static constexpr bool is_always_strided() noexcept { return true; } static constexpr bool is_unique() noexcept { return true; } static constexpr bool is_exhaustive() noexcept { return true; } static constexpr bool is_strided() noexcept { return true; } constexpr index_type stride(rank_type) const noexcept; template<class OtherExtents> friend constexpr bool operator==(const mapping&, const mapping<OtherExtents>&) noexcept; private: extents_type extents_{}; // exposition only }; }
If Extents is not a specialization of extents, then the program is ill-formed.
layout_­left​::​mapping<E> is a trivially copyable type that models regular for each E.

24.7.7.5.2 Constructors [mdspan.layout.left.cons]

constexpr mapping(const extents_type& e) noexcept;
Preconditions: The size of the multidimensional index space e is representable as a value of type index_­type ([basic.fundamental]).
Effects: Direct-non-list-initializes extents_­ with e.
template<class OtherExtents> constexpr explicit(!is_convertible_v<OtherExtents, extents_type>) mapping(const mapping<OtherExtents>& other) noexcept;
Constraints: is_­constructible_­v<extents_­type, OtherExtents> is true.
Preconditions: other.required_­span_­size() is representable as a value of type index_­type ([basic.fundamental]).
Effects: Direct-non-list-initializes extents_­ with other.extents().
template<class OtherExents> constexpr explicit(!is_convertible_v<OtherExtents, extents_type>) mapping(const layout_right::mapping<OtherExtents>& other) noexcept;
Constraints:
  • extents_­type​::​rank() <= 1 is true, and
  • is_­constructible_­v<extents_­type, OtherExtents> is true.
Preconditions: other.required_­span_­size() is representable as a value of type index_­type ([basic.fundamental]).
Effects: Direct-non-list-initializes extents_­ with other.extents().
template<class OtherExtents> constexpr explicit(extents_type::rank() > 0) mapping(const layout_stride::mapping<OtherExtents>& other);
Constraints: is_­constructible_­v<extents_­type, OtherExtents> is true.
Preconditions:
  • If extents_­type​::​rank() > 0 is true, then for all r in the range [0, extents_­type​::​rank()), other.stride(r) equals extents().fwd-prod-of-extents(r), and
  • other.required_­span_­size() is representable as a value of type index_­type ([basic.fundamental]).
Effects: Direct-non-list-initializes extents_­ with other.extents().

24.7.7.5.3 Observers [mdspan.layout.left.obs]

constexpr index_type required_span_size() const noexcept;
Returns: extents().fwd-prod-of-extents(extents_­type​::​rank()).
template<class... Indices> constexpr index_type operator()(Indices... i) const noexcept;
Constraints:
  • sizeof...(Indices) == extents_­type​::​rank() is true,
  • (is_­convertible_­v<Indices, index_­type> && ...) is true, and
  • (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);
constexpr index_type stride(rank_type i) const;
Constraints: extents_­type​::​rank() > 0 is true.
Preconditions: i < extents_­type​::​rank() is true.
Returns: extents().fwd-prod-of-extents(i).
template<class OtherExtents> friend constexpr bool operator==(const mapping& x, const mapping<OtherExtents>& y) noexcept;
Constraints: extents_­type​::​rank() == OtherExtents​::​rank() is true.
Effects: Equivalent to: return x.extents() == y.extents();

24.7.7.6 Class template layout_­right​::​mapping [mdspan.layout.right]

24.7.7.6.1 Overview [mdspan.layout.right.overview]

layout_­right provides a layout mapping where the rightmost extent is stride 1, and strides increase right-to-left as the product of extents.
namespace std { template<class Extents> class layout_right::mapping { public: using extents_type = Extents; using index_type = typename extents_type::index_type; using size_type = typename extents_type::size_type; using rank_type = typename extents_type::rank_type; using layout_type = layout_right; // [mdspan.layout.right.cons], constructors constexpr mapping() noexcept = default; constexpr mapping(const mapping&) noexcept = default; constexpr mapping(const extents_type&) noexcept; template<class OtherExtents> constexpr explicit(!is_convertible_v<OtherExtents, extents_type>) mapping(const mapping<OtherExtents>&) noexcept; template<class OtherExtents> constexpr explicit(!is_convertible_v<OtherExtents, extents_type>) mapping(const layout_left::mapping<OtherExtents>&) noexcept; template<class OtherExtents> constexpr explicit(extents_type::rank() > 0) mapping(const layout_stride::mapping<OtherExtents>&) noexcept; constexpr mapping& operator=(const mapping&) noexcept = default; // [mdspan.layout.right.obs], observers constexpr const extents_type& extents() const noexcept { return extents_; } constexpr index_type required_span_size() const noexcept; template<class... Indices> constexpr index_type operator()(Indices...) const noexcept; static constexpr bool is_always_unique() noexcept { return true; } static constexpr bool is_always_exhaustive() noexcept { return true; } static constexpr bool is_always_strided() noexcept { return true; } static constexpr bool is_unique() noexcept { return true; } static constexpr bool is_exhaustive() noexcept { return true; } static constexpr bool is_strided() noexcept { return true; } constexpr index_type stride(rank_type) const noexcept; template<class OtherExtents> friend constexpr bool operator==(const mapping&, const mapping<OtherExtents>&) noexcept; private: extents_type extents_{}; // exposition only }; }
If Extents is not a specialization of extents, then the program is ill-formed.
layout_­right​::​mapping<E> is a trivially copyable type that models regular for each E.

24.7.7.6.2 Constructors [mdspan.layout.right.cons]

constexpr mapping(const extents_type& e) noexcept;
Preconditions: The size of the multidimensional index space e is representable as a value of type index_­type ([basic.fundamental]).
Effects: Direct-non-list-initializes extents_­ with e.
template<class OtherExtents> constexpr explicit(!is_convertible_v<OtherExtents, extents_type>) mapping(const mapping<OtherExtents>& other) noexcept;
Constraints: is_­constructible_­v<extents_­type, OtherExtents> is true.
Preconditions: other.required_­span_­size() is representable as a value of type index_­type ([basic.fundamental]).
Effects: Direct-non-list-initializes extents_­ with other.extents().
template<class OtherExtents> constexpr explicit(!is_convertible_v<OtherExtents, extents_type>) mapping(const layout_left::mapping<OtherExtents>& other) noexcept;
Constraints:
  • extents_­type​::​rank() <= 1 is true, and
  • is_­constructible_­v<extents_­type, OtherExtents> is true.
Preconditions: other.required_­span_­size() is representable as a value of type index_­type ([basic.fundamental]).
Effects: Direct-non-list-initializes extents_­ with other.extents().
template<class OtherExtents> constexpr explicit(extents_type::rank() > 0) mapping(const layout_stride::mapping<OtherExtents>& other) noexcept;
Constraints: is_­constructible_­v<extents_­type, OtherExtents> is true.
Preconditions:
  • If extents_­type​::​rank() > 0 is true, then for all r in the range [0, extents_­type​::​rank()), other.stride(r) equals extents().rev-prod-of-extents(r).
  • other.required_­span_­size() is representable as a value of type index_­type ([basic.fundamental]).
Effects: Direct-non-list-initializes extents_­ with other.extents().

24.7.7.6.3 Observers [mdspan.layout.right.obs]

index_type required_span_size() const noexcept;
Returns: extents().fwd-prod-of-extents(extents_­type​::​rank()).
template<class... Indices> constexpr index_type operator()(Indices... i) const noexcept;
Constraints:
  • sizeof...(Indices) == extents_­type​::​rank() is true,
  • (is_­convertible_­v<Indices, index_­type> && ...) is true, and
  • (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);
constexpr index_type stride(rank_type i) const noexcept;
Constraints: extents_­type​::​rank() > 0 is true.
Preconditions: i < extents_­type​::​rank() is true.
Returns: extents().rev-prod-of-extents(i).
template<class OtherExtents> friend constexpr bool operator==(const mapping& x, const mapping<OtherExtents>& y) noexcept;
Constraints: extents_­type​::​rank() == OtherExtents​::​rank() is true.
Effects: Equivalent to: return x.extents() == y.extents();

24.7.7.7 Class template layout_­stride​::​mapping [mdspan.layout.stride]

24.7.7.7.1 Overview [mdspan.layout.stride.overview]

layout_­stride provides a layout mapping where the strides are user-defined.
namespace std { template<class Extents> class layout_stride::mapping { public: using extents_type = Extents; using index_type = typename extents_type::index_type; using size_type = typename extents_type::size_type; using rank_type = typename extents_type::rank_type; using layout_type = layout_stride; private: static constexpr rank_type rank_ = extents_type::rank(); // exposition only public: // [mdspan.layout.stride.cons], constructors constexpr mapping() noexcept = default; constexpr mapping(const mapping&) noexcept = default; template<class OtherIndexType> constexpr mapping(const extents_type&, span<OtherIndexType, rank_>) noexcept; template<class OtherIndexType> constexpr mapping(const extents_type&, const array<OtherIndexType, rank_>&) noexcept; template<class StridedLayoutMapping> constexpr explicit(see below) mapping(const StridedLayoutMapping&) noexcept; constexpr mapping& operator=(const mapping&) noexcept = default; // [mdspan.layout.stride.obs], observers constexpr const extents_type& extents() const noexcept { return extents_; } constexpr array<index_type, rank_> strides() const noexcept { return strides_; } constexpr index_type required_span_size() const noexcept; template<class... Indices> constexpr index_type operator()(Indices...) const noexcept; static constexpr bool is_always_unique() noexcept { return true; } static constexpr bool is_always_exhaustive() noexcept { return false; } static constexpr bool is_always_strided() noexcept { return true; } static constexpr bool is_unique() noexcept { return true; } constexpr bool is_exhaustive() const noexcept; static constexpr bool is_strided() noexcept { return true; } constexpr index_type stride(rank_type i) const noexcept { return strides_[i]; } template<class OtherMapping> friend constexpr bool operator==(const mapping&, const OtherMapping&) noexcept; private: extents_type extents_{}; // exposition only array<index_type, rank_> strides_{}; // exposition only }; }
If Extents is not a specialization of extents, then the program is ill-formed.
layout_­stride​::​mapping<E> is a trivially copyable type that models regular for each E.

24.7.7.7.2 Exposition-only helpers [mdspan.layout.stride.expo]

Let REQUIRED-SPAN-SIZE(e, strides) be:
  • 1, if e.rank() == 0 is true,
  • otherwise 0, if the size of the multidimensional index space e is 0,
  • otherwise 1 plus the sum of products of (e.extent(r) - 1) and strides[r] for all r in the range [0, e.rank()).
Let OFFSET(m) be:
  • m(), if e.rank() == 0 is true,
  • otherwise 0, if the size of the multidimensional index space e is 0,
  • otherwise m(z...) for a pack of integers z that is a multidimensional index in m.extents() and each element of z equals 0.
Let is-extents be the exposition-only variable template defined as follows: template<class T> constexpr bool is-extents = false; // exposition only template<class IndexType, size_t... Args> constexpr bool is-extents<extents<IndexType, Args...>> = true; // exposition only
Let layout-mapping-alike be the exposition-only concept defined as follows: template<class M> concept layout-mapping-alike = requires { // exposition only requires is-extents<typename M::extents_type>; { M::is_always_strided() } -> same_­as<bool>; { M::is_always_exhaustive() } -> same_­as<bool>; { M::is_always_unique() } -> same_­as<bool>; bool_constant<M::is_always_strided()>::value; bool_constant<M::is_always_exhaustive()>::value; bool_constant<M::is_always_unique()>::value; };
[Note 1:
This concept checks that the functions M​::​is_­always_­strided(), M​::​is_­always_­exhaustive(), and M​::​is_­always_­unique() exist, are constant expressions, and have a return type of bool.
โ€” end note]

24.7.7.7.3 Constructors [mdspan.layout.stride.cons]

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;
Constraints:
  • is_­convertible_­v<const OtherIndexType&, index_­type> is true, and
  • is_­nothrow_­constructible_­v<index_­type, const OtherIndexType&> is true.
Preconditions:
  • s[i] > 0 is true for all i in the range [0, rank_­).
  • REQUIRED-SPAN-SIZE(e, s) is representable as a value of type index_­type ([basic.fundamental]).
  • If rank_­ is greater than 0, then there exists a permutation P of the integers in the range [0, rank_­), such that s[] >= s[] * e.extent(p) is true for all i in the range [1, rank_­), where is the element of P.
    [Note 1:
    For layout_­stride, this condition is necessary and sufficient for is_­unique() to be true.
    โ€” end note]
Effects: Direct-non-list-initializes extents_­ with e, and for all d in the range [0, rank_­), direct-non-list-initializes strides_­[d] with as_­const(s[d]).
template<class StridedLayoutMapping> constexpr explicit(see below) mapping(const StridedLayoutMapping& other) noexcept;
Constraints:
  • layout-mapping-alike<StridedLayoutMapping> is satisfied.
  • is_­constructible_­v<extents_­type, typename StridedLayoutMapping​::​extents_­type> is
    true.
  • StridedLayoutMapping​::​is_­always_­unique() is true.
  • StridedLayoutMapping​::​is_­always_­strided() is true.
Preconditions:
Effects: Direct-non-list-initializes extents_­ with other.extents(), and for all d in the range [0, rank_­), direct-non-list-initializes strides_­[d] with other.stride(d).
Remarks: The expression inside explicit is equivalent to: !(is_convertible_v<typename StridedLayoutMapping::extents_type, extents_type> && (is-mapping-of<layout_left, LayoutStrideMapping> || is-mapping-of<layout_right, LayoutStrideMapping> || is-mapping-of<layout_stride, LayoutStrideMapping>))

24.7.7.7.4 Observers [mdspan.layout.stride.obs]

constexpr index_type required_span_size() const noexcept;
Returns: REQUIRED-SPAN-SIZE(extents(), strides_­).
template<class... Indices> constexpr index_type operator()(Indices... i) const noexcept;
Constraints:
  • sizeof...(Indices) == rank_­ is true,
  • (is_­convertible_­v<Indices, index_­type> && ...) is true, and
  • (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);
constexpr bool is_exhaustive() const noexcept;
Returns:
  • true if rank_­ is 0.
  • Otherwise, true if there is a permutation P of the integers in the range [0, rank_­) such that stride() equals 1, and stride() equals stride() * extents().extent() for i in the range [1, rank_­), where is the element of P.
  • Otherwise, false.
template<class OtherMapping> friend constexpr bool operator==(const mapping& x, const OtherMapping& y) noexcept;
Constraints:
Preconditions: OtherMapping meets the layout mapping requirements ([mdspan.layout.policy.reqmts]).
Returns: true if x.extents() == y.extents() is true, OFFSET(y) == 0 is true, and each of x.stride(r) == y.stride(r) is true for r in the range [0, x.extents().rank()).
Otherwise, false.

24.7.8 Accessor policy [mdspan.accessor]

24.7.8.1 General [mdspan.accessor.general]

An accessor policy defines types and operations by which a reference to a single object is created from an abstract data handle to a number of such objects and an index.
A range of indices is an accessible range of a given data handle and an accessor if, for each i in the range, the accessor policy's access function produces a valid reference to an object.
  • A denotes an accessor policy.
  • a denotes a value of type A or const A.
  • p denotes a value of type A​::​data_­handle_­type or const A​::​data_­handle_­type.
    [Note 1:
    The type A​::​data_­handle_­type need not be dereferenceable.
    โ€” end note]
  • n, i, and j each denote values of type size_­t.

24.7.8.2 Requirements [mdspan.accessor.reqmts]

A type A meets the accessor policy requirements if
  • A models copyable,
  • is_­nothrow_­move_­constructible_­v<A> is true,
  • is_­nothrow_­move_­assignable_­v<A> is true,
  • is_­nothrow_­swappable_­v<A> is true, and
  • the following types and expressions are well-formed and have the specified semantics.
typename A::element_type
Result: A complete object type that is not an abstract class type.
typename A::data_handle_type
Result: A type that models copyable, and for which is_­nothrow_­move_­constructible_­v<A​::​data_­handle_­type> is true, is_­nothrow_­move_­assignable_­v<A​::​data_­handle_­type> is true, and is_­nothrow_­swappable_­v<A​::​data_­handle_­type> is true.
[Note 1:
The type of data_­handle_­type need not be element_­type*.
โ€” end note]
typename A::reference
Result: A type that models common_­reference_­with<A​::​reference&&, A​::​element_­type&>.
[Note 2:
The type of reference need not be element_­type&.
โ€” end note]
typename A::offset_policy
Result: A type OP such that:
  • OP meets the accessor policy requirements,
  • constructible_­from<OP, const A&> is modeled, and
  • is_­same_­v<typename OP​::​element_­type, typename A​::​element_­type> is true.
a.access(p, i)
Result: A​::​reference
Remarks: The expression is equality preserving.
[Note 3:
Concrete accessor policies can impose preconditions for their access function.
However, they might not.
For example, an accessor where p is span<A​::​element_­type, dynamic_­extent> and access(p, i) returns p[i % p.size()] does not need to impose a precondition on i.
โ€” end note]
a.offset(p, i)
Result: A​::​offset_­policy​::​data_­handle_­type
Returns: q such that for b being A​::​offset_­policy(a), and any integer n for which [0, n) is an accessible range of p and a:
  • is an accessible range of q and b; and
  • b.access(q, j) provides access to the same element as a.access(p, i + j), for every j in the range .
Remarks: The expression is equality-preserving.

24.7.8.3 Class template default_­accessor [mdspan.accessor.default]

24.7.8.3.1 Overview [mdspan.accessor.default.overview]

namespace std { template<class ElementType> struct default_accessor { using offset_policy = default_accessor; using element_type = ElementType; using reference = ElementType&; using data_handle_type = ElementType*; constexpr default_accessor() noexcept = default; template<class OtherElementType> constexpr default_accessor(default_accessor<OtherElementType>) noexcept; constexpr reference access(data_handle_type p, size_t i) const noexcept; constexpr data_handle_type offset(data_handle_type p, size_t i) const noexcept; }; }
default_­accessor meets the accessor policy requirements.
ElementType is required to be a complete object type that is neither an abstract class type nor an array type.
Each specialization of default_­accessor is a trivially copyable type that models semiregular.
is an accessible range for an object p of type data_­handle_­type and an object of type default_­accessor if and only if [p, p + n) is a valid range.

24.7.8.3.2 Members [mdspan.accessor.default.members]

template<class OtherElementType> constexpr default_accessor(default_accessor<OtherElementType>) noexcept {}
Constraints: is_­convertible_­v<OtherElementType(*)[], element_­type(*)[]> is true.
constexpr reference access(data_handle_type p, size_t i) const noexcept;
Effects: Equivalent to: return p[i];
constexpr data_handle_type offset(data_handle_type p, size_t i) const noexcept;
Effects: Equivalent to: return p + i;

24.7.9 Class template mdspan [mdspan.mdspan]

24.7.9.1 Overview [mdspan.mdspan.overview]

mdspan is a view of a multidimensional array of elements.
namespace std { template<class ElementType, class Extents, class LayoutPolicy, class AccessorPolicy> class mdspan { public: using extents_type = Extents; using layout_type = LayoutPolicy; using accessor_type = AccessorPolicy; using mapping_type = typename layout_type::template mapping<extents_type>; using element_type = ElementType; using value_type = remove_cv_t<element_type>; using index_type = typename extents_type::index_type; using size_type = typename extents_type::size_type; using rank_type = typename extents_type::rank_type; using data_handle_type = typename accessor_type::data_handle_type; using reference = typename accessor_type::reference; static constexpr rank_type rank() noexcept { return extents_type::rank(); } static constexpr rank_type rank_dynamic() noexcept { return extents_type::rank_dynamic(); } static constexpr size_t static_extent(rank_type r) noexcept { return extents_type::static_extent(r); } constexpr index_type extent(rank_type r) const noexcept { return extents().extent(r); } // [mdspan.mdspan.cons], constructors constexpr mdspan(); constexpr mdspan(const mdspan& rhs) = default; constexpr mdspan(mdspan&& rhs) = default; template<class... OtherIndexTypes> constexpr explicit mdspan(data_handle_type ptr, OtherIndexTypes... exts); template<class OtherIndexType, size_t N> constexpr explicit(N != rank_dynamic()) mdspan(data_handle_type p, span<OtherIndexType, N> exts); template<class OtherIndexType, size_t N> constexpr explicit(N != rank_dynamic()) mdspan(data_handle_type p, const array<OtherIndexType, N>& exts); constexpr mdspan(data_handle_type p, const extents_type& ext); constexpr mdspan(data_handle_type p, const mapping_type& m); constexpr mdspan(data_handle_type p, const mapping_type& m, const accessor_type& a); template<class OtherElementType, class OtherExtents, class OtherLayoutPolicy, class OtherAccessorPolicy> constexpr explicit(see below) mdspan(const mdspan<OtherElementType, OtherExtents, OtherLayoutPolicy, OtherAccessorPolicy>& other); constexpr mdspan& operator=(const mdspan& rhs) = default; constexpr mdspan& operator=(mdspan&& rhs) = default; // [mdspan.mdspan.members], members template<class... OtherIndexTypes> constexpr reference operator[](OtherIndexTypes... indices) const; template<class OtherIndexType> constexpr reference operator[](span<OtherIndexType, rank()> indices) const; template<class OtherIndexType> constexpr reference operator[](const array<OtherIndexType, rank()>& indices) const; constexpr size_type size() const noexcept; [[nodiscard]] constexpr bool empty() const noexcept; friend constexpr void swap(mdspan& x, mdspan& y) noexcept; constexpr const extents_type& extents() const noexcept { return map_.extents(); } constexpr const data_handle_type& data_handle() const noexcept { return ptr_; } constexpr const mapping_type& mapping() const noexcept { return map_; } constexpr const accessor_type& accessor() const noexcept { return acc_; } static constexpr bool is_always_unique() { return mapping_type::is_always_unique(); } static constexpr bool is_always_exhaustive() { return mapping_type::is_always_exhaustive(); } static constexpr bool is_always_strided() { return mapping_type::is_always_strided(); } constexpr bool is_unique() const { return map_.is_unique(); } constexpr bool is_exhaustive() const { return map_.is_exhaustive(); } constexpr bool is_strided() const { return map_.is_strided(); } constexpr index_type stride(rank_type r) const { return map_.stride(r); } private: accessor_type acc_; // exposition only mapping_type map_; // exposition only data_handle_type ptr_; // exposition only }; template<class CArray> requires(is_array_v<CArray> && rank_v<CArray> == 1) mdspan(CArray&) -> mdspan<remove_all_extents_t<CArray>, extents<size_t, extent_v<CArray, 0>>>; template<class Pointer> requires(is_pointer_v<remove_reference_t<Pointer>>) mdspan(Pointer&&) -> mdspan<remove_pointer_t<remove_reference_t<Pointer>>, extents<size_t>>; template<class ElementType, class... Integrals> requires((is_convertible_v<Integrals, size_t> && ...) && sizeof...(Integrals) > 0) explicit mdspan(ElementType*, Integrals...) -> mdspan<ElementType, dextents<size_t, sizeof...(Integrals)>>; template<class ElementType, class OtherIndexType, size_t N> mdspan(ElementType*, span<OtherIndexType, N>) -> mdspan<ElementType, dextents<size_t, N>>; template<class ElementType, class OtherIndexType, size_t N> mdspan(ElementType*, const array<OtherIndexType, N>&) -> mdspan<ElementType, dextents<size_t, N>>; template<class ElementType, class IndexType, size_t... ExtentsPack> mdspan(ElementType*, const extents<IndexType, ExtentsPack...>&) -> mdspan<ElementType, extents<IndexType, ExtentsPack...>>; template<class ElementType, class MappingType> mdspan(ElementType*, const MappingType&) -> mdspan<ElementType, typename MappingType::extents_type, typename MappingType::layout_type>; template<class MappingType, class AccessorType> mdspan(const typename AccessorType::data_handle_type&, const MappingType&, const AccessorType&) -> mdspan<typename AccessorType::element_type, typename MappingType::extents_type, typename MappingType::layout_type, AccessorType>; }
Mandates:
  • ElementType is a complete object type that is neither an abstract class type nor an array type,
  • Extents is a specialization of extents, and
  • is_­same_­v<ElementType, typename AccessorPolicy​::​element_­type> is true.
LayoutPolicy shall meet the layout mapping policy requirements ([mdspan.layout.policy.reqmts]), and AccessorPolicy shall meet the accessor policy requirements ([mdspan.accessor.reqmts]).
Each specialization MDS of mdspan models copyable and
  • is_­nothrow_­move_­constructible_­v<MDS> is true,
  • is_­nothrow_­move_­assignable_­v<MDS> is true, and
  • is_­nothrow_­swappable_­v<MDS> is true.
A specialization of mdspan is a trivially copyable type if its accessor_­type, mapping_­type, and data_­handle_­type are trivially copyable types.

24.7.9.2 Constructors [mdspan.mdspan.cons]

constexpr mdspan();
Constraints:
  • rank_­dynamic() > 0 is true.
  • is_­default_­constructible_­v<data_­handle_­type> is true.
  • is_­default_­constructible_­v<mapping_­type> is true.
  • is_­default_­constructible_­v<accessor_­type> is true.
Preconditions: [0, map_­.required_­span_­size()) is an accessible range of ptr_­ and acc_­ for the values of map_­ and acc_­ after the invocation of this constructor.
Effects: Value-initializes ptr_­, map_­, and acc_­.
template<class... OtherIndexTypes> constexpr explicit mdspan(data_handle_type p, OtherIndexTypes... exts);
Let N be sizeof...(OtherIndexTypes).
Constraints:
  • (is_­convertible_­v<OtherIndexTypes, index_­type> && ...) is true,
  • (is_­nothrow_­constructible<index_­type, OtherIndexTypes> && ...) is true,
  • N == rank() || N == rank_­dynamic() is true,
  • is_­constructible_­v<mapping_­type, extents_­type> is true, and
  • is_­default_­constructible_­v<accessor_­type> is true.
Preconditions: [0, map_­.required_­span_­size()) is an accessible range of p and acc_­ for the values of map_­ and acc_­ after the invocation of this constructor.
Effects:
  • Direct-non-list-initializes ptr_­ with std​::​move(p),
  • direct-non-list-initializes map_­ with extents_­type(static_­cast<index_­type>(std​::​move(exts​))...), and
  • value-initializes acc_­.
template<class OtherIndexType, size_t N> constexpr explicit(N != rank_dynamic()) mdspan(data_handle_type p, span<OtherIndexType, N> exts); template<class OtherIndexType, size_t N> constexpr explicit(N != rank_dynamic()) mdspan(data_handle_type p, const array<OtherIndexType, N>& exts);
Constraints:
  • is_­convertible_­v<const OtherIndexType&, index_­type> is true,
  • (is_­nothrow_­constructible<index_­type, const OtherIndexType&> && ...) is true,
  • N == rank() || N == rank_­dynamic() is true,
  • is_­constructible_­v<mapping_­type, extents_­type> is true, and
  • is_­default_­constructible_­v<accessor_­type> is true.
Preconditions: [0, map_­.required_­span_­size()) is an accessible range of p and acc_­ for the values of map_­ and acc_­ after the invocation of this constructor.
Effects:
  • Direct-non-list-initializes ptr_­ with std​::​move(p),
  • direct-non-list-initializes map_­ with extents_­type(exts), and
  • value-initializes acc_­.
constexpr mdspan(data_handle_type p, const extents_type& ext);
Constraints:
  • is_­constructible_­v<mapping_­type, const extents_­type&> is true, and
  • is_­default_­constructible_­v<accessor_­type> is true.
Preconditions: [0, map_­.required_­span_­size()) is an accessible range of p and acc_­ for the values of map_­ and acc_­ after the invocation of this constructor.
Effects:
  • Direct-non-list-initializes ptr_­ with std​::​move(p),
  • direct-non-list-initializes map_­ with ext, and
  • value-initializes acc_­.
constexpr mdspan(data_handle_type p, const mapping_type& m);
Constraints: is_­default_­constructible_­v<accessor_­type> is true.
Preconditions: [0, m.required_­span_­size()) is an accessible range of p and acc_­ for the value of acc_­ after the invocation of this constructor.
Effects:
  • Direct-non-list-initializes ptr_­ with std​::​move(p),
  • direct-non-list-initializes map_­ with m, and
  • value-initializes acc_­.
constexpr mdspan(data_handle_type p, const mapping_type& m, const accessor_type& a);
Preconditions: [0, m.required_­span_­size()) is an accessible range of p and a.
Effects:
  • Direct-non-list-initializes ptr_­ with std​::​move(p),
  • direct-non-list-initializes map_­ with m, and
  • direct-non-list-initializes acc_­ with a.
template<class OtherElementType, class OtherExtents, class OtherLayoutPolicy, class OtherAccessor> constexpr explicit(see below) mdspan(const mdspan<OtherElementType, OtherExtents, OtherLayoutPolicy, OtherAccessor>& other);
Constraints:
  • is_­constructible_­v<mapping_­type, const OtherLayoutPolicy​::​template mapping<Oth-
    erExtents>&>
    is true, and
  • is_­constructible_­v<accessor_­type, const OtherAccessor&> is true.
Mandates:
  • is_­constructible_­v<data_­handle_­type, const OtherAccessor​::​data_­handle_­type&> is
    true, and
  • is_­constructible_­v<extents_­type, OtherExtents> is true.
Preconditions:
  • For each rank index r of extents_­type, static_­extent(r) == dynamic_­extent || static_­extent(r) == other.extent(r) is true.
  • [0, map_­.required_­span_­size()) is an accessible range of ptr_­ and acc_­ for values of ptr_­, map_­, and acc_­ after the invocation of this constructor.
Effects:
  • Direct-non-list-initializes ptr_­ with other.ptr_­,
  • direct-non-list-initializes map_­ with other.map_­, and
  • direct-non-list-initializes acc_­ with other.acc_­.
Remarks: The expression inside explicit is equivalent to: !is_convertible_v<const OtherLayoutPolicy::template mapping<OtherExtents>&, mapping_type> || !is_convertible_v<const OtherAccessor&, accessor_type>

24.7.9.3 Members [mdspan.mdspan.members]

template<class... OtherIndexTypes> constexpr reference operator[](OtherIndexTypes... indices) const;
Constraints:
  • (is_­convertible_­v<OtherIndexTypes, index_­type> && ...) is true,
  • (is_­nothrow_­constructible_­v<index_­type, OtherIndexTypes> && ...) is true, and
  • sizeof...(OtherIndexTypes) == rank() is true.
Let I be extents_­type​::​index-cast(std​::​move(indices)).
Preconditions: I is a multidimensional index in extents().
[Note 1:
This implies that map_­(I) < map_­.required_­span_­size() is true.
โ€” end note]
Effects: Equivalent to: return acc_.access(ptr_, map_(static_cast<index_type>(std::move(indices))...));
template<class OtherIndexType> constexpr reference operator[](span<OtherIndexType, rank()> indices) const; template<class OtherIndexType> constexpr reference operator[](const array<OtherIndexType, rank()>& indices) const;
Constraints:
  • is_­convertible_­v<const OtherIndexType&, index_­type> is true, and
  • is_­nothrow_­constructible_­v<index_­type, const OtherIndexType&> is true.
Effects: Let P be a parameter pack such that is_same_v<make_index_sequence<rank()>, index_sequence<P...>> is true.
Equivalent to: return operator[](as_const(indices[P])...);
constexpr size_type size() const noexcept;
Preconditions: The size of the multidimensional index space extents() is representable as a value of type size_­type ([basic.fundamental]).
Returns: extents().fwd-prod-of-extents(rank()).
[[nodiscard]] constexpr bool empty() const noexcept;
Returns: true if the size of the multidimensional index space extents() is 0, otherwise false.
friend constexpr void swap(mdspan& x, mdspan& y) noexcept;
Effects: Equivalent to: swap(x.ptr_, y.ptr_); swap(x.map_, y.map_); swap(x.acc_, y.acc_);