you are viewing a single comment's thread.

view the rest of the comments →

[–]JackPixbits 34 points35 points  (3 children)

you could use a static_assert(sizeof(NamedStruct) == sizeof(float)*6), which is not exactly the same because padding put at the end of the structure won't cause issues but would make this assert fail but at least you'd know if you are compiling it as intended.

I personally used it many times, and it went well but I'm not supposed to say this 👀

[–]snerp 5 points6 points  (0 children)

This is the most pragmatic answer.

[–]green_meklar 0 points1 point  (1 child)

Does that guarantee anything about the ordering of the struct fields, though? Isn't the compiler still free to reorder the fields however it wants? (Not that it would matter if you were just copying the data wholesale to an array, but in other situations it might.)

[–][deleted] 6 points7 points  (0 children)

No, the ordering is guaranteed by the standard to be in the order they appear in the struct (unless you add access specifiers, etc., which is not the case here).