Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

arbitrarily sized enum values

I'm compiling code that was written for GCC, in Visual C++ 2012. I'm getting warnings thrown about enum value truncation with the following enum (due to the value being outside the range of an int):

enum tile_flags {
   TILE_FLAG_INNER_FLAME= 0x10000000ULL,
    TILE_FLAG_CONSTRICTED= 0x20000000ULL,

    TILE_FLAG_MIMIC_INEPT 0x2000000000ULL
    TILE_FLAG_MIMIC 0x4000000000ULL
    TILE_FLAG_MIMIC_RAVEN 0x6000000000ULL
    TILE_FLAG_MIMIC_MASK 0x6000000000ULL
}

When compiling for x86, it would appear MSVC simply truncates the enum values to fit in 32bits. However, no truncation occurs in GCC. What is happening on GCC? And how can I make this work for MSVC?

like image 946
Mr. Smith Avatar asked Feb 11 '26 13:02

Mr. Smith


1 Answers

From N3485, § 7.2/6:

For an enumeration whose underlying type is not fixed, the underlying type is an integral type that can represent all the enumerator values defined in the enumeration. If no integral type can represent all the enumerator values, the enumeration is ill-formed. It is implementation-defined which integral type is used as the underlying type except that the underlying type shall not be larger than int unless the value of an enumerator cannot fit in an int or unsigned int. If the enumerator-list is empty, the underlying type is as if the enumeration had a single enumerator with value 0.

Therefore, if MSVC has the necessary long long support, it should make that the underlying type anyway. Seeing as how it doesn't, there's one thing you can try in order to coax it.

Specify the underlying type:

enum tile_flags : unsigned long long {
    ...
};
like image 184
chris Avatar answered Feb 13 '26 09:02

chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!