1 // For enum's underlying/compatible type: 2 // std C: unspecified 3 // GCC: 'unsigned int' if no negative values, 4 // otherwise 'int' (see GCC manul 4.9). 5 // But also accept ulong, long 6 // For the type of the enumerators: 7 // std C: 'int' 8 // GCC: 'int' if the value fit in a 'int' 9 // otherwise same as the enum underlying type? 10 // 11 // The following tests match GCC's choices 12 13 #define is_unsigned(X) ((typeof(X))-1 > 0) 14 15 enum u { 16 U = 1U, // fit in 'int' 17 // no negatives 18 }; 19 _Static_assert(sizeof(enum u) == sizeof(int), "size"); 20 _Static_assert(is_unsigned(enum u), "enum u"); 21 _Static_assert(is_unsigned(U) == 0, "value U"); // fail 22 23 enum v { 24 V = __INT_MAX__ + 1U, // doesn't fit in 'int' 25 // no negatives 26 }; 27 _Static_assert(sizeof(enum v) == sizeof(int), "size"); 28 _Static_assert(is_unsigned(enum v), "enum v"); 29 _Static_assert(is_unsigned(V) == 1, "value V"); 30 31 enum w { 32 W = __LONG_MAX__ + 1UL, // doesn't fit in 'long' 33 }; 34 _Static_assert(sizeof(enum w) == sizeof(long), "size"); 35 _Static_assert(is_unsigned(enum w), "enum w"); 36 _Static_assert(is_unsigned(W) == 1, "value W"); 37 38 enum x { 39 A = 1, // fit in 'int' 40 B = 0x100000000UL, // doesn't fit in int 41 }; 42 _Static_assert(sizeof(enum x) == sizeof(long), "size"); 43 _Static_assert(is_unsigned(enum x), "enum x"); 44 _Static_assert(sizeof(A) == sizeof(int), "size A"); // fail 45 _Static_assert(is_unsigned(A) == 0, "enum A"); // fail 46 _Static_assert(sizeof(B) == sizeof(long), "size B"); 47 _Static_assert(is_unsigned(B) == 1, "enum B"); 48 49 enum y { 50 C = 1, // fit in 'int' 51 D = 0x100000000L, // doesn't fit in int 52 }; 53 _Static_assert(sizeof(enum y) == sizeof(long), "size"); 54 _Static_assert(is_unsigned(enum y), "enum y"); 55 _Static_assert(sizeof(C) == sizeof(int), "size C"); // fail 56 _Static_assert(is_unsigned(C) == 0, "enum C"); // fail 57 _Static_assert(sizeof(D) == sizeof(long), "size D"); 58 _Static_assert(is_unsigned(D) == 1, "enum D"); 59 60 /* 61 * check-name: enum-sign-gcc 62 * check-command: sparse -m64 $file 63 * check-assert: sizeof(long) == 8 64 */ 65