Header Reference: <mdspan>

Header Reference: <mdspan>

Multidimensional non-owning views, layouts, and accessor policies from <mdspan>.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

What header pages are for

Header reference pages are meant to answer a practical question quickly: what this header provides, when to reach for it, and which usage rules are easiest to get wrong.

  • Start here when you already know roughly which header you need but want a fast operational summary.
  • Use the example section below as a minimal pattern, then adapt it to your real container, ownership, text, or concurrency workflow.
  • Jump to broader index pages when you need exhaustive coverage rather than a header-focused summary.

Header Reference: <mdspan>

Main facilities

What this header is for

Use <mdspan> to represent multidimensional views over existing contiguous or strided storage without owning the data.

Common patterns

Notes

Small worked example

#include <array>
#include <mdspan>

int main() {
	std::array<int, 6> storage{1, 2, 3, 4, 5, 6};
	std::mdspan matrix{storage.data(), 2, 3};
	return matrix(1, 2);
}

This is the core mdspan pattern: the storage lives elsewhere, while the mdspan provides multidimensional indexing rules over that storage.

Layout choice rule

Minimal example

#include <mdspan>

int main() {
    // Start with the primary facility from <mdspan>.
    // Then verify lifetime, invalidation, ordering, or error-handling rules.
    return 0;
}

What to verify before relying on this header

  • Whether the type models ownership, borrowing, type erasure, value transport, or a compile-time constraint.
  • Which operations are constant time versus potentially allocating, dispatching, or instantiating more template machinery than expected.
  • How the facility composes with nearby headers like ``, ``, ``, or ``.