std::mdspan(3)
π NAME π
std::mdspan β and yeah, the name is literally std::mdspan, bestie. This is the std::mdspan(3) page from the C++ Standard Libary manual (the header really spells it βLibaryβ π β iconic typo behavior).
β¨ Synopsis β¨
Defined in header <mdspan>:
template<
class T,
class Extents, (since C++23)
class LayoutPolicy = std::layout_right,
class AccessorPolicy = std::default_accessor<T>
> class mdspan;
std::mdspan is a view over a contiguous sequence of objects, and it reinterprets that sequence as a multidimensional array. Multidimensional-array main character energy, fr fr.
Every specialization MDS of mdspan models copyable and ticks these boxes:
std::is_nothrow_move_constructible_v<MDS>istrue,std::is_nothrow_move_assignable_v<MDS>istrue, andstd::is_nothrow_swappable_v<MDS>istrue.
A specialization of mdspan is a TriviallyCopyable type if its accessor_type, mapping_type and data_handle_type are TriviallyCopyable types. No cap, that's the rule.
πΈ Template parameters πΈ
Tβ element type; a complete object type that is neither an abstract class type nor an array type.Extentsβ specifies number of dimensions, their sizes, and which are known at compile time. Must be a specialization ofstd::extents.LayoutPolicyβ specifies how to convert multidimensional index to underlying 1D index (column-major 3D array, symmetric triangular 2D matrix, etc).AccessorPolicyβ specifies how to convert underlying 1D index to a reference toT. Must satisfy the constraint thatstd::is_same_v<T, typename AccessorPolicy::element_type>istrue.
This section is incomplete. Reason: add the requirement of layout policy and accessor policy.
π Member types π
extents_typeβExtentslayout_typeβLayoutPolicyaccessor_typeβAccessorPolicymapping_typeβLayoutPolicy::mapping<Extents>element_typeβTvalue_typeβstd::remove_cv_t<T>index_typeβExtents::index_typesize_typeβExtents::size_typerank_typeβExtents::rank_typedata_handle_typeβAccessorPolicy::data_handle_typereferenceβAccessorPolicy::reference
π¦ Member objects π¦
acc_(private) β the accessor of typeaccessor_type(exposition-only member object*)map_(private) β the layout mapping of typemapping_type(exposition-only member object*)ptr_(private) β the underlying data handle of typedata_handle_type(exposition-only member object*)
π Member functions π
constructorβ constructs anmdspan(public member function)operator=β assigns anmdspan(public member function)
π₯ Element access π₯
operator[]β accesses an element at the specified multidimensional index (public member function)
π Observers π
sizeβ returns the size of the multidimensional index space (public member function)emptyβ checks if the size of the index space is zero (public member function)strideβ obtains the stride along the specified dimension (public member function)extentsβ obtains the extents object (public member function)data_handleβ obtains the pointer to the underlying 1D sequence (public member function)mappingβ obtains the mapping object (public member function)accessorβ obtains the accessor policy object (public member function)is_uniqueβ determines if thismdspan's mapping is unique (every combination of indices maps to a different underlying element) (public member function)is_exhaustiveβ determines if thismdspan's mapping is exhaustive (every underlying element can be accessed with some combination of indices) (public member function)is_stridedβ determines if thismdspan's mapping is strided (in each dimension, incrementing an index jumps over the same number of underlying elements every time) (public member function)is_always_unique[static]β determines if thismdspan's layout mapping is always unique (public static member function)is_always_exhaustive[static]β determines if thismdspan's layout mapping is always exhaustive (public static member function)is_always_strided[static]β determines if thismdspan's layout mapping is always strided (public static member function)
π Non-member functions π
std::swap(std::mdspan)(C++23)β specializes thestd::swapalgorithm formdspan(function template)
πΈ Subviews πΈ
submdspan_extents(C++26)β creates new extents from the existing extents and slice specifiers (function template)submdspan(C++26)β returns a view of a subset of an existingmdspan(function template)
π Helper types and templates π
extents(C++23)β a descriptor of a multidimensional index space of some rank (class template)dextents(C++23)β convenience alias template for an all-dynamicstd::extents(alias template)layout_right(C++23)β row-major multidimensional array layout mapping policy; rightmost extent has stride 1 (class)layout_left(C++23)β column-major multidimensional array layout mapping policy; leftmost extent has stride 1 (class)layout_stride(C++23)β a layout mapping policy with user-defined strides (class)default_accessor(C++23)β a type for indexed access to elements ofmdspan(class template)
π¦ Subviews helpers π¦
full_extent/full_extent_t(C++26)β a slice specifier tag describing full range of indices in the specified extent (tag)strided_slice(C++26)β a slice specifier representing a set of regularly spaced indices as indicated by an offset, an extent, and a stride (class template)submdspan_mapping_result(C++26)β a return type of the overloads ofsubmdspan_mapping(class template)
β¨ Deduction guides β¨
No extra notes on this page for the deduction guides β just the section heading sitting there with main character energy. π
β Notes β
Feature-test macros, bestie:
__cpp_lib_mdspanβ202207Lβ(C++23)βstd::mdspan__cpp_lib_submdspanβ202306Lβ(C++26)βstd::submdspan
π Example π
You can preview this on Compiler Explorer. The page has the little // Run this code marker, then this exact code:
// Run this code
#include <cstddef>
#include <mdspan>
#include <print>
#include <vector>
int main()
{
std::vector v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
// View data as contiguous memory representing 2 rows of 6 ints each
auto ms2 = std::mdspan(v.data(), 2, 6);
// View the same data as a 3D array 2 x 3 x 2
auto ms3 = std::mdspan(v.data(), 2, 3, 2);
// Write data using 2D view
for (std::size_t i = 0; i != ms2.extent(0); i++)
for (std::size_t j = 0; j != ms2.extent(1); j++)
ms2[i, j] = i * 1000 + j;
// Read back using 3D view
for (std::size_t i = 0; i != ms3.extent(0); i++)
{
std::println("slice @ i = {}", i);
for (std::size_t j = 0; j != ms3.extent(1); j++)
{
for (std::size_t k = 0; k != ms3.extent(2); k++)
std::print("{} ", ms3[i, j, k]);
std::println("");
}
}
}
π§ Output π§
slice @ i = 0
0 1
2 3
4 5
slice @ i = 1
1000 1001
1002 1003
1004 1005
π See also π
span(C++20)β a non-owning view over a contiguous sequence of objects (class template)valarrayβ numeric arrays, array masks and array slices (class template)
π Category π
- Todo with reason
That's the whole page, bestie. Source/version and date: http://cppreference.com, 2024.06.10, std::mdspan(3). Go be a multidimensional icon! π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms