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

Jonathan Wakely jwakely@redhat.com
Mon Sep 29 15:18:42 GMT 2025


On Mon, 29 Sept 2025 at 13:27, Luc Grosheintz <luc.grosheintz@gmail.com>
wrote:

> 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).
>

I think this is in flux, via P3663, which is being reviewed by LWG at the
moment. The latest draft (with a lot of input from Tomasz) is
https://isocpp.org/files/papers/D3663R3.html





>
> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20250929/b240c474/attachment-0001.htm>


More information about the Libstdc++ mailing list