The below code can be compiled successfully in Visual C++. I like it and it is sweet!
#include <stdio.h>
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4201)
#pragma pack(push,1)
#define PACKED
#else
#define PACKED __attribute__ ((__packed__))
#endif
union A {
struct {
int a:1;
int b:2;
int c1:29;
}PACKED;
struct {
int a:1;
int b:2;
int c2:28;
int d:1;
}PACKED;
int val;
}PACKED;
#ifdef _MSC_VER
#pragma pack(pop)
#pragma warning(pop)
#endif
#undef PACKED
int main(){
A test;
test.val = 0x1078FFF7;
printf("sizeof(A): %d, test.a: %d.\n", sizeof(A), test.a);
return -1;
}
Output with the file built with MSC:
sizeof(A): 4, test.a: -1.
But in GCC, including the latest gcc-7, it failed to be compiled, :(
struct.cpp:13:15: error: redeclaration of ‘signed char:1 A::<unnamed struct>::a’
int a:1;
^
struct.cpp:7:15: note: previous declaration ‘signed char:1 A::<unnamed struct>::a’
int a:1;
^
struct.cpp:14:15: error: redeclaration of ‘signed char:2 A::<unnamed struct>::b’
int b:2;
^
struct.cpp:8:15: note: previous declaration ‘signed char:2 A::<unnamed struct>::b’
int b:2;
^
Is it a bug in GCC?
Thanks for your comments, I just understood this question may be invalid for C; But for C++ part, I still have the concern. Personally I like Visual C++ compile behavior, it can save tons of code in my scenario