the same meaning?

I am reading materials about cpp memory model.
The two snippet codes have the same meaning?
1
2
3
4
5
6
7
8
struct {
	char a;
	int b : 5;
	int c : 11;
	int	: 0;
	int d : 8;
	struct { int ee : 8; } e;
};

/*****************************************/
1
2
3
4
5
6
7
8
struct {
	char a;
	int b : 5,
		c : 11,
		: 0,
		d : 8;
	struct { int ee : 8; } e;
};
Yes.

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#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
}

http://coliru.stacked-crooked.com/a/c37f466d6ce86839
1
2
3
4
5
 
struct A
{
    int :0;
};

what is this anonymous (?) data-member called and what are its uses?
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
Thanks
Topic archived. No new replies allowed.