The compiler-independent and portable solution is exactly the code that you have shown us. The compiler takes care of aligning everything correctly if you don't do anything to disrupt it. (such as using SIMD types which require special alignment, or using #pragma pack
)
If you're using a #pragma pack
, then:
- why didn't you show us?
- don't.
If the code you have shown us gives you any warnings at /W4, blame Microsoft. The code is correct, portable and safe (unlike code which uses the pack
pragma).
Microsoft has some weird ideas about warnings at /W4. Most compilers only issue warnings for things that look like they might contain errors.
Microsoft has a whole load of warnings basically saying "I have no reason to suspect that there is a problem with this code, but if you had written it differently, it might contain a bug", which is pure nonsense.
With other compilers I generally recommend compiling with all warnings enabled. On Microsoft's compiler, that's not really viable. (Although you can use /W4, and selectively silence specific nonsense warnings)
What I think they're specifically telling you is that the structure contains padding bytes (and so, if you did use #pragma pack
, it would alter the layout of the class, and one of the affected class members would be "sensitive" to this change).
If possible, consider rearranging the class members so they are listed in descending order of size. That gives you more compact class layouts, with padding (if any) only at the end of the class.
But there is nothing wrong with the class definition you're currently using.