The following code example is taken from the book
C++23 - The Complete Guide
by Nicolai M. Josuttis,
Leanpub, 2026
The code is licensed under a
Creative Commons Attribution 4.0 International License.
// raw code
#include <print>
#include <mdspan>
int main()
{
std::array arr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
// mdspan with 1 dimension and no elements:
std::mdspan mds1{arr.data(), 0};
std::println("rank: {}", mds1.rank());
std::println("size: {}", mds1.size());
std::println("empty: {}\n", mds1.empty());
// mdspan with 1 dimension and no elements and fixed extent:
std::mdspan<int, std::extents<std::size_t, 0>> mdsf1{arr.data()};
std::println("rank: {}", mdsf1.rank());
std::println("size: {}", mdsf1.size());
std::println("empty: {}\n", mdsf1.empty());
// mdspan with 0 dimensions:
std::mdspan mds0{arr.data()};
std::println("rank: {}", mds0.rank());
std::println("size: {}", mds0.size());
std::println("empty: {}\n", mds0.empty());
// mdspan with 0 dimensions and fixed extent:
std::mdspan<int, std::extents<std::size_t>> mdsf0{arr.data()};
std::println("rank: {}", mdsf0.rank());
std::println("size: {}", mdsf0.size());
std::println("empty: {}", mdsf0.empty());
}