jeudi 22 octobre 2020

VS2019 alignas in struct definition overrules pragma pack

I am trying to pack the structure "Test" using #pragma pack.

In VS2019 16.7.2, the following static assertion fails.

struct alignas(4 * sizeof(float)) Vec4
{
    float x,y,z,w;
};

#pragma pack(push,1)
struct Test
{
    char f;
    Vec4 vec;
};
#pragma pack(pop)

static_assert(sizeof(Test) == sizeof(char) + sizeof(Vec4), "Incorrect size.");

The size of Test is 2*sizeof(Vec4), which shows that the alignas on the struct definition itself overrules the pack.

The static assertion does not fail with GCC 10.2 or Clang 11.0.0.

Does anyone know of a way to pack Test as desired ? (without altering the alignment of the Vec4 struct)

Thanks.

Ben

Aucun commentaire:

Enregistrer un commentaire