0

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?

Voidev
  • 15
  • 1
  • 4
  • 3
    If I understand correctly, yes it should work. Have you tried it? Did it work? If not, can you post the full code you tried, and what didn't work? – John Zwinck Jul 05 '20 at 11:37
  • @JohnZwinck It looks like I can construct the span with the entire array, however not with an iterator/size combination. I suppose I can use subspan() though, so that might work. I'm having trouble compiling C++20 generally still so I can't yet test further than what my IDE tells me – Voidev Jul 05 '20 at 11:56

1 Answers1

2

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

Davis Herring
  • 36,443
  • 4
  • 48
  • 76