Given an aggregate struct/class in which every member variable is of the same data type:
struct MatrixStack {
Matrix4x4 translation { ... };
Matrix4x4 rotation { ... };
Matrix4x4 projection { ... };
} matrixStack;
How valid is it to cast it to an array of its members? e.g.
const Matrix4x4 *ptr = reinterpret_cast<const Matrix4x4*>(&matrixStack);
assert(ptr == &matrixStack.translation);
assert(ptr + 1 == &matrixStack.rotation);
assert(ptr + 2 == &matrixStack.projection);
auto squashed = std::accumulate(ptr, ptr + 3, identity(), multiply());
I am doing this because in most cases I need named member access for clarity, whereas in some other cases I need to pass the an array into some other API. By using reinterpret_cast, I can avoid allocation.