However, the standard (object model) does not specify anything about how bit-fields are to be allocated within a class object (implementation defined).
If we define two different structs, the types are obviously not the same;
ie. struct X {}; and struct Y {}; are different types.
however the two types would be layout-compatible.
The common initial sequence of two standard-layout struct types is the longest sequence of non-static data members and bit-fields in declaration order, starting with the first such entity in each of the structs, such that corresponding entities have layout-compatible types and either neither entity is a bit-field or both are bit-fields with the same width.
Two standard-layout struct types are layout-compatible classes if their common initial sequence comprises all members and bit-fields of both classes.
#include <iostream>
#include <type_traits>
#include <cstddef>
struct A {
char a;
int b : 5;
int c : 11;
int : 0;
int d : 8;
struct { int ee : 8; } e;
};
struct B {
char a;
int b : 5,
c : 11,
: 0,
d : 8;
struct { int ee : 8; } e;
};
int main() {
std::cout << std::boolalpha
<< "A and B are the same type? " << std::is_same<A,B>::value << '\n' // false
<< "A and B have the same size? " << ( sizeof(A) == sizeof(B) ) << '\n' // true
<< "A and B are standard layout types? " // true
<< ( std::is_standard_layout<A>::value && std::is_standard_layout<A>::value ) << '\n'
<< "offsetof(A,e) and offsetof(B,e) are the same? " // true
<< ( offsetof(A,e) == offsetof(B,e) ) << '\n' ;
struct X {}; struct Y {};
std::cout << "\nX and Y are the same type? " << std::is_same<X,Y>::value << '\n' ; // false
}
The special unnamed bit field of size zero can be forced used to break up padding.
It specifies that the next bit field begins at the beginning of its allocation unit. http://en.cppreference.com/w/cpp/language/bit_field