yannick
yannick

Reputation: 189

Is it possible to construct a mdspan from a span?

Follwing code works:

std::array Array{1, 2, 3, 4, 5, 6, 7, 8};
std::mdspan<int, std::dextents<size_t, 2>> Span(Array.data(), 4, 2 );

This one produces a compilation error:

std::array Array{1, 2, 3, 4, 5, 6, 7, 8};
std::span sp(Array.data(), 8);
std::mdspan<int, std::dextents<size_t, 2>> Span(sp, 4, 2 );

E.g. the error on clang:

<source>:26:48: error: no matching constructor for initialization of 'std::mdspan<int, std::dextents<size_t, 2>>' (aka 'mdspan<int, extents<unsigned long, 18446744073709551615UL, 18446744073709551615UL>>')
   26 |     std::mdspan<int, std::dextents<size_t, 2>> Span(sp), 4, 2);

<...some more lines...>

Is it possible to construct a mdspan from a span ? if yes, what am I missing ? (it is a simplified source code, obviously we wouldn't need dynamic extents here)

Upvotes: 5

Views: 130

Answers (1)

wohlstad
wohlstad

Reputation: 29009

At the moment std::mdspan does not have a constructor that accepts a std::span.
But as @NathanOliver commented, std::span has a data() method to access the span's undelying data as a pointer.
It can be used in your case with std::mdspan constructor (2):

std::array Array2{ 1, 2, 3, 4, 5, 6, 7, 8 };
std::span sp(Array2.data(), 8);
//-------------------------------------------------vvvvvvv--------
std::mdspan<int, std::dextents<size_t, 2>> Span2(sp.data(), 4, 2);

Live demo

Upvotes: 5

Related Questions