Home » C++23: A Multidimensional View | heise online

C++23: A Multidimensional View | heise online

by admin
C++23: A Multidimensional View |  heise online

A std::mdspan is a non-owning multidimensional view of a connected sequence of objects. This can be a simple C array, a pointer of size, a std::array, a std::vector, or a std::string.

Advertisement

Rainer Grimm has been working as a software architect, team and training manager for many years. He enjoys writing articles on the programming languages ​​C++, Python and Haskell, but also enjoys speaking frequently at specialist conferences. On his blog Modern C++ he deals intensively with his passion C++.

This multidimensional view is often referred to as a multidimensional array.

The number of dimensions and the size of each dimension determine the shape of the multidimensional array. The number of dimensions is called rank and the size of each dimension is called extension. std::mdspan’s size is the product of all dimensions that are not 0. One can access the elements of a std::mdspan using the multidimensional index operator [] access.

Each dimension of a std::mdspan can have one static extent or one dynamic extent have. static extent means that their length is determined at compile time; dynamic extent means that their length is fixed at runtime.

Here is the definition of a std::mdspan:

template
> class mdspan;

T: the connected sequence of objects,Extents: specifies the number of dimensions as their size; every dimension can have one static extent or one dynamic extent have.LayoutPolicy: sets the layout policy for accessing the underlying storage.AccessorPolicy: determines how the underlying elements are referenced.

Thanks to class template argument deduction (CTAG) in C++17, the compiler can often automatically derive the template arguments from the data types of the initializers:

See also  how much technology is there in MotoGP?...

// mdspan.cpp

#include
#include
#include

int main() {

std::vector myVec{1, 2, 3, 4, 5, 6, 7, 8}; // (1)

std::mdspan m{myVec.data(), 2, 4}; // (2)
std::cout In this example, I apply class template argument deduction three times. In (1) it is used for a std::vector and in (2) and (3) for a std::mdspan. The first two-dimensional array m has the shape (2, 4), the second m2 has the shape (4, 2). In (4) and (5) the ranks of the two std::mdspan are shown. Thanks to the expansion of each dimension (6 and 7) and the index operator in (8), iterating through multidimensional arrays is quite easy.

If a multidimensional array a static extent you have to specify the template arguments.

// staticDynamicExtent.cpp

#include
#include
#include
#include
#include

int main() {

std::vector myVec{1, 2, 3, 4, 5, 6, 7, 8};

std::mdspan>
m{myVec.data()}; // (1)
std::cout > m2{myVec.data(), 4, 2};// (2)
std::cout The staticDynamicExtent.cpp program is based on the previous mdspan.cpp program and produces the same output. The difference is that the std::mdspan m (1) has a static extent has. For completeness: std::mdspan m2 (2) has one dynamic extent. Accordingly, the form of m is given with template arguments, but the form of m2 is given with function arguments.

A std::mdspan allows you to specify the layout strategy for accessing the underlying storage. By default, std::layout_right (C, C++, or Python style) is used, but you can also specify std::layout_left (Fortran or MATLAB style). The following graphic illustrates the order in which the elements of the std::mdspan are accessed.

Looping through two std::mdspan with the std::layout_right and std::layout_left layout strategies shows the difference.

// mdspanLayout.cpp

See also  Time in C++20: Basic chrono terminology with time duration and point in time

#include
#include
#include

int main() {

std::vector myVec{1, 2, 3, 4, 5, 6, 7, 8};

std::mdspanstd::layout_right> m2{myVec.data(), 4, 2};
std::cout std::layout_left> m2{myVec.data(), 4, 2};
std::cout The std::mdspan m uses std::layout_right (1), the other std::mdspan uses std::layout_left (2). Thanks to the deduction of class template arguments, the constructor call of std::mdspan (2) does not require explicit template arguments and is equivalent to the expression std::mdspan m2{myVec.data(), 4, 2}.

The output of the program shows the two different layout strategies:

The following table provides an overview of the std::mdspan md interface.

C++20 does not offer any specific coroutines, but does provide a framework for implementing them. That changes with C++23. std::generator is the first concrete coroutine. (rme)

To home page

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy