ORNL / ORNL/cpp-proposals-pub

P0009: Consider adding conversion to std::array

Open
#278 2 comments 0 reactions 0 assignees View on GitHub

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.