hmmm..
#pragma pack affects default alignment, but it shouldn't affect explicit alignment.
This code outputs 8 for the size and true for is_aligned call on my machine inspite of #pragma pack(1).
#include <iostream>
#pragma pack(1)
struct X
{
char a;
__declspec (align(4)) long b;
};
bool is_aligned(void *ptr, int boundary)
{
return ((uintptr_t)ptr & (boundary - 1)) == 0;
}
int main()
{
X x;
std::cout << sizeof(X) << std::endl;
std::cout << is_aligned(&x.b, 4) << std::endl;
}
I'm not sure about gcc.
Anyway, if this is not portable enough then assert is good solution too.
MSVC is inconsistent: with
#pragma pack(1) it obeys the alignment requirements sometimes, but not others.
gcc always ignores alignment requirements with
#pragma pack(1).
I'll put asserts in the atomic ops.