kokkos / mdspan

Reference implementation of mdspan targeting C++23
Other
417 stars 69 forks source link

Assigning to a mdspan to const #44

Closed tpadioleau closed 3 years ago

tpadioleau commented 3 years ago

The snippet below does not seem to compile

namespace stdex = std::experimental;
std::vector<double> v(10);
stdex::mdspan<double, stdex::dynamic_extent> a(v.data(), v.size());
stdex::mdspan<double const, stdex::dynamic_extent> b = a; // This currently does not compile

The compilation error seems to come from the accessor_basic that is not convertible from accessor_basic<double> to accessor_basic<double const>.

I think one might expect to be able to assign a mdspan to a mdspan to const as it is the case for std::span.

A fix could be to add a conversion operator like this one

MDSPAN_INLINE_FUNCTION
constexpr operator accessor_basic<std::add_const_t<ElementType>>() const noexcept {
  return {};
}
crtrott commented 3 years ago

Yeah

dalg24 commented 3 years ago

Actually I think this should be covered by the converting constructor. https://github.com/kokkos/mdspan/blob/606026cb3ff17accb227ab115b2232d40eb470f1/include/experimental/__p0009_bits/basic_mdspan.hpp#L182-L195

It looks like the issue comes from the accessor not being convertible https://github.com/kokkos/mdspan/blob/606026cb3ff17accb227ab115b2232d40eb470f1/include/experimental/__p0009_bits/basic_mdspan.hpp#L186

crtrott commented 3 years ago

actually if the problem is just the accessor then the above doesn't help, since is_convertible for the accessor types doesn't come back true.