Is the below code well-defined and guaranteed to work as expected?
#include <iostream>
#include <string>
using std::string;
int main() {
string const a[2][2][2] = {
"1", "2", "3", "4", "5", "6", "7", "8",
};
auto constexpr sz = sizeof(a) / sizeof(string);
auto const p = reinterpret_cast<string const*>(&a);
for (std::size_t i = 0; i < sz; ++i) std::cout << p[i] << '\n';
}
To my knowledge, since we cast it to a pointer, no out-of-bounds rules apply as long as we access valid memory, and thusly, this is OK. But I couldn't be sure.
Note: I am not asking if the array is contiguous (it is), I am asking if casting a multi-dimensional array to a flat pointer, and using it to access all elements is well defined (not UB).