Namespaces
Variants
Views
Actions

std::mdspan

From cppreference.com
< cpp‎ | container
Revision as of 19:14, 17 September 2022 by Cubbi (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
 
 
 
 
Defined in header <mdspan>
template<

    class T,
    class Extents,
    class LayoutPolicy = std::layout_right,
    class AccessorPolicy = std::default_accessor<T>

> class mdspan;
(since C++23)

std::mdspan is a non-owning view into a contiguous sequence of objects that reinterprets it as a multidimensional array.

Contents

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 of std::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 to T

Member types

Member type Definition
extents_type Extents
layout_type LayoutPolicy
accessor_type AccessorPolicy
mapping_type LayoutPolicy::mapping<Extents>
element_type T
value_type std::remove_cv_t<T>
index_type Extents::index_type
size_type Extents::size_type
rank_type Extents::rank_type
data_handle_type AccessorPolicy::data_handle_type
reference AccessorPolicy::reference

Member functions

constructs an mdspan
(public member function) [edit]
assigns an mdspan
(public member function) [edit]
Element access
accesses an element at the specified multidimensional index
(public member function) [edit]
Observers
returns the size of the multidimensional index space
(public member function) [edit]
checks if the size of the index space is zero
(public member function) [edit]
obtains the stride along the specified dimension
(public member function) [edit]
(C++23)
a descriptor of a multidimensional index space of some rank
(class template) [edit]
obtains the pointer to the underlying 1D sequence
(public member function) [edit]
obtains the mapping object
(public member function) [edit]
obtains the accessor policy object
(public member function) [edit]
determines if this mdspan's mapping is unique (every combination of indices maps to a different underlying element)
(public member function) [edit]
determines if this mdspan's mapping is exhaustive (every underlying element can be accessed with some combination of indices)
(public member function) [edit]
determines if this mdspan's mapping is strided (in each dimension, incrementing an index jumps over the same number of underlying elements every time)
(public member function) [edit]
determines if this mdspan's layout mapping is always unique
(public static member function) [edit]
determines if this mdspan's layout mapping is always exhaustive
(public static member function) [edit]
determines if this mdspan's layout mapping is always strided
(public static member function) [edit]


Non-member functions

specializes the std::swap algorithm for mdspan
(function template) [edit]

Helper types and templates

(C++23)
a descriptor of a multidimensional index space of some rank
(class template) [edit]
(C++23)(C++26)
convenience alias template for an all-dynamic std::extents
(alias template)[edit]
row-major multidimensional array layout mapping policy; rightmost extent has stride 1
(class) [edit]
column-major multidimensional array layout mapping policy; leftmost extent has stride 1
(class) [edit]
a layout mapping policy with user-defined strides
(class) [edit]

Deduction guides

Notes

Feature-test macro Value Std Feature
__cpp_lib_mdspan  

Example

can be previewed at https://godbolt.org/z/PK7bccGr3

#include <vector>
#include <mdspan>
#include <print>
 
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(size_t i=0; i != ms2.extent(0); i++)
    for(size_t j=0; j != ms2.extent(1); j++)
      ms2[i, j] = i*1000 + j;
 
  // read back using 3D view
  for(size_t i=0; i != ms3.extent(0); i++)
  {
    std::println("slice @ i = {}", i);
    for(size_t j=0; j != ms3.extent(1); j++)
    {
      for(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

(C++20)
a non-owning view over a contiguous sequence of objects
(class template) [edit]
numeric arrays, array masks and array slices
(class template) [edit]