# Details of std::mdspan from C++23

DevFeed: [Details of std::mdspan from C++23](<https://devfeed.tech/articles/details-of-std-mdspan-from-c-23-53309.md>)

Original publisher: [Read original article](<https://www.cppstories.com/2025/cpp23_mdspan/>)

Published: 2025-02-27T00:00:00Z

Content type: tutorial

Language: en

Sources: [C++ Stories](<https://devfeed.tech/sources/c-stories.md>)

Topics: [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [layout](<https://devfeed.tech/topics/layout.md>), [data](<https://devfeed.tech/topics/data.md>), [object](<https://devfeed.tech/topics/object.md>), [constructor](<https://devfeed.tech/topics/constructor.md>)

Tags: [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [c-plus-plus-23](<https://devfeed.tech/tags/c-plus-plus-23.md>), [data](<https://devfeed.tech/tags/data.md>), [details](<https://devfeed.tech/tags/details.md>), [layout](<https://devfeed.tech/tags/layout.md>)

## AI overview

This tutorial explains C++23's std::mdspan, a multidimensional data view. It covers its declaration, static and dynamic extents, construction options, object size, and layout policies, with notes on related C++26 additions.

## Source excerpt

In this article, we'll see details of std::mdspan, a new view type tailored to multidimensional data. We'll go through type declaration, creation techniques, and options to customize the internal functionality. Type declaration The type is declared in the following way: template< class T, class Extents, class LayoutPolicy = std::layout_right, class AccessorPolicy = std::default_accessor<T> > class mdspan; And it has its own header <mdspan>. The main proposal for this feature can be found at https://wg21.link/P0009 Following the pattern from std::span, we have a few options to create mdspan: with dynamic or static extent. The key difference is that rather than just one dimension, we can specify multiple. The declaration also gives more options in the form of LayoutPolicy and AccessorPolicy. More on that later. Static Extent std::vector<int> vec = {1, 2, 3, 2, 4, 5, 3, 5, 6, 7, 8, 9}; std::mdspan<int, std::extents<size_t, 3, 3>> mat3x3 { vec.data() }; std::print("sizeof: {}, rank: {}, ext 0: {}, ext 1: {}\n", sizeof(mat3x3), mat3x3.rank(), mat3x3.extent(0), mat3x3.extent(1)); The output: sizeof: 8, rank: 2, ext 0: 3, ext 1: 3 Run @Compiler Explorer Dynamic Extent And the dynamic option: std::mdspan<int, std::dextents<size_t, 2>> mat2x2 { vec.data(), 2, 2 }; std::print("sizeof: {}, rank: {}, ext 0: {}, ext 1: {}\n", sizeof(mat2x2), mat2x2.rank(), mat2x2.extent(0), mat2x2.extent(1)); The output: sizeof: 24, rank: 2, ext 0: 2, ext 1: 2 Run @Compiler Explorer The line: std::mdspan<int, std::dextents<size_t, 2>> Is equivalent: std::mdspan<int, std::extents<size_t, std::dynamic_extents, std::dynamic_extents>> In other words, dextents stands for dynamic extents. And as of C++26, we can also simplify it with std::dims: std::mdspan<int, std::dims<2>> mat2x2 { vec.data(), 2, 2 }; Sizeof mdspan As you can see, similarly to std::span, when the size is known at compile time, the object can be smaller (it doesn't have to store the dynamic size). Run here: @Compiler Explorer Constru