mardi 28 août 2018

Overflow on bitfield for signed underlying type enum

This is for C++11 only:

If I have a regular enum like below:

enum TestType
{
Test0 = 0,
Test1,
Test2,
Test3,
Test4,
Test5,
Test6,
Test7
}

and a packed struct like this:

struct
{
TestType a : 3
uint32_t b : 5
} TestStruct;

Is TestStruct.a guaranteed to be equal to its assigned enum value? Or is there a possibility the compiler will assign a signed underlying type and then treat the bitfield a as ranging -4 to 3.

Aucun commentaire:

Enregistrer un commentaire