I've got an std::array<std::shared_ptr<void>, N>
, and I have methods for accessing parts of this buffer as different types, which I would like to use std::span<std::shared_ptr<T>>
for.
Is there a way to construct a span like this without invoking UB?
No, this is impossible: regardless of the ability to convert void*
to T*
, you can’t convert void**
(a pointer to your first pointer) to T**
because there are no actual T*
objects there, and you certainly can’t convert std::shared_ptr<A>*
to std::shared_ptr<B>*
for any distinct A
and B
—std::shared_ptr<T>
isn’t just a T*
internally (because of the control block), and even if it were you’re not allowed to “unwrap” arrays of structures and treat them as arrays of their contents (with the magic exception of std::complex
).