P0009: Consider adding conversion to std::array
Nobody has claimed this yet.
- Dominant language
- HTML
- Stars
- 29
- Forks
- 26
- PR merge metrics
- No merged PRs in 30d
Description
A colleague asked if we could add operator std::array<value_type, rank()> to mdspan. It would have to be constrained on rank() == 1 and static_extent(0) != dynamic_extent. The mdspan_to_array function that follows illustrates an implementation approach.
https://godbolt.org/z/h6PeTdzPo
#include <https://raw.githubusercontent.com/kokkos/mdspan/single-header/mdspan.hpp>
#include <array>
#include <cassert>
#include <iostream>
#include <type_traits>
#include <utility>
namespace stdex = std::experimental;
template<class ElementType, class IndexType, std::size_t N, class Accessor>
requires( N != std::dynamic_extent )
std::array<ElementType, N>
mdspan_to_array(stdex::mdspan<ElementType, stdex::extents<IndexType, N>, stdex::layout_right, Accessor> m)
{
return [&]<std::size_t ... Indices>(std::integer_sequence<IndexType, Indices...>) {
return std::array<std::remove_cv_t<ElementType>, N>{ m[Indices]... };
}( std::make_integer_sequence<IndexType, N>() );
}
int main() {
std::array d{ 0, 1, 2, 3, 4, 5, 6, 7, 8 };
stdex::mdspan<int, stdex::extents<std::size_t, 9>> d_md{d.data()};
auto d_arr = mdspan_to_array(d_md);
static_assert(std::is_same_v<decltype(d), decltype(d_arr)>);
for(std::size_t k = 0; k < d.size(); ++k) {
assert(d_arr[k] == d[k]);
}
return 0;
}
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked Godbolt example and its mdspan_to_array function, then review how the proposed operator would be constrained by rank() and static_extent(0). Done means producing an agreed design for the std::array conversion and documenting or validating its behavior for fixed-size, rank-one mdspan objects.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100