C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Make std::span convertible to a const'd span

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Sun, 22 Sep 2024 12:44:00 -0400
On Sun, Sep 22, 2024 at 12:20 PM Oliver Schädlich via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> It would be nice to make it possible that a span<Xxx> could be converted
> implicitly to a
> span<const Xxx>
>
That part already works fine in C++20.

> as well as the iterators inside such spans could be converted to their
> const'd parent span's counterpart.
>
That's tougher. libc++ supports converting `span<T>::iterator` into
`span<const T>::iterator`, but libstdc++ does not.
The basic question is how much are iterators "just pointers" (so that e.g.
a non-const `T*` should certainly be convertible to `const T*`), and how
much are they "intentionally more than pointers" (e.g. so you can't
accidentally pass a `vector<T>::iterator` to a function that expects a
`span<T>::iterator`). Different vendors have landed at different points on
that continuum — not necessarily by careful reasoning, mind you, perhaps
just by historical accident — but either way, it would be hard to change
their behaviors at this point, because of the massive effect it would have
on overload resolution.

Related reading:
https://quuxplusone.github.io/blog/2022/03/03/why-isnt-vector-iterator-just-t-star/

HTH,
Arthur

Received on 2024-09-22 16:44:18