I'm trying to use alignas, but it feels like it's being silently ignored.
I'm supposed to have a BITMAPFILEHEADER struct manually declared to avoid dependencies, but it has a 4-byte alignment requirement, and reading directly file to struct gives bad values.
Forcing alignment to 2-byte (alignas) doesn't seem to work, reading from the "original" BITMAPFILEHEADER works correctly, but makes use of
bfh will be aligned to a 2 byte boundary, the elements of the struct that happen to make up bfh are not relatively affected in any way. Otherwise the alignas would be part of the type information! What a mess that would be!
So I should write alignas for each element in the struct?
The example over there doesn't seem to do anything, neither writing alignas(2) over everything.
I accidentally wrote I resolved the issue, but I used the older structure.
it has a 4-byte alignment requirement, and reading directly file to struct gives bad values. Forcing alignment to 2-byte (alignas) doesn't seem to work,
You cannot weaken the alignment with alignas (see the cppreference link above or ยง7.6.2[dcl.align]/5)
You could always go the array-and-reference route - make an array of the correct size, read into it, and have your structure automatically make its reference members refer to the correct locations in the array.
Also if you're so concerned about padding and data alignment I am surprised you're using the raw primitive types - you should be using std::uint16_t and std::uint32_t.