std::vector
is not the right tool for this.
std::vector
is owning, thus if you want to reinterpret the memory allocated by
X
as a "vector" of arrays of ints, you should use a non-owning container such as std::span
. Even then, you would have to use reinterpret_cast
, which is recommended to avoid whenever possible.
Such as:
std::span<std::array<uint32_t, 3>> points (reinterpret_cast<std::array<uint32_t, 3>*>(
X.data()), X.size()/3);
Beware though, depending on the number of values in the std::array that you cast into, you might get into issues because of padding: consecutive arrays don't have to touch each other in memory.
For all of these reasons, I would instead recommend to either iterate over each third element or provide a wrapper class around X
, which encapsulates the strong semantic bond between the owned data and the view on it.
That's also because the view is now ephemeral: it has to be recreated whenever X
reallocates.