Search code examples
c++clangportabilitybit-fields

Meaning of Clang warning "-Wsigned-enum-bitfield"


Please explain to me the meaning of "-Wsigned-enum-bitfield" Clang warning.

In my code I've got a definition of a struct with bitfields:

struct Options {
    BackgroundType backgroundType : 2;
    LineSpace lineSpace : 2;
    bool letterSpacing : 1;
    bool shadow : 1;
    Qt::AlignmentFlag alignment : 9;
} options;

options.alignment = Qt::AlignLeft;

When compiling this code with Clang, I get the following warning:

enums in the Microsoft ABI are signed integers by default; consider giving the enum Qt::AlignmentFlag an unsigned underlying type to make this code portable

Enum Qt::AlignmentFlag is external to my code, and I can't change it. I'm inclined to simply ignore this warning. What's the worst that could happen if I leave this code as it is?


Solution

  • If the enum uses all the 9 bits, you get a negative number when reading it back. Might give you problems when comparing it to some other value.