If you were given the following:
#include <cassert>
struct vec {
float x,y,z;
vec(float x, float y, float z) : x(x), y(y), z(z) {}
};
int main() {
float verts[] = {1,2,3,4,5,6,7,8,9}; // length guaranteed to be a multiple of 3
assert(sizeof(vec) == sizeof(float) * 3);
// Option 1
for (int i = 0; i < 3; ++i) {
auto a = reinterpret_cast<vec*>(verts)[i];
// ...
}
// Option 2
for (int i = 0; i < 9; i += 3) {
vec a(verts[i], verts[i+1], verts[i+2]); // -O3 averts the copy, so d/w
// ...
}
return 0;
}
Which is considered better practice?
The advantage to the latter option is of course far more explicit, and carries all the advantages/disadvantages that contains; but may not be portable depending on the assertions you can make.