libstdc++: Handling user-defined interlike object in submdspan.

Luc Grosheintz luc.grosheintz@gmail.com
Mon Sep 29 12:26:54 GMT 2025


Dear libstdc++ devs,

While implementing submdspan, I ran into difficulty related to
user-defined "integers", i.e. any class that converts to `index_type`
(the latter is always a signed or unsigned integer).

The user-defined types might not be copyable or only convert to
`index_type` from an rvalue.

Commonly, we've been following the pattern that we convert this
user-defined object to `index_type` exactly once via something
like:

    __index_cast<index_type>(std::move(__indices))...

store the converted value; and work with those.

It's not clear to me if the standard is more lenient for submdspan,
because it seems to very liberally pass __slices to functions, e.g.

	  auto __offset = __mdspan::__submdspan_offset(*this,
                             __slices...);
	  auto __sub_exts = submdspan_extents(extents(), __slices...);

and preconditions are written as:

     0 <= first_<index_type, k>(slices...)
       <= last_<k>(extents(), slices...)
       <= extents().extent(k)

Notably, it doesn't move slices and calls two different functions.

The question is should it support custom types that aren't copyable?
(Everything is much easier if all types are move and copy
constructable).

The idea I'm pursuing is to follow the pattern of casting slices to
"standard slices" immediately at the user-facing API layer, e.g.

   template<typename _Slices...>
   constexpr auto
   submdspan_extents(_Slices... __slices)
   {
     return __submdspan_extents(__slice_cast(std::move(__slices))...);
   }

However, if we look at submdspan we see it's defined as:

   constexpr auto
   submdspan(const auto& view, SliceSpecifiers... slices)
   {
     auto result = submdspan_mapping(view.mapping(), slices...);
     return mdspan(...);
   }

the twist is that it calls `submdspan_mapping` which accepts the
slices by value and must be ADL-able, i.e., we can't create standard
slices and then call an internal function.

Does the approach make sense? It could also be interpreted as an attempt
to reduce template instantiations.

Kind regards,
Luc


More information about the Libstdc++ mailing list