Is this behavior undefined?
std::array<int, 5> x = {3, 5, 1, 2, 3}; std::array<int, 3>& y = *reinterpret_cast<std::array<int, 3>*>(&x[1]); for(int i = 0; i != 3; i++) { std::cout << y[i] << "\n"; }
Maybe yes, but I really feel that there should be a safe way to cut std::array
s.
EDIT: Next sentence Radek:
template<unsigned N, unsigned start, unsigned end, typename T> std::array<T, end - start>& array_slice(std::array<T, N>& x) { static_assert(start <= end, "start <= end"); static_assert(end <= N-1, "end <= N"); return *reinterpret_cast<std::array<T, end - start>*>(&x[start]); }
EDIT: Well, I decided that I was unhappy with std::array
and move on to something else, to any ideas?
source share